1 | /* hash.h -- decls for hash table
|
---|
2 | Copyright (C) 1995, 1999, 2002 Free Software Foundation, Inc.
|
---|
3 | Written by Greg McGary <[email protected]> <[email protected]>
|
---|
4 |
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 2, or (at your option)
|
---|
8 | any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License along with
|
---|
16 | this program; see the file COPYING. If not, write to the Free Software
|
---|
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
|
---|
18 |
|
---|
19 | #ifndef _hash_h_
|
---|
20 | #define _hash_h_
|
---|
21 |
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <ctype.h>
|
---|
24 |
|
---|
25 | #if defined __cplusplus || (defined __STDC__ && __STDC__) || defined WINDOWS32
|
---|
26 | # if !defined __GLIBC__ || !defined __P
|
---|
27 | # undef __P
|
---|
28 | # define __P(protos) protos
|
---|
29 | # endif
|
---|
30 | #else /* Not C++ or ANSI C. */
|
---|
31 | # undef __P
|
---|
32 | # define __P(protos) ()
|
---|
33 | /* We can get away without defining `const' here only because in this file
|
---|
34 | it is used only inside the prototype for `fnmatch', which is elided in
|
---|
35 | non-ANSI C where `const' is problematical. */
|
---|
36 | #endif /* C++ or ANSI C. */
|
---|
37 |
|
---|
38 | typedef unsigned long (*hash_func_t) __P((void const *key));
|
---|
39 | typedef int (*hash_cmp_func_t) __P((void const *x, void const *y));
|
---|
40 | typedef void (*hash_map_func_t) __P((void const *item));
|
---|
41 | typedef void (*hash_map_arg_func_t) __P((void const *item, void *arg));
|
---|
42 |
|
---|
43 | struct hash_table
|
---|
44 | {
|
---|
45 | void **ht_vec;
|
---|
46 | unsigned long ht_size; /* total number of slots (power of 2) */
|
---|
47 | unsigned long ht_capacity; /* usable slots, limited by loading-factor */
|
---|
48 | unsigned long ht_fill; /* items in table */
|
---|
49 | unsigned long ht_empty_slots; /* empty slots not including deleted slots */
|
---|
50 | unsigned long ht_collisions; /* # of failed calls to comparison function */
|
---|
51 | unsigned long ht_lookups; /* # of queries */
|
---|
52 | unsigned int ht_rehashes; /* # of times we've expanded table */
|
---|
53 | hash_func_t ht_hash_1; /* primary hash function */
|
---|
54 | hash_func_t ht_hash_2; /* secondary hash function */
|
---|
55 | hash_cmp_func_t ht_compare; /* comparison function */
|
---|
56 | #ifdef CONFIG_WITH_STRCACHE2
|
---|
57 | struct strcache2 *ht_strcache; /* the string cache pointer. */
|
---|
58 | unsigned int ht_off_string; /* offsetof (struct key, string) */
|
---|
59 | #endif
|
---|
60 | };
|
---|
61 |
|
---|
62 | typedef int (*qsort_cmp_t) __P((void const *, void const *));
|
---|
63 |
|
---|
64 | void hash_init __P((struct hash_table *ht, unsigned long size,
|
---|
65 | hash_func_t hash_1, hash_func_t hash_2, hash_cmp_func_t hash_cmp));
|
---|
66 | void hash_load __P((struct hash_table *ht, void *item_table,
|
---|
67 | unsigned long cardinality, unsigned long size));
|
---|
68 | void **hash_find_slot __P((struct hash_table *ht, void const *key));
|
---|
69 | void *hash_find_item __P((struct hash_table *ht, void const *key));
|
---|
70 | void *hash_insert __P((struct hash_table *ht, const void *item));
|
---|
71 | void *hash_insert_at __P((struct hash_table *ht, const void *item, void const *slot));
|
---|
72 | void *hash_delete __P((struct hash_table *ht, void const *item));
|
---|
73 | void *hash_delete_at __P((struct hash_table *ht, void const *slot));
|
---|
74 | void hash_delete_items __P((struct hash_table *ht));
|
---|
75 | void hash_free_items __P((struct hash_table *ht));
|
---|
76 | void hash_free __P((struct hash_table *ht, int free_items));
|
---|
77 | #ifdef CONFIG_WITH_ALLOC_CACHES
|
---|
78 | void hash_free_items_cached __P((struct hash_table *ht, struct alloccache *cache));
|
---|
79 | void hash_free_cached __P((struct hash_table *ht, int free_items, struct alloccache *cache));
|
---|
80 | #endif
|
---|
81 | void hash_map __P((struct hash_table *ht, hash_map_func_t map));
|
---|
82 | void hash_map_arg __P((struct hash_table *ht, hash_map_arg_func_t map, void *arg));
|
---|
83 | void hash_print_stats __P((struct hash_table *ht, FILE *out_FILE));
|
---|
84 | void **hash_dump __P((struct hash_table *ht, void **vector_0, qsort_cmp_t compare));
|
---|
85 |
|
---|
86 | #ifdef CONFIG_WITH_STRCACHE2
|
---|
87 | void hash_init_strcached __P((struct hash_table *ht, unsigned long size,
|
---|
88 | struct strcache2 *strcache, unsigned int off_strptr));
|
---|
89 | void **hash_find_slot_strcached __P((struct hash_table *ht, const void *key));
|
---|
90 | void *hash_find_item_strcached __P((struct hash_table *ht, void const *key));
|
---|
91 | void *hash_insert_strcached __P((struct hash_table *ht, const void *item));
|
---|
92 | void *hash_delete_strcached __P((struct hash_table *ht, void const *item));
|
---|
93 | #endif /* CONFIG_WITH_STRCACHE2 */
|
---|
94 |
|
---|
95 | extern void *hash_deleted_item;
|
---|
96 | #define HASH_VACANT(item) ((item) == 0 || (void *) (item) == hash_deleted_item)
|
---|
97 |
|
---|
98 | |
---|
99 |
|
---|
100 | /* hash and comparison macros for case-sensitive string keys. */
|
---|
101 |
|
---|
102 | #ifndef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
103 | #define STRING_HASH_1(KEY, RESULT) do { \
|
---|
104 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
105 | while (*++_key_) \
|
---|
106 | (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
|
---|
107 | } while (0)
|
---|
108 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
109 | # define STRING_HASH_1(KEY, RESULT) do { \
|
---|
110 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
---|
111 | unsigned int _ch_ = *_key_; \
|
---|
112 | while (_ch_) \
|
---|
113 | { \
|
---|
114 | unsigned char _ch2_ = *++_key_; \
|
---|
115 | (RESULT) += (_ch_ << (_ch2_ & 0xf)); \
|
---|
116 | _ch_ = _ch2_; \
|
---|
117 | } \
|
---|
118 | } while (0)
|
---|
119 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
120 | #define return_STRING_HASH_1(KEY) do { \
|
---|
121 | unsigned long _result_ = 0; \
|
---|
122 | STRING_HASH_1 ((KEY), _result_); \
|
---|
123 | return _result_; \
|
---|
124 | } while (0)
|
---|
125 |
|
---|
126 | #ifndef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
127 | #define STRING_HASH_2(KEY, RESULT) do { \
|
---|
128 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
129 | while (*++_key_) \
|
---|
130 | (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
|
---|
131 | } while (0)
|
---|
132 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
133 | # define STRING_HASH_2(KEY, RESULT) do { \
|
---|
134 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
---|
135 | unsigned int _ch_ = *_key_; \
|
---|
136 | while (_ch_) \
|
---|
137 | { \
|
---|
138 | unsigned char _ch2_ = *++_key_; \
|
---|
139 | (RESULT) += (_ch_ << (_ch2_ & 0x7)); \
|
---|
140 | _ch_ = _ch2_; \
|
---|
141 | } \
|
---|
142 | } while (0)
|
---|
143 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
144 | #define return_STRING_HASH_2(KEY) do { \
|
---|
145 | unsigned long _result_ = 0; \
|
---|
146 | STRING_HASH_2 ((KEY), _result_); \
|
---|
147 | return _result_; \
|
---|
148 | } while (0)
|
---|
149 |
|
---|
150 | #define STRING_COMPARE(X, Y, RESULT) do { \
|
---|
151 | RESULT = strcmp ((X), (Y)); \
|
---|
152 | } while (0)
|
---|
153 | #define return_STRING_COMPARE(X, Y) do { \
|
---|
154 | return strcmp ((X), (Y)); \
|
---|
155 | } while (0)
|
---|
156 |
|
---|
157 | #ifndef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
158 | #define STRING_N_HASH_1(KEY, N, RESULT) do { \
|
---|
159 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
160 | int _n_ = (N); \
|
---|
161 | if (_n_) \
|
---|
162 | while (--_n_ && *++_key_) \
|
---|
163 | (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
|
---|
164 | (RESULT) += *++_key_; \
|
---|
165 | } while (0)
|
---|
166 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
167 | # define STRING_N_HASH_1(KEY, N, RESULT) do { \
|
---|
168 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
---|
169 | unsigned int _ch_ = *_key_; \
|
---|
170 | int _n_ = (N); \
|
---|
171 | if (_n_) \
|
---|
172 | { \
|
---|
173 | for (;;) \
|
---|
174 | { \
|
---|
175 | unsigned char _ch2_; \
|
---|
176 | if (!--_n_) \
|
---|
177 | { \
|
---|
178 | (RESULT) += _ch_; \
|
---|
179 | break; \
|
---|
180 | } \
|
---|
181 | _ch2_ = *++_key_; \
|
---|
182 | (RESULT) += (_ch_ << (_ch2_ & 0xf)); \
|
---|
183 | _ch_ = _ch2_; \
|
---|
184 | if (!_ch_) break; \
|
---|
185 | } \
|
---|
186 | } \
|
---|
187 | else \
|
---|
188 | (RESULT) += _ch_; \
|
---|
189 | } while (0)
|
---|
190 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
191 | #define return_STRING_N_HASH_1(KEY, N) do { \
|
---|
192 | unsigned long _result_ = 0; \
|
---|
193 | STRING_N_HASH_1 ((KEY), (N), _result_); \
|
---|
194 | return _result_; \
|
---|
195 | } while (0)
|
---|
196 |
|
---|
197 | #ifndef CONFIG_WITH_OPTIMIZATION_HACKS
|
---|
198 | #define STRING_N_HASH_2(KEY, N, RESULT) do { \
|
---|
199 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
200 | int _n_ = (N); \
|
---|
201 | if (_n_) \
|
---|
202 | while (--_n_ && *++_key_) \
|
---|
203 | (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
|
---|
204 | (RESULT) += *++_key_; \
|
---|
205 | } while (0)
|
---|
206 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
207 | # define STRING_N_HASH_2(KEY, N, RESULT) do { \
|
---|
208 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
---|
209 | unsigned int _ch_ = *_key_; \
|
---|
210 | int _n_ = (N); \
|
---|
211 | if (_n_) \
|
---|
212 | { \
|
---|
213 | for (;;) \
|
---|
214 | { \
|
---|
215 | unsigned char _ch2_; \
|
---|
216 | if (!--_n_) \
|
---|
217 | { \
|
---|
218 | (RESULT) += _ch_; \
|
---|
219 | break; \
|
---|
220 | } \
|
---|
221 | _ch2_ = *++_key_; \
|
---|
222 | (RESULT) += (_ch_ << (_ch2_ & 0x7)); \
|
---|
223 | _ch_ = _ch2_; \
|
---|
224 | if (!_ch_) break; \
|
---|
225 | } \
|
---|
226 | } \
|
---|
227 | else \
|
---|
228 | (RESULT) += _ch_; \
|
---|
229 | } while (0)
|
---|
230 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
231 | #define return_STRING_N_HASH_2(KEY, N) do { \
|
---|
232 | unsigned long _result_ = 0; \
|
---|
233 | STRING_N_HASH_2 ((KEY), (N), _result_); \
|
---|
234 | return _result_; \
|
---|
235 | } while (0)
|
---|
236 |
|
---|
237 | #define STRING_N_COMPARE(X, Y, N, RESULT) do { \
|
---|
238 | RESULT = strncmp ((X), (Y), (N)); \
|
---|
239 | } while (0)
|
---|
240 | #define return_STRING_N_COMPARE(X, Y, N) do { \
|
---|
241 | return strncmp ((X), (Y), (N)); \
|
---|
242 | } while (0)
|
---|
243 |
|
---|
244 | #ifdef HAVE_CASE_INSENSITIVE_FS
|
---|
245 |
|
---|
246 | /* hash and comparison macros for case-insensitive string _key_s. */
|
---|
247 |
|
---|
248 | #if 1 /*ndef CONFIG_WITH_OPTIMIZATION_HACKS - testme */
|
---|
249 | #define ISTRING_HASH_1(KEY, RESULT) do { \
|
---|
250 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
251 | while (*++_key_) \
|
---|
252 | (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0xf)); \
|
---|
253 | } while (0)
|
---|
254 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
255 | #define ISTRING_HASH_1(KEY, RESULT) do { \
|
---|
256 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
---|
257 | unsigned int _ch_ = *_key_;
|
---|
258 | while (_ch_) \
|
---|
259 | { \
|
---|
260 | unsigned _ch2_ = *++_key_; \
|
---|
261 | (RESULT) += ((isupper (_ch_) ? tolower (_ch_) : _ch_) << (_ch2_ & 0xf)); \
|
---|
262 | _ch_ = _ch2_; \
|
---|
263 | } \
|
---|
264 | } while (0)
|
---|
265 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
266 | #define return_ISTRING_HASH_1(KEY) do { \
|
---|
267 | unsigned long _result_ = 0; \
|
---|
268 | ISTRING_HASH_1 ((KEY), _result_); \
|
---|
269 | return _result_; \
|
---|
270 | } while (0)
|
---|
271 |
|
---|
272 | #if 1 /* ndef CONFIG_WITH_OPTIMIZATION_HACKS - testme */
|
---|
273 | #define ISTRING_HASH_2(KEY, RESULT) do { \
|
---|
274 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
275 | while (*++_key_) \
|
---|
276 | (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0x7)); \
|
---|
277 | } while (0)
|
---|
278 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
279 | #define ISTRING_HASH_2(KEY, RESULT) do { \
|
---|
280 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
---|
281 | unsigned int _ch_ = *_key_;
|
---|
282 | while (_ch_) \
|
---|
283 | { \
|
---|
284 | unsigned _ch2_ = *++_key_; \
|
---|
285 | (RESULT) += ((isupper (_ch_) ? tolower (_ch_) : _ch_) << (_ch2_ & 0x7)); \
|
---|
286 | _ch_ = _ch2_; \
|
---|
287 | } \
|
---|
288 | } while (0)
|
---|
289 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
---|
290 | #define return_ISTRING_HASH_2(KEY) do { \
|
---|
291 | unsigned long _result_ = 0; \
|
---|
292 | ISTRING_HASH_2 ((KEY), _result_); \
|
---|
293 | return _result_; \
|
---|
294 | } while (0)
|
---|
295 |
|
---|
296 | #define ISTRING_COMPARE(X, Y, RESULT) do { \
|
---|
297 | RESULT = strcasecmp ((X), (Y)); \
|
---|
298 | } while (0)
|
---|
299 | #define return_ISTRING_COMPARE(X, Y) do { \
|
---|
300 | return strcasecmp ((X), (Y)); \
|
---|
301 | } while (0)
|
---|
302 |
|
---|
303 | #else
|
---|
304 |
|
---|
305 | #define ISTRING_HASH_1(KEY, RESULT) STRING_HASH_1 ((KEY), (RESULT))
|
---|
306 | #define return_ISTRING_HASH_1(KEY) return_STRING_HASH_1 (KEY)
|
---|
307 |
|
---|
308 | #define ISTRING_HASH_2(KEY, RESULT) STRING_HASH_2 ((KEY), (RESULT))
|
---|
309 | #define return_ISTRING_HASH_2(KEY) return_STRING_HASH_2 (KEY)
|
---|
310 |
|
---|
311 | #define ISTRING_COMPARE(X, Y, RESULT) STRING_COMPARE ((X), (Y), (RESULT))
|
---|
312 | #define return_ISTRING_COMPARE(X, Y) return_STRING_COMPARE ((X), (Y))
|
---|
313 |
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | /* hash and comparison macros for integer _key_s. */
|
---|
317 |
|
---|
318 | #define INTEGER_HASH_1(KEY, RESULT) do { \
|
---|
319 | (RESULT) += ((unsigned long)(KEY)); \
|
---|
320 | } while (0)
|
---|
321 | #define return_INTEGER_HASH_1(KEY) do { \
|
---|
322 | unsigned long _result_ = 0; \
|
---|
323 | INTEGER_HASH_1 ((KEY), _result_); \
|
---|
324 | return _result_; \
|
---|
325 | } while (0)
|
---|
326 |
|
---|
327 | #define INTEGER_HASH_2(KEY, RESULT) do { \
|
---|
328 | (RESULT) += ~((unsigned long)(KEY)); \
|
---|
329 | } while (0)
|
---|
330 | #define return_INTEGER_HASH_2(KEY) do { \
|
---|
331 | unsigned long _result_ = 0; \
|
---|
332 | INTEGER_HASH_2 ((KEY), _result_); \
|
---|
333 | return _result_; \
|
---|
334 | } while (0)
|
---|
335 |
|
---|
336 | #define INTEGER_COMPARE(X, Y, RESULT) do { \
|
---|
337 | (RESULT) = X - Y; \
|
---|
338 | } while (0)
|
---|
339 | #define return_INTEGER_COMPARE(X, Y) do { \
|
---|
340 | int _result_; \
|
---|
341 | INTEGER_COMPARE (X, Y, _result_); \
|
---|
342 | return _result_; \
|
---|
343 | } while (0)
|
---|
344 |
|
---|
345 | /* hash and comparison macros for address keys. */
|
---|
346 |
|
---|
347 | #define ADDRESS_HASH_1(KEY, RESULT) INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3, (RESULT))
|
---|
348 | #define ADDRESS_HASH_2(KEY, RESULT) INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3, (RESULT))
|
---|
349 | #define ADDRESS_COMPARE(X, Y, RESULT) INTEGER_COMPARE ((X), (Y), (RESULT))
|
---|
350 | #define return_ADDRESS_HASH_1(KEY) return_INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3)
|
---|
351 | #define return_ADDRESS_HASH_2(KEY) return_INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3)
|
---|
352 | #define return_ADDRESS_COMPARE(X, Y) return_INTEGER_COMPARE ((X), (Y))
|
---|
353 |
|
---|
354 | #endif /* not _hash_h_ */
|
---|