]> git.ipfire.org Git - thirdparty/git.git/commitdiff
hashmap: hashmap_{put,remove} return hashmap_entry *
authorEric Wong <e@80x24.org>
Sun, 6 Oct 2019 23:30:39 +0000 (23:30 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 7 Oct 2019 01:20:11 +0000 (10:20 +0900)
And add *_entry variants to perform container_of as necessary
to simplify most callers.

Signed-off-by: Eric Wong <e@80x24.org>
Reviewed-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
hashmap.c
hashmap.h
range-diff.c
remote.c
submodule-config.c
t/helper/test-hashmap.c

index deb5fdf28c82e3d36b82878f36f8cd23543a40d5..1b60f97cf20344dd798e64305522eab109156875 100644 (file)
--- a/hashmap.c
+++ b/hashmap.c
@@ -219,8 +219,9 @@ void hashmap_add(struct hashmap *map, struct hashmap_entry *entry)
        }
 }
 
        }
 }
 
-void *hashmap_remove(struct hashmap *map, const struct hashmap_entry *key,
-               const void *keydata)
+struct hashmap_entry *hashmap_remove(struct hashmap *map,
+                                       const struct hashmap_entry *key,
+                                       const void *keydata)
 {
        struct hashmap_entry *old;
        struct hashmap_entry **e = find_entry_ptr(map, key, keydata);
 {
        struct hashmap_entry *old;
        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,
        return old;
 }
 
        return old;
 }
 
-void *hashmap_put(struct hashmap *map, struct hashmap_entry *entry)
+struct hashmap_entry *hashmap_put(struct hashmap *map,
+                               struct hashmap_entry *entry)
 {
        struct hashmap_entry *old = hashmap_remove(map, entry, NULL);
        hashmap_add(map, entry);
 {
        struct hashmap_entry *old = hashmap_remove(map, entry, NULL);
        hashmap_add(map, entry);
index 8d4b3907b49f725fe0af0bd772c68f51773523bf..bc3b10e0970ffb145f964ca49f2b779e44b5ba7d 100644 (file)
--- a/hashmap.h
+++ b/hashmap.h
@@ -349,7 +349,11 @@ void hashmap_add(struct hashmap *map, struct hashmap_entry *entry);
  * `entry` is the entry to add or replace.
  * Returns the replaced entry, or NULL if not found (i.e. the entry was added).
  */
  * `entry` is the entry to add or replace.
  * Returns the replaced entry, or NULL if not found (i.e. the entry was added).
  */
-void *hashmap_put(struct hashmap *map, struct hashmap_entry *entry);
+struct hashmap_entry *hashmap_put(struct hashmap *map,
+                               struct hashmap_entry *entry);
+
+#define hashmap_put_entry(map, keyvar, type, member) \
+       container_of_or_null(hashmap_put(map, &(keyvar)->member), type, member)
 
 /*
  * Removes a hashmap entry matching the specified key. If the hashmap contains
 
 /*
  * 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);
  *
  * Argument explanation is the same as in `hashmap_get`.
  */
  *
  * Argument explanation is the same as in `hashmap_get`.
  */
-void *hashmap_remove(struct hashmap *map, const struct hashmap_entry *key,
-               const void *keydata);
+struct hashmap_entry *hashmap_remove(struct hashmap *map,
+                                       const struct hashmap_entry *key,
+                                       const void *keydata);
+
+#define hashmap_remove_entry(map, keyvar, keydata, type, member) \
+       container_of_or_null(hashmap_remove(map, &(keyvar)->member, keydata), \
+                               type, member)
 
 /*
  * Returns the `bucket` an entry is stored in.
 
 /*
  * Returns the `bucket` an entry is stored in.
index c51cfd5556999efc1a515f89d80b3575b03e3e7b..e5e7820bfea79092be469ed7a7bdfcfcf98eeed3 100644 (file)
@@ -229,7 +229,9 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
                util->patch = b->items[i].string;
                util->diff = util->patch + util->diff_offset;
                hashmap_entry_init(&util->e, strhash(util->diff));
                util->patch = b->items[i].string;
                util->diff = util->patch + util->diff_offset;
                hashmap_entry_init(&util->e, strhash(util->diff));
-               other = hashmap_remove(&map, &util->e, NULL);
+               other = hashmap_remove_entry(&map, util, NULL,
+                                       struct patch_util,
+                                       e /* member name */);
                if (other) {
                        if (other->matching >= 0)
                                BUG("already assigned!");
                if (other) {
                        if (other->matching >= 0)
                                BUG("already assigned!");
index fa9cadcfbd1844002b4e77af417d61c5844f8a4f..5fcddcd88d918b85029e40e74e456a98ec189579 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -162,7 +162,8 @@ static struct remote *make_remote(const char *name, int len)
        remotes[remotes_nr++] = ret;
 
        hashmap_entry_init(&ret->ent, lookup_entry.hash);
        remotes[remotes_nr++] = ret;
 
        hashmap_entry_init(&ret->ent, lookup_entry.hash);
-       replaced = hashmap_put(&remotes_hash, &ret->ent);
+       replaced = hashmap_put_entry(&remotes_hash, ret, struct remote,
+                                       ent /* member name */);
        assert(replaced == NULL);  /* no previous entry overwritten */
        return ret;
 }
        assert(replaced == NULL);  /* no previous entry overwritten */
        return ret;
 }
index 5319933e1d6eaebec9e8a7b38b0159aa960f7df7..a289d195f6ab9476f5b7a36796f5e58b8deb9a4e 100644 (file)
@@ -141,7 +141,9 @@ static void cache_remove_path(struct submodule_cache *cache,
        struct submodule_entry *removed;
        hashmap_entry_init(&e.ent, hash);
        e.config = submodule;
        struct submodule_entry *removed;
        hashmap_entry_init(&e.ent, hash);
        e.config = submodule;
-       removed = hashmap_remove(&cache->for_path, &e.ent, NULL);
+       removed = hashmap_remove_entry(&cache->for_path, &e, NULL,
+                                       struct submodule_entry,
+                                       ent /* member name */);
        free(removed);
 }
 
        free(removed);
 }
 
