]>
Commit | Line | Data |
---|---|---|
cee9ca66 PS |
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 "idxmap.h" | |
9 | ||
ac3d33df JK |
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(idx, const git_index_entry *, git_index_entry *) | |
18 | __KHASH_TYPE(idxicase, const git_index_entry *, git_index_entry *) | |
19 | ||
8f1ff26b PS |
20 | /* This is __ac_X31_hash_string but with tolower and it takes the entry's stage into account */ |
21 | static kh_inline khint_t idxentry_hash(const git_index_entry *e) | |
22 | { | |
23 | const char *s = e->path; | |
24 | khint_t h = (khint_t)git__tolower(*s); | |
25 | if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)git__tolower(*s); | |
ac3d33df | 26 | return h + GIT_INDEX_ENTRY_STAGE(e); |
8f1ff26b PS |
27 | } |
28 | ||
ac3d33df JK |
29 | #define idxentry_equal(a, b) (GIT_INDEX_ENTRY_STAGE(a) == GIT_INDEX_ENTRY_STAGE(b) && strcmp(a->path, b->path) == 0) |
30 | #define idxentry_icase_equal(a, b) (GIT_INDEX_ENTRY_STAGE(a) == GIT_INDEX_ENTRY_STAGE(b) && strcasecmp(a->path, b->path) == 0) | |
8f1ff26b PS |
31 | |
32 | __KHASH_IMPL(idx, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_equal) | |
33 | __KHASH_IMPL(idxicase, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_icase_equal) | |
cee9ca66 | 34 | |
0c9c969a | 35 | int git_idxmap_new(git_idxmap **out) |
cee9ca66 | 36 | { |
0c9c969a UG |
37 | *out = kh_init(idx); |
38 | GIT_ERROR_CHECK_ALLOC(*out); | |
cee9ca66 PS |
39 | |
40 | return 0; | |
41 | } | |
42 | ||
0c9c969a | 43 | int git_idxmap_icase_new(git_idxmap_icase **out) |
cee9ca66 | 44 | { |
0c9c969a UG |
45 | *out = kh_init(idxicase); |
46 | GIT_ERROR_CHECK_ALLOC(*out); | |
cee9ca66 PS |
47 | |
48 | return 0; | |
49 | } | |
50 | ||
0c9c969a | 51 | void git_idxmap_free(git_idxmap *map) |
cee9ca66 | 52 | { |
0c9c969a | 53 | kh_destroy(idx, map); |
cee9ca66 PS |
54 | } |
55 | ||
0c9c969a | 56 | void git_idxmap_icase_free(git_idxmap_icase *map) |
cee9ca66 | 57 | { |
0c9c969a | 58 | kh_destroy(idxicase, map); |
cee9ca66 PS |
59 | } |
60 | ||
0c9c969a | 61 | void git_idxmap_clear(git_idxmap *map) |
cee9ca66 | 62 | { |
0c9c969a | 63 | kh_clear(idx, map); |
cee9ca66 PS |
64 | } |
65 | ||
0c9c969a | 66 | void git_idxmap_icase_clear(git_idxmap_icase *map) |
cee9ca66 | 67 | { |
0c9c969a | 68 | kh_clear(idxicase, map); |
cee9ca66 PS |
69 | } |
70 | ||
0c9c969a | 71 | int git_idxmap_resize(git_idxmap *map, size_t size) |
cee9ca66 | 72 | { |
0c9c969a UG |
73 | if (!git__is_uint32(size) || |
74 | kh_resize(idx, map, (khiter_t)size) < 0) { | |
75 | git_error_set_oom(); | |
76 | return -1; | |
77 | } | |
78 | return 0; | |
cee9ca66 PS |
79 | } |
80 | ||
0c9c969a | 81 | int git_idxmap_icase_resize(git_idxmap_icase *map, size_t size) |
cee9ca66 | 82 | { |
0c9c969a UG |
83 | if (!git__is_uint32(size) || |
84 | kh_resize(idxicase, map, (khiter_t)size) < 0) { | |
85 | git_error_set_oom(); | |
86 | return -1; | |
87 | } | |
88 | return 0; | |
cee9ca66 PS |
89 | } |
90 | ||
0c9c969a | 91 | void *git_idxmap_get(git_idxmap *map, const git_index_entry *key) |
cee9ca66 | 92 | { |
0c9c969a UG |
93 | size_t idx = kh_get(idx, map, key); |
94 | if (idx == kh_end(map) || !kh_exist(map, idx)) | |
95 | return NULL; | |
96 | return kh_val(map, idx); | |
cee9ca66 PS |
97 | } |
98 | ||
0c9c969a | 99 | int git_idxmap_set(git_idxmap *map, const git_index_entry *key, void *value) |
cee9ca66 | 100 | { |
0c9c969a UG |
101 | size_t idx; |
102 | int rval; | |
cee9ca66 | 103 | |
0c9c969a UG |
104 | idx = kh_put(idx, map, key, &rval); |
105 | if (rval < 0) | |
106 | return -1; | |
107 | ||
108 | if (rval == 0) | |
109 | kh_key(map, idx) = key; | |
110 | ||
111 | kh_val(map, idx) = value; | |
112 | ||
113 | return 0; | |
cee9ca66 PS |
114 | } |
115 | ||
0c9c969a | 116 | int git_idxmap_icase_set(git_idxmap_icase *map, const git_index_entry *key, void *value) |
cee9ca66 | 117 | { |
0c9c969a UG |
118 | size_t idx; |
119 | int rval; | |
120 | ||
121 | idx = kh_put(idxicase, map, key, &rval); | |
122 | if (rval < 0) | |
123 | return -1; | |
124 | ||
125 | if (rval == 0) | |
126 | kh_key(map, idx) = key; | |
127 | ||
128 | kh_val(map, idx) = value; | |
129 | ||
130 | return 0; | |
cee9ca66 PS |
131 | } |
132 | ||
0c9c969a | 133 | void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key) |
ac3d33df | 134 | { |
0c9c969a UG |
135 | size_t idx = kh_get(idxicase, map, key); |
136 | if (idx == kh_end(map) || !kh_exist(map, idx)) | |
137 | return NULL; | |
138 | return kh_val(map, idx); | |
ac3d33df JK |
139 | } |
140 | ||
0c9c969a | 141 | void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval) |
cee9ca66 | 142 | { |
0c9c969a UG |
143 | khiter_t idx = kh_put(idx, map, key, rval); |
144 | ||
145 | if ((*rval) >= 0) { | |
146 | if ((*rval) == 0) | |
147 | kh_key(map, idx) = key; | |
148 | kh_val(map, idx) = value; | |
149 | } | |
cee9ca66 PS |
150 | } |
151 | ||
0c9c969a | 152 | void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key, void *value, int *rval) |
ac3d33df | 153 | { |
0c9c969a UG |
154 | khiter_t idx = kh_put(idxicase, map, key, rval); |
155 | ||
156 | if ((*rval) >= 0) { | |
157 | if ((*rval) == 0) | |
158 | kh_key(map, idx) = key; | |
159 | kh_val(map, idx) = value; | |
160 | } | |
ac3d33df JK |
161 | } |
162 | ||
0c9c969a | 163 | int git_idxmap_delete(git_idxmap *map, const git_index_entry *key) |
cee9ca66 | 164 | { |
0c9c969a UG |
165 | khiter_t idx = kh_get(idx, map, key); |
166 | if (idx == kh_end(map)) | |
167 | return GIT_ENOTFOUND; | |
cee9ca66 | 168 | kh_del(idx, map, idx); |
0c9c969a | 169 | return 0; |
cee9ca66 PS |
170 | } |
171 | ||
0c9c969a | 172 | int git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key) |
cee9ca66 | 173 | { |
0c9c969a UG |
174 | khiter_t idx = kh_get(idxicase, map, key); |
175 | if (idx == kh_end(map)) | |
176 | return GIT_ENOTFOUND; | |
cee9ca66 | 177 | kh_del(idxicase, map, idx); |
0c9c969a | 178 | return 0; |
cee9ca66 | 179 | } |