diff options
author | Stefan Beller <sbeller@google.com> | 2017-06-30 12:14:05 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-06-30 12:49:28 -0700 |
commit | 7663cdc86c860d5b5293a1dd4b0fb6c4e006d08e (patch) | |
tree | 19a347bfc7b3eb59f2f30c581c8408679001d764 /hashmap.c | |
parent | e0aaa1b6532cfce93d87af9bc813fb2e7a7ce9d7 (diff) | |
download | git-7663cdc86c860d5b5293a1dd4b0fb6c4e006d08e.tar.gz |
hashmap.h: compare function has access to a data field
When using the hashmap a common need is to have access to caller provided
data in the compare function. A couple of times we abuse the keydata field
to pass in the data needed. This happens for example in patch-ids.c.
This patch changes the function signature of the compare function
to have one more void pointer available. The pointer given for each
invocation of the compare function must be defined in the init function
of the hashmap and is just passed through.
Documentation of this new feature is deferred to a later patch.
This is a rather mechanical conversion, just adding the new pass-through
parameter. However while at it improve the naming of the fields of all
compare functions used by hashmaps by ensuring unused parameters are
prefixed with 'unused_' and naming the parameters what they are (instead
of 'unused' make it 'unused_keydata').
Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'hashmap.c')
-rw-r--r-- | hashmap.c | 17 |
1 files changed, 12 insertions, 5 deletions
@@ -95,7 +95,9 @@ static inline int entry_equals(const struct hashmap *map, const struct hashmap_entry *e1, const struct hashmap_entry *e2, const void *keydata) { - return (e1 == e2) || (e1->hash == e2->hash && !map->cmpfn(e1, e2, keydata)); + return (e1 == e2) || + (e1->hash == e2->hash && + !map->cmpfn(map->cmpfn_data, e1, e2, keydata)); } static inline unsigned int bucket(const struct hashmap *map, @@ -140,19 +142,23 @@ static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map, return e; } -static int always_equal(const void *unused1, const void *unused2, const void *unused3) +static int always_equal(const void *unused_cmp_data, + const void *unused1, + const void *unused2, + const void *unused_keydata) { return 0; } void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function, - size_t initial_size) + const void *cmpfn_data, size_t initial_size) { unsigned int size = HASHMAP_INITIAL_SIZE; memset(map, 0, sizeof(*map)); map->cmpfn = equals_function ? equals_function : always_equal; + map->cmpfn_data = cmpfn_data; /* calculate initial table size and allocate the table */ initial_size = (unsigned int) ((uint64_t) initial_size * 100 @@ -260,7 +266,8 @@ struct pool_entry { unsigned char data[FLEX_ARRAY]; }; -static int pool_entry_cmp(const struct pool_entry *e1, +static int pool_entry_cmp(const void *unused_cmp_data, + const struct pool_entry *e1, const struct pool_entry *e2, const unsigned char *keydata) { @@ -275,7 +282,7 @@ const void *memintern(const void *data, size_t len) /* initialize string pool hashmap */ if (!map.tablesize) - hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, 0); + hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, NULL, 0); /* lookup interned string in pool */ hashmap_entry_init(&key, memhash(data, len)); |