Skip to content

Commit 18cf569

Browse files
committed
maps: provide high-level iteration interface
Currently, our headers need to leak some implementation details of maps due to their direct use of indices in the implementation of their foreach macros. This makes it impossible to completely hide the map structures away, and also makes it impossible to include the khash implementation header in the C files of the respective map only. This is now being fixed by providing a high-level iteration interface `map_iterate`, which takes as inputs the map that shall be iterated over, an iterator as well as the locations where keys and values shall be put into. For simplicity's sake, the iterator is a simple `size_t` that shall initialized to `0` on the first call. All existing foreach macros are then adjusted to make use of this new function.
1 parent c50a8ac commit 18cf569

File tree

8 files changed

+180
-25
lines changed

8 files changed

+180
-25
lines changed

src/offmap.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ int git_offmap_exists(git_offmap *map, const git_off_t key)
8282
return kh_get(off, map, key) != kh_end(map);
8383
}
8484

85+
int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, git_off_t *key)
86+
{
87+
size_t i = *iter;
88+
89+
while (i < git_offmap_end(map) && !git_offmap_has_data(map, i))
90+
i++;
91+
92+
if (i >= git_offmap_end(map))
93+
return GIT_ITEROVER;
94+
95+
if (key)
96+
*key = git_offmap_key_at(map, i);
97+
if (value)
98+
*value = git_offmap_value_at(map, i);
99+
*iter = ++i;
100+
101+
return 0;
102+
}
103+
85104
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key)
86105
{
87106
return kh_get(off, map, key);

src/offmap.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,27 @@ int git_offmap_delete(git_offmap *map, const git_off_t key);
9999
*/
100100
int git_offmap_exists(git_offmap *map, const git_off_t key);
101101

102+
/**
103+
* Iterate over entries of the map.
104+
*
105+
* This functions allows to iterate over all key-value entries of
106+
* the map. The current position is stored in the `iter` variable
107+
* and should be initialized to `0` before the first call to this
108+
* function.
109+
*
110+
* @param map map to iterate over
111+
* @param value pointer to the variable where to store the current
112+
* value. May be NULL.
113+
* @param iter iterator storing the current position. Initialize
114+
* with zero previous to the first call.
115+
* @param key pointer to the variable where to store the current
116+
* key. May be NULL.
117+
* @return `0` if the next entry was correctly retrieved.
118+
* GIT_ITEROVER if no entries are left. A negative error
119+
* code otherwise.
120+
*/
121+
int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, git_off_t *key);
122+
102123
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key);
103124
int git_offmap_valid_index(git_offmap *map, size_t idx);
104125

@@ -115,18 +136,13 @@ void git_offmap_insert(git_offmap *map, const git_off_t key, void *value, int *r
115136
size_t git_offmap_begin(git_offmap *map);
116137
size_t git_offmap_end(git_offmap *map);
117138

118-
#define git_offmap_foreach(h, kvar, vvar, code) { size_t __i; \
119-
for (__i = git_offmap_begin(h); __i != git_offmap_end(h); ++__i) { \
120-
if (!git_offmap_has_data(h,__i)) continue; \
121-
(kvar) = git_offmap_key_at(h,__i); \
122-
(vvar) = git_offmap_value_at(h,__i); \
139+
#define git_offmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \
140+
while (git_offmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \
123141
code; \
124142
} }
125143

126-
#define git_offmap_foreach_value(h, vvar, code) { size_t __i; \
127-
for (__i = git_offmap_begin(h); __i != git_offmap_end(h); ++__i) { \
128-
if (!git_offmap_has_data(h,__i)) continue; \
129-
(vvar) = git_offmap_value_at(h,__i); \
144+
#define git_offmap_foreach_value(h, vvar, code) { size_t __i = 0; \
145+
while (git_offmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \
130146
code; \
131147
} }
132148

src/oidmap.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ int git_oidmap_exists(git_oidmap *map, const git_oid *key)
8888
return kh_get(oid, map, key) != kh_end(map);
8989
}
9090

