diff options
| author | Patrick Steinhardt <ps@pks.im> | 2018-12-01 09:37:40 +0100 |
|---|---|---|
| committer | Patrick Steinhardt <ps@pks.im> | 2019-02-15 13:16:48 +0100 |
| commit | 18cf56989af8d8bdf6f9e736b65b1f91ef624177 (patch) | |
| tree | 1d128b878b8df569e7507bfcd60e174cfce7f16c /src/offmap.c | |
| parent | c50a8ac2c734e4301223e39e4ddce98e6c368294 (diff) | |
| download | libgit2-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/offmap.c')
| -rw-r--r-- | src/offmap.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/offmap.c b/src/offmap.c index 09f8a2eb0..162501d8f 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); |
