summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-01-23 10:49:25 +0100
committerPatrick Steinhardt <ps@pks.im>2019-02-15 13:16:48 +0100
commitd00c24a9dd1809a6211b00c366d986bc40934aaf (patch)
tree7468a8cc7aba705f5ba709d8f5aceb99116b483a
parentb9d0b664071fed96e690a70ee4472facf554eb70 (diff)
downloadlibgit2-d00c24a9dd1809a6211b00c366d986bc40934aaf.tar.gz
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.
-rw-r--r--src/idxmap.c28
-rw-r--r--src/idxmap.h22
-rw-r--r--src/index.c19
3 files changed, 60 insertions, 9 deletions
diff --git a/src/idxmap.c b/src/idxmap.c
index 8ed9849c9..85c22fea3 100644
--- a/src/idxmap.c
+++ b/src/idxmap.c
@@ -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);
diff --git a/src/idxmap.h b/src/idxmap.h
index 442f5f509..893ef9be7 100644
--- a/src/idxmap.h
+++ b/src/idxmap.h
@@ -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);
diff --git a/src/index.c b/src/index.c
index 3f58eec36..06c5ca3a3 100644
--- a/src/index.c
+++ b/src/index.c
@@ -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(