index 4ec5e11556d624b683bc69b642c091cfde9c11fb..07a93a2aec243afe3bd2d4a3f09a899f4fc8a709 100644 (file)
@@ -189,7 +189,9 @@ int cmd__hashmap(int argc, const char **argv)
                        entry = alloc_test_entry(hash, p1, p2);
 
                        /* add / replace entry */
                        entry = alloc_test_entry(hash, p1, p2);
 
                        /* add / replace entry */
-                       entry = hashmap_put(&map, &entry->ent);
+                       entry = hashmap_put_entry(&map, entry,
+                                               struct test_entry,
+                                               ent /* member name */);
 
                        /* print and free replaced entry, if any */
                        puts(entry ? get_value(entry) : "NULL");
 
                        /* print and free replaced entry, if any */
                        puts(entry ? get_value(entry) : "NULL");
@@ -212,10 +214,13 @@ int cmd__hashmap(int argc, const char **argv)
 
                        /* setup static key */
                        struct hashmap_entry key;
 
                        /* setup static key */
                        struct hashmap_entry key;
+                       struct hashmap_entry *rm;
                        hashmap_entry_init(&key, hash);
 
                        /* remove entry from hashmap */
                        hashmap_entry_init(&key, hash);
 
                        /* remove entry from hashmap */
-                       entry = hashmap_remove(&map, &key, p1);
+                       rm = hashmap_remove(&map, &key, p1);
+                       entry = rm ? container_of(rm, struct test_entry, ent)
+                                       : NULL;
 
                        /* print result and free entry*/
                        puts(entry ? get_value(entry) : "NULL");
 
                        /* print result and free entry*/
                        puts(entry ? get_value(entry) : "NULL");