diff options
| author | Patrick Steinhardt <ps@pks.im> | 2019-01-23 10:49:25 +0100 |
|---|---|---|
| committer | Patrick Steinhardt <ps@pks.im> | 2019-02-15 13:16:48 +0100 |
| commit | d00c24a9dd1809a6211b00c366d986bc40934aaf (patch) | |
| tree | 7468a8cc7aba705f5ba709d8f5aceb99116b483a /src/idxmap.c | |
| parent | b9d0b664071fed96e690a70ee4472facf554eb70 (diff) | |
| download | libgit2-d00c24a9dd1809a6211b00c366d986bc40934aaf.tar.gz | |
idxmap: introduce high-level getter for values
The current way of looking up an entry from a map is tightly coupled with the
map implementation, as one first has to look up the index of the key and then
retrieve the associated value by using the index. As a caller, you usually do
not care about any indices at all, though, so this is more complicated than
really necessary. Furthermore, it invites for errors to happen if the correct
error checking sequence is not being followed.
Introduce new high-level functions `git_idxmap_get` and `git_idxmap_icase_get`
that take a map and a key and return a pointer to the associated value if such a
key exists. Otherwise, a `NULL` pointer is returned. Adjust all callers that can
trivially be converted.
Diffstat (limited to 'src/idxmap.c')
| -rw-r--r-- | src/idxmap.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/idxmap.c b/src/idxmap.c index 8ed9849c9..85c22fea3 100644 --- a/src/idxmap.c +++ b/src/idxmap.c @@ -68,6 +68,24 @@ void git_idxmap_icase_clear(git_idxmap_icase *map) kh_clear(idxicase, map); } +void *git_idxmap_get(git_idxmap *map, const git_index_entry *key) +{ + size_t idx = git_idxmap_lookup_index(map, key); + if (!git_idxmap_valid_index(map, idx) || + !git_idxmap_has_data(map, idx)) + return NULL; + return kh_val(map, idx); +} + +void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key) +{ + size_t idx = git_idxmap_icase_lookup_index(map, key); + if (!git_idxmap_icase_valid_index(map, idx) || + !git_idxmap_icase_has_data(map, idx)) + return NULL; + return kh_val(map, idx); +} + void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval) { khiter_t idx = kh_put(idx, map, key, rval); @@ -110,11 +128,21 @@ int git_idxmap_valid_index(git_idxmap *map, size_t idx) return idx != kh_end(map); } +int git_idxmap_icase_valid_index(git_idxmap_icase *map, size_t idx) +{ + return idx != kh_end(map); +} + int git_idxmap_has_data(git_idxmap *map, size_t idx) { return kh_exist(map, idx); } +int git_idxmap_icase_has_data(git_idxmap_icase *map, size_t idx) +{ + return kh_exist(map, idx); +} + void git_idxmap_resize(git_idxmap *map, size_t size) { kh_resize(idx, map, size); |
