#define _XOPEN_SOURCE 500 #include #include #include #include GEHASHMAP_DEF(char *, int, atoimap) GEHASHMAP_DEF(int, unsigned int, itoumap) bool atoimap_key_iseq(char *a, char *b) { return strcmp(a, b) == 0; } bool itoumap_key_iseq(int a, int b) { return a == b; } size_t atoimap_key_hash(char *s) { char c; size_t x = 5381; while ((c = *s++)) x = ((x << 5) + x) + c; return x; } size_t itoumap_key_hash(int n) { return n; } int main(void) { int i; unsigned int u; struct atoimap_entry *entry, *tmp; atoimap_t *map_a; itoumap_t *map_b; atoimap_new(map_a = malloc(sizeof(atoimap_t)), 0, 0); itoumap_new(map_b = malloc(sizeof(itoumap_t)), 0, 0); atoimap_set(map_a, "Thomas Voß", 5); atoimap_set(map_a, "THOMAS VOẞ", 6); atoimap_set(map_a, "Thomas Voss", 7); atoimap_get(map_a, "Thomas Voß", &i); printf("Thomas Voß -> %d\n", i); atoimap_get(map_a, "THOMAS VOẞ", &i); printf("THOMAS VOẞ -> %d\n", i); atoimap_get(map_a, "Thomas Voss", &i); printf("Thomas Voss -> %d\n", i); GEHASHMAP_FOREACH_SAFE(entry, tmp, *map_a) atoimap_remove(map_a, entry->key); GEHASHMAP_FOREACH(entry, *map_a) puts(entry->key); itoumap_set(map_b, -42, +42); itoumap_set(map_b, -69, +69); itoumap_set(map_b, -420, +420); /* Inefficient */ if (itoumap_has(map_b, -21)) { itoumap_get(map_b, -21, &u); printf("%d -> %u\n", -21, u); } /* Efficient */ if (itoumap_get(map_b, -42, &u)) printf("%d -> %u\n", -42, u); if (itoumap_get(map_b, -69, &u)) printf("%d -> %u\n", -69, u); itoumap_remove(map_b, -420); if (itoumap_get(map_b, -420, &u)) printf("%d -> %u\n", -420, u); atoimap_free(map_a); itoumap_free(map_b); atoimap_new(map_a, 0, 0); atoimap_set(map_a, strdup("Thomas Voß"), 5); atoimap_set(map_a, strdup("THOMAS VOẞ"), 6); atoimap_set(map_a, strdup("Thomas Voss"), 7); GEHASHMAP_FOREACH(entry, *map_a) free(entry->key); atoimap_free(map_a); free(map_a); free(map_b); }