Commit c50a8ac2 by Patrick Steinhardt

maps: use high-level function to check existence of keys

Some callers were still using the tightly-coupled pattern of `lookup_index` and
`valid_index` to verify that an entry exists in a map. Instead, use the more
high-level `exists` functions to decouple map users from its implementation.
parent 84a089da
......@@ -439,7 +439,6 @@ static int apply_one(
git_filemode_t pre_filemode;
git_index_entry pre_entry, post_entry;
bool skip_preimage = false;
size_t pos;
int error;
if ((error = git_patch_from_diff(&patch, diff, i)) < 0)
......@@ -464,8 +463,7 @@ static int apply_one(
*/
if (delta->status != GIT_DELTA_RENAMED &&
delta->status != GIT_DELTA_ADDED) {
pos = git_strmap_lookup_index(removed_paths, delta->old_file.path);
if (git_strmap_valid_index(removed_paths, pos)) {
if (git_strmap_exists(removed_paths, delta->old_file.path)) {
error = apply_err("path '%s' has been renamed or deleted", delta->old_file.path);
goto done;
}
......
......@@ -77,6 +77,11 @@ int git_offmap_delete(git_offmap *map, const git_off_t key)
return 0;
}
int git_offmap_exists(git_offmap *map, const git_off_t key)
{
return kh_get(off, map, key) != kh_end(map);
}
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key)
{
return kh_get(off, map, key);
......@@ -87,11 +92,6 @@ int git_offmap_valid_index(git_offmap *map, size_t idx)
return idx != kh_end(map);
}
int git_offmap_exists(git_offmap *map, const git_off_t key)
{
return kh_get(off, map, key) != kh_end(map);
}
int git_offmap_has_data(git_offmap *map, size_t idx)
{
return kh_exist(map, idx);
......
......@@ -90,10 +90,18 @@ int git_offmap_set(git_offmap *map, const git_off_t key, void *value);
*/
int git_offmap_delete(git_offmap *map, const git_off_t key);
/**
* Check whether a key exists in the given map.
*
* @param map map to query for the key
* @param key key to search for
* @return 0 if the key has not been found, 1 otherwise
*/
int git_offmap_exists(git_offmap *map, const git_off_t key);
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key);
int git_offmap_valid_index(git_offmap *map, size_t idx);
int git_offmap_exists(git_offmap *map, const git_off_t key);
int git_offmap_has_data(git_offmap *map, size_t idx);
git_off_t git_offmap_key_at(git_offmap *map, size_t idx);
......
......@@ -83,6 +83,11 @@ int git_oidmap_delete(git_oidmap *map, const git_oid *key)
return 0;
}
int git_oidmap_exists(git_oidmap *map, const git_oid *key)
{
return kh_get(oid, map, key) != kh_end(map);
}
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key)
{
return kh_get(oid, map, key);
......@@ -93,11 +98,6 @@ int git_oidmap_valid_index(git_oidmap *map, size_t idx)
return idx != kh_end(map);
}
int git_oidmap_exists(git_oidmap *map, const git_oid *key)
{
return kh_get(oid, map, key) != kh_end(map);
}
int git_oidmap_has_data(git_oidmap *map, size_t idx)
{
return kh_exist(map, idx);
......
......@@ -90,10 +90,18 @@ int git_oidmap_set(git_oidmap *map, const git_oid *key, void *value);
*/
int git_oidmap_delete(git_oidmap *map, const git_oid *key);
/**
* Check whether a key exists in the given map.
*
* @param map map to query for the key
* @param key key to search for
* @return 0 if the key has not been found, 1 otherwise
*/
int git_oidmap_exists(git_oidmap *map, const git_oid *key);
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key);
int git_oidmap_valid_index(git_oidmap *map, size_t idx);
int git_oidmap_exists(git_oidmap *map, const git_oid *key);
int git_oidmap_has_data(git_oidmap *map, size_t idx);
const git_oid *git_oidmap_key(git_oidmap *map, size_t idx);
......
......@@ -76,6 +76,11 @@ int git_strmap_delete(git_strmap *map, const char *key)
return 0;
}
int git_strmap_exists(git_strmap *map, const char *key)
{
return kh_get(str, map, key) != kh_end(map);
}
size_t git_strmap_lookup_index(git_strmap *map, const char *key)
{
return kh_get(str, map, key);
......@@ -86,11 +91,6 @@ int git_strmap_valid_index(git_strmap *map, size_t idx)
return idx != kh_end(map);
}
int git_strmap_exists(git_strmap *map, const char *key)
{
return kh_get(str, map, key) != kh_end(map);
}
int git_strmap_has_data(git_strmap *map, size_t idx)
{
return kh_exist(map, idx);
......
......@@ -88,10 +88,18 @@ int git_strmap_set(git_strmap *map, const char *key, void *value);
*/
int git_strmap_delete(git_strmap *map, const char *key);
/**
* Check whether a key exists in the given map.
*
* @param map map to query for the key
* @param key key to search for
* @return 0 if the key has not been found, 1 otherwise
*/
int git_strmap_exists(git_strmap *map, const char *key);
size_t git_strmap_lookup_index(git_strmap *map, const char *key);
int git_strmap_valid_index(git_strmap *map, size_t idx);
int git_strmap_exists(git_strmap *map, const char *key);
int git_strmap_has_data(git_strmap *map, size_t idx);
const char *git_strmap_key(git_strmap *map, size_t idx);
......
......@@ -1907,7 +1907,6 @@ static int submodule_load_each(const git_config_entry *entry, void *payload)
{
lfc_data *data = payload;
const char *namestart, *property;
size_t pos;
git_strmap *map = data->map;
git_buf name = GIT_BUF_INIT;
git_submodule *sm;
......@@ -1939,8 +1938,7 @@ static int submodule_load_each(const git_config_entry *entry, void *payload)
* a new submodule, load the config and insert it. If it's
* already inserted, we've already loaded it, so we skip.
*/
pos = git_strmap_lookup_index(map, name.ptr);
if (git_strmap_valid_index(map, pos)) {
if (git_strmap_exists(map, name.ptr)) {
error = 0;
goto done;
}
......
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