Commit 661fc57b by Patrick Steinhardt

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.
parent d00c24a9
......@@ -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);
......
......@@ -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);
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment