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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#include <assert.h>
#include <stdio.h>
#include "uthash.h"
typedef struct {
char name[32];
int weight;
UT_hash_handle hh;
} hstruct_t;
static int cmpfunc(const hstruct_t *s1, const hstruct_t *s2)
{
return (s1->weight < s2->weight) ? -1 : (s1->weight > s2->weight);
}
// Test that CMPFUNC can safely be a macro.
#define CMPFUNC(a,b) cmpfunc(a,b)
void printtable(const hstruct_t *hTable)
{
const hstruct_t *search, *tmp;
HASH_ITER(hh, hTable, search, tmp) {
printf("%d: %s\n", search->weight, search->name);
}
printf("###\n");
}
void delitem(hstruct_t **hTable, const char *name)
{
hstruct_t *item;
HASH_FIND_STR(*hTable, name, item);
HASH_DEL(*hTable, item);
}
int main()
{
hstruct_t *hTable = NULL;
hstruct_t *replaced = NULL;
unsigned hashvalue;
hstruct_t tst[] = {
{"muh1", 2, {0}},
{"muh2", 8, {0}},
{"muh3", 1, {0}},
{"muh4", 8, {0}},
{"muh5", 3, {0}},
{"muh6", 5, {0}},
{"muh7", 6, {0}},
{"muh8", 15, {0}},
{"muh9", 6, {0}},
{"muh10", 9, {0}},
{"muh11", 10, {0}},
{"muh12", 43, {0}},
{"muh12", 7, {0}}
};
int index;
for (index = 0; index < 11; ++index) {
HASH_ADD_INORDER(hh, hTable, name[0], strlen(tst[index].name), &tst[index], CMPFUNC);
}
// test HASH_ADD_BYHASHVALUE_INORDER
HASH_VALUE(tst[11].name, strlen(tst[11].name), hashvalue);
HASH_ADD_BYHASHVALUE_INORDER(hh, hTable, name[0], strlen(tst[11].name), hashvalue, &tst[11], CMPFUNC);
printtable(hTable);
// replace "43: muh12" with "7: muh12"
HASH_REPLACE_INORDER(hh, hTable, name[0], strlen(tst[11].name), &tst[12], replaced, CMPFUNC);
assert(replaced == &tst[11]);
printtable(hTable);
// rehash "1: muh3" to "9: muh3"
tst[2].weight = 9;
HASH_REPLACE_INORDER(hh, hTable, name[0], strlen(tst[2].name), &tst[2], replaced, CMPFUNC);
assert(replaced == &tst[2]);
printtable(hTable);
// rehash "6: muh7" to "16: muh7"
tst[6].weight = 16;
HASH_VALUE(&tst[6].name[0], strlen(tst[6].name), hashvalue);
HASH_REPLACE_BYHASHVALUE_INORDER(hh, hTable, name[0], strlen(tst[6].name), hashvalue, &tst[6], replaced, CMPFUNC);
assert(replaced == &tst[6]);
printtable(hTable);
// remove "8: muh2"...
HASH_DELETE(hh, hTable, &tst[1]);
printtable(hTable);
// ...and then reinsert "8: muh2"
HASH_VALUE(tst[1].name, strlen(tst[1].name), hashvalue);
HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, hTable, tst[1].name, strlen(tst[1].name), hashvalue, &tst[1], CMPFUNC);
printtable(hTable);
delitem(&hTable, "muh1");
delitem(&hTable, "muh7");
delitem(&hTable, "muh3");
delitem(&hTable, "muh9");
delitem(&hTable, "muh2");
delitem(&hTable, "muh11");
delitem(&hTable, "muh4");
delitem(&hTable, "muh6");
delitem(&hTable, "muh5");
delitem(&hTable, "muh8");
delitem(&hTable, "muh10");
delitem(&hTable, "muh12");
printtable(hTable);
return 0;
}
|