2 * include/linux/ghash.h -- generic hashing with fuzzy retrieval
4 * (C) 1997 Thomas Schoebel-Theuer
6 * The algorithms implemented here seem to be a completely new invention,
7 * and I'll publish the fundamentals in a paper.
12 /* HASHSIZE _must_ be a power of two!!! */
15 #define DEF_HASH_FUZZY_STRUCTS(NAME,HASHSIZE,TYPE) \
17 struct NAME##_table {\
18 TYPE * hashtable[HASHSIZE];\
30 #define DEF_HASH_FUZZY(LINKAGE,NAME,HASHSIZE,TYPE,PTRS,KEYTYPE,KEY,KEYCMP,KEYEQ,HASHFN)\
32 LINKAGE void insert_##NAME##_hash(struct NAME##_table * tbl, TYPE * elem)\
34 int ix = HASHFN(elem->KEY);\
35 TYPE ** base = &tbl->hashtable[ix];\
40 while(ptr && KEYCMP(ptr->KEY, elem->KEY)) {\
41 base = &ptr->PTRS.next_hash;\
45 elem->PTRS.next_hash = ptr;\
46 elem->PTRS.prev_hash = prev;\
48 ptr->PTRS.prev_hash = elem;\
54 ptr = tbl->sorted_list;\
57 prev = ptr->PTRS.prev_sorted;\
60 TYPE * next = ptr->PTRS.next_hash;\
61 if(next && KEYCMP(next->KEY, elem->KEY)) {\
64 } else if(KEYCMP(ptr->KEY, elem->KEY)) {\
66 ptr = ptr->PTRS.next_sorted;\
70 elem->PTRS.next_sorted = ptr;\
71 elem->PTRS.prev_sorted = prev;\
73 ptr->PTRS.prev_sorted = elem;\
76 prev->PTRS.next_sorted = elem;\
78 tbl->sorted_list = elem;\
82 LINKAGE void remove_##NAME##_hash(struct NAME##_table * tbl, TYPE * elem)\
84 TYPE * next = elem->PTRS.next_hash;\
85 TYPE * prev = elem->PTRS.prev_hash;\
89 next->PTRS.prev_hash = prev;\
91 prev->PTRS.next_hash = next;\
93 int ix = HASHFN(elem->KEY);\
94 tbl->hashtable[ix] = next;\
97 next = elem->PTRS.next_sorted;\
98 prev = elem->PTRS.prev_sorted;\
100 next->PTRS.prev_sorted = prev;\
102 prev->PTRS.next_sorted = next;\
104 tbl->sorted_list = next;\
107 LINKAGE TYPE * find_##NAME##_hash(struct NAME##_table * tbl, KEYTYPE pos)\
109 int ix = hashfn(pos);\
110 TYPE * ptr = tbl->hashtable[ix];\
111 while(ptr && KEYCMP(ptr->KEY, pos))\
112 ptr = ptr->PTRS.next_hash;\
113 if(ptr && !KEYEQ(ptr->KEY, pos))\
118 LINKAGE TYPE * find_##NAME##_hash_fuzzy(struct NAME##_table * tbl, KEYTYPE pos)\
125 ptr = tbl->sorted_list;\
126 if(!ptr || KEYCMP(pos, ptr->KEY))\
132 next = tbl->hashtable[(ix+offset) & ((HASHSIZE)-1)];\
133 if(next && (KEYCMP(next->KEY, pos) || KEYEQ(next->KEY, pos))\
134 && KEYCMP(ptr->KEY, next->KEY))\
139 next = ptr->PTRS.next_hash;\
141 if(KEYCMP(next->KEY, pos)) {\
146 next = ptr->PTRS.next_sorted;\
147 if(next && KEYCMP(next->KEY, pos)) {\
156 #define DEF_HASH_STRUCTS(NAME,HASHSIZE,TYPE) \
158 struct NAME##_table {\
159 TYPE * hashtable[HASHSIZE];\
163 struct NAME##_ptrs {\
168 #define DEF_HASH(LINKAGE,NAME,HASHSIZE,TYPE,PTRS,KEYTYPE,KEY,KEYCMP,KEYEQ,HASHFN)\
170 LINKAGE void insert_##NAME##_hash(struct NAME##_table * tbl, TYPE * elem)\
172 int ix = HASHFN(elem->KEY);\
173 TYPE ** base = &tbl->hashtable[ix];\
178 while(ptr && KEYCMP(ptr->KEY, elem->KEY)) {\
179 base = &ptr->PTRS.next_hash;\
183 elem->PTRS.next_hash = ptr;\
184 elem->PTRS.prev_hash = prev;\
186 ptr->PTRS.prev_hash = elem;\
191 LINKAGE void remove_##NAME##_hash(struct NAME##_table * tbl, TYPE * elem)\
193 TYPE * next = elem->PTRS.next_hash;\
194 TYPE * prev = elem->PTRS.prev_hash;\
198 next->PTRS.prev_hash = prev;\
200 prev->PTRS.next_hash = next;\
202 int ix = HASHFN(elem->KEY);\
203 tbl->hashtable[ix] = next;\
207 LINKAGE TYPE * find_##NAME##_hash(struct NAME##_table * tbl, KEYTYPE pos)\
209 int ix = hashfn(pos);\
210 TYPE * ptr = tbl->hashtable[ix];\
211 while(ptr && KEYCMP(ptr->KEY, pos))\
212 ptr = ptr->PTRS.next_hash;\
213 if(ptr && !KEYEQ(ptr->KEY, pos))\