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 | #define STRING_HASH_1(KEY, RESULT) do { \
|
---|
103 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
104 | while (*++_key_) \
|
---|
105 | (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
|
---|
106 | } while (0)
|
---|
107 | #define return_STRING_HASH_1(KEY) do { \
|
---|
108 | unsigned long _result_ = 0; \
|
---|
109 | STRING_HASH_1 ((KEY), _result_); \
|
---|
110 | return _result_; \
|
---|
111 | } while (0)
|
---|
112 |
|
---|
113 | #define STRING_HASH_2(KEY, RESULT) do { \
|
---|
114 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
115 | while (*++_key_) \
|
---|
116 | (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
|
---|
117 | } while (0)
|
---|
118 | #define return_STRING_HASH_2(KEY) do { \
|
---|
119 | unsigned long _result_ = 0; \
|
---|
120 | STRING_HASH_2 ((KEY), _result_); \
|
---|
121 | return _result_; \
|
---|
122 | } while (0)
|
---|
123 |
|
---|
124 | #define STRING_COMPARE(X, Y, RESULT) do { \
|
---|
125 | RESULT = strcmp ((X), (Y)); \
|
---|
126 | } while (0)
|
---|
127 | #define return_STRING_COMPARE(X, Y) do { \
|
---|
128 | return strcmp ((X), (Y)); \
|
---|
129 | } while (0)
|
---|
130 |
|
---|
131 |
|
---|
132 | #define STRING_N_HASH_1(KEY, N, RESULT) do { \
|
---|
133 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
134 | int _n_ = (N); \
|
---|
135 | if (_n_) \
|
---|
136 | while (--_n_ && *++_key_) \
|
---|
137 | (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
|
---|
138 | (RESULT) += *++_key_; \
|
---|
139 | } while (0)
|
---|
140 | #define return_STRING_N_HASH_1(KEY, N) do { \
|
---|
141 | unsigned long _result_ = 0; \
|
---|
142 | STRING_N_HASH_1 ((KEY), (N), _result_); \
|
---|
143 | return _result_; \
|
---|
144 | } while (0)
|
---|
145 |
|
---|
146 | #define STRING_N_HASH_2(KEY, N, RESULT) do { \
|
---|
147 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
148 | int _n_ = (N); \
|
---|
149 | if (_n_) \
|
---|
150 | while (--_n_ && *++_key_) \
|
---|
151 | (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
|
---|
152 | (RESULT) += *++_key_; \
|
---|
153 | } while (0)
|
---|
154 | #define return_STRING_N_HASH_2(KEY, N) do { \
|
---|
155 | unsigned long _result_ = 0; \
|
---|
156 | STRING_N_HASH_2 ((KEY), (N), _result_); \
|
---|
157 | return _result_; \
|
---|
158 | } while (0)
|
---|
159 |
|
---|
160 | #define STRING_N_COMPARE(X, Y, N, RESULT) do { \
|
---|
161 | RESULT = strncmp ((X), (Y), (N)); \
|
---|
162 | } while (0)
|
---|
163 | #define return_STRING_N_COMPARE(X, Y, N) do { \
|
---|
164 | return strncmp ((X), (Y), (N)); \
|
---|
165 | } while (0)
|
---|
166 |
|
---|
167 | #ifdef HAVE_CASE_INSENSITIVE_FS
|
---|
168 |
|
---|
169 | /* hash and comparison macros for case-insensitive string _key_s. */
|
---|
170 |
|
---|
171 | #define ISTRING_HASH_1(KEY, RESULT) do { \
|
---|
172 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
173 | while (*++_key_) \
|
---|
174 | (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0xf)); \
|
---|
175 | } while (0)
|
---|
176 | #define return_ISTRING_HASH_1(KEY) do { \
|
---|
177 | unsigned long _result_ = 0; \
|
---|
178 | ISTRING_HASH_1 ((KEY), _result_); \
|
---|
179 | return _result_; \
|
---|
180 | } while (0)
|
---|
181 |
|
---|
182 | #define ISTRING_HASH_2(KEY, RESULT) do { \
|
---|
183 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
184 | while (*++_key_) \
|
---|
185 | (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0x7)); \
|
---|
186 | } while (0)
|
---|
187 | #define return_ISTRING_HASH_2(KEY) do { \
|
---|
188 | unsigned long _result_ = 0; \
|
---|
189 | ISTRING_HASH_2 ((KEY), _result_); \
|
---|
190 | return _result_; \
|
---|
191 | } while (0)
|
---|
192 |
|
---|
193 | #define ISTRING_COMPARE(X, Y, RESULT) do { \
|
---|
194 | RESULT = strcasecmp ((X), (Y)); \
|
---|
195 | } while (0)
|
---|
196 | #define return_ISTRING_COMPARE(X, Y) do { \
|
---|
197 | return strcasecmp ((X), (Y)); \
|
---|
198 | } while (0)
|
---|
199 |
|
---|
200 | #else
|
---|
201 |
|
---|
202 | #define ISTRING_HASH_1(KEY, RESULT) STRING_HASH_1 ((KEY), (RESULT))
|
---|
203 | #define return_ISTRING_HASH_1(KEY) return_STRING_HASH_1 (KEY)
|
---|
204 |
|
---|
205 | #define ISTRING_HASH_2(KEY, RESULT) STRING_HASH_2 ((KEY), (RESULT))
|
---|
206 | #define return_ISTRING_HASH_2(KEY) return_STRING_HASH_2 (KEY)
|
---|
207 |
|
---|
208 | #define ISTRING_COMPARE(X, Y, RESULT) STRING_COMPARE ((X), (Y), (RESULT))
|
---|
209 | #define return_ISTRING_COMPARE(X, Y) return_STRING_COMPARE ((X), (Y))
|
---|
210 |
|
---|
211 | #endif
|
---|
212 |
|
---|
213 | /* hash and comparison macros for integer _key_s. */
|
---|
214 |
|
---|
215 | #define INTEGER_HASH_1(KEY, RESULT) do { \
|
---|
216 | (RESULT) += ((unsigned long)(KEY)); \
|
---|
217 | } while (0)
|
---|
218 | #define return_INTEGER_HASH_1(KEY) do { \
|
---|
219 | unsigned long _result_ = 0; \
|
---|
220 | INTEGER_HASH_1 ((KEY), _result_); \
|
---|
221 | return _result_; \
|
---|
222 | } while (0)
|
---|
223 |
|
---|
224 | #define INTEGER_HASH_2(KEY, RESULT) do { \
|
---|
225 | (RESULT) += ~((unsigned long)(KEY)); \
|
---|
226 | } while (0)
|
---|
227 | #define return_INTEGER_HASH_2(KEY) do { \
|
---|
228 | unsigned long _result_ = 0; \
|
---|
229 | INTEGER_HASH_2 ((KEY), _result_); \
|
---|
230 | return _result_; \
|
---|
231 | } while (0)
|
---|
232 |
|
---|
233 | #define INTEGER_COMPARE(X, Y, RESULT) do { \
|
---|
234 | (RESULT) = X - Y; \
|
---|
235 | } while (0)
|
---|
236 | #define return_INTEGER_COMPARE(X, Y) do { \
|
---|
237 | int _result_; \
|
---|
238 | INTEGER_COMPARE (X, Y, _result_); \
|
---|
239 | return _result_; \
|
---|
240 | } while (0)
|
---|
241 |
|
---|
242 | /* hash and comparison macros for address keys. */
|
---|
243 |
|
---|
244 | #define ADDRESS_HASH_1(KEY, RESULT) INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3, (RESULT))
|
---|
245 | #define ADDRESS_HASH_2(KEY, RESULT) INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3, (RESULT))
|
---|
246 | #define ADDRESS_COMPARE(X, Y, RESULT) INTEGER_COMPARE ((X), (Y), (RESULT))
|
---|
247 | #define return_ADDRESS_HASH_1(KEY) return_INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3)
|
---|
248 | #define return_ADDRESS_HASH_2(KEY) return_INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3)
|
---|
249 | #define return_ADDRESS_COMPARE(X, Y) return_INTEGER_COMPARE ((X), (Y))
|
---|
250 |
|
---|
251 | #endif /* not _hash_h_ */
|
---|