1#ifndef HASHMAP_H
2#define HASHMAP_H
34
/*
5* Generic implementation of hash-based key-value mappings.
6* See Documentation/technical/api-hashmap.txt.
7*/
89
/* FNV-1 functions */
1011
extern unsigned int strhash(const char *buf);
12extern unsigned int strihash(const char *buf);
13extern unsigned int memhash(const void *buf, size_t len);
14extern unsigned int memihash(const void *buf, size_t len);
1516
static inline unsigned int sha1hash(const unsigned char *sha1)
17{
18/*
19* Equivalent to 'return *(unsigned int *)sha1;', but safe on
20* platforms that don't support unaligned reads.
21*/
22unsigned int hash;
23memcpy(&hash, sha1, sizeof(hash));
24return hash;
25}
2627
/* data structures */
2829
struct hashmap_entry {
30struct hashmap_entry *next;
31unsigned int hash;
32};
3334
typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
35const void *keydata);
3637
struct hashmap {
38struct hashmap_entry **table;
39hashmap_cmp_fn cmpfn;
40unsigned int size, tablesize, grow_at, shrink_at;
41};
4243
struct hashmap_iter {
44struct hashmap *map;
45struct hashmap_entry *next;
46unsigned int tablepos;
47};
4849
/* hashmap functions */
5051
extern void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
52size_t initial_size);
53extern void hashmap_free(struct hashmap *map, int free_entries);
5455
/* hashmap_entry functions */
5657
static inline void hashmap_entry_init(void *entry, unsigned int hash)
58{
59struct hashmap_entry *e = entry;
60e->hash = hash;
61e->next = NULL;
62}
63extern void *hashmap_get(const struct hashmap *map, const void *key,
64const void *keydata);
65extern void *hashmap_get_next(const struct hashmap *map, const void *entry);
66extern void hashmap_add(struct hashmap *map, void *entry);
67extern void *hashmap_put(struct hashmap *map, void *entry);
68extern void *hashmap_remove(struct hashmap *map, const void *key,
69const void *keydata);
7071
static inline void *hashmap_get_from_hash(const struct hashmap *map,
72unsigned int hash, const void *keydata)
73{
74struct hashmap_entry key;
75hashmap_entry_init(&key, hash);
76return hashmap_get(map, &key, keydata);
77}
7879
/* hashmap_iter functions */
8081
extern void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter);
82extern void *hashmap_iter_next(struct hashmap_iter *iter);
83static inline void *hashmap_iter_first(struct hashmap *map,
84struct hashmap_iter *iter)
85{
86hashmap_iter_init(map, iter);
87return hashmap_iter_next(iter);
88}
8990
#endif