91+
int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oid **key)
92+
{
93+
size_t i = *iter;
94+
95+
while (i < git_oidmap_end(map) && !git_oidmap_has_data(map, i))
96+
i++;
97+
98+
if (i >= git_oidmap_end(map))
99+
return GIT_ITEROVER;
100+
101+
if (key)
102+
*key = git_oidmap_key(map, i);
103+
if (value)
104+
*value = git_oidmap_value_at(map, i);
105+
*iter = ++i;
106+
107+
return 0;
108+
}
109+
91110
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key)
92111
{
93112
return kh_get(oid, map, key);

src/oidmap.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,27 @@ int git_oidmap_delete(git_oidmap *map, const git_oid *key);
9999
*/
100100
int git_oidmap_exists(git_oidmap *map, const git_oid *key);
101101

102+
/**
103+
* Iterate over entries of the map.
104+
*
105+
* This functions allows to iterate over all key-value entries of
106+
* the map. The current position is stored in the `iter` variable
107+
* and should be initialized to `0` before the first call to this
108+
* function.
109+
*
110+
* @param map map to iterate over
111+
* @param value pointer to the variable where to store the current
112+
* value. May be NULL.
113+
* @param iter iterator storing the current position. Initialize
114+
* with zero previous to the first call.
115+
* @param key pointer to the variable where to store the current
116+
* key. May be NULL.
117+
* @return `0` if the next entry was correctly retrieved.
118+
* GIT_ITEROVER if no entries are left. A negative error
119+
* code otherwise.
120+
*/
121+
int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oid **key);
122+
102123
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key);
103124
int git_oidmap_valid_index(git_oidmap *map, size_t idx);
104125

@@ -116,10 +137,8 @@ void git_oidmap_insert(git_oidmap *map, const git_oid *key, void *value, int *rv
116137
size_t git_oidmap_begin(git_oidmap *map);
117138
size_t git_oidmap_end(git_oidmap *map);
118139

119-
#define git_oidmap_foreach_value(h, vvar, code) { size_t __i; \
120-
for (__i = git_oidmap_begin(h); __i != git_oidmap_end(h); ++__i) { \
121-
if (!git_oidmap_has_data(h,__i)) continue; \
122-
(vvar) = git_oidmap_value_at(h,__i); \
140+
#define git_oidmap_foreach_value(h, vvar, code) { size_t __i = 0; \
141+
while (git_oidmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \
123142
code; \
124143
} }
125144

src/strmap.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ int git_strmap_exists(git_strmap *map, const char *key)
8181
return kh_get(str, map, key) != kh_end(map);
8282
}
8383

84+
int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char **key)
85+
{
86+
size_t i = *iter;
87+
88+
while (i < git_strmap_end(map) && !git_strmap_has_data(map, i))
89+
i++;
90+
91+
if (i >= git_strmap_end(map))
92+
return GIT_ITEROVER;
93+
94+
if (key)
95+
*key = git_strmap_key(map, i);
96+
if (value)
97+
*value = git_strmap_value_at(map, i);
98+
*iter = ++i;
99+
100+
return 0;
101+
}
102+
84103
size_t git_strmap_lookup_index(git_strmap *map, const char *key)
85104
{
86105
return kh_get(str, map, key);

src/strmap.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,27 @@ int git_strmap_delete(git_strmap *map, const char *key);
9797
*/
9898
int git_strmap_exists(git_strmap *map, const char *key);
9999

100+
/**
101+
* Iterate over entries of the map.
102+
*
103+
* This functions allows to iterate over all key-value entries of
104+
* the map. The current position is stored in the `iter` variable
105+
* and should be initialized to `0` before the first call to this
106+
* function.
107+
*
108+
* @param map map to iterate over
109+
* @param value pointer to the variable where to store the current
110+
* value. May be NULL.
111+
* @param iter iterator storing the current position. Initialize
112+
* with zero previous to the first call.
113+
* @param key pointer to the variable where to store the current
114+
* key. May be NULL.
115+
* @return `0` if the next entry was correctly retrieved.
116+
* GIT_ITEROVER if no entries are left. A negative error
117+
* code otherwise.
118+
*/
119+
int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char **key);
120+
100121
size_t git_strmap_lookup_index(git_strmap *map, const char *key);
101122
int git_strmap_valid_index(git_strmap *map, size_t idx);
102123

