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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gehashmap.h>
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);
}
|