Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Schindelin <johannes.schindelin@gmx.de>2017-11-30 02:51:41 +0300
committerJunio C Hamano <gitster@pobox.com>2017-12-06 00:37:43 +0300
commit826c778f7c3bd6c8356a8ecfd1cb0e4326fcf1d8 (patch)
tree24d40fd01c10d696c3faadf26b7fa1584bcfcebc /hashmap.h
parent9b185bef0c15cec1ea8753ce091e42ea041f2c31 (diff)
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>
Diffstat (limited to 'hashmap.h')
-rw-r--r--hashmap.h60
1 files changed, 29 insertions, 31 deletions
diff --git a/hashmap.h b/hashmap.h
index 7cb29a6aed..7ce79f3f72 100644
--- a/hashmap.h
+++ b/hashmap.h
@@ -18,75 +18,71 @@
*
* #define COMPARE_VALUE 1
*
- * static int long2string_cmp(const struct long2string *e1,
+ * static int long2string_cmp(const void *hashmap_cmp_fn_data,
+ * const struct long2string *e1,
* const struct long2string *e2,
- * const void *keydata, const void *userdata)
+ * const void *keydata)
* {
- * char *string = keydata;
- * unsigned *flags = (unsigned*)userdata;
+ * const char *string = keydata;
+ * unsigned flags = *(unsigned *)hashmap_cmp_fn_data;
*
* if (flags & COMPARE_VALUE)
- * return !(e1->key == e2->key) || (keydata ?
- * strcmp(e1->value, keydata) : strcmp(e1->value, e2->value));
+ * return e1->key != e2->key ||
+ * strcmp(e1->value, string ? string : e2->value);
* else
- * return !(e1->key == e2->key);
+ * return e1->key != e2->key;
* }
*
* int main(int argc, char **argv)
* {
* long key;
- * char *value, *action;
- *
- * unsigned flags = ALLOW_DUPLICATE_KEYS;
+ * char value[255], action[32];
+ * unsigned flags = 0;
*
* hashmap_init(&map, (hashmap_cmp_fn) long2string_cmp, &flags, 0);
*
- * while (scanf("%s %l %s", action, key, value)) {
+ * while (scanf("%s %ld %s", action, &key, value)) {
*
* if (!strcmp("add", action)) {
* struct long2string *e;
- * e = malloc(sizeof(struct long2string) + strlen(value));
+ * FLEX_ALLOC_STR(e, value, value);
* hashmap_entry_init(e, memhash(&key, sizeof(long)));
* e->key = key;
- * memcpy(e->value, value, strlen(value));
* hashmap_add(&map, e);
* }
*
* if (!strcmp("print_all_by_key", action)) {
- * flags &= ~COMPARE_VALUE;
- *
- * struct long2string k;
+ * struct long2string k, *e;
* hashmap_entry_init(&k, memhash(&key, sizeof(long)));
* k.key = key;
*
- * struct long2string *e = hashmap_get(&map, &k, NULL);
+ * flags &= ~COMPARE_VALUE;
+ * e = hashmap_get(&map, &k, NULL);
* if (e) {
- * printf("first: %l %s\n", e->key, e->value);
- * while (e = hashmap_get_next(&map, e))
- * printf("found more: %l %s\n", e->key, e->value);
+ * printf("first: %ld %s\n", e->key, e->value);
+ * while ((e = hashmap_get_next(&map, e)))
+ * printf("found more: %ld %s\n", e->key, e->value);
* }
* }
*
* if (!strcmp("has_exact_match", action)) {
- * flags |= COMPARE_VALUE;
- *
* struct long2string *e;
- * e = malloc(sizeof(struct long2string) + strlen(value));
+ * FLEX_ALLOC_STR(e, value, value);
* hashmap_entry_init(e, memhash(&key, sizeof(long)));
* e->key = key;
- * memcpy(e->value, value, strlen(value));
*
- * printf("%s found\n", hashmap_get(&map, e, NULL) ? "" : "not");
+ * flags |= COMPARE_VALUE;
+ * printf("%sfound\n", hashmap_get(&map, e, NULL) ? "" : "not ");
+ * free(e);
* }
*
* if (!strcmp("has_exact_match_no_heap_alloc", action)) {
- * flags |= COMPARE_VALUE;
- *
- * struct long2string e;
- * hashmap_entry_init(e, memhash(&key, sizeof(long)));
- * e.key = key;
+ * struct long2string k;
+ * hashmap_entry_init(&k, memhash(&key, sizeof(long)));
+ * k.key = key;
*
- * printf("%s found\n", hashmap_get(&map, e, value) ? "" : "not");
+ * flags |= COMPARE_VALUE;
+ * printf("%sfound\n", hashmap_get(&map, &k, value) ? "" : "not ");
* }
*
* if (!strcmp("end", action)) {
@@ -94,6 +90,8 @@
* break;
* }
* }
+ *
+ * return 0;
* }
*/