Skip to content

Commit 5d5baf9

Browse files
committed
hashmap: adjust documentation to reflect reality
The hashmap API is just complicated enough that even at least one long-time Git contributor has to look up how to use it every time he finds a new use case. When that happens, it is really useful if the provided example code is correct... While at it, "fix a memory leak", avoid statements before variable declarations, fix a const -> no-const cast, several %l specifiers (which want to be %ld), avoid using an undefined constant, call scanf() correctly, use FLEX_ALLOC_STR() where appropriate, and adjust the style here and there. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 81ab034 commit 5d5baf9

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

hashmap.h

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,82 +18,80 @@
1818
*
1919
* #define COMPARE_VALUE 1
2020
*
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,
2223
* const struct long2string *e2,
23-
* const void *keydata, const void *userdata)
24+
* const void *keydata)
2425
* {
25-
* char *string = keydata;
26-
* unsigned *flags = (unsigned*)userdata;
26+
* const char *string = keydata;
27+
* unsigned flags = *(unsigned *)hashmap_cmp_fn_data;
2728
*
2829
* 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);
3132
* else
32-
* return !(e1->key == e2->key);
33+
* return e1->key != e2->key;
3334
* }
3435
*
3536
* int main(int argc, char **argv)
3637
* {
3738
* long key;
38-
* char *value, *action;
39-
*
40-
* unsigned flags = ALLOW_DUPLICATE_KEYS;
39+
* char value[255], action[32];
40+
* unsigned flags = 0;
4141
*
4242
* hashmap_init(&map, (hashmap_cmp_fn) long2string_cmp, &flags, 0);
4343
*
44-
* while (scanf("%s %l %s", action, key, value)) {
44+
* while (scanf("%s %ld %s", action, &key, value)) {
4545
*
4646
* if (!strcmp("add", action)) {
4747
* struct long2string *e;
48-
* e = malloc(sizeof(struct long2string) + strlen(value));
48+
* FLEX_ALLOC_STR(e, value, value);
4949
* hashmap_entry_init(e, memhash(&key, sizeof(long)));
5050
* e->key = key;
51-
* memcpy(e->value, value, strlen(value));
5251
* hashmap_add(&map, e);
5352
* }
5453
*
5554
* if (!strcmp("print_all_by_key", action)) {
56-
* flags &= ~COMPARE_VALUE;
57-
*
58-
* struct long2string k;
55+
* struct long2string k, *e;
5956
* hashmap_entry_init(&k, memhash(&key, sizeof(long)));
6057
* k.key = key;
6158
*
62-
* struct long2string *e = hashmap_get(&map, &k, NULL);
59+
* flags &= ~COMPARE_VALUE;
60+
* e = hashmap_get(&map, &k, NULL);
6361
* 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);
6765
* }
6866
* }
6967
*
7068
* if (!strcmp("has_exact_match", action)) {
71-
* flags |= COMPARE_VALUE;
72-
*
7369
* struct long2string *e;
74-
* e = malloc(sizeof(struct long2string) + strlen(value));
70+
* FLEX_ALLOC_STR(e, value, value);
7571
* hashmap_entry_init(e, memhash(&key, sizeof(long)));
7672
* e->key = key;
77-
* memcpy(e->value, value, strlen(value));
7873
*
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);
8077
* }
8178
*
8279
* 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;
8883
*
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 ");
9086
* }
9187
*
9288
* if (!strcmp("end", action)) {
9389
* hashmap_free(&map, 1);
9490
* break;
9591
* }
9692
* }
93+
*
94+
* return 0;
9795
* }
9896
*/
9997

0 commit comments

Comments
 (0)