1 | /* hash.h -- decls for hash table
|
---|
2 | Copyright (C) 1995, 1999, 2002, 2010 Free Software Foundation, Inc.
|
---|
3 | Written by Greg McGary <[email protected]> <[email protected]>
|
---|
4 |
|
---|
5 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
6 | terms of the GNU General Public License as published by the Free Software
|
---|
7 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
8 | version.
|
---|
9 |
|
---|
10 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
12 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License along with
|
---|
15 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
16 |
|
---|
17 | #ifndef _hash_h_
|
---|
18 | #define _hash_h_
|
---|
19 |
|
---|
20 | #include <stdio.h>
|
---|
21 | #include <ctype.h>
|
---|
22 |
|
---|
23 | #if defined __cplusplus || (defined __STDC__ && __STDC__) || defined WINDOWS32
|
---|
24 | # if !defined __GLIBC__ || !defined __P
|
---|
25 | # undef __P
|
---|
26 | # define __P(protos) protos
|
---|
27 | # endif
|
---|
28 | #else /* Not C++ or ANSI C. */
|
---|
29 | # undef __P
|
---|
30 | # define __P(protos) ()
|
---|
31 | /* We can get away without defining `const' here only because in this file
|
---|
32 | it is used only inside the prototype for `fnmatch', which is elided in
|
---|
33 | non-ANSI C where `const' is problematical. */
|
---|
34 | #endif /* C++ or ANSI C. */
|
---|
35 |
|
---|
36 | typedef unsigned long (*hash_func_t) __P((void const *key));
|
---|
37 | typedef int (*hash_cmp_func_t) __P((void const *x, void const *y));
|
---|
38 | typedef void (*hash_map_func_t) __P((void const *item));
|
---|
39 | typedef void (*hash_map_arg_func_t) __P((void const *item, void *arg));
|
---|
40 |
|
---|
41 | struct hash_table
|
---|
42 | {
|
---|
43 | void **ht_vec;
|
---|
44 | unsigned long ht_size; /* total number of slots (power of 2) */
|
---|
45 | unsigned long ht_capacity; /* usable slots, limited by loading-factor */
|
---|
46 | unsigned long ht_fill; /* items in table */
|
---|
47 | unsigned long ht_empty_slots; /* empty slots not including deleted slots */
|
---|
48 | unsigned long ht_collisions; /* # of failed calls to comparison function */
|
---|
49 | unsigned long ht_lookups; /* # of queries */
|
---|
50 | unsigned int ht_rehashes; /* # of times we've expanded table */
|
---|
51 | hash_func_t ht_hash_1; /* primary hash function */
|
---|
52 | hash_func_t ht_hash_2; /* secondary hash function */
|
---|
53 | hash_cmp_func_t ht_compare; /* comparison function */
|
---|
54 | #ifdef CONFIG_WITH_STRCACHE2
|
---|
55 | struct strcache2 *ht_strcache; /* the string cache pointer. */
|
---|
56 | unsigned int ht_off_string; /* offsetof (struct key, string) */
|
---|
57 | #endif
|
---|
58 | };
|
---|
59 |
|
---|
60 | typedef int (*qsort_cmp_t) __P((void const *, void const *));
|
---|
61 |
|
---|
62 | void hash_init __P((struct hash_table *ht, unsigned long size,
|
---|
63 | hash_func_t hash_1, hash_func_t hash_2, hash_cmp_func_t hash_cmp));
|
---|
64 | void hash_load __P((struct hash_table *ht, void *item_table,
|
---|
65 | unsigned long cardinality, unsigned long size));
|
---|
66 | void **hash_find_slot __P((struct hash_table *ht, void const *key));
|
---|
67 | void *hash_find_item __P((struct hash_table *ht, void const *key));
|
---|
68 | void *hash_insert __P((struct hash_table *ht, const void *item));
|
---|
69 | void *hash_insert_at __P((struct hash_table *ht, const void *item, void const *slot));
|
---|
70 | void *hash_delete __P((struct hash_table *ht, void const *item));
|
---|
71 | void *hash_delete_at __P((struct hash_table *ht, void const *slot));
|
---|
72 | void hash_delete_items __P((struct hash_table *ht));
|
---|
73 | void hash_free_items __P((struct hash_table *ht));
|
---|
74 | void hash_free __P((struct hash_table *ht, int free_items));
|
---|
75 | #ifdef CONFIG_WITH_ALLOC_CACHES
|
---|
76 | void hash_free_items_cached __P((struct hash_table *ht, struct alloccache *cache));
|
---|
77 | void hash_free_cached __P((struct hash_table *ht, int free_items, struct alloccache *cache));
|
---|
78 | #endif
|
---|
79 | void hash_map __P((struct hash_table *ht, hash_map_func_t map));
|
---|
80 | void hash_map_arg __P((struct hash_table *ht, hash_map_arg_func_t map, void *arg));
|
---|
81 | void hash_print_stats __P((struct hash_table *ht, FILE *out_FILE));
|
---|
82 | void **hash_dump __P((struct hash_table *ht, void **vector_0, qsort_cmp_t compare));
|
---|
83 |
|
---|
84 | #ifdef CONFIG_WITH_STRCACHE2
|
---|
85 | void hash_init_strcached __P((struct hash_table *ht, unsigned long size,
|
---|
86 | struct strcache2 *strcache, unsigned int off_strptr));
|
---|
87 | void **hash_find_slot_strcached __P((struct hash_table *ht, const void *key));
|
---|
88 | void *hash_find_item_strcached __P((struct hash_table *ht, void const *key));
|
---|
89 | void *hash_insert_strcached __P((struct hash_table *ht, const void *item));
|
---|
90 | void *hash_delete_strcached __P((struct hash_table *ht, void const *item));
|
---|
91 | #endif /* CONFIG_WITH_STRCACHE2 */
|
---|
92 |
|
---|
93 | extern void *hash_deleted_item;
|
---|
94 | #define HASH_VACANT(item) ((item) == 0 || (void *) (item) == hash_deleted_item)
|
---|
95 |
|
---|
96 | |
---|
97 |
|
---|
98 | /* hash and comparison macros for case-sensitive string keys. */
|
---|
99 |
|
---|
100 | /* Due to the strcache, it's not uncommon for the string pointers to
|
---|
101 | be identical. Take advantage of that to short-circuit string compares. */
|
---|
102 |
|
---|
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 | #define return_STRING_HASH_1(KEY) do { \
|
---|
109 | unsigned long _result_ = 0; \
|
---|
110 | STRING_HASH_1 ((KEY), _result_); \
|
---|
111 | return _result_; \
|
---|
112 | } while (0)
|
---|
113 |
|
---|
114 | #define STRING_HASH_2(KEY, RESULT) do { \
|
---|
115 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
116 | while (*++_key_) \
|
---|
117 | (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
|
---|
118 | } while (0)
|
---|
119 | #define return_STRING_HASH_2(KEY) do { \
|
---|
120 | unsigned long _result_ = 0; \
|
---|
121 | STRING_HASH_2 ((KEY), _result_); \
|
---|
122 | return _result_; \
|
---|
123 | } while (0)
|
---|
124 |
|
---|
125 | #define STRING_COMPARE(X, Y, RESULT) do { \
|
---|
126 | RESULT = (X) == (Y) ? 0 : strcmp ((X), (Y)); \
|
---|
127 | } while (0)
|
---|
128 | #define return_STRING_COMPARE(X, Y) do { \
|
---|
129 | return (X) == (Y) ? 0 : strcmp ((X), (Y)); \
|
---|
130 | } while (0)
|
---|
131 |
|
---|
132 |
|
---|
133 | #define STRING_N_HASH_1(KEY, N, RESULT) do { \
|
---|
134 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
135 | int _n_ = (N); \
|
---|
136 | if (_n_) \
|
---|
137 | while (--_n_ && *++_key_) \
|
---|
138 | (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
|
---|
139 | (RESULT) += *++_key_; \
|
---|
140 | } while (0)
|
---|
141 | #define return_STRING_N_HASH_1(KEY, N) do { \
|
---|
142 | unsigned long _result_ = 0; \
|
---|
143 | STRING_N_HASH_1 ((KEY), (N), _result_); \
|
---|
144 | return _result_; \
|
---|
145 | } while (0)
|
---|
146 |
|
---|
147 | #define STRING_N_HASH_2(KEY, N, RESULT) do { \
|
---|
148 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
149 | int _n_ = (N); \
|
---|
150 | if (_n_) \
|
---|
151 | while (--_n_ && *++_key_) \
|
---|
152 | (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
|
---|
153 | (RESULT) += *++_key_; \
|
---|
154 | } while (0)
|
---|
155 | #define return_STRING_N_HASH_2(KEY, N) do { \
|
---|
156 | unsigned long _result_ = 0; \
|
---|
157 | STRING_N_HASH_2 ((KEY), (N), _result_); \
|
---|
158 | return _result_; \
|
---|
159 | } while (0)
|
---|
160 |
|
---|
161 | #define STRING_N_COMPARE(X, Y, N, RESULT) do { \
|
---|
162 | RESULT = (X) == (Y) ? 0 : strncmp ((X), (Y), (N)); \
|
---|
163 | } while (0)
|
---|
164 | #define return_STRING_N_COMPARE(X, Y, N) do { \
|
---|
165 | return (X) == (Y) ? 0 : strncmp ((X), (Y), (N)); \
|
---|
166 | } while (0)
|
---|
167 |
|
---|
168 | #ifdef HAVE_CASE_INSENSITIVE_FS
|
---|
169 |
|
---|
170 | /* hash and comparison macros for case-insensitive string _key_s. */
|
---|
171 |
|
---|
172 | #define ISTRING_HASH_1(KEY, RESULT) do { \
|
---|
173 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
174 | while (*++_key_) \
|
---|
175 | (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0xf)); \
|
---|
176 | } while (0)
|
---|
177 | #define return_ISTRING_HASH_1(KEY) do { \
|
---|
178 | unsigned long _result_ = 0; \
|
---|
179 | ISTRING_HASH_1 ((KEY), _result_); \
|
---|
180 | return _result_; \
|
---|
181 | } while (0)
|
---|
182 |
|
---|
183 | #define ISTRING_HASH_2(KEY, RESULT) do { \
|
---|
184 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
185 | while (*++_key_) \
|
---|
186 | (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0x7)); \
|
---|
187 | } while (0)
|
---|
188 | #define return_ISTRING_HASH_2(KEY) do { \
|
---|
189 | unsigned long _result_ = 0; \
|
---|
190 | ISTRING_HASH_2 ((KEY), _result_); \
|
---|
191 | return _result_; \
|
---|
192 | } while (0)
|
---|
193 |
|
---|
194 | #define ISTRING_COMPARE(X, Y, RESULT) do { \
|
---|
195 | RESULT = (X) == (Y) ? 0 : strcasecmp ((X), (Y)); \
|
---|
196 | } while (0)
|
---|
197 | #define return_ISTRING_COMPARE(X, Y) do { \
|
---|
198 | return (X) == (Y) ? 0 : strcasecmp ((X), (Y)); \
|
---|
199 | } while (0)
|
---|
200 |
|
---|
201 | #else
|
---|
202 |
|
---|
203 | #define ISTRING_HASH_1(KEY, RESULT) STRING_HASH_1 ((KEY), (RESULT))
|
---|
204 | #define return_ISTRING_HASH_1(KEY) return_STRING_HASH_1 (KEY)
|
---|
205 |
|
---|
206 | #define ISTRING_HASH_2(KEY, RESULT) STRING_HASH_2 ((KEY), (RESULT))
|
---|
207 | #define return_ISTRING_HASH_2(KEY) return_STRING_HASH_2 (KEY)
|
---|
208 |
|
---|
209 | #define ISTRING_COMPARE(X, Y, RESULT) STRING_COMPARE ((X), (Y), (RESULT))
|
---|
210 | #define return_ISTRING_COMPARE(X, Y) return_STRING_COMPARE ((X), (Y))
|
---|
211 |
|
---|
212 | #endif
|
---|
213 |
|
---|
214 | /* hash and comparison macros for integer _key_s. */
|
---|
215 |
|
---|
216 | #define INTEGER_HASH_1(KEY, RESULT) do { \
|
---|
217 | (RESULT) += ((unsigned long)(KEY)); \
|
---|
218 | } while (0)
|
---|
219 | #define return_INTEGER_HASH_1(KEY) do { \
|
---|
220 | unsigned long _result_ = 0; \
|
---|
221 | INTEGER_HASH_1 ((KEY), _result_); \
|
---|
222 | return _result_; \
|
---|
223 | } while (0)
|
---|
224 |
|
---|
225 | #define INTEGER_HASH_2(KEY, RESULT) do { \
|
---|
226 | (RESULT) += ~((unsigned long)(KEY)); \
|
---|
227 | } while (0)
|
---|
228 | #define return_INTEGER_HASH_2(KEY) do { \
|
---|
229 | unsigned long _result_ = 0; \
|
---|
230 | INTEGER_HASH_2 ((KEY), _result_); \
|
---|
231 | return _result_; \
|
---|
232 | } while (0)
|
---|
233 |
|
---|
234 | #define INTEGER_COMPARE(X, Y, RESULT) do { \
|
---|
235 | (RESULT) = X - Y; \
|
---|
236 | } while (0)
|
---|
237 | #define return_INTEGER_COMPARE(X, Y) do { \
|
---|
238 | int _result_; \
|
---|
239 | INTEGER_COMPARE (X, Y, _result_); \
|
---|
240 | return _result_; \
|
---|
241 | } while (0)
|
---|
242 |
|
---|
243 | /* hash and comparison macros for address keys. */
|
---|
244 |
|
---|
245 | #define ADDRESS_HASH_1(KEY, RESULT) INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3, (RESULT))
|
---|
246 | #define ADDRESS_HASH_2(KEY, RESULT) INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3, (RESULT))
|
---|
247 | #define ADDRESS_COMPARE(X, Y, RESULT) INTEGER_COMPARE ((X), (Y), (RESULT))
|
---|
248 | #define return_ADDRESS_HASH_1(KEY) return_INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3)
|
---|
249 | #define return_ADDRESS_HASH_2(KEY) return_INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3)
|
---|
250 | #define return_ADDRESS_COMPARE(X, Y) return_INTEGER_COMPARE ((X), (Y))
|
---|
251 |
|
---|
252 | #endif /* not _hash_h_ */
|
---|