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>
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)
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #define strcmpi stricmp
29 #if defined __cplusplus || (defined __STDC__ && __STDC__) || defined WINDOWS32
30 # if !defined __GLIBC__ || !defined __P
32 # define __P(protos) protos
34 #else /* Not C++ or ANSI C. */
36 # define __P(protos) ()
37 /* We can get away without defining `const' here only because in this file
38 it is used only inside the prototype for `fnmatch', which is elided in
39 non-ANSI C where `const' is problematical. */
40 #endif /* C++ or ANSI C. */
42 typedef unsigned long (*hash_func_t
) __P((void const *key
));
43 typedef int (*hash_cmp_func_t
) __P((void const *x
, void const *y
));
44 typedef void (*hash_map_func_t
) __P((void const *item
));
45 typedef void (*hash_map_arg_func_t
) __P((void const *item
, void *arg
));
50 unsigned long ht_size
; /* total number of slots (power of 2) */
51 unsigned long ht_capacity
; /* usable slots, limited by loading-factor */
52 unsigned long ht_fill
; /* items in table */
53 unsigned long ht_empty_slots
; /* empty slots not including deleted slots */
54 unsigned long ht_collisions
; /* # of failed calls to comparison function */
55 unsigned long ht_lookups
; /* # of queries */
56 unsigned int ht_rehashes
; /* # of times we've expanded table */
57 hash_func_t ht_hash_1
; /* primary hash function */
58 hash_func_t ht_hash_2
; /* secondary hash function */
59 hash_cmp_func_t ht_compare
; /* comparison function */
62 typedef int (*qsort_cmp_t
) __P((void const *, void const *));
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
, void *item
));
71 void *hash_insert_at
__P((struct hash_table
*ht
, 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 void hash_map
__P((struct hash_table
*ht
, hash_map_func_t map
));
78 void hash_map_arg
__P((struct hash_table
*ht
, hash_map_arg_func_t map
, void *arg
));
79 void hash_print_stats
__P((struct hash_table
*ht
, FILE *out_FILE
));
80 void **hash_dump
__P((struct hash_table
*ht
, void **vector_0
, qsort_cmp_t compare
));
82 extern void *hash_deleted_item
;
83 #define HASH_VACANT(item) ((item) == 0 || (void *) (item) == hash_deleted_item)
86 /* hash and comparison macros for case-sensitive string keys. */
88 #define STRING_HASH_1(KEY, RESULT) do { \
89 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
91 (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
93 #define return_STRING_HASH_1(KEY) do { \
94 unsigned long _result_ = 0; \
95 STRING_HASH_1 ((KEY), _result_); \
99 #define STRING_HASH_2(KEY, RESULT) do { \
100 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
102 (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
104 #define return_STRING_HASH_2(KEY) do { \
105 unsigned long _result_ = 0; \
106 STRING_HASH_2 ((KEY), _result_); \
110 #define STRING_COMPARE(X, Y, RESULT) do { \
111 RESULT = strcmp ((X), (Y)); \
113 #define return_STRING_COMPARE(X, Y) do { \
114 return strcmp ((X), (Y)); \
118 #define STRING_N_HASH_1(KEY, N, RESULT) do { \
119 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
122 while (--_n_ && *++_key_) \
123 (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
124 (RESULT) += *++_key_; \
126 #define return_STRING_N_HASH_1(KEY, N) do { \
127 unsigned long _result_ = 0; \
128 STRING_N_HASH_1 ((KEY), (N), _result_); \
132 #define STRING_N_HASH_2(KEY, N, RESULT) do { \
133 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
136 while (--_n_ && *++_key_) \
137 (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
138 (RESULT) += *++_key_; \
140 #define return_STRING_N_HASH_2(KEY, N) do { \
141 unsigned long _result_ = 0; \
142 STRING_N_HASH_2 ((KEY), (N), _result_); \
146 #define STRING_N_COMPARE(X, Y, N, RESULT) do { \
147 RESULT = strncmp ((X), (Y), (N)); \
149 #define return_STRING_N_COMPARE(X, Y, N) do { \
150 return strncmp ((X), (Y), (N)); \
153 #ifdef HAVE_CASE_INSENSITIVE_FS
155 /* hash and comparison macros for case-insensitive string _key_s. */
157 #define ISTRING_HASH_1(KEY, RESULT) do { \
158 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
160 (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0xf)); \
162 #define return_ISTRING_HASH_1(KEY) do { \
163 unsigned long _result_ = 0; \
164 ISTRING_HASH_1 ((KEY), _result_); \
168 #define ISTRING_HASH_2(KEY, RESULT) do { \
169 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
171 (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0x7)); \
173 #define return_ISTRING_HASH_2(KEY) do { \
174 unsigned long _result_ = 0; \
175 ISTRING_HASH_2 ((KEY), _result_); \
179 #define ISTRING_COMPARE(X, Y, RESULT) do { \
180 RESULT = strcmpi ((X), (Y)); \
182 #define return_ISTRING_COMPARE(X, Y) do { \
183 return strcmpi ((X), (Y)); \
188 #define ISTRING_HASH_1(KEY, RESULT) STRING_HASH_1 ((KEY), (RESULT))
189 #define return_ISTRING_HASH_1(KEY) return_STRING_HASH_1 (KEY)
191 #define ISTRING_HASH_2(KEY, RESULT) STRING_HASH_2 ((KEY), (RESULT))
192 #define return_ISTRING_HASH_2(KEY) return_STRING_HASH_2 (KEY)
194 #define ISTRING_COMPARE(X, Y, RESULT) STRING_COMPARE ((X), (Y), (RESULT))
195 #define return_ISTRING_COMPARE(X, Y) return_STRING_COMPARE ((X), (Y))
199 /* hash and comparison macros for integer _key_s. */
201 #define INTEGER_HASH_1(KEY, RESULT) do { \
202 (RESULT) += ((unsigned long)(KEY)); \
204 #define return_INTEGER_HASH_1(KEY) do { \
205 unsigned long _result_ = 0; \
206 INTEGER_HASH_1 ((KEY), _result_); \
210 #define INTEGER_HASH_2(KEY, RESULT) do { \
211 (RESULT) += ~((unsigned long)(KEY)); \
213 #define return_INTEGER_HASH_2(KEY) do { \
214 unsigned long _result_ = 0; \
215 INTEGER_HASH_2 ((KEY), _result_); \
219 #define INTEGER_COMPARE(X, Y, RESULT) do { \
222 #define return_INTEGER_COMPARE(X, Y) do { \
224 INTEGER_COMPARE (X, Y, _result_); \
228 /* hash and comparison macros for address keys. */
230 #define ADDRESS_HASH_1(KEY, RESULT) INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3, (RESULT))
231 #define ADDRESS_HASH_2(KEY, RESULT) INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3, (RESULT))
232 #define ADDRESS_COMPARE(X, Y, RESULT) INTEGER_COMPARE ((X), (Y), (RESULT))
233 #define return_ADDRESS_HASH_1(KEY) return_INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3)
234 #define return_ADDRESS_HASH_2(KEY) return_INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3)
235 #define return_ADDRESS_COMPARE(X, Y) return_INTEGER_COMPARE ((X), (Y))
237 #endif /* not _hash_h_ */