]> git.proxmox.com Git - libgit2.git/blob - src/oidmap.c
c42e5c25af5e097048ba81a7b68176d3fcf41263
[libgit2.git] / src / oidmap.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "oidmap.h"
9
10 #define kmalloc git__malloc
11 #define kcalloc git__calloc
12 #define krealloc git__realloc
13 #define kreallocarray git__reallocarray
14 #define kfree git__free
15 #include "khash.h"
16
17 __KHASH_TYPE(oid, const git_oid *, void *)
18
19 GIT_INLINE(khint_t) git_oidmap_hash(const git_oid *oid)
20 {
21 khint_t h;
22 memcpy(&h, oid, sizeof(khint_t));
23 return h;
24 }
25
26 __KHASH_IMPL(oid, static kh_inline, const git_oid *, void *, 1, git_oidmap_hash, git_oid_equal)
27
28 git_oidmap *git_oidmap_alloc()
29 {
30 return kh_init(oid);
31 }
32
33 void git_oidmap_free(git_oidmap *map)
34 {
35 kh_destroy(oid, map);
36 }
37
38 void git_oidmap_clear(git_oidmap *map)
39 {
40 kh_clear(oid, map);
41 }
42
43 size_t git_oidmap_size(git_oidmap *map)
44 {
45 return kh_size(map);
46 }
47
48 size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key)
49 {
50 return kh_get(oid, map, key);
51 }
52
53 int git_oidmap_valid_index(git_oidmap *map, size_t idx)
54 {
55 return idx != kh_end(map);
56 }
57
58 int git_oidmap_exists(git_oidmap *map, const git_oid *key)
59 {
60 return kh_get(oid, map, key) != kh_end(map);
61 }
62
63 int git_oidmap_has_data(git_oidmap *map, size_t idx)
64 {
65 return kh_exist(map, idx);
66 }
67
68 const git_oid *git_oidmap_key(git_oidmap *map, size_t idx)
69 {
70 return kh_key(map, idx);
71 }
72
73 void git_oidmap_set_key_at(git_oidmap *map, size_t idx, git_oid *key)
74 {
75 kh_key(map, idx) = key;
76 }
77
78 void *git_oidmap_value_at(git_oidmap *map, size_t idx)
79 {
80 return kh_val(map, idx);
81 }
82
83 void git_oidmap_set_value_at(git_oidmap *map, size_t idx, void *value)
84 {
85 kh_val(map, idx) = value;
86 }
87
88 void git_oidmap_delete_at(git_oidmap *map, size_t idx)
89 {
90 kh_del(oid, map, idx);
91 }
92
93 int git_oidmap_put(git_oidmap *map, const git_oid *key, int *err)
94 {
95 return kh_put(oid, map, key, err);
96 }
97
98 void git_oidmap_insert(git_oidmap *map, const git_oid *key, void *value, int *rval)
99 {
100 khiter_t idx = kh_put(oid, map, key, rval);
101
102 if ((*rval) >= 0) {
103 if ((*rval) == 0)
104 kh_key(map, idx) = key;
105 kh_val(map, idx) = value;
106 }
107 }
108
109 void git_oidmap_delete(git_oidmap *map, const git_oid *key)
110 {
111 khiter_t idx = git_oidmap_lookup_index(map, key);
112 if (git_oidmap_valid_index(map, idx))
113 git_oidmap_delete_at(map, idx);
114 }
115
116 size_t git_oidmap_begin(git_oidmap *map)
117 {
118 GIT_UNUSED(map);
119 return 0;
120 }
121
122 size_t git_oidmap_end(git_oidmap *map)
123 {
124 return map->n_buckets;
125 }