Merge branch 'js/hashmap-update-sample'

Code comment update.

* js/hashmap-update-sample:
  hashmap: adjust documentation to reflect reality
This commit is contained in:
Junio C Hamano 2017-12-13 13:28:58 -08:00
commit d9195982d8

View File

@ -18,75 +18,71 @@
* *
* #define COMPARE_VALUE 1 * #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 struct long2string *e2,
* const void *keydata, const void *userdata) * const void *keydata)
* { * {
* char *string = keydata; * const char *string = keydata;
* unsigned *flags = (unsigned*)userdata; * unsigned flags = *(unsigned *)hashmap_cmp_fn_data;
* *
* if (flags & COMPARE_VALUE) * if (flags & COMPARE_VALUE)
* return !(e1->key == e2->key) || (keydata ? * return e1->key != e2->key ||
* strcmp(e1->value, keydata) : strcmp(e1->value, e2->value)); * strcmp(e1->value, string ? string : e2->value);
* else * else
* return !(e1->key == e2->key); * return e1->key != e2->key;
* } * }
* *
* int main(int argc, char **argv) * int main(int argc, char **argv)
* { * {
* long key; * long key;
* char *value, *action; * char value[255], action[32];
* * unsigned flags = 0;
* unsigned flags = ALLOW_DUPLICATE_KEYS;
* *
* hashmap_init(&map, (hashmap_cmp_fn) long2string_cmp, &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)) { * if (!strcmp("add", action)) {
* struct long2string *e; * struct long2string *e;
* e = malloc(sizeof(struct long2string) + strlen(value)); * FLEX_ALLOC_STR(e, value, value);
* hashmap_entry_init(e, memhash(&key, sizeof(long))); * hashmap_entry_init(e, memhash(&key, sizeof(long)));
* e->key = key; * e->key = key;
* memcpy(e->value, value, strlen(value));
* hashmap_add(&map, e); * hashmap_add(&map, e);
* } * }
* *
* if (!strcmp("print_all_by_key", action)) { * if (!strcmp("print_all_by_key", action)) {
* flags &= ~COMPARE_VALUE; * struct long2string k, *e;
*
* struct long2string k;
* hashmap_entry_init(&k, memhash(&key, sizeof(long))); * hashmap_entry_init(&k, memhash(&key, sizeof(long)));
* k.key = key; * k.key = key;
* *
* struct long2string *e = hashmap_get(&map, &k, NULL); * flags &= ~COMPARE_VALUE;
* e = hashmap_get(&map, &k, NULL);
* if (e) { * if (e) {
* printf("first: %l %s\n", e->key, e->value); * printf("first: %ld %s\n", e->key, e->value);
* while (e = hashmap_get_next(&map, e)) * while ((e = hashmap_get_next(&map, e)))
* printf("found more: %l %s\n", e->key, e->value); * printf("found more: %ld %s\n", e->key, e->value);
* } * }
* } * }
* *
* if (!strcmp("has_exact_match", action)) { * if (!strcmp("has_exact_match", action)) {
* flags |= COMPARE_VALUE;
*
* struct long2string *e; * struct long2string *e;
* e = malloc(sizeof(struct long2string) + strlen(value)); * FLEX_ALLOC_STR(e, value, value);
* hashmap_entry_init(e, memhash(&key, sizeof(long))); * hashmap_entry_init(e, memhash(&key, sizeof(long)));
* e->key = key; * 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)) { * if (!strcmp("has_exact_match_no_heap_alloc", action)) {
* struct long2string k;
* hashmap_entry_init(&k, memhash(&key, sizeof(long)));
* k.key = key;
*
* flags |= COMPARE_VALUE; * flags |= COMPARE_VALUE;
* * printf("%sfound\n", hashmap_get(&map, &k, value) ? "" : "not ");
* struct long2string e;
* hashmap_entry_init(e, memhash(&key, sizeof(long)));
* e.key = key;
*
* printf("%s found\n", hashmap_get(&map, e, value) ? "" : "not");
* } * }
* *
* if (!strcmp("end", action)) { * if (!strcmp("end", action)) {
@ -94,6 +90,8 @@
* break; * break;
* } * }
* } * }
*
* return 0;
* } * }
*/ */