summaryrefslogtreecommitdiff
path: root/src/strmap.h
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2018-12-01 09:37:40 +0100
committerPatrick Steinhardt <ps@pks.im>2019-02-15 13:16:48 +0100
commit18cf56989af8d8bdf6f9e736b65b1f91ef624177 (patch)
tree1d128b878b8df569e7507bfcd60e174cfce7f16c /src/strmap.h
parentc50a8ac2c734e4301223e39e4ddce98e6c368294 (diff)
downloadlibgit2-18cf56989af8d8bdf6f9e736b65b1f91ef624177.tar.gz
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.
Diffstat (limited to 'src/strmap.h')
-rw-r--r--src/strmap.h34
1 files changed, 25 insertions, 9 deletions
diff --git a/src/strmap.h b/src/strmap.h
index 6ed920c37..ce547ac3a 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; \
} }