1 | /* hash.h -- decls for hash table
|
---|
2 | Copyright (C) 1995, 1999, 2002, 2010 Free Software Foundation, Inc.
|
---|
3 | Written by Greg McGary <gkm@gnu.org> <greg@mcgary.org>
|
---|
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 | };
|
---|
55 |
|
---|
56 | typedef int (*qsort_cmp_t) __P((void const *, void const *));
|
---|
57 |
|
---|
58 | void hash_init __P((struct hash_table *ht, unsigned long size,
|
---|
59 | hash_func_t hash_1, hash_func_t hash_2, hash_cmp_func_t hash_cmp));
|
---|
60 | void hash_load __P((struct hash_table *ht, void *item_table,
|
---|
61 | unsigned long cardinality, unsigned long size));
|
---|
62 | void **hash_find_slot __P((struct hash_table *ht, void const *key));
|
---|
63 | void *hash_find_item __P((struct hash_table *ht, void const *key));
|
---|
64 | void *hash_insert __P((struct hash_table *ht, const void *item));
|
---|
65 | void *hash_insert_at __P((struct hash_table *ht, const void *item, void const *slot));
|
---|
66 | void *hash_delete __P((struct hash_table *ht, void const *item));
|
---|
67 | void *hash_delete_at __P((struct hash_table *ht, void const *slot));
|
---|
68 | void hash_delete_items __P((struct hash_table *ht));
|
---|
69 | void hash_free_items __P((struct hash_table *ht));
|
---|
70 | void hash_free __P((struct hash_table *ht, int free_items));
|
---|
71 | void hash_map __P((struct hash_table *ht, hash_map_func_t map));
|
---|
72 | void hash_map_arg __P((struct hash_table *ht, hash_map_arg_func_t map, void *arg));
|
---|
73 | void hash_print_stats __P((struct hash_table *ht, FILE *out_FILE));
|
---|
74 | void **hash_dump __P((struct hash_table *ht, void **vector_0, qsort_cmp_t compare));
|
---|
75 |
|
---|
76 | extern void *hash_deleted_item;
|
---|
77 | #define HASH_VACANT(item) ((item) == 0 || (void *) (item) == hash_deleted_item)
|
---|
78 |
|
---|
79 | |
---|
80 |
|
---|
81 | /* hash and comparison macros for case-sensitive string keys. */
|
---|
82 |
|
---|
83 | /* Due to the strcache, it's not uncommon for the string pointers to
|
---|
84 | be identical. Take advantage of that to short-circuit string compares. */
|
---|
85 |
|
---|
86 | #define STRING_HASH_1(KEY, RESULT) do { \
|
---|
87 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
88 | while (*++_key_) \
|
---|
89 | (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
|
---|
90 | } while (0)
|
---|
91 | #define return_STRING_HASH_1(KEY) do { \
|
---|
92 | unsigned long _result_ = 0; \
|
---|
93 | STRING_HASH_1 ((KEY), _result_); \
|
---|
94 | return _result_; \
|
---|
95 | } while (0)
|
---|
96 |
|
---|
97 | #define STRING_HASH_2(KEY, RESULT) do { \
|
---|
98 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
99 | while (*++_key_) \
|
---|
100 | (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
|
---|
101 | } while (0)
|
---|
102 | #define return_STRING_HASH_2(KEY) do { \
|
---|
103 | unsigned long _result_ = 0; \
|
---|
104 | STRING_HASH_2 ((KEY), _result_); \
|
---|
105 | return _result_; \
|
---|
106 | } while (0)
|
---|
107 |
|
---|
108 | #define STRING_COMPARE(X, Y, RESULT) do { \
|
---|
109 | RESULT = (X) == (Y) ? 0 : strcmp ((X), (Y)); \
|
---|
110 | } while (0)
|
---|
111 | #define return_STRING_COMPARE(X, Y) do { \
|
---|
112 | return (X) == (Y) ? 0 : strcmp ((X), (Y)); \
|
---|
113 | } while (0)
|
---|
114 |
|
---|
115 |
|
---|
116 | #define STRING_N_HASH_1(KEY, N, RESULT) do { \
|
---|
117 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
118 | int _n_ = (N); \
|
---|
119 | if (_n_) \
|
---|
120 | while (--_n_ && *++_key_) \
|
---|
121 | (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
|
---|
122 | (RESULT) += *++_key_; \
|
---|
123 | } while (0)
|
---|
124 | #define return_STRING_N_HASH_1(KEY, N) do { \
|
---|
125 | unsigned long _result_ = 0; \
|
---|
126 | STRING_N_HASH_1 ((KEY), (N), _result_); \
|
---|
127 | return _result_; \
|
---|
128 | } while (0)
|
---|
129 |
|
---|
130 | #define STRING_N_HASH_2(KEY, N, RESULT) do { \
|
---|
131 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
132 | int _n_ = (N); \
|
---|
133 | if (_n_) \
|
---|
134 | while (--_n_ && *++_key_) \
|
---|
135 | (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
|
---|
136 | (RESULT) += *++_key_; \
|
---|
137 | } while (0)
|
---|
138 | #define return_STRING_N_HASH_2(KEY, N) do { \
|
---|
139 | unsigned long _result_ = 0; \
|
---|
140 | STRING_N_HASH_2 ((KEY), (N), _result_); \
|
---|
141 | return _result_; \
|
---|
142 | } while (0)
|
---|
143 |
|
---|
144 | #define STRING_N_COMPARE(X, Y, N, RESULT) do { \
|
---|
145 | RESULT = (X) == (Y) ? 0 : strncmp ((X), (Y), (N)); \
|
---|
146 | } while (0)
|
---|
147 | #define return_STRING_N_COMPARE(X, Y, N) do { \
|
---|
148 | return (X) == (Y) ? 0 : strncmp ((X), (Y), (N)); \
|
---|
149 | } while (0)
|
---|
150 |
|
---|
151 | #ifdef HAVE_CASE_INSENSITIVE_FS
|
---|
152 |
|
---|
153 | /* hash and comparison macros for case-insensitive string _key_s. */
|
---|
154 |
|
---|
155 | #define ISTRING_HASH_1(KEY, RESULT) do { \
|
---|
156 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
157 | while (*++_key_) \
|
---|
158 | (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0xf)); \
|
---|
159 | } while (0)
|
---|
160 | #define return_ISTRING_HASH_1(KEY) do { \
|
---|
161 | unsigned long _result_ = 0; \
|
---|
162 | ISTRING_HASH_1 ((KEY), _result_); \
|
---|
163 | return _result_; \
|
---|
164 | } while (0)
|
---|
165 |
|
---|
166 | #define ISTRING_HASH_2(KEY, RESULT) do { \
|
---|
167 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
---|
168 | while (*++_key_) \
|
---|
169 | (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0x7)); \
|
---|
170 | } while (0)
|
---|
171 | #define return_ISTRING_HASH_2(KEY) do { \
|
---|
172 | unsigned long _result_ = 0; \
|
---|
173 | ISTRING_HASH_2 ((KEY), _result_); \
|
---|
174 | return _result_; \
|
---|
175 | } while (0)
|
---|
176 |
|
---|
177 | #define ISTRING_COMPARE(X, Y, RESULT) do { \
|
---|
178 | RESULT = (X) == (Y) ? 0 : strcasecmp ((X), (Y)); \
|
---|
179 | } while (0)
|
---|
180 | #define return_ISTRING_COMPARE(X, Y) do { \
|
---|
181 | return (X) == (Y) ? 0 : strcasecmp ((X), (Y)); \
|
---|
182 | } while (0)
|
---|
183 |
|
---|
184 | #else
|
---|
185 |
|
---|
186 | #define ISTRING_HASH_1(KEY, RESULT) STRING_HASH_1 ((KEY), (RESULT))
|
---|
187 | #define return_ISTRING_HASH_1(KEY) return_STRING_HASH_1 (KEY)
|
---|
188 |
|
---|
189 | #define ISTRING_HASH_2(KEY, RESULT) STRING_HASH_2 ((KEY), (RESULT))
|
---|
190 | #define return_ISTRING_HASH_2(KEY) return_STRING_HASH_2 (KEY)
|
---|
191 |
|
---|
192 | #define ISTRING_COMPARE(X, Y, RESULT) STRING_COMPARE ((X), (Y), (RESULT))
|
---|
193 | #define return_ISTRING_COMPARE(X, Y) return_STRING_COMPARE ((X), (Y))
|
---|
194 |
|
---|
195 | #endif
|
---|
196 |
|
---|
197 | /* hash and comparison macros for integer _key_s. */
|
---|
198 |
|
---|
199 | #define INTEGER_HASH_1(KEY, RESULT) do { \
|
---|
200 | (RESULT) += ((unsigned long)(KEY)); \
|
---|
201 | } while (0)
|
---|
202 | #define return_INTEGER_HASH_1(KEY) do { \
|
---|
203 | unsigned long _result_ = 0; \
|
---|
204 | INTEGER_HASH_1 ((KEY), _result_); \
|
---|
205 | return _result_; \
|
---|
206 | } while (0)
|
---|
207 |
|
---|
208 | #define INTEGER_HASH_2(KEY, RESULT) do { \
|
---|
209 | (RESULT) += ~((unsigned long)(KEY)); \
|
---|
210 | } while (0)
|
---|
211 | #define return_INTEGER_HASH_2(KEY) do { \
|
---|
212 | unsigned long _result_ = 0; \
|
---|
213 | INTEGER_HASH_2 ((KEY), _result_); \
|
---|
214 | return _result_; \
|
---|
215 | } while (0)
|
---|
216 |
|
---|
217 | #define INTEGER_COMPARE(X, Y, RESULT) do { \
|
---|
218 | (RESULT) = X - Y; \
|
---|
219 | } while (0)
|
---|
220 | #define return_INTEGER_COMPARE(X, Y) do { \
|
---|
221 | int _result_; \
|
---|
222 | INTEGER_COMPARE (X, Y, _result_); \
|
---|
223 | return _result_; \
|
---|
224 | } while (0)
|
---|
225 |
|
---|
226 | /* hash and comparison macros for address keys. */
|
---|
227 |
|
---|
228 | #define ADDRESS_HASH_1(KEY, RESULT) INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3, (RESULT))
|
---|
229 | #define ADDRESS_HASH_2(KEY, RESULT) INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3, (RESULT))
|
---|
230 | #define ADDRESS_COMPARE(X, Y, RESULT) INTEGER_COMPARE ((X), (Y), (RESULT))
|
---|
231 | #define return_ADDRESS_HASH_1(KEY) return_INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3)
|
---|
232 | #define return_ADDRESS_HASH_2(KEY) return_INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3)
|
---|
233 | #define return_ADDRESS_COMPARE(X, Y) return_INTEGER_COMPARE ((X), (Y))
|
---|
234 |
|
---|
235 | #endif /* not _hash_h_ */
|
---|