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 <johannes.schindelin@gmx.de> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Nov 30, 2017 at 00:51 UTC
826c778f7c3bd6c8356a8ecfd1cb0e4326fcf1d8
1 file changed
+29
-31
hashmap.h
+29
-31
@@ -18,75 +18,71 @@
18
*
19
* #define COMPARE_VALUE 1
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,
23
* const struct long2string *e2,
23
- * const void *keydata, const void *userdata)
24
+ * const void *keydata)
25
* {
25
- * char *string = keydata;
26
- * unsigned *flags = (unsigned*)userdata;
26
+ * const char *string = keydata;
27
+ * unsigned flags = *(unsigned *)hashmap_cmp_fn_data;
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);
32
* else
32
- * return !(e1->key == e2->key);
33
+ * return e1->key != e2->key;
34
* }
35
*
36
* int main(int argc, char **argv)
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
*
42
* hashmap_init(&map, (hashmap_cmp_fn) long2string_cmp, &flags, 0);
43
*
44
- * while (scanf("%s %l %s", action, key, value)) {
44
+ * while (scanf("%s %ld %s", action, &key, value)) {
45
*
46
* if (!strcmp("add", action)) {
47
* struct long2string *e;
48
- * e = malloc(sizeof(struct long2string) + strlen(value));
48
+ * FLEX_ALLOC_STR(e, value, value);
49
* hashmap_entry_init(e, memhash(&key, sizeof(long)));
50
* e->key = key;
51
- * memcpy(e->value, value, strlen(value));
51
* hashmap_add(&map, e);
52
* }
53
*
54
* if (!strcmp("print_all_by_key", action)) {
56
- * flags &= ~COMPARE_VALUE;
57
- *
58
- * struct long2string k;
55
+ * struct long2string k, *e;
56
* hashmap_entry_init(&k, memhash(&key, sizeof(long)));
57
* k.key = key;
58
*
62
- * struct long2string *e = hashmap_get(&map, &k, NULL);
59
+ * flags &= ~COMPARE_VALUE;
60
+ * e = hashmap_get(&map, &k, NULL);
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);
65
* }
66
* }
67
*
68
* if (!strcmp("has_exact_match", action)) {
71
- * flags |= COMPARE_VALUE;
72
- *
69
* struct long2string *e;
74
- * e = malloc(sizeof(struct long2string) + strlen(value));
70
+ * FLEX_ALLOC_STR(e, value, value);
71
* hashmap_entry_init(e, memhash(&key, sizeof(long)));
72
* e->key = key;
77
- * memcpy(e->value, value, strlen(value));
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);
77
* }
78
*
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;
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 ");
86
* }
87
*
88
* if (!strcmp("end", action)) {
@@ -94,6 +90,8 @@
90
* break;
91
* }
92
* }
93
+ *
94
+ * return 0;
95
* }
96
*/
97