1 | #ifndef HASHMAP_H
|
---|
2 | #define HASHMAP_H
|
---|
3 |
|
---|
4 | /*
|
---|
5 | * Generic implementation of hash-based key-value mappings.
|
---|
6 | * See Documentation/technical/api-hashmap.txt.
|
---|
7 | */
|
---|
8 |
|
---|
9 | /* FNV-1 functions */
|
---|
10 |
|
---|
11 | extern unsigned int strhash(const char *buf);
|
---|
12 | extern unsigned int strihash(const char *buf);
|
---|
13 | extern unsigned int memhash(const void *buf, size_t len);
|
---|
14 | extern unsigned int memihash(const void *buf, size_t len);
|
---|
15 |
|
---|
16 | /* data structures */
|
---|
17 |
|
---|
18 | struct hashmap_entry {
|
---|
19 | struct hashmap_entry *next;
|
---|
20 | unsigned int hash;
|
---|
21 | };
|
---|
22 |
|
---|
23 | typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
|
---|
24 | const void *keydata);
|
---|
25 |
|
---|
26 | struct hashmap {
|
---|
27 | struct hashmap_entry **table;
|
---|
28 | hashmap_cmp_fn cmpfn;
|
---|
29 | unsigned int size, tablesize, grow_at, shrink_at;
|
---|
30 | };
|
---|
31 |
|
---|
32 | struct hashmap_iter {
|
---|
33 | struct hashmap *map;
|
---|
34 | struct hashmap_entry *next;
|
---|
35 | unsigned int tablepos;
|
---|
36 | };
|
---|
37 |
|
---|
38 | /* hashmap functions */
|
---|
39 |
|
---|
40 | extern void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
|
---|
41 | size_t initial_size);
|
---|
42 | extern void hashmap_free(struct hashmap *map, int free_entries);
|
---|
43 |
|
---|
44 | /* hashmap_entry functions */
|
---|
45 |
|
---|
46 | static inline void hashmap_entry_init(void *entry, unsigned int hash)
|
---|
47 | {
|
---|
48 | struct hashmap_entry *e = entry;
|
---|
49 | e->hash = hash;
|
---|
50 | e->next = NULL;
|
---|
51 | }
|
---|
52 | extern void *hashmap_get(const struct hashmap *map, const void *key,
|
---|
53 | const void *keydata);
|
---|
54 | extern void *hashmap_get_next(const struct hashmap *map, const void *entry);
|
---|
55 | extern void hashmap_add(struct hashmap *map, void *entry);
|
---|
56 | extern void *hashmap_put(struct hashmap *map, void *entry);
|
---|
57 | extern void *hashmap_remove(struct hashmap *map, const void *key,
|
---|
58 | const void *keydata);
|
---|
59 |
|
---|
60 | /* hashmap_iter functions */
|
---|
61 |
|
---|
62 | extern void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter);
|
---|
63 | extern void *hashmap_iter_next(struct hashmap_iter *iter);
|
---|
64 | static inline void *hashmap_iter_first(struct hashmap *map,
|
---|
65 | struct hashmap_iter *iter)
|
---|
66 | {
|
---|
67 | hashmap_iter_init(map, iter);
|
---|
68 | return hashmap_iter_next(iter);
|
---|
69 | }
|
---|
70 |
|
---|
71 | #endif
|
---|