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