Skip to content

Commit 8a973d0

Browse files
Eric Wonggitster
authored andcommitted
hashmap: hashmap_{put,remove} return hashmap_entry *
And add *_entry variants to perform container_of as necessary to simplify most callers. Signed-off-by: Eric Wong <[email protected]> Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 87571c3 commit 8a973d0

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

hashmap.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ void hashmap_add(struct hashmap *map, struct hashmap_entry *entry)
219219
}
220220
}
221221

222-
void *hashmap_remove(struct hashmap *map, const struct hashmap_entry *key,
223-
const void *keydata)
222+
struct hashmap_entry *hashmap_remove(struct hashmap *map,
223+
const struct hashmap_entry *key,
224+
const void *keydata)
224225
{
225226
struct hashmap_entry *old;
226227
struct hashmap_entry **e = find_entry_ptr(map, key, keydata);
@@ -242,7 +243,8 @@ void *hashmap_remove(struct hashmap *map, const struct hashmap_entry *key,
242243
return old;
243244
}
244245

245-
void *hashmap_put(struct hashmap *map, struct hashmap_entry *entry)
246+
struct hashmap_entry *hashmap_put(struct hashmap *map,
247+
struct hashmap_entry *entry)
246248
{
247249
struct hashmap_entry *old = hashmap_remove(map, entry, NULL);
248250
hashmap_add(map, entry);

hashmap.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,11 @@ void hashmap_add(struct hashmap *map, struct hashmap_entry *entry);
349349
* `entry` is the entry to add or replace.
350350
* Returns the replaced entry, or NULL if not found (i.e. the entry was added).
351351
*/
352-
void *hashmap_put(struct hashmap *map, struct hashmap_entry *entry);
352+
struct hashmap_entry *hashmap_put(struct hashmap *map,
353+
struct hashmap_entry *entry);
354+
355+
#define hashmap_put_entry(map, keyvar, type, member) \
356+
container_of_or_null(hashmap_put(map, &(keyvar)->member), type, member)
353357

354358
/*
355359
* Removes a hashmap entry matching the specified key. If the hashmap contains
@@ -358,8 +362,13 @@ void *hashmap_put(struct hashmap *map, struct hashmap_entry *entry);
358362
*
359363
* Argument explanation is the same as in `hashmap_get`.
360364
*/
361-
void *hashmap_remove(struct hashmap *map, const struct hashmap_entry *key,
362-
const void *keydata);
365+
struct hashmap_entry *hashmap_remove(struct hashmap *map,
366+
const struct hashmap_entry *key,
367+
const void *keydata);
368+
369+
#define hashmap_remove_entry(map, keyvar, keydata, type, member) \
370+
container_of_or_null(hashmap_remove(map, &(keyvar)->member, keydata), \
371+
type, member)
363372

364373
/*
365374
* Returns the `bucket` an entry is stored in.

range-diff.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
229229
util->patch = b->items[i].string;
230230
util->diff = util->patch + util->diff_offset;
231231
hashmap_entry_init(&util->e, strhash(util->diff));
232-
other = hashmap_remove(&map, &util->e, NULL);
232+
other = hashmap_remove_entry(&map, util, NULL,
233+
struct patch_util,
234+
e /* member name */);
233235
if (other) {
234236
if (other->matching >= 0)
235237
BUG("already assigned!");

remote.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ static struct remote *make_remote(const char *name, int len)
162162
remotes[remotes_nr++] = ret;
163163

164164
hashmap_entry_init(&ret->ent, lookup_entry.hash);
165-
replaced = hashmap_put(&remotes_hash, &ret->ent);
165+
replaced = hashmap_put_entry(&remotes_hash, ret, struct remote,
166+
ent /* member name */);
166167
assert(replaced == NULL); /* no previous entry overwritten */
167168
return ret;
168169
}

submodule-config.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ static void cache_remove_path(struct submodule_cache *cache,
141141
struct submodule_entry *removed;
142142
hashmap_entry_init(&e.ent, hash);
143143
e.config = submodule;
144-
removed = hashmap_remove(&cache->for_path, &e.ent, NULL);
144+
removed = hashmap_remove_entry(&cache->for_path, &e, NULL,
145+
struct submodule_entry,
146+
ent /* member name */);
145147
free(removed);
146148
}
147149

t/helper/test-hashmap.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ int cmd__hashmap(int argc, const char **argv)
189189
entry = alloc_test_entry(hash, p1, p2);
190190

191191
/* add / replace entry */
192-
entry = hashmap_put(&map, &entry->ent);
192+
entry = hashmap_put_entry(&map, entry,
193+
struct test_entry,
194+
ent /* member name */);
193195

194196
/* print and free replaced entry, if any */
195197
puts(entry ? get_value(entry) : "NULL");
@@ -212,10 +214,13 @@ int cmd__hashmap(int argc, const char **argv)
212214

213215
/* setup static key */
214216
struct hashmap_entry key;
217+
struct hashmap_entry *rm;
215218
hashmap_entry_init(&key, hash);
216219

217220
/* remove entry from hashmap */
218-
entry = hashmap_remove(&map, &key, p1);
221+
rm = hashmap_remove(&map, &key, p1);
222+
entry = rm ? container_of(rm, struct test_entry, ent)
223+
: NULL;
219224

220225
/* print result and free entry*/
221226
puts(entry ? get_value(entry) : "NULL");

0 commit comments

Comments
 (0)