Skip to content

Commit fdfabdc

Browse files
committed
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 parent 6a9117f commit fdfabdc

File tree

3 files changed

+24
-153
lines changed

3 files changed

+24
-153
lines changed

src/strmap.c

Lines changed: 9 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ size_t git_strmap_size(git_strmap *map)
4343

4444
void *git_strmap_get(git_strmap *map, const char *key)
4545
{
46-
size_t idx = git_strmap_lookup_index(map, key);
47-
if (!git_strmap_valid_index(map, idx) ||
48-
!git_strmap_has_data(map, idx))
46+
size_t idx = kh_get(str, map, key);
47+
if (idx == kh_end(map) || !kh_exist(map, idx))
4948
return NULL;
5049
return kh_val(map, idx);
5150
}
@@ -69,10 +68,10 @@ int git_strmap_set(git_strmap *map, const char *key, void *value)
6968

7069
int git_strmap_delete(git_strmap *map, const char *key)
7170
{
72-
khiter_t idx = git_strmap_lookup_index(map, key);
73-
if (!git_strmap_valid_index(map, idx))
71+
khiter_t idx = kh_get(str, map, key);
72+
if (idx == kh_end(map))
7473
return GIT_ENOTFOUND;
75-
git_strmap_delete_at(map, idx);
74+
kh_del(str, map, idx);
7675
return 0;
7776
}
7877

@@ -85,108 +84,17 @@ int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char *
8584
{
8685
size_t i = *iter;
8786

88-
while (i < git_strmap_end(map) && !git_strmap_has_data(map, i))
87+
while (i < map->n_buckets && !kh_exist(map, i))
8988
i++;
9089

91-
if (i >= git_strmap_end(map))
90+
if (i >= map->n_buckets)
9291
return GIT_ITEROVER;
9392

9493
if (key)
95-
*key = git_strmap_key(map, i);
94+
*key = kh_key(map, i);
9695
if (value)
97-
*value = git_strmap_value_at(map, i);
96+
*value = kh_val(map, i);
9897
*iter = ++i;
9998

10099
return 0;
101100
}
102-
103-
size_t git_strmap_lookup_index(git_strmap *map, const char *key)
104-
{
105-
return kh_get(str, map, key);
106-
}
107-
108-
int git_strmap_valid_index(git_strmap *map, size_t idx)
109-
{
110-
return idx != kh_end(map);
111-
}
112-
113-
int git_strmap_has_data(git_strmap *map, size_t idx)
114-
{
115-
return kh_exist(map, idx);
116-
}
117-
118-
const char *git_strmap_key(git_strmap *map, size_t idx)
119-
{
120-
return kh_key(map, idx);
121-
}
122-
123-
void git_strmap_set_key_at(git_strmap *map, size_t idx, char *key)
124-
{
125-
kh_val(map, idx) = key;
126-
}
127-
128-
void *git_strmap_value_at(git_strmap *map, size_t idx)
129-
{
130-
return kh_val(map, idx);
131-
}
132-
133-
void git_strmap_set_value_at(git_strmap *map, size_t idx, void *value)
134-
{
135-
kh_val(map, idx) = value;
136-
}
137-
138-
void git_strmap_delete_at(git_strmap *map, size_t idx)
139-
{
140-
kh_del(str, map, idx);
141-
}
142-
143-
int git_strmap_put(git_strmap *map, const char *key, int *err)
144-
{
145-
return kh_put(str, map, key, err);
146-
}
147-
148-
void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval)
149-
{
150-
khiter_t idx = kh_put(str, map, key, rval);
151-
152-
if ((*rval) >= 0) {
153-
if ((*rval) == 0)
154-
kh_key(map, idx) = key;
155-
kh_val(map, idx) = value;
156-
}
157-
}
158-
159-
size_t git_strmap_begin(git_strmap *map)
160-
{
161-
GIT_UNUSED(map);
162-
return 0;
163-
}
164-
165-
size_t git_strmap_end(git_strmap *map)
166-
{
167-
return map->n_buckets;
168-
}
169-
170-
int git_strmap_next(
171-
void **data,
172-
size_t* iter,
173-
git_strmap *map)
174-
{
175-
if (!map)
176-
return GIT_ERROR;
177-
178-
while (*iter != git_strmap_end(map)) {
179-
if (!(git_strmap_has_data(map, *iter))) {
180-
++(*iter);
181-
continue;
182-
}
183-
184-
*data = git_strmap_value_at(map, *iter);
185-
186-
++(*iter);
187-
188-
return GIT_OK;
189-
}
190-
191-
return GIT_ITEROVER;
192-
}

src/strmap.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,6 @@ int git_strmap_exists(git_strmap *map, const char *key);
118118
*/
119119
int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char **key);
120120

