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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
diff --git a/src/idxmap.c b/src/idxmap.c
index 8ed9849..85c22fe 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 442f5f5..893ef9b 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 3f58eec..06c5ca3 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(