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 int hashmap_replace(Hashmap
*h
, const void *key
, void *value
);
30 void* hashmap_get(Hashmap
*h
, const void *key
);
31 void* hashmap_remove(Hashmap
*h
, const void *key
);
32 void* hashmap_remove_value(Hashmap
*h
, const void *key
, void *value
);
34 int hashmap_merge(Hashmap
*h
, Hashmap
*other
);
36 unsigned hashmap_size(Hashmap
*h
);
37 bool hashmap_isempty(Hashmap
*h
);
39 void *hashmap_iterate(Hashmap
*h
, void **state
, const void **key
);
40 void *hashmap_iterate_backwards(Hashmap
*h
, void **state
, const void **key
);
42 void hashmap_clear(Hashmap
*h
);
43 void *hashmap_steal_first(Hashmap
*h
);
44 void* hashmap_first(Hashmap
*h
);
45 void* hashmap_last(Hashmap
*h
);
47 #define HASHMAP_FOREACH(e, h, state) \
48 for ((state) = NULL, (e) = hashmap_iterate((h), &(state), NULL); (e); (e) = hashmap_iterate((h), &(state), NULL))
50 #define HASHMAP_FOREACH_KEY(e, k, h, state) \
51 for ((state) = NULL, (e) = hashmap_iterate((h), &(state), (const void**) &(k)); (e); (e) = hashmap_iterate((h), &(state), (const void**) &(k)))
53 #define HASHMAP_FOREACH_BACKWARDS(e, h, state) \
54 for ((state) = NULL, (e) = hashmap_iterate_backwards((h), &(state), NULL); (e); (e) = hashmap_iterate_backwards((h), &(state), NULL))