summaryrefslogtreecommitdiff
path: root/src/offmap.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2018-11-30 18:28:05 +0100
committerPatrick Steinhardt <ps@pks.im>2019-02-15 13:16:48 +0100
commitaa2456239b4644c43d3cc9e002ed718e5078e7cc (patch)
tree9344afe02269f8b49ca33b180659bbc9d723486c /src/offmap.c
parent2e0a304839764236654e73d38fa380b317a3fac1 (diff)
downloadlibgit2-aa2456239b4644c43d3cc9e002ed718e5078e7cc.tar.gz
offmap: 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 a new high-level function `git_offmap_get` that takes a map and a key and returns 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/offmap.c')
-rw-r--r--src/offmap.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/offmap.c b/src/offmap.c
index 2b447b392..561b63cf2 100644
--- a/src/offmap.c
+++ b/src/offmap.c
@@ -42,6 +42,15 @@ size_t git_offmap_size(git_offmap *map)
return kh_size(map);
}
+void *git_offmap_get(git_offmap *map, const git_off_t key)
+{
+ size_t idx = git_offmap_lookup_index(map, key);
+ if (!git_offmap_valid_index(map, idx) ||
+ !git_offmap_has_data(map, idx))
+ return NULL;
+ return kh_val(map, idx);
+}
+
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key)
{
return kh_get(off, map, key);