Commit 18cf56989af8d8bdf6f9e736b65b1f91ef624177

Patrick Steinhardt 2018-12-01T09:37:40

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.

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);