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