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