]>
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 PS |
34 | |
35 | int git_idxmap_alloc(git_idxmap **map) | |
36 | { | |
37 | if ((*map = kh_init(idx)) == NULL) { | |
ac3d33df | 38 | git_error_set_oom(); |
cee9ca66 PS |
39 | return -1; |
40 | } | |
41 | ||
42 | return 0; | |
43 | } | |
44 | ||
45 | int git_idxmap_icase_alloc(git_idxmap_icase **map) | |
46 | { | |
47 | if ((*map = kh_init(idxicase)) == NULL) { | |
ac3d33df | 48 | git_error_set_oom(); |
cee9ca66 PS |
49 | return -1; |
50 | } | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval) | |
56 | { | |
57 | khiter_t idx = kh_put(idx, map, key, rval); | |
58 | ||
59 | if ((*rval) >= 0) { | |
60 | if ((*rval) == 0) | |
61 | kh_key(map, idx) = key; | |
62 | kh_val(map, idx) = value; | |
63 | } | |
64 | } | |
65 | ||
66 | void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key, void *value, int *rval) | |
67 | { | |
68 | khiter_t idx = kh_put(idxicase, map, key, rval); | |
69 | ||
70 | if ((*rval) >= 0) { | |
71 | if ((*rval) == 0) | |
72 | kh_key(map, idx) = key; | |
73 | kh_val(map, idx) = value; | |
74 | } | |
75 | } | |
76 | ||
77 | size_t git_idxmap_lookup_index(git_idxmap *map, const git_index_entry *key) | |
78 | { | |
79 | return kh_get(idx, map, key); | |
80 | } | |
81 | ||
82 | size_t git_idxmap_icase_lookup_index(git_idxmap_icase *map, const git_index_entry *key) | |
83 | { | |
84 | return kh_get(idxicase, map, key); | |
85 | } | |
86 | ||
87 | void *git_idxmap_value_at(git_idxmap *map, size_t idx) | |
88 | { | |
89 | return kh_val(map, idx); | |
90 | } | |
91 | ||
92 | int git_idxmap_valid_index(git_idxmap *map, size_t idx) | |
93 | { | |
94 | return idx != kh_end(map); | |
95 | } | |
96 | ||
97 | int git_idxmap_has_data(git_idxmap *map, size_t idx) | |
98 | { | |
99 | return kh_exist(map, idx); | |
100 | } | |
101 | ||
102 | void git_idxmap_resize(git_idxmap *map, size_t size) | |
103 | { | |
104 | kh_resize(idx, map, size); | |
105 | } | |
106 | ||
107 | void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size) | |
108 | { | |
109 | kh_resize(idxicase, map, size); | |
110 | } | |
111 | ||
94af9155 | 112 | void git_idxmap_free(git_idxmap *map) |
cee9ca66 PS |
113 | { |
114 | kh_destroy(idx, map); | |
115 | } | |
116 | ||
ac3d33df JK |
117 | void git_idxmap_icase_free(git_idxmap_icase *map) |
118 | { | |
119 | kh_destroy(idxicase, map); | |
120 | } | |
121 | ||
cee9ca66 PS |
122 | void git_idxmap_clear(git_idxmap *map) |
123 | { | |
124 | kh_clear(idx, map); | |
125 | } | |
126 | ||
ac3d33df JK |
127 | void git_idxmap_icase_clear(git_idxmap_icase *map) |
128 | { | |
129 | kh_clear(idxicase, map); | |
130 | } | |
131 | ||
cee9ca66 PS |
132 | void git_idxmap_delete_at(git_idxmap *map, size_t idx) |
133 | { | |
134 | kh_del(idx, map, idx); | |
135 | } | |
136 | ||
137 | void git_idxmap_icase_delete_at(git_idxmap_icase *map, size_t idx) | |
138 | { | |
139 | kh_del(idxicase, map, idx); | |
140 | } | |
141 | ||
142 | void git_idxmap_delete(git_idxmap *map, const git_index_entry *key) | |
143 | { | |
144 | khiter_t idx = git_idxmap_lookup_index(map, key); | |
145 | if (git_idxmap_valid_index(map, idx)) | |
146 | git_idxmap_delete_at(map, idx); | |
147 | } | |
148 | void git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key) | |
149 | { | |
150 | khiter_t idx = git_idxmap_icase_lookup_index(map, key); | |
151 | if (git_idxmap_valid_index((git_idxmap *)map, idx)) | |
152 | git_idxmap_icase_delete_at(map, idx); | |
153 | } |