121-
size_t git_strmap_lookup_index(git_strmap *map, const char *key);
122-
int git_strmap_valid_index(git_strmap *map, size_t idx);
123-
124-
int git_strmap_has_data(git_strmap *map, size_t idx);
125-
126-
const char *git_strmap_key(git_strmap *map, size_t idx);
127-
void git_strmap_set_key_at(git_strmap *map, size_t idx, char *key);
128-
void *git_strmap_value_at(git_strmap *map, size_t idx);
129-
void git_strmap_set_value_at(git_strmap *map, size_t idx, void *value);
130-
void git_strmap_delete_at(git_strmap *map, size_t idx);
131-
132-
int git_strmap_put(git_strmap *map, const char *key, int *err);
133-
void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval);
134-
135121
#define git_strmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \
136122
while (git_strmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \
137123
code; \
@@ -142,12 +128,4 @@ void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval)
142128
code; \
143129
} }
144130

145-
size_t git_strmap_begin(git_strmap *map);
146-
size_t git_strmap_end(git_strmap *map);
147-
148-
int git_strmap_next(
149-
void **data,
150-
size_t *iter,
151-
git_strmap *map);
152-
153131
#endif

tests/core/strmap.c

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ void test_core_strmap__0(void)
2222
static void insert_strings(git_strmap *table, size_t count)
2323
{
2424
size_t i, j, over;
25-
int err;
2625
char *str;
2726

2827
for (i = 0; i < count; ++i) {
@@ -35,17 +34,16 @@ static void insert_strings(git_strmap *table, size_t count)
3534
for (j = 0, over = i / 26; over > 0; j++, over = over / 26)
3635
str[j] = 'A' + (over % 26);
3736

38-
git_strmap_insert(table, str, str, &err);
39-
cl_assert(err >= 0);
37+
cl_git_pass(git_strmap_set(table, str, str));
4038
}
4139

4240
cl_assert_equal_i(git_strmap_size(table), count);
4341
}
4442

45-
void test_core_strmap__1(void)
43+
void test_core_strmap__inserted_strings_can_be_retrieved(void)
4644
{
47-
int i;
4845
char *str;
46+
int i;
4947

5048
insert_strings(g_table, 20);
5149

@@ -59,25 +57,18 @@ void test_core_strmap__1(void)
5957
cl_assert(i == 20);
6058
}
6159

62-
void test_core_strmap__2(void)
60+
void test_core_strmap__deleted_entry_cannot_be_retrieved(void)
6361
{
64-
size_t pos;
65-
int i;
6662
char *str;
63+
int i;
6764

6865
insert_strings(g_table, 20);
6966

70-
cl_assert(git_strmap_exists(g_table, "aaaaaaaaa"));
71-
cl_assert(git_strmap_exists(g_table, "ggggggggg"));
72-
cl_assert(!git_strmap_exists(g_table, "aaaaaaaab"));
73-
cl_assert(!git_strmap_exists(g_table, "abcdefghi"));
74-
7567
cl_assert(git_strmap_exists(g_table, "bbbbbbbbb"));
76-
pos = git_strmap_lookup_index(g_table, "bbbbbbbbb");
77-
cl_assert(git_strmap_valid_index(g_table, pos));
78-
cl_assert_equal_s(git_strmap_value_at(g_table, pos), "bbbbbbbbb");
79-
free(git_strmap_value_at(g_table, pos));
80-
git_strmap_delete_at(g_table, pos);
68+
str = git_strmap_get(g_table, "bbbbbbbbb");
69+
cl_assert_equal_s(str, "bbbbbbbbb");
70+
cl_git_pass(git_strmap_delete(g_table, "bbbbbbbbb"));
71+
free(str);
8172

8273
cl_assert(!git_strmap_exists(g_table, "bbbbbbbbb"));
8374

@@ -86,10 +77,10 @@ void test_core_strmap__2(void)
8677
cl_assert_equal_i(i, 19);
8778
}
8879

89-
void test_core_strmap__3(void)
80+
void test_core_strmap__inserting_many_keys_succeeds(void)
9081
{
91-
int i;
9282
char *str;
83+
int i;
9384

9485
insert_strings(g_table, 10000);
9586

@@ -102,13 +93,10 @@ void test_core_strmap__get_succeeds_with_existing_entries(void)
10293
{
10394
const char *keys[] = { "foo", "bar", "gobble" };
10495
char *values[] = { "oof", "rab", "elbbog" };
105-
int error;
10696
size_t i;
10797

108-
for (i = 0; i < ARRAY_SIZE(keys); i++) {
109-
git_strmap_insert(g_table, keys[i], values[i], &error);
110-
cl_assert_equal_i(error, 1);
111-
}
98+
for (i = 0; i < ARRAY_SIZE(keys); i++)
99+
cl_git_pass(git_strmap_set(g_table, keys[i], values[i]));
112100

113101
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "oof");
114102
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)
119107
{
120108
const char *keys[] = { "foo", "bar", "gobble" };
121109
char *values[] = { "oof", "rab", "elbbog" };
122-
int error;
123110
size_t i;
124111

125-
for (i = 0; i < ARRAY_SIZE(keys); i++) {
126-
git_strmap_insert(g_table, keys[i], values[i], &error);
127-
cl_assert_equal_i(error, 1);
128-
}
112+
for (i = 0; i < ARRAY_SIZE(keys); i++)
113+
cl_git_pass(git_strmap_set(g_table, keys[i], values[i]));
129114

130115
cl_assert_equal_p(git_strmap_get(g_table, "other"), NULL);
131116
}

0 commit comments

Comments
 (0)