idxmap: introduce high-level setter for key/value pairs Currently, one would use the function `git_idxmap_insert` to insert key/value pairs into a map. This function has historically been a macro, which is why its syntax is kind of weird: instead of returning an error code directly, it instead has 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. Introduce a new function `git_idxmap_set`, which takes as parameters the map, key and value and directly returns an error code. Convert all callers of `git_idxmap_insert` to make use of it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
diff --git a/src/idxmap.c b/src/idxmap.c
index 85c22fe..3511229 100644
--- a/src/idxmap.c
+++ b/src/idxmap.c
@@ -77,6 +77,40 @@ void *git_idxmap_get(git_idxmap *map, const git_index_entry *key)
return kh_val(map, idx);
}
+int git_idxmap_set(git_idxmap *map, const git_index_entry *key, void *value)
+{
+ size_t idx;
+ int rval;
+
+ idx = kh_put(idx, map, key, &rval);
+ if (rval < 0)
+ return -1;
+
+ if (rval == 0)
+ kh_key(map, idx) = key;
+
+ kh_val(map, idx) = value;
+
+ return 0;
+}
+
+int git_idxmap_icase_set(git_idxmap_icase *map, const git_index_entry *key, void *value)
+{
+ size_t idx;
+ int rval;
+
+ idx = kh_put(idxicase, map, key, &rval);
+ if (rval < 0)
+ return -1;
+
+ if (rval == 0)
+ kh_key(map, idx) = key;
+
+ kh_val(map, idx) = value;
+
+ return 0;
+}
+
void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key)
{
size_t idx = git_idxmap_icase_lookup_index(map, key);
diff --git a/src/idxmap.h b/src/idxmap.h
index 893ef9b..55d60d5 100644
--- a/src/idxmap.h
+++ b/src/idxmap.h
@@ -96,6 +96,36 @@ void *git_idxmap_get(git_idxmap *map, const git_index_entry *key);
*/
void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key);
+/**
+ * Set the entry for key to value.
+ *
+ * If the map has no corresponding entry for the given key, a new
+ * entry will be created with the given value. If an entry exists
+ * already, its value will be updated to match the given value.
+ *
+ * @param map map to create new entry in
+ * @param key key to set
+ * @param value value to associate the key with; may be NULL
+ * @return zero if the key was successfully set, a negative error
+ * code otherwise
+ */
+int git_idxmap_set(git_idxmap *map, const git_index_entry *key, void *value);
+
+/**
+ * Set the entry for key to value.
+ *
+ * If the map has no corresponding entry for the given key, a new
+ * entry will be created with the given value. If an entry exists
+ * already, its value will be updated to match the given value.
+ *
+ * @param map map to create new entry in
+ * @param key key to set
+ * @param value value to associate the key with; may be NULL
+ * @return zero if the key was successfully set, a negative error
+ * code otherwise
+ */
+int git_idxmap_icase_set(git_idxmap_icase *map, const git_index_entry *key, void *value);
+
void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval);
void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key, void *value, int *rval);
diff --git a/src/index.c b/src/index.c
index 06c5ca3..308b89a 100644
--- a/src/index.c
+++ b/src/index.c
@@ -29,9 +29,9 @@
#define INSERT_IN_MAP_EX(idx, map, e, err) do { \
if ((idx)->ignore_case) \
- git_idxmap_icase_insert((git_idxmap_icase *) (map), (e), (e), (err)); \
+ (err) = git_idxmap_icase_set((git_idxmap_icase *) (map), (e), (e)); \
else \
- git_idxmap_insert((map), (e), (e), (err)); \
+ (err) = git_idxmap_set((map), (e), (e)); \
} while (0)
#define INSERT_IN_MAP(idx, e, err) INSERT_IN_MAP_EX(idx, (idx)->entries_map, e, err)
@@ -1402,7 +1402,7 @@ static int index_insert(
if ((error = git_vector_insert_sorted(&index->entries, entry, index_no_dups)) < 0)
goto out;
- INSERT_IN_MAP(index, entry, &error);
+ INSERT_IN_MAP(index, entry, error);
}
index->dirty = 1;
@@ -1635,7 +1635,7 @@ int git_index__fill(git_index *index, const git_vector *source_entries)
if ((ret = git_vector_insert(&index->entries, entry)) < 0)
break;
- INSERT_IN_MAP(index, entry, &ret);
+ INSERT_IN_MAP(index, entry, ret);
if (ret < 0)
break;
@@ -2628,7 +2628,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
goto done;
}
- INSERT_IN_MAP(index, entry, &error);
+ INSERT_IN_MAP(index, entry, error);
if (error < 0) {
index_entry_free(entry);
@@ -3131,7 +3131,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
git_idxmap_resize(entries_map, entries.length);
git_vector_foreach(&entries, i, e) {
- INSERT_IN_MAP_EX(index, entries_map, e, &error);
+ INSERT_IN_MAP_EX(index, entries_map, e, error);
if (error < 0) {
git_error_set(GIT_ERROR_INDEX, "failed to insert entry into map");
@@ -3252,7 +3252,7 @@ static int git_index_read_iterator(
if (add_entry) {
if ((error = git_vector_insert(&new_entries, add_entry)) == 0)
- INSERT_IN_MAP_EX(index, new_entries_map, add_entry, &error);
+ INSERT_IN_MAP_EX(index, new_entries_map, add_entry, error);
}
if (remove_entry && error >= 0)