strmap: remove legacy low-level interface Remove the low-level interface that was exposing implementation details of `git_strmap` to callers. From now on, only the high-level functions shall be used to retrieve or modify values of a map. Adjust remaining existing callers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
diff --git a/src/strmap.c b/src/strmap.c
index 1c7d5ef..c6e5b6d 100644
--- a/src/strmap.c
+++ b/src/strmap.c
@@ -43,9 +43,8 @@ size_t git_strmap_size(git_strmap *map)
void *git_strmap_get(git_strmap *map, const char *key)
{
- size_t idx = git_strmap_lookup_index(map, key);
- if (!git_strmap_valid_index(map, idx) ||
- !git_strmap_has_data(map, idx))
+ size_t idx = kh_get(str, map, key);
+ if (idx == kh_end(map) || !kh_exist(map, idx))
return NULL;
return kh_val(map, idx);
}
@@ -69,10 +68,10 @@ int git_strmap_set(git_strmap *map, const char *key, void *value)
int git_strmap_delete(git_strmap *map, const char *key)
{
- khiter_t idx = git_strmap_lookup_index(map, key);
- if (!git_strmap_valid_index(map, idx))
+ khiter_t idx = kh_get(str, map, key);
+ if (idx == kh_end(map))
return GIT_ENOTFOUND;
- git_strmap_delete_at(map, idx);
+ kh_del(str, map, idx);
return 0;
}
@@ -85,108 +84,17 @@ int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char *
{
size_t i = *iter;
- while (i < git_strmap_end(map) && !git_strmap_has_data(map, i))
+ while (i < map->n_buckets && !kh_exist(map, i))
i++;
- if (i >= git_strmap_end(map))
+ if (i >= map->n_buckets)
return GIT_ITEROVER;
if (key)
- *key = git_strmap_key(map, i);
+ *key = kh_key(map, i);
if (value)
- *value = git_strmap_value_at(map, i);
+ *value = kh_val(map, i);
*iter = ++i;
return 0;
}
-
-size_t git_strmap_lookup_index(git_strmap *map, const char *key)
-{
- return kh_get(str, map, key);
-}
-
-int git_strmap_valid_index(git_strmap *map, size_t idx)
-{
- return idx != kh_end(map);
-}
-
-int git_strmap_has_data(git_strmap *map, size_t idx)
-{
- return kh_exist(map, idx);
-}
-
-const char *git_strmap_key(git_strmap *map, size_t idx)
-{
- return kh_key(map, idx);
-}
-
-void git_strmap_set_key_at(git_strmap *map, size_t idx, char *key)
-{
- kh_val(map, idx) = key;
-}
-
-void *git_strmap_value_at(git_strmap *map, size_t idx)
-{
- return kh_val(map, idx);
-}
-
-void git_strmap_set_value_at(git_strmap *map, size_t idx, void *value)
-{
- kh_val(map, idx) = value;
-}
-
-void git_strmap_delete_at(git_strmap *map, size_t idx)
-{
- kh_del(str, map, idx);
-}
-
-int git_strmap_put(git_strmap *map, const char *key, int *err)
-{
- return kh_put(str, map, key, err);
-}
-
-void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval)
-{
- khiter_t idx = kh_put(str, map, key, rval);
-
- if ((*rval) >= 0) {
- if ((*rval) == 0)
- kh_key(map, idx) = key;
- kh_val(map, idx) = value;
- }
-}
-
-size_t git_strmap_begin(git_strmap *map)
-{
- GIT_UNUSED(map);
- return 0;
-}
-
-size_t git_strmap_end(git_strmap *map)
-{
- return map->n_buckets;
-}
-
-int git_strmap_next(
- void **data,
- size_t* iter,
- git_strmap *map)
-{
- if (!map)
- return GIT_ERROR;
-
- while (*iter != git_strmap_end(map)) {
- if (!(git_strmap_has_data(map, *iter))) {
- ++(*iter);
- continue;
- }
-
- *data = git_strmap_value_at(map, *iter);
-
- ++(*iter);
-
- return GIT_OK;
- }
-
- return GIT_ITEROVER;
-}
diff --git a/src/strmap.h b/src/strmap.h
index ce547ac..9f5e4cc 100644
--- a/src/strmap.h
+++ b/src/strmap.h
@@ -118,20 +118,6 @@ int git_strmap_exists(git_strmap *map, const char *key);
*/
int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char **key);
-size_t git_strmap_lookup_index(git_strmap *map, const char *key);
-int git_strmap_valid_index(git_strmap *map, size_t idx);
-
-int git_strmap_has_data(git_strmap *map, size_t idx);
-
-const char *git_strmap_key(git_strmap *map, size_t idx);
-void git_strmap_set_key_at(git_strmap *map, size_t idx, char *key);
-void *git_strmap_value_at(git_strmap *map, size_t idx);
-void git_strmap_set_value_at(git_strmap *map, size_t idx, void *value);
-void git_strmap_delete_at(git_strmap *map, size_t idx);
-
-int git_strmap_put(git_strmap *map, const char *key, int *err);
-void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval);
-
#define git_strmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \
while (git_strmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \
code; \
@@ -142,12 +128,4 @@ void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval)
code; \
} }
-size_t git_strmap_begin(git_strmap *map);
-size_t git_strmap_end(git_strmap *map);
-
-int git_strmap_next(
- void **data,
- size_t *iter,
- git_strmap *map);
-
#endif
diff --git a/tests/core/strmap.c b/tests/core/strmap.c
index d3a3939..ba118ae 100644
--- a/tests/core/strmap.c
+++ b/tests/core/strmap.c
@@ -22,7 +22,6 @@ void test_core_strmap__0(void)
static void insert_strings(git_strmap *table, size_t count)
{
size_t i, j, over;
- int err;
char *str;
for (i = 0; i < count; ++i) {
@@ -35,17 +34,16 @@ static void insert_strings(git_strmap *table, size_t count)
for (j = 0, over = i / 26; over > 0; j++, over = over / 26)
str[j] = 'A' + (over % 26);
- git_strmap_insert(table, str, str, &err);
- cl_assert(err >= 0);
+ cl_git_pass(git_strmap_set(table, str, str));
}
cl_assert_equal_i(git_strmap_size(table), count);
}
-void test_core_strmap__1(void)
+void test_core_strmap__inserted_strings_can_be_retrieved(void)
{
- int i;
char *str;
+ int i;
insert_strings(g_table, 20);
@@ -59,25 +57,18 @@ void test_core_strmap__1(void)
cl_assert(i == 20);
}
-void test_core_strmap__2(void)
+void test_core_strmap__deleted_entry_cannot_be_retrieved(void)
{
- size_t pos;
- int i;
char *str;
+ int i;
insert_strings(g_table, 20);
- cl_assert(git_strmap_exists(g_table, "aaaaaaaaa"));
- cl_assert(git_strmap_exists(g_table, "ggggggggg"));
- cl_assert(!git_strmap_exists(g_table, "aaaaaaaab"));
- cl_assert(!git_strmap_exists(g_table, "abcdefghi"));
-
cl_assert(git_strmap_exists(g_table, "bbbbbbbbb"));
- pos = git_strmap_lookup_index(g_table, "bbbbbbbbb");
- cl_assert(git_strmap_valid_index(g_table, pos));
- cl_assert_equal_s(git_strmap_value_at(g_table, pos), "bbbbbbbbb");
- free(git_strmap_value_at(g_table, pos));
- git_strmap_delete_at(g_table, pos);
+ str = git_strmap_get(g_table, "bbbbbbbbb");
+ cl_assert_equal_s(str, "bbbbbbbbb");
+ cl_git_pass(git_strmap_delete(g_table, "bbbbbbbbb"));
+ free(str);
cl_assert(!git_strmap_exists(g_table, "bbbbbbbbb"));
@@ -86,10 +77,10 @@ void test_core_strmap__2(void)
cl_assert_equal_i(i, 19);
}
-void test_core_strmap__3(void)
+void test_core_strmap__inserting_many_keys_succeeds(void)
{
- int i;
char *str;
+ int i;
insert_strings(g_table, 10000);
@@ -102,13 +93,10 @@ void test_core_strmap__get_succeeds_with_existing_entries(void)
{
const char *keys[] = { "foo", "bar", "gobble" };
char *values[] = { "oof", "rab", "elbbog" };
- int error;
size_t i;
- for (i = 0; i < ARRAY_SIZE(keys); i++) {
- git_strmap_insert(g_table, keys[i], values[i], &error);
- cl_assert_equal_i(error, 1);
- }
+ for (i = 0; i < ARRAY_SIZE(keys); i++)
+ cl_git_pass(git_strmap_set(g_table, keys[i], values[i]));
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "oof");
cl_assert_equal_s(git_strmap_get(g_table, "bar"), "rab");
@@ -119,13 +107,10 @@ void test_core_strmap__get_returns_null_on_nonexisting_key(void)
{
const char *keys[] = { "foo", "bar", "gobble" };
char *values[] = { "oof", "rab", "elbbog" };
- int error;
size_t i;
- for (i = 0; i < ARRAY_SIZE(keys); i++) {
- git_strmap_insert(g_table, keys[i], values[i], &error);
- cl_assert_equal_i(error, 1);
- }
+ for (i = 0; i < ARRAY_SIZE(keys); i++)
+ cl_git_pass(git_strmap_set(g_table, keys[i], values[i]));
cl_assert_equal_p(git_strmap_get(g_table, "other"), NULL);
}