summaryrefslogtreecommitdiff
path: root/src/oidmap.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-01-23 10:48:55 +0100
committerPatrick Steinhardt <ps@pks.im>2019-02-15 13:16:48 +0100
commit2e0a304839764236654e73d38fa380b317a3fac1 (patch)
treefdba155593c31b1bb6260ad6bb09dba644bb8a56 /src/oidmap.c
parent9694ef2064be3ecc3f173af296ab091f0498234d (diff)
downloadlibgit2-2e0a304839764236654e73d38fa380b317a3fac1.tar.gz
oidmap: introduce high-level setter for key/value pairs
Currently, one would use either `git_oidmap_insert` to insert key/value pairs into a map or `git_oidmap_put` to insert a key only. These function have historically been macros, which is why their syntax is kind of weird: instead of returning an error code directly, they instead have to be passed a pointer to where the return value shall be stored. This does not match libgit2's common idiom of directly returning error codes.Furthermore, `git_oidmap_put` is tightly coupled with implementation details of the map as it exposes the index of inserted entries. Introduce a new function `git_oidmap_set`, which takes as parameters the map, key and value and directly returns an error code. Convert all trivial callers of `git_oidmap_insert` and `git_oidmap_put` to make use of it.
Diffstat (limited to 'src/oidmap.c')
-rw-r--r--src/oidmap.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/oidmap.c b/src/oidmap.c
index 1e2b8124b..2890fdb28 100644
--- a/src/oidmap.c
+++ b/src/oidmap.c
@@ -57,6 +57,23 @@ void *git_oidmap_get(git_oidmap *map, const git_oid *key)
return kh_val(map, idx);
}
+int git_oidmap_set(git_oidmap *map, const git_oid *key, void *value)
+{
+ size_t idx;
+ int rval;
+
+ idx = kh_put(oid, map, key, &rval);
+ if (rval < 0)
+ return -1;
+
+ if (rval == 0)
+ kh_key(map, idx) = key;
+
+ kh_val(map, idx) = value;
+
+ return 0;
+}
+
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key)
{
return kh_get(oid, map, key);