]> git.proxmox.com Git - libgit2.git/blob - src/hashtable.h
Include common.h in hashtable.h
[libgit2.git] / src / hashtable.h
1 #ifndef INCLUDE_hashtable_h__
2 #define INCLUDE_hashtable_h__
3
4 #include "git2/common.h"
5 #include "git2/oid.h"
6 #include "git2/odb.h"
7 #include "common.h"
8
9 #define GIT_HASHTABLE_HASHES 3
10
11 typedef uint32_t (*git_hash_ptr)(const void *, int hash_id);
12 typedef int (*git_hash_keyeq_ptr)(const void *key_a, const void *key_b);
13
14 struct git_hashtable_node {
15 const void *key;
16 void *value;
17 };
18
19 struct git_hashtable {
20 struct git_hashtable_node *nodes;
21
22 size_t size_mask;
23 size_t size;
24 size_t key_count;
25
26 int is_resizing;
27
28 git_hash_ptr hash;
29 git_hash_keyeq_ptr key_equal;
30 };
31
32 typedef struct git_hashtable_node git_hashtable_node;
33 typedef struct git_hashtable git_hashtable;
34
35 git_hashtable *git_hashtable_alloc(size_t min_size,
36 git_hash_ptr hash,
37 git_hash_keyeq_ptr key_eq);
38 void *git_hashtable_lookup(git_hashtable *h, const void *key);
39 int git_hashtable_remove(git_hashtable *table, const void *key);
40 void git_hashtable_free(git_hashtable *h);
41 void git_hashtable_clear(git_hashtable *h);
42 int git_hashtable_merge(git_hashtable *self, git_hashtable *other);
43
44 int git_hashtable_insert2(git_hashtable *h, const void *key, void *value, void **old_value);
45
46 GIT_INLINE(int) git_hashtable_insert(git_hashtable *h, const void *key, void *value)
47 {
48 void *_unused;
49 return git_hashtable_insert2(h, key, value, &_unused);
50 }
51
52 #define git_hashtable_node_at(nodes, pos) ((git_hashtable_node *)(&nodes[pos]))
53
54 #define GIT_HASHTABLE_FOREACH(self, pkey, pvalue, code) {\
55 git_hashtable *_self = (self);\
56 git_hashtable_node *_nodes = _self->nodes;\
57 unsigned int _i, _size = _self->size;\
58 for (_i = 0; _i < _size; _i ++) {\
59 git_hashtable_node *_node = git_hashtable_node_at(_nodes, _i);\
60 if (_node->key)\
61 {\
62 pkey = _node->key;\
63 pvalue = _node->value;\
64 code;\
65 }\
66 }\
67 }
68
69 #define GIT_HASHTABLE_FOREACH_DELETE() {\
70 _node->key = NULL; _node->value = NULL; _self->key_count--;\
71 }
72
73
74 #endif