diff options
author | Thomas Voss <mail@thomasvoss.com> | 2022-12-23 23:38:50 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2022-12-23 23:38:50 +0100 |
commit | 2ba320056c0f08c6171fd61766eb880f88b54b75 (patch) | |
tree | a17042afdb68f35f99eff3aa5d123e01bb3f4961 /examples | |
parent | 175ee3240d1f5cc929b994ea5eef2b11363c7046 (diff) |
Include more maps and functions in hashmap example
Diffstat (limited to 'examples')
-rw-r--r-- | examples/gehashmap.c | 82 |
1 files changed, 65 insertions, 17 deletions
diff --git a/examples/gehashmap.c b/examples/gehashmap.c index b0763ac..b7a86ff 100644 --- a/examples/gehashmap.c +++ b/examples/gehashmap.c @@ -4,23 +4,37 @@ #include <gehashmap.h> -GEHASHMAP_API(char *, int, funmap); -GEHASHMAP_IMPL(char *, int, funmap); +GEHASHMAP_API(char *, int, atoimap); +GEHASHMAP_API(int, unsigned int, itoumap); +GEHASHMAP_IMPL(char *, int, atoimap); +GEHASHMAP_IMPL(int, unsigned int, itoumap); bool -funmap_key_iseq(char *a, char *b) +atoimap_key_iseq(char *a, char *b) { return strcmp(a, b) == 0; } +bool +itoumap_key_iseq(int a, int b) +{ + return a == b; +} + void -funmap_key_free(char *s) +atoimap_key_free(char *s) { free(s); } +void +itoumap_key_free(int a) +{ + (void) a; +} + size_t -funmap_key_hash(char *s) +atoimap_key_hash(char *s) { char c; size_t x = 5381; @@ -31,19 +45,53 @@ funmap_key_hash(char *s) return x; } +size_t +itoumap_key_hash(int n) +{ + return n; +} + int main(void) { - int n; - funmap_t *map; - - funmap_new(map = malloc(sizeof(funmap_t))); - funmap_set(map, strdup("Thomas Voß"), 5); - funmap_set(map, strdup("THOMAS VOẞ"), 6); - funmap_set(map, strdup("Thomas Voss"), 7); - funmap_get(map, "Thomas Voß", &n); printf("Thomas Voß -> ‚%d‘\n", n); - funmap_get(map, "THOMAS VOẞ", &n); printf("THOMAS VOẞ -> ‚%d‘\n", n); - funmap_get(map, "Thomas Voss", &n); printf("Thomas Voss -> ‚%d‘\n", n); - funmap_free(map); - free(map); + int i; + unsigned int u; + atoimap_t *map_a; + itoumap_t *map_b; + + atoimap_new(map_a = malloc(sizeof(atoimap_t))); + itoumap_new(map_b = malloc(sizeof(itoumap_t))); + + atoimap_set(map_a, strdup("Thomas Voß"), 5); + atoimap_set(map_a, strdup("THOMAS VOẞ"), 6); + atoimap_set(map_a, strdup("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); + + 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); + + free(map_a); + free(map_b); } |