|
18 | 18 | *
|
19 | 19 | * #define COMPARE_VALUE 1
|
20 | 20 | *
|
21 |
| - * static int long2string_cmp(const struct long2string *e1, |
| 21 | + * static int long2string_cmp(const void *hashmap_cmp_fn_data, |
| 22 | + * const struct long2string *e1, |
22 | 23 | * const struct long2string *e2,
|
23 |
| - * const void *keydata, const void *userdata) |
| 24 | + * const void *keydata) |
24 | 25 | * {
|
25 |
| - * char *string = keydata; |
26 |
| - * unsigned *flags = (unsigned*)userdata; |
| 26 | + * const char *string = keydata; |
| 27 | + * unsigned flags = *(unsigned *)hashmap_cmp_fn_data; |
27 | 28 | *
|
28 | 29 | * if (flags & COMPARE_VALUE)
|
29 |
| - * return !(e1->key == e2->key) || (keydata ? |
30 |
| - * strcmp(e1->value, keydata) : strcmp(e1->value, e2->value)); |
| 30 | + * return e1->key != e2->key || |
| 31 | + * strcmp(e1->value, string ? string : e2->value); |
31 | 32 | * else
|
32 |
| - * return !(e1->key == e2->key); |
| 33 | + * return e1->key != e2->key; |
33 | 34 | * }
|
34 | 35 | *
|
35 | 36 | * int main(int argc, char **argv)
|
36 | 37 | * {
|
37 | 38 | * long key;
|
38 |
| - * char *value, *action; |
39 |
| - * |
40 |
| - * unsigned flags = ALLOW_DUPLICATE_KEYS; |
| 39 | + * char value[255], action[32]; |
| 40 | + * unsigned flags = 0; |
41 | 41 | *
|
42 | 42 | * hashmap_init(&map, (hashmap_cmp_fn) long2string_cmp, &flags, 0);
|
43 | 43 | *
|
44 |
| - * while (scanf("%s %l %s", action, key, value)) { |
| 44 | + * while (scanf("%s %ld %s", action, &key, value)) { |
45 | 45 | *
|
46 | 46 | * if (!strcmp("add", action)) {
|
47 | 47 | * struct long2string *e;
|
48 |
| - * e = malloc(sizeof(struct long2string) + strlen(value)); |
| 48 | + * FLEX_ALLOC_STR(e, value, value); |
49 | 49 | * hashmap_entry_init(e, memhash(&key, sizeof(long)));
|
50 | 50 | * e->key = key;
|
51 |
| - * memcpy(e->value, value, strlen(value)); |
52 | 51 | * hashmap_add(&map, e);
|
53 | 52 | * }
|
54 | 53 | *
|
55 | 54 | * if (!strcmp("print_all_by_key", action)) {
|
56 |
| - * flags &= ~COMPARE_VALUE; |
57 |
| - * |
58 |
| - * struct long2string k; |
| 55 | + * struct long2string k, *e; |
59 | 56 | * hashmap_entry_init(&k, memhash(&key, sizeof(long)));
|
60 | 57 | * k.key = key;
|
61 | 58 | *
|
62 |
| - * struct long2string *e = hashmap_get(&map, &k, NULL); |
| 59 | + * flags &= ~COMPARE_VALUE; |
| 60 | + * e = hashmap_get(&map, &k, NULL); |
63 | 61 | * if (e) {
|
64 |
| - * printf("first: %l %s\n", e->key, e->value); |
65 |
| - * while (e = hashmap_get_next(&map, e)) |
66 |
| - * printf("found more: %l %s\n", e->key, e->value); |
| 62 | + * printf("first: %ld %s\n", e->key, e->value); |
| 63 | + * while ((e = hashmap_get_next(&map, e))) |
| 64 | + * printf("found more: %ld %s\n", e->key, e->value); |
67 | 65 | * }
|
68 | 66 | * }
|
69 | 67 | *
|
70 | 68 | * if (!strcmp("has_exact_match", action)) {
|
71 |
| - * flags |= COMPARE_VALUE; |
72 |
| - * |
73 | 69 | * struct long2string *e;
|
74 |
| - * e = malloc(sizeof(struct long2string) + strlen(value)); |
| 70 | + * FLEX_ALLOC_STR(e, value, value); |
75 | 71 | * hashmap_entry_init(e, memhash(&key, sizeof(long)));
|
76 | 72 | * e->key = key;
|
77 |
| - * memcpy(e->value, value, strlen(value)); |
78 | 73 | *
|
79 |
| - * printf("%s found\n", hashmap_get(&map, e, NULL) ? "" : "not"); |
| 74 | + * flags |= COMPARE_VALUE; |
| 75 | + * printf("%sfound\n", hashmap_get(&map, e, NULL) ? "" : "not "); |
| 76 | + * free(e); |
80 | 77 | * }
|
81 | 78 | *
|
82 | 79 | * if (!strcmp("has_exact_match_no_heap_alloc", action)) {
|
83 |
| - * flags |= COMPARE_VALUE; |
84 |
| - * |
85 |
| - * struct long2string e; |
86 |
| - * hashmap_entry_init(e, memhash(&key, sizeof(long))); |
87 |
| - * e.key = key; |
| 80 | + * struct long2string k; |
| 81 | + * hashmap_entry_init(&k, memhash(&key, sizeof(long))); |
| 82 | + * k.key = key; |
88 | 83 | *
|
89 |
| - * printf("%s found\n", hashmap_get(&map, e, value) ? "" : "not"); |
| 84 | + * flags |= COMPARE_VALUE; |
| 85 | + * printf("%sfound\n", hashmap_get(&map, &k, value) ? "" : "not "); |
90 | 86 | * }
|
91 | 87 | *
|
92 | 88 | * if (!strcmp("end", action)) {
|
93 | 89 | * hashmap_free(&map, 1);
|
94 | 90 | * break;
|
95 | 91 | * }
|
96 | 92 | * }
|
| 93 | + * |
| 94 | + * return 0; |
97 | 95 | * }
|
98 | 96 | */
|
99 | 97 |
|
|
0 commit comments