summaryrefslogtreecommitdiff
path: root/examples/gehashmap.c
blob: b0763acaf878e4a9c5d8bf11ece27b76bc10f589 (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
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <gehashmap.h>

GEHASHMAP_API(char *, int, funmap);
GEHASHMAP_IMPL(char *, int, funmap);

bool
funmap_key_iseq(char *a, char *b)
{
	return strcmp(a, b) == 0;
}

void
funmap_key_free(char *s)
{
	free(s);
}

size_t
funmap_key_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_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);
}