]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - hashmap.h
hashmap: factor out getting a hash code from a SHA1
[thirdparty/git.git] / hashmap.h
... / ...
CommitLineData
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
11extern 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);
15
16static 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 */
22 unsigned int hash;
23 memcpy(&hash, sha1, sizeof(hash));
24 return hash;
25}
26
27/* data structures */
28
29struct hashmap_entry {
30 struct hashmap_entry *next;
31 unsigned int hash;
32};
33
34typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
35 const void *keydata);
36
37struct hashmap {
38 struct hashmap_entry **table;
39 hashmap_cmp_fn cmpfn;
40 unsigned int size, tablesize, grow_at, shrink_at;
41};
42
43struct hashmap_iter {
44 struct hashmap *map;
45 struct hashmap_entry *next;
46 unsigned int tablepos;
47};
48
49/* hashmap functions */
50
51extern void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
52 size_t initial_size);
53extern void hashmap_free(struct hashmap *map, int free_entries);
54
55/* hashmap_entry functions */
56
57static inline void hashmap_entry_init(void *entry, unsigned int hash)
58{
59 struct hashmap_entry *e = entry;
60 e->hash = hash;
61 e->next = NULL;
62}
63extern void *hashmap_get(const struct hashmap *map, const void *key,
64 const 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,
69 const void *keydata);
70
71/* hashmap_iter functions */
72
73extern void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter);
74extern void *hashmap_iter_next(struct hashmap_iter *iter);
75static inline void *hashmap_iter_first(struct hashmap *map,
76 struct hashmap_iter *iter)
77{
78 hashmap_iter_init(map, iter);
79 return hashmap_iter_next(iter);
80}
81
82#endif