diff options
author | Patrick Steinhardt <ps@pks.im> | 2018-12-01 01:16:25 +0100 |
---|---|---|
committer | Patrick Steinhardt <ps@pks.im> | 2019-02-15 13:16:48 +0100 |
commit | 661fc57b2cd4bcc1024aa3c6a7ffb3190571685e (patch) | |
tree | 423e59ea6b752c222436b61e3be79bc39f98f4f0 | |
parent | d00c24a9dd1809a6211b00c366d986bc40934aaf (diff) | |
download | libgit2-661fc57b2cd4bcc1024aa3c6a7ffb3190571685e.tar.gz |
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.
-rw-r--r-- | src/idxmap.c | 34 | ||||
-rw-r--r-- | src/idxmap.h | 30 | ||||
-rw-r--r-- | src/index.c | 14 |
3 files changed, 71 insertions, 7 deletions
diff --git a/src/idxmap.c b/src/idxmap.c index 85c22fea3..3511229dd 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 893ef9be7..55d60d5f1 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 06c5ca3a3..308b89a16 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) |