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>

Stefan Beller committed Jun 30, 2017 at 12:14 UTC 7663cdc86c860d5b5293a1dd4b0fb6c4e006d08e
19 files changed +113 -66
attr.c
+4 -3
@@ -76,9 +76,10 @@ struct attr_hash_entry {
76 };
77
78 /* attr_hashmap comparison function */
79 -static int attr_hash_entry_cmp(const struct attr_hash_entry *a,
79 +static int attr_hash_entry_cmp(void *unused_cmp_data,
80 + const struct attr_hash_entry *a,
81 const struct attr_hash_entry *b,
81 - void *unused)
82 + void *unused_keydata)
83 {
84 return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
85 }
@@ -86,7 +87,7 @@ static int attr_hash_entry_cmp(const struct attr_hash_entry *a,
87 /* Initialize an 'attr_hashmap' object */
88 static void attr_hashmap_init(struct attr_hashmap *map)
89 {
89 - hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, 0);
90 + hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, NULL, 0);
91 }
92
93 /*
builtin/describe.c
+5 -3
@@ -54,8 +54,10 @@ static const char *prio_names[] = {
54 N_("head"), N_("lightweight"), N_("annotated"),
55 };
56
57 -static int commit_name_cmp(const struct commit_name *cn1,
58 - const struct commit_name *cn2, const void *peeled)
57 +static int commit_name_cmp(const void *unused_cmp_data,
58 + const struct commit_name *cn1,
59 + const struct commit_name *cn2,
60 + const void *peeled)
61 {
62 return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled);
63 }
@@ -501,7 +503,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
503 return cmd_name_rev(args.argc, args.argv, prefix);
504 }
505
504 - hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0);
506 + hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, NULL, 0);
507 for_each_rawref(get_name, NULL);
508 if (!names.size && !always)
509 die(_("No names found, cannot describe anything."));
builtin/difftool.c
+15 -9
@@ -130,8 +130,10 @@ struct working_tree_entry {
130 char path[FLEX_ARRAY];
131 };
132
133 -static int working_tree_entry_cmp(struct working_tree_entry *a,
134 - struct working_tree_entry *b, void *keydata)
133 +static int working_tree_entry_cmp(const void *unused_cmp_data,
134 + struct working_tree_entry *a,
135 + struct working_tree_entry *b,
136 + void *unused_keydata)
137 {
138 return strcmp(a->path, b->path);
139 }
@@ -146,7 +148,9 @@ struct pair_entry {
148 const char path[FLEX_ARRAY];
149 };
150
149 -static int pair_cmp(struct pair_entry *a, struct pair_entry *b, void *keydata)
151 +static int pair_cmp(const void *unused_cmp_data,
152 + struct pair_entry *a, struct pair_entry *b,
153 + void *unused_keydata)
154 {
155 return strcmp(a->path, b->path);
156 }
@@ -174,7 +178,9 @@ struct path_entry {
178 char path[FLEX_ARRAY];
179 };
180
177 -static int path_entry_cmp(struct path_entry *a, struct path_entry *b, void *key)
181 +static int path_entry_cmp(const void *unused_cmp_data,
182 + struct path_entry *a, struct path_entry *b,
183 + void *key)
184 {
185 return strcmp(a->path, key ? key : b->path);
186 }
@@ -367,9 +373,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
373 wtdir_len = wtdir.len;
374
375 hashmap_init(&working_tree_dups,
370 - (hashmap_cmp_fn)working_tree_entry_cmp, 0);
371 - hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, 0);
372 - hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, 0);
376 + (hashmap_cmp_fn)working_tree_entry_cmp, NULL, 0);
377 + hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, NULL, 0);
378 + hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, NULL, 0);
379
380 child.no_stdin = 1;
381 child.git_cmd = 1;
@@ -580,9 +586,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
586 * files through the symlink.
587 */
588 hashmap_init(&wt_modified, (hashmap_cmp_fn)path_entry_cmp,
583 - wtindex.cache_nr);
589 + NULL, wtindex.cache_nr);
590 hashmap_init(&tmp_modified, (hashmap_cmp_fn)path_entry_cmp,
585 - wtindex.cache_nr);
591 + NULL, wtindex.cache_nr);
592
593 for (i = 0; i < wtindex.cache_nr; i++) {
594 struct hashmap_entry dummy;
builtin/fast-export.c
+4 -3
@@ -93,8 +93,9 @@ struct anonymized_entry {
93 size_t anon_len;
94 };
95
96 -static int anonymized_entry_cmp(const void *va, const void *vb,
97 - const void *data)
96 +static int anonymized_entry_cmp(const void *unused_cmp_data,
97 + const void *va, const void *vb,
98 + const void *unused_keydata)
99 {
100 const struct anonymized_entry *a = va, *b = vb;
101 return a->orig_len != b->orig_len ||
@@ -113,7 +114,7 @@ static const void *anonymize_mem(struct hashmap *map,
114 struct anonymized_entry key, *ret;
115
116 if (!map->cmpfn)
116 - hashmap_init(map, anonymized_entry_cmp, 0);
117 + hashmap_init(map, anonymized_entry_cmp, NULL, 0);
118
119 hashmap_entry_init(&key, memhash(orig, *len));
120 key.orig = orig;
config.c
+6 -3
@@ -1753,15 +1753,18 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
1753 return 0;
1754 }
1755
1756 -static int config_set_element_cmp(const struct config_set_element *e1,
1757 - const struct config_set_element *e2, const void *unused)
1756 +static int config_set_element_cmp(const void *unused_cmp_data,
1757 + const struct config_set_element *e1,
1758 + const struct config_set_element *e2,
1759 + const void *unused_keydata)
1760 {
1761 return strcmp(e1->key, e2->key);
1762 }
1763
1764 void git_configset_init(struct config_set *cs)
1765 {
1764 - hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
1766 + hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp,
1767 + NULL, 0);
1768 cs->hash_initialized = 1;
1769 cs->list.nr = 0;
1770 cs->list.alloc = 0;
convert.c
+2 -1
@@ -583,7 +583,8 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
583
584 if (!subprocess_map_initialized) {
585 subprocess_map_initialized = 1;
586 - hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp, 0);
586 + hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp,
587 + NULL, 0);
588 entry = NULL;
589 } else {
590 entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
diffcore-rename.c
+1 -1
@@ -341,7 +341,7 @@ static int find_exact_renames(struct diff_options *options)
341 /* Add all sources to the hash table in reverse order, because
342 * later on they will be retrieved in LIFO order.
343 */
344 - hashmap_init(&file_table, NULL, rename_src_nr);
344 + hashmap_init(&file_table, NULL, NULL, rename_src_nr);
345 for (i = rename_src_nr-1; i >= 0; i--)
346 insert_file_table(&file_table, i, rename_src[i].p->one);
347
hashmap.c
+12 -5
@@ -95,7 +95,9 @@ static inline int entry_equals(const struct hashmap *map,
95 const struct hashmap_entry *e1, const struct hashmap_entry *e2,
96 const void *keydata)
97 {
98 - return (e1 == e2) || (e1->hash == e2->hash && !map->cmpfn(e1, e2, keydata));
98 + return (e1 == e2) ||
99 + (e1->hash == e2->hash &&
100 + !map->cmpfn(map->cmpfn_data, e1, e2, keydata));
101 }
102
103 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,
142 return e;
143 }
144
143 -static int always_equal(const void *unused1, const void *unused2, const void *unused3)
145 +static int always_equal(const void *unused_cmp_data,
146 + const void *unused1,
147 + const void *unused2,
148 + const void *unused_keydata)
149 {
150 return 0;
151 }
152
153 void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
149 - size_t initial_size)
154 + const void *cmpfn_data, size_t initial_size)
155 {
156 unsigned int size = HASHMAP_INITIAL_SIZE;
157
158 memset(map, 0, sizeof(*map));
159
160 map->cmpfn = equals_function ? equals_function : always_equal;
161 + map->cmpfn_data = cmpfn_data;
162
163 /* calculate initial table size and allocate the table */
164 initial_size = (unsigned int) ((uint64_t) initial_size * 100
@@ -260,7 +266,8 @@ struct pool_entry {
266 unsigned char data[FLEX_ARRAY];
267 };
268
263 -static int pool_entry_cmp(const struct pool_entry *e1,
269 +static int pool_entry_cmp(const void *unused_cmp_data,
270 + const struct pool_entry *e1,
271 const struct pool_entry *e2,
272 const unsigned char *keydata)
273 {
@@ -275,7 +282,7 @@ const void *memintern(const void *data, size_t len)
282
283 /* initialize string pool hashmap */
284 if (!map.tablesize)
278 - hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, 0);
285 + hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, NULL, 0);
286
287 /* lookup interned string in pool */
288 hashmap_entry_init(&key, memhash(data, len));
hashmap.h
+8 -4
@@ -32,12 +32,14 @@ struct hashmap_entry {
32 unsigned int hash;
33 };
34
35 -typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
36 - const void *keydata);
35 +typedef int (*hashmap_cmp_fn)(const void *hashmap_cmp_fn_data,
36 + const void *entry, const void *entry_or_key,
37 + const void *keydata);
38
39 struct hashmap {
40 struct hashmap_entry **table;
41 hashmap_cmp_fn cmpfn;
42 + const void *cmpfn_data;
43 unsigned int size, tablesize, grow_at, shrink_at;
44 unsigned disallow_rehash : 1;
45 };
@@ -50,8 +52,10 @@ struct hashmap_iter {
52
53 /* hashmap functions */
54
53 -extern void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
54 - size_t initial_size);
55 +extern void hashmap_init(struct hashmap *map,
56 + hashmap_cmp_fn equals_function,
57 + const void *equals_function_data,
58 + size_t initial_size);
59 extern void hashmap_free(struct hashmap *map, int free_entries);
60
61 /* hashmap_entry functions */
name-hash.c
+10 -6
@@ -16,8 +16,10 @@ struct dir_entry {
16 char name[FLEX_ARRAY];
17 };
18
19 -static int dir_entry_cmp(const struct dir_entry *e1,
20 - const struct dir_entry *e2, const char *name)
19 +static int dir_entry_cmp(const void *unused_cmp_data,
20 + const struct dir_entry *e1,
21 + const struct dir_entry *e2,
22 + const char *name)
23 {
24 return e1->namelen != e2->namelen || strncasecmp(e1->name,
25 name ? name : e2->name, e1->namelen);
@@ -107,8 +109,10 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
109 add_dir_entry(istate, ce);
110 }
111
110 -static int cache_entry_cmp(const struct cache_entry *ce1,
111 - const struct cache_entry *ce2, const void *remove)
112 +static int cache_entry_cmp(const void *unused_cmp_data,
113 + const struct cache_entry *ce1,
114 + const struct cache_entry *ce2,
115 + const void *remove)
116 {
117 /*
118 * For remove_name_hash, find the exact entry (pointer equality); for
@@ -571,9 +575,9 @@ static void lazy_init_name_hash(struct index_state *istate)
575 if (istate->name_hash_initialized)
576 return;
577 hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
574 - istate->cache_nr);
578 + NULL, istate->cache_nr);
579 hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp,
576 - istate->cache_nr);
580 + NULL, istate->cache_nr);
581
582 if (lookup_lazy_params(istate)) {
583 hashmap_disallow_rehash(&istate->dir_hash, 1);
oidset.c
+3 -2
@@ -6,7 +6,8 @@ struct oidset_entry {
6 struct object_id oid;
7 };
8
9 -static int oidset_hashcmp(const void *va, const void *vb,
9 +static int oidset_hashcmp(const void *unused_cmp_data,
10 + const void *va, const void *vb,
11 const void *vkey)
12 {
13 const struct oidset_entry *a = va, *b = vb;
@@ -30,7 +31,7 @@ int oidset_insert(struct oidset *set, const struct object_id *oid)
31 struct oidset_entry *entry;
32
33 if (!set->map.cmpfn)
33 - hashmap_init(&set->map, oidset_hashcmp, 0);
34 + hashmap_init(&set->map, oidset_hashcmp, NULL, 0);
35
36 if (oidset_contains(set, oid))
37 return 1;
patch-ids.c
+4 -2
@@ -35,7 +35,8 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
35 * the side of safety. The actual value being negative does not have
36 * any significance; only that it is non-zero matters.
37 */
38 -static int patch_id_cmp(struct patch_id *a,
38 +static int patch_id_cmp(const void *unused_cmp_data,
39 + struct patch_id *a,
40 struct patch_id *b,
41 struct diff_options *opt)
42 {
@@ -57,7 +58,8 @@ int init_patch_ids(struct patch_ids *ids)
58 ids->diffopts.detect_rename = 0;
59 DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
60 diff_setup_done(&ids->diffopts);
60 - hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256);
61 + hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,
62 + NULL, 256);
63 return 0;
64 }
65
refs.c
+3 -2
@@ -1525,7 +1525,8 @@ struct ref_store_hash_entry
1525 char name[FLEX_ARRAY];
1526 };
1527
1528 -static int ref_store_hash_cmp(const void *entry, const void *entry_or_key,
1528 +static int ref_store_hash_cmp(const void *unused_cmp_data,
1529 + const void *entry, const void *entry_or_key,
1530 const void *keydata)
1531 {
1532 const struct ref_store_hash_entry *e1 = entry, *e2 = entry_or_key;
@@ -1608,7 +1609,7 @@ static void register_ref_store_map(struct hashmap *map,
1609 const char *name)
1610 {
1611 if (!map->tablesize)
1611 - hashmap_init(map, ref_store_hash_cmp, 0);
1612 + hashmap_init(map, ref_store_hash_cmp, NULL, 0);
1613
1614 if (hashmap_put(map, alloc_ref_store_hash_entry(name, refs)))
1615 die("BUG: %s ref_store '%s' initialized twice", type, name);
remote.c
+5 -2
@@ -133,7 +133,10 @@ struct remotes_hash_key {
133 int len;
134 };
135
136 -static int remotes_hash_cmp(const struct remote *a, const struct remote *b, const struct remotes_hash_key *key)
136 +static int remotes_hash_cmp(const void *unused_cmp_data,
137 + const struct remote *a,
138 + const struct remote *b,
139 + const struct remotes_hash_key *key)
140 {
141 if (key)
142 return strncmp(a->name, key->str, key->len) || a->name[key->len];
@@ -144,7 +147,7 @@ static int remotes_hash_cmp(const struct remote *a, const struct remote *b, cons
147 static inline void init_remotes_hash(void)
148 {
149 if (!remotes_hash.cmpfn)
147 - hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, 0);
150 + hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, NULL, 0);
151 }
152
153 static struct remote *make_remote(const char *name, int len)
sha1_file.c
+3 -2
@@ -2389,7 +2389,8 @@ static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
2389 return a->p == b->p && a->base_offset == b->base_offset;
2390 }
2391
2392 -static int delta_base_cache_hash_cmp(const void *va, const void *vb,
2392 +static int delta_base_cache_hash_cmp(const void *unused_cmp_data,
2393 + const void *va, const void *vb,
2394 const void *vkey)
2395 {
2396 const struct delta_base_cache_entry *a = va, *b = vb;
@@ -2472,7 +2473,7 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
2473 list_add_tail(&ent->lru, &delta_base_cache_lru);
2474
2475 if (!delta_base_cache.cmpfn)
2475 - hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, 0);
2476 + hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0);
2477 hashmap_entry_init(ent, pack_entry_hash(p, base_offset));
2478 hashmap_add(&delta_base_cache, ent);
2479 }
sub-process.c
+4 -3
@@ -5,9 +5,10 @@
5 #include "sigchain.h"
6 #include "pkt-line.h"
7
8 -int cmd2process_cmp(const struct subprocess_entry *e1,
9 - const struct subprocess_entry *e2,
10 - const void *unused)
8 +int cmd2process_cmp(const void *unused_cmp_data,
9 + const struct subprocess_entry *e1,
10 + const struct subprocess_entry *e2,
11 + const void *unused_keydata)
12 {
13 return strcmp(e1->cmd, e2->cmd);
14 }
sub-process.h
+4 -2
@@ -20,8 +20,10 @@ struct subprocess_entry {
20
21 /* subprocess functions */
22
23 -int cmd2process_cmp(const struct subprocess_entry *e1,
24 - const struct subprocess_entry *e2, const void *unused);
23 +extern int cmd2process_cmp(const void *unused_cmp_data,
24 + const struct subprocess_entry *e1,
25 + const struct subprocess_entry *e2,
26 + const void *unused_keydata);
27
28 typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
29 int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
submodule-config.c
+8 -6
@@ -34,17 +34,19 @@ enum lookup_type {
34 static struct submodule_cache the_submodule_cache;
35 static int is_cache_init;
36
37 -static int config_path_cmp(const struct submodule_entry *a,
37 +static int config_path_cmp(const void *unused_cmp_data,
38 + const struct submodule_entry *a,
39 const struct submodule_entry *b,
39 - const void *unused)
40 + const void *unused_keydata)
41 {
42 return strcmp(a->config->path, b->config->path) ||
43 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
44 }
45
45 -static int config_name_cmp(const struct submodule_entry *a,
46 +static int config_name_cmp(const void *unused_cmp_data,
47 + const struct submodule_entry *a,
48 const struct submodule_entry *b,
47 - const void *unused)
49 + const void *unused_keydata)
50 {
51 return strcmp(a->config->name, b->config->name) ||
52 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
@@ -52,8 +54,8 @@ static int config_name_cmp(const struct submodule_entry *a,
54
55 static void cache_init(struct submodule_cache *cache)
56 {
55 - hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
56 - hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
57 + hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, NULL, 0);
58 + hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, NULL, 0);
59 }
60
61 static void free_one_config(struct submodule_entry *entry)
t/helper/test-hashmap.c
+12 -7
@@ -13,14 +13,18 @@ static const char *get_value(const struct test_entry *e)
13 return e->key + strlen(e->key) + 1;
14 }
15
16 -static int test_entry_cmp(const struct test_entry *e1,
17 - const struct test_entry *e2, const char* key)
16 +static int test_entry_cmp(const void *unused_cmp_data,
17 + const struct test_entry *e1,
18 + const struct test_entry *e2,
19 + const char* key)
20 {
21 return strcmp(e1->key, key ? key : e2->key);
22 }
23
22 -static int test_entry_cmp_icase(const struct test_entry *e1,
23 - const struct test_entry *e2, const char* key)
24 +static int test_entry_cmp_icase(const void *unused_cmp_data,
25 + const struct test_entry *e1,
26 + const struct test_entry *e2,
27 + const char* key)
28 {
29 return strcasecmp(e1->key, key ? key : e2->key);
30 }
@@ -92,7 +96,8 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
96 if (method & TEST_ADD) {
97 /* test adding to the map */
98 for (j = 0; j < rounds; j++) {
95 - hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, 0);
99 + hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp,
100 + NULL, 0);
101
102 /* add entries */
103 for (i = 0; i < TEST_SIZE; i++) {
@@ -104,7 +109,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
109 }
110 } else {
111 /* test map lookups */
107 - hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, 0);
112 + hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, NULL, 0);
113
114 /* fill the map (sparsely if specified) */
115 j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
@@ -147,7 +152,7 @@ int cmd_main(int argc, const char **argv)
152 /* init hash map */
153 icase = argc > 1 && !strcmp("ignorecase", argv[1]);
154 hashmap_init(&map, (hashmap_cmp_fn) (icase ? test_entry_cmp_icase
150 - : test_entry_cmp), 0);
155 + : test_entry_cmp), NULL, 0);
156
157 /* process commands from stdin */
158 while (fgets(line, sizeof(line), stdin)) {