@@ -111,18 +132,13 @@ void git_strmap_delete_at(git_strmap *map, size_t idx);
111132
int git_strmap_put(git_strmap *map, const char *key, int *err);
112133
void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval);
113134

114-
#define git_strmap_foreach(h, kvar, vvar, code) { size_t __i; \
115-
for (__i = git_strmap_begin(h); __i != git_strmap_end(h); ++__i) { \
116-
if (!git_strmap_has_data(h,__i)) continue; \
117-
(kvar) = git_strmap_key(h,__i); \
118-
(vvar) = git_strmap_value_at(h,__i); \
135+
#define git_strmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \
136+
while (git_strmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \
119137
code; \
120138
} }
121139

122-
#define git_strmap_foreach_value(h, vvar, code) { size_t __i; \
123-
for (__i = git_strmap_begin(h); __i != git_strmap_end(h); ++__i) { \
124-
if (!git_strmap_has_data(h,__i)) continue; \
125-
(vvar) = git_strmap_value_at(h,__i); \
140+
#define git_strmap_foreach_value(h, vvar, code) { size_t __i = 0; \
141+
while (git_strmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \
126142
code; \
127143
} }
128144

tests/core/strmap.c

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void test_core_strmap__2(void)
8383

8484
i = 0;
8585
git_strmap_foreach_value(g_table, str, { i++; free(str); });
86-
cl_assert(i == 19);
86+
cl_assert_equal_i(i, 19);
8787
}
8888

8989
void test_core_strmap__3(void)
@@ -95,7 +95,7 @@ void test_core_strmap__3(void)
9595

9696
i = 0;
9797
git_strmap_foreach_value(g_table, str, { i++; free(str); });
98-
cl_assert(i == 10000);
98+
cl_assert_equal_i(i, 10000);
9999
}
100100

101101
void test_core_strmap__get_succeeds_with_existing_entries(void)
@@ -156,3 +156,50 @@ void test_core_strmap__set_updates_existing_key(void)
156156

157157
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "other");
158158
}
159+
160+
void test_core_strmap__iteration(void)
161+
{
162+
struct {
163+
char *key;
164+
char *value;
165+
int seen;
166+
} entries[] = {
167+
{ "foo", "oof" },
168+
{ "bar", "rab" },
169+
{ "gobble", "elbbog" },
170+
};
171+
const char *key, *value;
172+
size_t i, n;
173+
174+
for (i = 0; i < ARRAY_SIZE(entries); i++)
175+
cl_git_pass(git_strmap_set(g_table, entries[i].key, entries[i].value));
176+
177+
i = 0, n = 0;
178+
while (git_strmap_iterate((void **) &value, g_table, &i, &key) == 0) {
179+
size_t j;
180+
181+
for (j = 0; j < ARRAY_SIZE(entries); j++) {
182+
if (strcmp(entries[j].key, key))
183+
continue;
184+
185+
cl_assert_equal_i(entries[j].seen, 0);
186+
cl_assert_equal_s(entries[j].value, value);
187+
entries[j].seen++;
188+
break;
189+
}
190+
191+
n++;
192+
}
193+
194+
for (i = 0; i < ARRAY_SIZE(entries); i++)
195+
cl_assert_equal_i(entries[i].seen, 1);
196+
197+
cl_assert_equal_i(n, ARRAY_SIZE(entries));
198+
}
199+
200+
void test_core_strmap__iterating_empty_map_stops_immediately(void)
201+
{
202+
size_t i = 0;
203+
204+
cl_git_fail_with(git_strmap_iterate(NULL, g_table, &i, NULL), GIT_ITEROVER);
205+
}

tests/pack/sharing.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void test_pack_sharing__open_two_repos(void)
2424
cl_git_pass(git_object_lookup(&obj2, repo2, &id, GIT_OBJECT_ANY));
2525

2626
pos = 0;
27-
while ((error = git_strmap_next(&data, &pos, git__pack_cache)) == 0) {
27+
while ((error = git_strmap_iterate(&data, git__pack_cache, &pos, NULL)) == 0) {
2828
struct git_pack_file *pack = (struct git_pack_file *) data;
2929

3030
cl_assert_equal_i(2, pack->refcount.val);

0 commit comments

Comments
 (0)