summaryrefslogtreecommitdiffstats
path: root/include/linux/ghash.h
blob: 278f6c2f630df5cbc5edcf4e6691e00fc2abb00e (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * include/linux/ghash.h -- generic hashing with fuzzy retrieval
 *
 * (C) 1997 Thomas Schoebel-Theuer
 *
 * The algorithms implemented here seem to be a completely new invention,
 * and I'll publish the fundamentals in a paper.
 */

#ifndef _GHASH_H
#define _GHASH_H
/* HASHSIZE _must_ be a power of two!!! */


#define DEF_HASH_FUZZY_STRUCTS(NAME,HASHSIZE,TYPE) \
\
struct NAME##_table {\
	TYPE * hashtable[HASHSIZE];\
	TYPE * sorted_list;\
	int nr_entries;\
};\
\
struct NAME##_ptrs {\
	TYPE * next_hash;\
	TYPE * prev_hash;\
	TYPE * next_sorted;\
	TYPE * prev_sorted;\
};

#define DEF_HASH_FUZZY(LINKAGE,NAME,HASHSIZE,TYPE,PTRS,KEYTYPE,KEY,KEYCMP,KEYEQ,HASHFN)\
\
LINKAGE void insert_##NAME##_hash(struct NAME##_table * tbl, TYPE * elem)\
{\
	int ix = HASHFN(elem->KEY);\
	TYPE ** base = &tbl->hashtable[ix];\
	TYPE * ptr = *base;\
	TYPE * prev = NULL;\
\
	tbl->nr_entries++;\
	while(ptr && KEYCMP(ptr->KEY, elem->KEY)) {\
		base = &ptr->PTRS.next_hash;\
		prev = ptr;\
		ptr = *base;\
	}\
	elem->PTRS.next_hash = ptr;\
	elem->PTRS.prev_hash = prev;\
	if(ptr) {\
		ptr->PTRS.prev_hash = elem;\
	}\
	*base = elem;\
\
	ptr = prev;\
	if(!ptr) {\
		ptr = tbl->sorted_list;\
		prev = NULL;\
	} else {\
		prev = ptr->PTRS.prev_sorted;\
	}\
	while(ptr) {\
		TYPE * next = ptr->PTRS.next_hash;\
		if(next && KEYCMP(next->KEY, elem->KEY)) {\
			prev = ptr;\
			ptr = next;\
		} else if(KEYCMP(ptr->KEY, elem->KEY)) {\
			prev = ptr;\
			ptr = ptr->PTRS.next_sorted;\
		} else\
			break;\
	}\
	elem->PTRS.next_sorted = ptr;\
	elem->PTRS.prev_sorted = prev;\
	if(ptr) {\
		ptr->PTRS.prev_sorted = elem;\
	}\
	if(prev) {\
		prev->PTRS.next_sorted = elem;\
	} else {\
		tbl->sorted_list = elem;\
	}\
}\
\
LINKAGE void remove_##NAME##_hash(struct NAME##_table * tbl, TYPE * elem)\
{\
	TYPE * next = elem->PTRS.next_hash;\
	TYPE * prev = elem->PTRS.prev_hash;\
\
	tbl->nr_entries--;\
	if(next)\
		next->PTRS.prev_hash = prev;\
	if(prev)\
		prev->PTRS.next_hash = next;\
	else {\
		int ix = HASHFN(elem->KEY);\
		tbl->hashtable[ix] = next;\
	}\
\
	next = elem->PTRS.next_sorted;\
	prev = elem->PTRS.prev_sorted;\
	if(next)\
		next->PTRS.prev_sorted = prev;\
	if(prev)\
		prev->PTRS.next_sorted = next;\
	else\
		tbl->sorted_list = next;\
}\
\
LINKAGE TYPE * find_##NAME##_hash(struct NAME##_table * tbl, KEYTYPE pos)\
{\
	int ix = hashfn(pos);\
	TYPE * ptr = tbl->hashtable[ix];\
	while(ptr && KEYCMP(ptr->KEY, pos))\
		ptr = ptr->PTRS.next_hash;\
	if(ptr && !KEYEQ(ptr->KEY, pos))\
		ptr = NULL;\
	return ptr;\
}\
\
LINKAGE TYPE * find_##NAME##_hash_fuzzy(struct NAME##_table * tbl, KEYTYPE pos)\
{\
	int ix;\
	int offset;\
	TYPE * ptr;\
	TYPE * next;\
\
	ptr = tbl->sorted_list;\
	if(!ptr || KEYCMP(pos, ptr->KEY))\
		return NULL;\
	ix = HASHFN(pos);\
	offset = HASHSIZE;\
	do {\
		offset >>= 1;\
		next = tbl->hashtable[(ix+offset) & ((HASHSIZE)-1)];\
		if(next && (KEYCMP(next->KEY, pos) || KEYEQ(next->KEY, pos))\
		   && KEYCMP(ptr->KEY, next->KEY))\
			ptr = next;\
	} while(offset);\
\
	for(;;) {\
		next = ptr->PTRS.next_hash;\
		if(next) {\
			if(KEYCMP(next->KEY, pos)) {\
				ptr = next;\
				continue;\
			}\
		}\
		next = ptr->PTRS.next_sorted;\
		if(next && KEYCMP(next->KEY, pos)) {\
			ptr = next;\
			continue;\
		}\
		return ptr;\
	}\
	return NULL;\
}

#define DEF_HASH_STRUCTS(NAME,HASHSIZE,TYPE) \
\
struct NAME##_table {\
	TYPE * hashtable[HASHSIZE];\
	int nr_entries;\
};\
\
struct NAME##_ptrs {\
	TYPE * next_hash;\
	TYPE * prev_hash;\
};

#define DEF_HASH(LINKAGE,NAME,HASHSIZE,TYPE,PTRS,KEYTYPE,KEY,KEYCMP,KEYEQ,HASHFN)\
\
LINKAGE void insert_##NAME##_hash(struct NAME##_table * tbl, TYPE * elem)\
{\
	int ix = HASHFN(elem->KEY);\
	TYPE ** base = &tbl->hashtable[ix];\
	TYPE * ptr = *base;\
	TYPE * prev = NULL;\
\
	tbl->nr_entries++;\
	while(ptr && KEYCMP(ptr->KEY, elem->KEY)) {\
		base = &ptr->PTRS.next_hash;\
		prev = ptr;\
		ptr = *base;\
	}\
	elem->PTRS.next_hash = ptr;\
	elem->PTRS.prev_hash = prev;\
	if(ptr) {\
		ptr->PTRS.prev_hash = elem;\
	}\
	*base = elem;\
}\
\
LINKAGE void remove_##NAME##_hash(struct NAME##_table * tbl, TYPE * elem)\
{\
	TYPE * next = elem->PTRS.next_hash;\
	TYPE * prev = elem->PTRS.prev_hash;\
\
	tbl->nr_entries--;\
	if(next)\
		next->PTRS.prev_hash = prev;\
	if(prev)\
		prev->PTRS.next_hash = next;\
	else {\
		int ix = HASHFN(elem->KEY);\
		tbl->hashtable[ix] = next;\
	}\
}\
\
LINKAGE TYPE * find_##NAME##_hash(struct NAME##_table * tbl, KEYTYPE pos)\
{\
	int ix = hashfn(pos);\
	TYPE * ptr = tbl->hashtable[ix];\
	while(ptr && KEYCMP(ptr->KEY, pos))\
		ptr = ptr->PTRS.next_hash;\
	if(ptr && !KEYEQ(ptr->KEY, pos))\
		ptr = NULL;\
	return ptr;\
}

#endif