From 7663cdc86c860d5b5293a1dd4b0fb6c4e006d08e Mon Sep 17 00:00:00 2001 From: Stefan Beller Date: Fri, 30 Jun 2017 12:14:05 -0700 Subject: 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 Signed-off-by: Junio C Hamano --- t/helper/test-hashmap.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 't') diff --git a/t/helper/test-hashmap.c b/t/helper/test-hashmap.c index 7aa9440e27..095d7395f3 100644 --- a/t/helper/test-hashmap.c +++ b/t/helper/test-hashmap.c @@ -13,14 +13,18 @@ static const char *get_value(const struct test_entry *e) return e->key + strlen(e->key) + 1; } -static int test_entry_cmp(const struct test_entry *e1, - const struct test_entry *e2, const char* key) +static int test_entry_cmp(const void *unused_cmp_data, + const struct test_entry *e1, + const struct test_entry *e2, + const char* key) { return strcmp(e1->key, key ? key : e2->key); } -static int test_entry_cmp_icase(const struct test_entry *e1, - const struct test_entry *e2, const char* key) +static int test_entry_cmp_icase(const void *unused_cmp_data, + const struct test_entry *e1, + const struct test_entry *e2, + const char* key) { return strcasecmp(e1->key, key ? key : e2->key); } @@ -92,7 +96,8 @@ static void perf_hashmap(unsigned int method, unsigned int rounds) if (method & TEST_ADD) { /* test adding to the map */ for (j = 0; j < rounds; j++) { - hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, 0); + hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, + NULL, 0); /* add entries */ for (i = 0; i < TEST_SIZE; i++) { @@ -104,7 +109,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds) } } else { /* test map lookups */ - hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, 0); + hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, NULL, 0); /* fill the map (sparsely if specified) */ j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE; @@ -147,7 +152,7 @@ int cmd_main(int argc, const char **argv) /* init hash map */ icase = argc > 1 && !strcmp("ignorecase", argv[1]); hashmap_init(&map, (hashmap_cmp_fn) (icase ? test_entry_cmp_icase - : test_entry_cmp), 0); + : test_entry_cmp), NULL, 0); /* process commands from stdin */ while (fgets(line, sizeof(line), stdin)) { -- cgit v1.2.3