1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
8 /* Pretty straightforward hash table implementation. As a minor
9 * optimization a NULL hashmap object will be treated as empty hashmap
10 * for all read operations. That way it is not necessary to
11 * instantiate an object for each Hashmap use. */
13 typedef struct Hashmap Hashmap
;
15 typedef unsigned (*hash_func_t
)(const void *p
);
16 typedef int (*compare_func_t
)(const void *a
, const void *b
);
18 unsigned string_hash_func(const void *p
);
19 int string_compare_func(const void *a
, const void *b
);
21 unsigned trivial_hash_func(const void *p
);
22 int trivial_compare_func(const void *a
, const void *b
);
24 Hashmap
*hashmap_new(hash_func_t hash_func
, compare_func_t compare_func
);
25 void hashmap_free(Hashmap
*h
);
26 Hashmap
*hashmap_copy(Hashmap
*h
);
28 int hashmap_put(Hashmap
*h
, const void *key
, void *value
);
29 void* hashmap_get(Hashmap
*h
, const void *key
);
30 void* hashmap_remove(Hashmap
*h
, const void *key
);
32 int hashmap_merge(Hashmap
*h
, Hashmap
*other
);
34 unsigned hashmap_size(Hashmap
*h
);
35 bool hashmap_isempty(Hashmap
*h
);
37 void *hashmap_iterate(Hashmap
*h
, void **state
, const void **key
);
38 void *hashmap_iterate_backwards(Hashmap
*h
, void **state
, const void **key
);
40 void *hashmap_steal_first(Hashmap
*h
);
41 void* hashmap_first(Hashmap
*h
);
42 void* hashmap_last(Hashmap
*h
);
44 #define HASHMAP_FOREACH(e, h, state) \
45 for ((state) = NULL, (e) = hashmap_iterate((h), &(state), NULL); (e); (e) = hashmap_iterate((h), &(state), NULL))
47 #define HASHMAP_FOREACH_BACKWARDS(e, h, state) \
48 for ((state) = NULL, (e) = hashmap_iterate_backwards((h), &(state), NULL); (e); (e) = hashmap_iterate_backwards((h), &(state), NULL))