summaryrefslogtreecommitdiff
path: root/examples/gehashmap.c
blob: f216d00d607eb964d213de38c83ee91983cf80e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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 <gehashmap.h>

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);
}