Commit d00c24a9 by Patrick Steinhardt

idxmap: 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 new high-level functions `git_idxmap_get` and `git_idxmap_icase_get`
that take a map and a key and return 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.
parent b9d0b664
......@@ -68,6 +68,24 @@ void git_idxmap_icase_clear(git_idxmap_icase *map)
kh_clear(idxicase, map);
}
void *git_idxmap_get(git_idxmap *map, const git_index_entry *key)
{
size_t idx = git_idxmap_lookup_index(map, key);
if (!git_idxmap_valid_index(map, idx) ||
!git_idxmap_has_data(map, idx))
return NULL;
return kh_val(map, idx);
}
void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key)
{
size_t idx = git_idxmap_icase_lookup_index(map, key);
if (!git_idxmap_icase_valid_index(map, idx) ||
!git_idxmap_icase_has_data(map, idx))
return NULL;
return kh_val(map, idx);
}
void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval)
{
khiter_t idx = kh_put(idx, map, key, rval);
......@@ -110,11 +128,21 @@ int git_idxmap_valid_index(git_idxmap *map, size_t idx)
return idx != kh_end(map);
}
int git_idxmap_icase_valid_index(git_idxmap_icase *map, size_t idx)
{
return idx != kh_end(map);
}
int git_idxmap_has_data(git_idxmap *map, size_t idx)
{
return kh_exist(map, idx);
}
int git_idxmap_icase_has_data(git_idxmap_icase *map, size_t idx)
{
return kh_exist(map, idx);
}
void git_idxmap_resize(git_idxmap *map, size_t size)
{
kh_resize(idx, map, size);
......
......@@ -76,6 +76,26 @@ void git_idxmap_clear(git_idxmap *map);
*/
void git_idxmap_icase_clear(git_idxmap_icase *map);
/**
* Return value associated with the given key.
*
* @param map map to search key in
* @param key key to search for; the index entry will be searched
* for by its case-sensitive path
* @return value associated with the given key or NULL if the key was not found
*/
void *git_idxmap_get(git_idxmap *map, const git_index_entry *key);
/**
* Return value associated with the given key.
*
* @param map map to search key in
* @param key key to search for; the index entry will be searched
* for by its case-insensitive path
* @return value associated with the given key or NULL if the key was not found
*/
void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key);
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);
......@@ -83,7 +103,9 @@ size_t git_idxmap_lookup_index(git_idxmap *map, const git_index_entry *key);
size_t git_idxmap_icase_lookup_index(git_idxmap_icase *map, const git_index_entry *key);
void *git_idxmap_value_at(git_idxmap *map, size_t idx);
int git_idxmap_valid_index(git_idxmap *map, size_t idx);
int git_idxmap_icase_valid_index(git_idxmap_icase *map, size_t idx);
int git_idxmap_has_data(git_idxmap *map, size_t idx);
int git_idxmap_icase_has_data(git_idxmap_icase *map, size_t idx);
void git_idxmap_resize(git_idxmap *map, size_t size);
void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size);
......
......@@ -36,11 +36,11 @@
#define INSERT_IN_MAP(idx, e, err) INSERT_IN_MAP_EX(idx, (idx)->entries_map, e, err)
#define LOOKUP_IN_MAP(p, idx, k) do { \
#define LOOKUP_IN_MAP(v, idx, k) do { \
if ((idx)->ignore_case) \
(p) = git_idxmap_icase_lookup_index((git_idxmap_icase *) index->entries_map, (k)); \
(v) = git_idxmap_icase_get((git_idxmap_icase *) index->entries_map, (k)); \
else \
(p) = git_idxmap_lookup_index(index->entries_map, (k)); \
(v) = git_idxmap_get(index->entries_map, (k)); \
} while (0)
#define DELETE_IN_MAP(idx, e) do { \
......@@ -852,20 +852,21 @@ const git_index_entry *git_index_get_bypath(
git_index *index, const char *path, int stage)
{
git_index_entry key = {{ 0 }};
size_t pos;
git_index_entry *value;
assert(index);
key.path = path;
GIT_INDEX_ENTRY_STAGE_SET(&key, stage);
LOOKUP_IN_MAP(pos, index, &key);
LOOKUP_IN_MAP(value, index, &key);
if (git_idxmap_valid_index(index->entries_map, pos))
return git_idxmap_value_at(index->entries_map, pos);
if (!value) {
git_error_set(GIT_ERROR_INDEX, "index does not contain '%s'", path);
return NULL;
}
git_error_set(GIT_ERROR_INDEX, "index does not contain '%s'", path);
return NULL;
return value;
}
void git_index_entry__init_from_stat(
......
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