| 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 | #ifndef CONFIG_WITH_OPTIMIZATION_HACKS
|
|---|
| 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 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 92 | # define STRING_HASH_1(KEY, RESULT) do { \
|
|---|
| 93 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
|---|
| 94 | unsigned int _ch_ = *_key_; \
|
|---|
| 95 | while (_ch_) \
|
|---|
| 96 | { \
|
|---|
| 97 | unsigned char _ch2_ = *++_key_; \
|
|---|
| 98 | (RESULT) += (_ch_ << (_ch2_ & 0xf)); \
|
|---|
| 99 | _ch_ = _ch2_; \
|
|---|
| 100 | } \
|
|---|
| 101 | } while (0)
|
|---|
| 102 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 103 | #define return_STRING_HASH_1(KEY) do { \
|
|---|
| 104 | unsigned long _result_ = 0; \
|
|---|
| 105 | STRING_HASH_1 ((KEY), _result_); \
|
|---|
| 106 | return _result_; \
|
|---|
| 107 | } while (0)
|
|---|
| 108 |
|
|---|
| 109 | #ifndef CONFIG_WITH_OPTIMIZATION_HACKS
|
|---|
| 110 | #define STRING_HASH_2(KEY, RESULT) do { \
|
|---|
| 111 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
|---|
| 112 | while (*++_key_) \
|
|---|
| 113 | (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
|
|---|
| 114 | } while (0)
|
|---|
| 115 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 116 | # define STRING_HASH_2(KEY, RESULT) do { \
|
|---|
| 117 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
|---|
| 118 | unsigned int _ch_ = *_key_; \
|
|---|
| 119 | while (_ch_) \
|
|---|
| 120 | { \
|
|---|
| 121 | unsigned char _ch2_ = *++_key_; \
|
|---|
| 122 | (RESULT) += (_ch_ << (_ch2_ & 0x7)); \
|
|---|
| 123 | _ch_ = _ch2_; \
|
|---|
| 124 | } \
|
|---|
| 125 | } while (0)
|
|---|
| 126 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 127 | #define return_STRING_HASH_2(KEY) do { \
|
|---|
| 128 | unsigned long _result_ = 0; \
|
|---|
| 129 | STRING_HASH_2 ((KEY), _result_); \
|
|---|
| 130 | return _result_; \
|
|---|
| 131 | } while (0)
|
|---|
| 132 |
|
|---|
| 133 | #define STRING_COMPARE(X, Y, RESULT) do { \
|
|---|
| 134 | RESULT = strcmp ((X), (Y)); \
|
|---|
| 135 | } while (0)
|
|---|
| 136 | #define return_STRING_COMPARE(X, Y) do { \
|
|---|
| 137 | return strcmp ((X), (Y)); \
|
|---|
| 138 | } while (0)
|
|---|
| 139 |
|
|---|
| 140 | #ifndef CONFIG_WITH_OPTIMIZATION_HACKS
|
|---|
| 141 | #define STRING_N_HASH_1(KEY, N, RESULT) do { \
|
|---|
| 142 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
|---|
| 143 | int _n_ = (N); \
|
|---|
| 144 | if (_n_) \
|
|---|
| 145 | while (--_n_ && *++_key_) \
|
|---|
| 146 | (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
|
|---|
| 147 | (RESULT) += *++_key_; \
|
|---|
| 148 | } while (0)
|
|---|
| 149 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 150 | # define STRING_N_HASH_1(KEY, N, RESULT) do { \
|
|---|
| 151 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
|---|
| 152 | unsigned int _ch_ = *_key_; \
|
|---|
| 153 | int _n_ = (N); \
|
|---|
| 154 | if (_n_) \
|
|---|
| 155 | { \
|
|---|
| 156 | for (;;) \
|
|---|
| 157 | { \
|
|---|
| 158 | unsigned char _ch2_; \
|
|---|
| 159 | if (!--_n_) \
|
|---|
| 160 | { \
|
|---|
| 161 | (RESULT) += _ch_; \
|
|---|
| 162 | break; \
|
|---|
| 163 | } \
|
|---|
| 164 | _ch2_ = *++_key_; \
|
|---|
| 165 | (RESULT) += (_ch_ << (_ch2_ & 0xf)); \
|
|---|
| 166 | _ch_ = _ch2_; \
|
|---|
| 167 | if (!_ch_) break; \
|
|---|
| 168 | } \
|
|---|
| 169 | } \
|
|---|
| 170 | else \
|
|---|
| 171 | (RESULT) += _ch_; \
|
|---|
| 172 | } while (0)
|
|---|
| 173 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 174 | #define return_STRING_N_HASH_1(KEY, N) do { \
|
|---|
| 175 | unsigned long _result_ = 0; \
|
|---|
| 176 | STRING_N_HASH_1 ((KEY), (N), _result_); \
|
|---|
| 177 | return _result_; \
|
|---|
| 178 | } while (0)
|
|---|
| 179 |
|
|---|
| 180 | #ifndef CONFIG_WITH_OPTIMIZATION_HACKS
|
|---|
| 181 | #define STRING_N_HASH_2(KEY, N, RESULT) do { \
|
|---|
| 182 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
|---|
| 183 | int _n_ = (N); \
|
|---|
| 184 | if (_n_) \
|
|---|
| 185 | while (--_n_ && *++_key_) \
|
|---|
| 186 | (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
|
|---|
| 187 | (RESULT) += *++_key_; \
|
|---|
| 188 | } while (0)
|
|---|
| 189 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 190 | # define STRING_N_HASH_2(KEY, N, RESULT) do { \
|
|---|
| 191 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
|---|
| 192 | unsigned int _ch_ = *_key_; \
|
|---|
| 193 | int _n_ = (N); \
|
|---|
| 194 | if (_n_) \
|
|---|
| 195 | { \
|
|---|
| 196 | for (;;) \
|
|---|
| 197 | { \
|
|---|
| 198 | unsigned char _ch2_; \
|
|---|
| 199 | if (!--_n_) \
|
|---|
| 200 | { \
|
|---|
| 201 | (RESULT) += _ch_; \
|
|---|
| 202 | break; \
|
|---|
| 203 | } \
|
|---|
| 204 | _ch2_ = *++_key_; \
|
|---|
| 205 | (RESULT) += (_ch_ << (_ch2_ & 0x7)); \
|
|---|
| 206 | _ch_ = _ch2_; \
|
|---|
| 207 | if (!_ch_) break; \
|
|---|
| 208 | } \
|
|---|
| 209 | } \
|
|---|
| 210 | else \
|
|---|
| 211 | (RESULT) += _ch_; \
|
|---|
| 212 | } while (0)
|
|---|
| 213 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 214 | #define return_STRING_N_HASH_2(KEY, N) do { \
|
|---|
| 215 | unsigned long _result_ = 0; \
|
|---|
| 216 | STRING_N_HASH_2 ((KEY), (N), _result_); \
|
|---|
| 217 | return _result_; \
|
|---|
| 218 | } while (0)
|
|---|
| 219 |
|
|---|
| 220 | #define STRING_N_COMPARE(X, Y, N, RESULT) do { \
|
|---|
| 221 | RESULT = strncmp ((X), (Y), (N)); \
|
|---|
| 222 | } while (0)
|
|---|
| 223 | #define return_STRING_N_COMPARE(X, Y, N) do { \
|
|---|
| 224 | return strncmp ((X), (Y), (N)); \
|
|---|
| 225 | } while (0)
|
|---|
| 226 |
|
|---|
| 227 | #ifdef HAVE_CASE_INSENSITIVE_FS
|
|---|
| 228 |
|
|---|
| 229 | /* hash and comparison macros for case-insensitive string _key_s. */
|
|---|
| 230 |
|
|---|
| 231 | #if 1 /*ndef CONFIG_WITH_OPTIMIZATION_HACKS - testme */
|
|---|
| 232 | #define ISTRING_HASH_1(KEY, RESULT) do { \
|
|---|
| 233 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
|---|
| 234 | while (*++_key_) \
|
|---|
| 235 | (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0xf)); \
|
|---|
| 236 | } while (0)
|
|---|
| 237 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 238 | #define ISTRING_HASH_1(KEY, RESULT) do { \
|
|---|
| 239 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
|---|
| 240 | unsigned int _ch_ = *_key_;
|
|---|
| 241 | while (_ch_) \
|
|---|
| 242 | { \
|
|---|
| 243 | unsigned _ch2_ = *++_key_; \
|
|---|
| 244 | (RESULT) += ((isupper (_ch_) ? tolower (_ch_) : _ch_) << (_ch2_ & 0xf)); \
|
|---|
| 245 | _ch_ = _ch2_; \
|
|---|
| 246 | } \
|
|---|
| 247 | } while (0)
|
|---|
| 248 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 249 | #define return_ISTRING_HASH_1(KEY) do { \
|
|---|
| 250 | unsigned long _result_ = 0; \
|
|---|
| 251 | ISTRING_HASH_1 ((KEY), _result_); \
|
|---|
| 252 | return _result_; \
|
|---|
| 253 | } while (0)
|
|---|
| 254 |
|
|---|
| 255 | #if 1 /* ndef CONFIG_WITH_OPTIMIZATION_HACKS - testme */
|
|---|
| 256 | #define ISTRING_HASH_2(KEY, RESULT) do { \
|
|---|
| 257 | unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
|
|---|
| 258 | while (*++_key_) \
|
|---|
| 259 | (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0x7)); \
|
|---|
| 260 | } while (0)
|
|---|
| 261 | #else /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 262 | #define ISTRING_HASH_2(KEY, RESULT) do { \
|
|---|
| 263 | unsigned char const *_key_ = (unsigned char const *) (KEY); \
|
|---|
| 264 | unsigned int _ch_ = *_key_;
|
|---|
| 265 | while (_ch_) \
|
|---|
| 266 | { \
|
|---|
| 267 | unsigned _ch2_ = *++_key_; \
|
|---|
| 268 | (RESULT) += ((isupper (_ch_) ? tolower (_ch_) : _ch_) << (_ch2_ & 0x7)); \
|
|---|
| 269 | _ch_ = _ch2_; \
|
|---|
| 270 | } \
|
|---|
| 271 | } while (0)
|
|---|
| 272 | #endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
|
|---|
| 273 | #define return_ISTRING_HASH_2(KEY) do { \
|
|---|
| 274 | unsigned long _result_ = 0; \
|
|---|
| 275 | ISTRING_HASH_2 ((KEY), _result_); \
|
|---|
| 276 | return _result_; \
|
|---|
| 277 | } while (0)
|
|---|
| 278 |
|
|---|
| 279 | #define ISTRING_COMPARE(X, Y, RESULT) do { \
|
|---|
| 280 | RESULT = strcmpi ((X), (Y)); \
|
|---|
| 281 | } while (0)
|
|---|
| 282 | #define return_ISTRING_COMPARE(X, Y) do { \
|
|---|
| 283 | return strcmpi ((X), (Y)); \
|
|---|
| 284 | } while (0)
|
|---|
| 285 |
|
|---|
| 286 | #else
|
|---|
| 287 |
|
|---|
| 288 | #define ISTRING_HASH_1(KEY, RESULT) STRING_HASH_1 ((KEY), (RESULT))
|
|---|
| 289 | #define return_ISTRING_HASH_1(KEY) return_STRING_HASH_1 (KEY)
|
|---|
| 290 |
|
|---|
| 291 | #define ISTRING_HASH_2(KEY, RESULT) STRING_HASH_2 ((KEY), (RESULT))
|
|---|
| 292 | #define return_ISTRING_HASH_2(KEY) return_STRING_HASH_2 (KEY)
|
|---|
| 293 |
|
|---|
| 294 | #define ISTRING_COMPARE(X, Y, RESULT) STRING_COMPARE ((X), (Y), (RESULT))
|
|---|
| 295 | #define return_ISTRING_COMPARE(X, Y) return_STRING_COMPARE ((X), (Y))
|
|---|
| 296 |
|
|---|
| 297 | #endif
|
|---|
| 298 |
|
|---|
| 299 | /* hash and comparison macros for integer _key_s. */
|
|---|
| 300 |
|
|---|
| 301 | #define INTEGER_HASH_1(KEY, RESULT) do { \
|
|---|
| 302 | (RESULT) += ((unsigned long)(KEY)); \
|
|---|
| 303 | } while (0)
|
|---|
| 304 | #define return_INTEGER_HASH_1(KEY) do { \
|
|---|
| 305 | unsigned long _result_ = 0; \
|
|---|
| 306 | INTEGER_HASH_1 ((KEY), _result_); \
|
|---|
| 307 | return _result_; \
|
|---|
| 308 | } while (0)
|
|---|
| 309 |
|
|---|
| 310 | #define INTEGER_HASH_2(KEY, RESULT) do { \
|
|---|
| 311 | (RESULT) += ~((unsigned long)(KEY)); \
|
|---|
| 312 | } while (0)
|
|---|
| 313 | #define return_INTEGER_HASH_2(KEY) do { \
|
|---|
| 314 | unsigned long _result_ = 0; \
|
|---|
| 315 | INTEGER_HASH_2 ((KEY), _result_); \
|
|---|
| 316 | return _result_; \
|
|---|
| 317 | } while (0)
|
|---|
| 318 |
|
|---|
| 319 | #define INTEGER_COMPARE(X, Y, RESULT) do { \
|
|---|
| 320 | (RESULT) = X - Y; \
|
|---|
| 321 | } while (0)
|
|---|
| 322 | #define return_INTEGER_COMPARE(X, Y) do { \
|
|---|
| 323 | int _result_; \
|
|---|
| 324 | INTEGER_COMPARE (X, Y, _result_); \
|
|---|
| 325 | return _result_; \
|
|---|
| 326 | } while (0)
|
|---|
| 327 |
|
|---|
| 328 | /* hash and comparison macros for address keys. */
|
|---|
| 329 |
|
|---|
| 330 | #define ADDRESS_HASH_1(KEY, RESULT) INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3, (RESULT))
|
|---|
| 331 | #define ADDRESS_HASH_2(KEY, RESULT) INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3, (RESULT))
|
|---|
| 332 | #define ADDRESS_COMPARE(X, Y, RESULT) INTEGER_COMPARE ((X), (Y), (RESULT))
|
|---|
| 333 | #define return_ADDRESS_HASH_1(KEY) return_INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3)
|
|---|
| 334 | #define return_ADDRESS_HASH_2(KEY) return_INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3)
|
|---|
| 335 | #define return_ADDRESS_COMPARE(X, Y) return_INTEGER_COMPARE ((X), (Y))
|
|---|
| 336 |
|
|---|
| 337 | #endif /* not _hash_h_ */
|
|---|