Skip to content

Commit d00c24a

Browse files
committed
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 parent b9d0b66 commit d00c24a

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

src/idxmap.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ void git_idxmap_icase_clear(git_idxmap_icase *map)
6868
kh_clear(idxicase, map);
6969
}
7070

71+
void *git_idxmap_get(git_idxmap *map, const git_index_entry *key)
72+
{
73+
size_t idx = git_idxmap_lookup_index(map, key);
74+
if (!git_idxmap_valid_index(map, idx) ||
75+
!git_idxmap_has_data(map, idx))
76+
return NULL;
77+
return kh_val(map, idx);
78+
}
79+
80+
void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key)
81+
{
82+
size_t idx = git_idxmap_icase_lookup_index(map, key);
83+
if (!git_idxmap_icase_valid_index(map, idx) ||
84+
!git_idxmap_icase_has_data(map, idx))
85+
return NULL;
86+
return kh_val(map, idx);
87+
}
88+
7189
void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval)
7290
{
7391
khiter_t idx = kh_put(idx, map, key, rval);
@@ -110,11 +128,21 @@ int git_idxmap_valid_index(git_idxmap *map, size_t idx)
110128
return idx != kh_end(map);
111129
}
112130

131+
int git_idxmap_icase_valid_index(git_idxmap_icase *map, size_t idx)
132+
{
133+
return idx != kh_end(map);
134+
}
135+
113136
int git_idxmap_has_data(git_idxmap *map, size_t idx)
114137
{
115138
return kh_exist(map, idx);
116139
}
117140

141+
int git_idxmap_icase_has_data(git_idxmap_icase *map, size_t idx)
142+
{
143+
return kh_exist(map, idx);
144+
}
145+
118146
void git_idxmap_resize(git_idxmap *map, size_t size)
119147
{
120148
kh_resize(idx, map, size);

src/idxmap.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,36 @@ void git_idxmap_clear(git_idxmap *map);
7676
*/
7777
void git_idxmap_icase_clear(git_idxmap_icase *map);
7878

79+
/**
80+
* Return value associated with the given key.
81+
*
82+
* @param map map to search key in
83+
* @param key key to search for; the index entry will be searched
84+
* for by its case-sensitive path
85+
* @return value associated with the given key or NULL if the key was not found
86+
*/
87+
void *git_idxmap_get(git_idxmap *map, const git_index_entry *key);
88+
89+
/**
90+
* Return value associated with the given key.
91+
*
92+
* @param map map to search key in
93+
* @param key key to search for; the index entry will be searched
94+
* for by its case-insensitive path
95+
* @return value associated with the given key or NULL if the key was not found
96+
*/
97+
void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key);
98+
7999
void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval);
80100
void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key, void *value, int *rval);
81101

82102
size_t git_idxmap_lookup_index(git_idxmap *map, const git_index_entry *key);
83103
size_t git_idxmap_icase_lookup_index(git_idxmap_icase *map, const git_index_entry *key);
84104
void *git_idxmap_value_at(git_idxmap *map, size_t idx);
85105
int git_idxmap_valid_index(git_idxmap *map, size_t idx);
106+
int git_idxmap_icase_valid_index(git_idxmap_icase *map, size_t idx);
86107
int git_idxmap_has_data(git_idxmap *map, size_t idx);
108+
int git_idxmap_icase_has_data(git_idxmap_icase *map, size_t idx);
87109

88110
void git_idxmap_resize(git_idxmap *map, size_t size);
89111
void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size);

src/index.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636

3737
#define INSERT_IN_MAP(idx, e, err) INSERT_IN_MAP_EX(idx, (idx)->entries_map, e, err)
3838

39-
#define LOOKUP_IN_MAP(p, idx, k) do { \
39+
#define LOOKUP_IN_MAP(v, idx, k) do { \
4040
if ((idx)->ignore_case) \
41-
(p) = git_idxmap_icase_lookup_index((git_idxmap_icase *) index->entries_map, (k)); \
41+
(v) = git_idxmap_icase_get((git_idxmap_icase *) index->entries_map, (k)); \
4242
else \
43-
(p) = git_idxmap_lookup_index(index->entries_map, (k)); \
43+
(v) = git_idxmap_get(index->entries_map, (k)); \
4444
} while (0)
4545

4646
#define DELETE_IN_MAP(idx, e) do { \
@@ -852,20 +852,21 @@ const git_index_entry *git_index_get_bypath(
852852
git_index *index, const char *path, int stage)
853853
{
854854
git_index_entry key = {{ 0 }};
855-
size_t pos;
855+
git_index_entry *value;
856856

857857
assert(index);
858858

859859
key.path = path;
860860
GIT_INDEX_ENTRY_STAGE_SET(&key, stage);
861861

862-
LOOKUP_IN_MAP(pos, index, &key);
862+
LOOKUP_IN_MAP(value, index, &key);
863863

864-
if (git_idxmap_valid_index(index->entries_map, pos))
865-
return git_idxmap_value_at(index->entries_map, pos);
864+
if (!value) {
865+
git_error_set(GIT_ERROR_INDEX, "index does not contain '%s'", path);
866+
return NULL;
867+
}
866868

867-
git_error_set(GIT_ERROR_INDEX, "index does not contain '%s'", path);
868-
return NULL;
869+
return value;
869870
}
870871

871872
void git_index_entry__init_from_stat(

0 commit comments

Comments
 (0)