-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_auto.c
98 lines (80 loc) · 2.03 KB
/
test_auto.c
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
#include "hashtable.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
hashtable_hash_t mt_hash(void *data) {
char *x;
int l, i;
hashtable_hash_t res;
x = (char*)data;
l = strlen(x);
if (l >= 4) {
return x[0] + x[1]*0x100 + x[l-1]*0x10000 + x[l-2]*0x1000000 + l * 0x100000000;
} else {
res = l;
while (l) {
res = res * 0x100 + x[l-1];
l--;
}
return res+1;
}
}
int mt_compare(void *data, void *key) {
char *x, *y;
x = (char*)data;
y = (char*)key;
return strcmp(x, y) == 0;
}
int main(int argc, char *argv[]) {
autoHashtable_t *ht;
char key[80], value[80];
char *res, *res2;
int tmp;
printf("Sizing suggestion? ");
gets(value);
sscanf(value, "%d", &tmp);
ht = autoHashtable_make(tmp, mt_compare, mt_hash, free, free);
for (;;) {
printf("Mode? 1:query/insert 2:remove 3:enum ");
gets(key);
if (key[0] == '1') {
for (;;) {
printf("Key? ");
gets(key);
if (key[0] == '\0') break;
res = (char*)autoHashtable_search(ht, key);
if (!res) {
printf("Not found. Data? ");
gets(value);
autoHashtable_insert(ht, strdup(key), strdup(value));
} else {
printf("Value = %s\n", res);
}
}
} else if (key[0] == '2') {
for (;;) {
printf("Key? ");
gets(key);
if (key[0] == '\0') break;
res = (char*)autoHashtable_search(ht, key);
if (!res) {
printf("Not found.\n");
} else {
printf("Value = %s\n", res);
}
if (autoHashtable_remove(ht, key)) {
printf("Removal OK\n");
} else {
printf("Removal FAILED\n");
}
}
} else if (key[0] == '3') {
hashtable_enum_t *s;
s = autoHashtable_enumerate(NULL, ht, (void**)(&res), (void**)(&res2));
while (s) {
printf("Key: %s Value: %s\n", res, res2);
s = autoHashtable_enumerate(s, ht, (void**)(&res), (void**)(&res2));
}
}
}
}