From 9baa39e871c2ed9934e3e1c381f3f38927346bf6 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Wed, 21 Dec 2022 23:14:57 +0100 Subject: Genesis commit --- examples/Makefile | 2 ++ examples/gehashmap.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 examples/Makefile create mode 100644 examples/gehashmap.c (limited to 'examples') diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..0d87a45 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,2 @@ +all: + cc -Og -g -ggdb gehashmap.c -o gehashmap diff --git a/examples/gehashmap.c b/examples/gehashmap.c new file mode 100644 index 0000000..f216d00 --- /dev/null +++ b/examples/gehashmap.c @@ -0,0 +1,42 @@ +#include +#include +#include + +size_t hash(char *); + +#define GEHASHMAP_NAME funmap +#define GEHASHMAP_KEY_T char * +#define GEHASHMAP_VAL_T int +#define GEHASHMAP_CMP_FUNC (int (*)(char *, char *)) strcmp +#define GEHASHMAP_FREE_FUNC (void (*)(char *)) free +#define GEHASHMAP_HASH_FUNC hash +#include + +size_t +hash(char *s) +{ + char c; + size_t x = 5381; + + while (c = *s++) + x = ((x << 5) + x) + c; + + return x; +} + +int +main(void) +{ + int n; + funmap_t *map; + + funmap_new(map = malloc(sizeof(funmap_t))); + funmap_put(map, strdup("Thomas Voß"), 5); + funmap_put(map, strdup("THOMAS VOẞ"), 6); + funmap_put(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); +} -- cgit v1.2.3