struct hashmap map; struct long2double { struct hashmap_entry ent; /* must be the first member! */ long key; double value; }; static int long2double_cmp(const struct long2double *e1, const struct long2double *e2, const void *unused) { return !(e1->key == e2->key); } void long2double_init(void) { hashmap_init(&map, (hashmap_cmp_fn) long2double_cmp, 0); } void long2double_free(void) { hashmap_free(&map, 1); } static struct long2double *find_entry(long key) { struct long2double k; hashmap_entry_init(&k, memhash(&key, sizeof(long))); k.key = key; return hashmap_get(&map, &k, NULL); } double get_value(long key) { struct long2double *e = find_entry(key); return e ? e->value : 0; } void set_value(long key, double value) { struct long2double *e = find_entry(key); if (!e) { e = malloc(sizeof(struct long2double)); hashmap_entry_init(e, memhash(&key, sizeof(long))); e->key = key; hashmap_add(&map, e); } e->value = value; }