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 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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
diff --git a/src/offmap.c b/src/offmap.c
index 09f8a2e..162501d 100644
--- a/src/offmap.c
+++ b/src/offmap.c
@@ -82,6 +82,25 @@ int git_offmap_exists(git_offmap *map, const git_off_t key)
return kh_get(off, map, key) != kh_end(map);
}
+int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, git_off_t *key)
+{
+ size_t i = *iter;
+
+ while (i < git_offmap_end(map) && !git_offmap_has_data(map, i))
+ i++;
+
+ if (i >= git_offmap_end(map))
+ return GIT_ITEROVER;
+
+ if (key)
+ *key = git_offmap_key_at(map, i);
+ if (value)
+ *value = git_offmap_value_at(map, i);
+ *iter = ++i;
+
+ return 0;
+}
+
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key)
{
return kh_get(off, map, key);
diff --git a/src/offmap.h b/src/offmap.h
index f2a8cad..456e2ae 100644
--- a/src/offmap.h
+++ b/src/offmap.h
@@ -99,6 +99,27 @@ int git_offmap_delete(git_offmap *map, const git_off_t key);
*/
int git_offmap_exists(git_offmap *map, const git_off_t key);
+/**
+ * Iterate over entries of the map.
+ *
+ * This functions allows to iterate over all key-value entries of
+ * the map. The current position is stored in the `iter` variable
+ * and should be initialized to `0` before the first call to this
+ * function.
+ *
+ * @param map map to iterate over
+ * @param value pointer to the variable where to store the current
+ * value. May be NULL.
+ * @param iter iterator storing the current position. Initialize
+ * with zero previous to the first call.
+ * @param key pointer to the variable where to store the current
+ * key. May be NULL.
+ * @return `0` if the next entry was correctly retrieved.
+ * GIT_ITEROVER if no entries are left. A negative error
+ * code otherwise.
+ */
+int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, git_off_t *key);
+
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key);
int git_offmap_valid_index(git_offmap *map, size_t idx);
@@ -115,18 +136,13 @@ void git_offmap_insert(git_offmap *map, const git_off_t key, void *value, int *r
size_t git_offmap_begin(git_offmap *map);
size_t git_offmap_end(git_offmap *map);
-#define git_offmap_foreach(h, kvar, vvar, code) { size_t __i; \
- for (__i = git_offmap_begin(h); __i != git_offmap_end(h); ++__i) { \
- if (!git_offmap_has_data(h,__i)) continue; \
- (kvar) = git_offmap_key_at(h,__i); \
- (vvar) = git_offmap_value_at(h,__i); \
+#define git_offmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \
+ while (git_offmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \
code; \
} }
-#define git_offmap_foreach_value(h, vvar, code) { size_t __i; \
- for (__i = git_offmap_begin(h); __i != git_offmap_end(h); ++__i) { \
- if (!git_offmap_has_data(h,__i)) continue; \
- (vvar) = git_offmap_value_at(h,__i); \
+#define git_offmap_foreach_value(h, vvar, code) { size_t __i = 0; \
+ while (git_offmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \
code; \
} }
diff --git a/src/oidmap.c b/src/oidmap.c
index 47a023f..8893a8b 100644
--- a/src/oidmap.c
+++ b/src/oidmap.c
@@ -88,6 +88,25 @@ int git_oidmap_exists(git_oidmap *map, const git_oid *key)
return kh_get(oid, map, key) != kh_end(map);
}
+int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oid **key)
+{
+ size_t i = *iter;
+
+ while (i < git_oidmap_end(map) && !git_oidmap_has_data(map, i))
+ i++;
+
+ if (i >= git_oidmap_end(map))
+ return GIT_ITEROVER;
+
+ if (key)
+ *key = git_oidmap_key(map, i);
+ if (value)
+ *value = git_oidmap_value_at(map, i);
+ *iter = ++i;
+
+ return 0;
+}
+
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key)
{
return kh_get(oid, map, key);
diff --git a/src/oidmap.h b/src/oidmap.h
index 048cbbb..6f57131 100644
--- a/src/oidmap.h
+++ b/src/oidmap.h
@@ -99,6 +99,27 @@ int git_oidmap_delete(git_oidmap *map, const git_oid *key);
*/
int git_oidmap_exists(git_oidmap *map, const git_oid *key);
+/**
+ * Iterate over entries of the map.
+ *
+ * This functions allows to iterate over all key-value entries of
+ * the map. The current position is stored in the `iter` variable
+ * and should be initialized to `0` before the first call to this
+ * function.
+ *
+ * @param map map to iterate over
+ * @param value pointer to the variable where to store the current
+ * value. May be NULL.
+ * @param iter iterator storing the current position. Initialize
+ * with zero previous to the first call.
+ * @param key pointer to the variable where to store the current
+ * key. May be NULL.
+ * @return `0` if the next entry was correctly retrieved.
+ * GIT_ITEROVER if no entries are left. A negative error
+ * code otherwise.
+ */
+int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oid **key);
+
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key);
int git_oidmap_valid_index(git_oidmap *map, size_t idx);
@@ -116,10 +137,8 @@ void git_oidmap_insert(git_oidmap *map, const git_oid *key, void *value, int *rv
size_t git_oidmap_begin(git_oidmap *map);
size_t git_oidmap_end(git_oidmap *map);
-#define git_oidmap_foreach_value(h, vvar, code) { size_t __i; \
- for (__i = git_oidmap_begin(h); __i != git_oidmap_end(h); ++__i) { \
- if (!git_oidmap_has_data(h,__i)) continue; \
- (vvar) = git_oidmap_value_at(h,__i); \
+#define git_oidmap_foreach_value(h, vvar, code) { size_t __i = 0; \
+ while (git_oidmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \
code; \
} }
diff --git a/src/strmap.c b/src/strmap.c
index 94c4668..1c7d5ef 100644
--- a/src/strmap.c
+++ b/src/strmap.c
@@ -81,6 +81,25 @@ int git_strmap_exists(git_strmap *map, const char *key)
return kh_get(str, map, key) != kh_end(map);
}
+int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char **key)
+{
+ size_t i = *iter;
+
+ while (i < git_strmap_end(map) && !git_strmap_has_data(map, i))
+ i++;
+
+ if (i >= git_strmap_end(map))
+ return GIT_ITEROVER;
+
+ if (key)
+ *key = git_strmap_key(map, i);
+ if (value)
+ *value = git_strmap_value_at(map, i);
+ *iter = ++i;
+
+ return 0;
+}
+
size_t git_strmap_lookup_index(git_strmap *map, const char *key)
{
return kh_get(str, map, key);
diff --git a/src/strmap.h b/src/strmap.h
index 6ed920c..ce547ac 100644
--- a/src/strmap.h
+++ b/src/strmap.h
@@ -97,6 +97,27 @@ int git_strmap_delete(git_strmap *map, const char *key);
*/
int git_strmap_exists(git_strmap *map, const char *key);
+/**
+ * Iterate over entries of the map.
+ *
+ * This functions allows to iterate over all key-value entries of
+ * the map. The current position is stored in the `iter` variable
+ * and should be initialized to `0` before the first call to this
+ * function.
+ *
+ * @param map map to iterate over
+ * @param value pointer to the variable where to store the current
+ * value. May be NULL.
+ * @param iter iterator storing the current position. Initialize
+ * with zero previous to the first call.
+ * @param key pointer to the variable where to store the current
+ * key. May be NULL.
+ * @return `0` if the next entry was correctly retrieved.
+ * GIT_ITEROVER if no entries are left. A negative error
+ * code otherwise.
+ */
+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);
@@ -111,18 +132,13 @@ 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; \
- for (__i = git_strmap_begin(h); __i != git_strmap_end(h); ++__i) { \
- if (!git_strmap_has_data(h,__i)) continue; \
- (kvar) = git_strmap_key(h,__i); \
- (vvar) = git_strmap_value_at(h,__i); \
+#define git_strmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \
+ while (git_strmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \
code; \
} }
-#define git_strmap_foreach_value(h, vvar, code) { size_t __i; \
- for (__i = git_strmap_begin(h); __i != git_strmap_end(h); ++__i) { \
- if (!git_strmap_has_data(h,__i)) continue; \
- (vvar) = git_strmap_value_at(h,__i); \
+#define git_strmap_foreach_value(h, vvar, code) { size_t __i = 0; \
+ while (git_strmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \
code; \
} }
diff --git a/tests/core/strmap.c b/tests/core/strmap.c
index 4d36b74..d3a3939 100644
--- a/tests/core/strmap.c
+++ b/tests/core/strmap.c
@@ -83,7 +83,7 @@ void test_core_strmap__2(void)
i = 0;
git_strmap_foreach_value(g_table, str, { i++; free(str); });
- cl_assert(i == 19);
+ cl_assert_equal_i(i, 19);
}
void test_core_strmap__3(void)
@@ -95,7 +95,7 @@ void test_core_strmap__3(void)
i = 0;
git_strmap_foreach_value(g_table, str, { i++; free(str); });
- cl_assert(i == 10000);
+ cl_assert_equal_i(i, 10000);
}
void test_core_strmap__get_succeeds_with_existing_entries(void)
@@ -156,3 +156,50 @@ void test_core_strmap__set_updates_existing_key(void)
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "other");
}
+
+void test_core_strmap__iteration(void)
+{
+ struct {
+ char *key;
+ char *value;
+ int seen;
+ } entries[] = {
+ { "foo", "oof" },
+ { "bar", "rab" },
+ { "gobble", "elbbog" },
+ };
+ const char *key, *value;
+ size_t i, n;
+
+ for (i = 0; i < ARRAY_SIZE(entries); i++)
+ cl_git_pass(git_strmap_set(g_table, entries[i].key, entries[i].value));
+
+ i = 0, n = 0;
+ while (git_strmap_iterate((void **) &value, g_table, &i, &key) == 0) {
+ size_t j;
+
+ for (j = 0; j < ARRAY_SIZE(entries); j++) {
+ if (strcmp(entries[j].key, key))
+ continue;
+
+ cl_assert_equal_i(entries[j].seen, 0);
+ cl_assert_equal_s(entries[j].value, value);
+ entries[j].seen++;
+ break;
+ }
+
+ n++;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(entries); i++)
+ cl_assert_equal_i(entries[i].seen, 1);
+
+ cl_assert_equal_i(n, ARRAY_SIZE(entries));
+}
+
+void test_core_strmap__iterating_empty_map_stops_immediately(void)
+{
+ size_t i = 0;
+
+ cl_git_fail_with(git_strmap_iterate(NULL, g_table, &i, NULL), GIT_ITEROVER);
+}
diff --git a/tests/pack/sharing.c b/tests/pack/sharing.c
index ec18006..eaf7686 100644
--- a/tests/pack/sharing.c
+++ b/tests/pack/sharing.c
@@ -24,7 +24,7 @@ void test_pack_sharing__open_two_repos(void)
cl_git_pass(git_object_lookup(&obj2, repo2, &id, GIT_OBJECT_ANY));
pos = 0;
- while ((error = git_strmap_next(&data, &pos, git__pack_cache)) == 0) {
+ while ((error = git_strmap_iterate(&data, git__pack_cache, &pos, NULL)) == 0) {
struct git_pack_file *pack = (struct git_pack_file *) data;
cl_assert_equal_i(2, pack->refcount.val);