Fix for the problem that SDL applications exited
[AROS-Contrib.git] / gnu / make / hash.h
blob3ccf4fbd062572fa5ce133b041250d0c7f59a3a1
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)
8 any later version.
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. */
19 #ifndef _hash_h_
20 #define _hash_h_
22 #include <stdio.h>
23 #include <ctype.h>
25 #ifdef _AMIGA
26 #define strcmpi stricmp
27 #endif
29 #if defined __cplusplus || (defined __STDC__ && __STDC__) || defined WINDOWS32
30 # if !defined __GLIBC__ || !defined __P
31 # undef __P
32 # define __P(protos) protos
33 # endif
34 #else /* Not C++ or ANSI C. */
35 # undef __P
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));
47 struct hash_table
49 void **ht_vec;
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; \
90 while (*++_key_) \
91 (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
92 } while (0)
93 #define return_STRING_HASH_1(KEY) do { \
94 unsigned long _result_ = 0; \
95 STRING_HASH_1 ((KEY), _result_); \
96 return _result_; \
97 } while (0)
99 #define STRING_HASH_2(KEY, RESULT) do { \
100 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
101 while (*++_key_) \
102 (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
103 } while (0)
104 #define return_STRING_HASH_2(KEY) do { \
105 unsigned long _result_ = 0; \
106 STRING_HASH_2 ((KEY), _result_); \
107 return _result_; \
108 } while (0)
110 #define STRING_COMPARE(X, Y, RESULT) do { \
111 RESULT = strcmp ((X), (Y)); \
112 } while (0)
113 #define return_STRING_COMPARE(X, Y) do { \
114 return strcmp ((X), (Y)); \
115 } while (0)
118 #define STRING_N_HASH_1(KEY, N, RESULT) do { \
119 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
120 int _n_ = (N); \
121 if (_n_) \
122 while (--_n_ && *++_key_) \
123 (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
124 (RESULT) += *++_key_; \
125 } while (0)
126 #define return_STRING_N_HASH_1(KEY, N) do { \
127 unsigned long _result_ = 0; \
128 STRING_N_HASH_1 ((KEY), (N), _result_); \
129 return _result_; \
130 } while (0)
132 #define STRING_N_HASH_2(KEY, N, RESULT) do { \
133 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
134 int _n_ = (N); \
135 if (_n_) \
136 while (--_n_ && *++_key_) \
137 (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
138 (RESULT) += *++_key_; \
139 } while (0)
140 #define return_STRING_N_HASH_2(KEY, N) do { \
141 unsigned long _result_ = 0; \
142 STRING_N_HASH_2 ((KEY), (N), _result_); \
143 return _result_; \
144 } while (0)
146 #define STRING_N_COMPARE(X, Y, N, RESULT) do { \
147 RESULT = strncmp ((X), (Y), (N)); \
148 } while (0)
149 #define return_STRING_N_COMPARE(X, Y, N) do { \
150 return strncmp ((X), (Y), (N)); \
151 } while (0)
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; \
159 while (*++_key_) \
160 (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0xf)); \
161 } while (0)
162 #define return_ISTRING_HASH_1(KEY) do { \
163 unsigned long _result_ = 0; \
164 ISTRING_HASH_1 ((KEY), _result_); \
165 return _result_; \
166 } while (0)
168 #define ISTRING_HASH_2(KEY, RESULT) do { \
169 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
170 while (*++_key_) \
171 (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0x7)); \
172 } while (0)
173 #define return_ISTRING_HASH_2(KEY) do { \
174 unsigned long _result_ = 0; \
175 ISTRING_HASH_2 ((KEY), _result_); \
176 return _result_; \
177 } while (0)
179 #define ISTRING_COMPARE(X, Y, RESULT) do { \
180 RESULT = strcmpi ((X), (Y)); \
181 } while (0)
182 #define return_ISTRING_COMPARE(X, Y) do { \
183 return strcmpi ((X), (Y)); \
184 } while (0)
186 #else
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))
197 #endif
199 /* hash and comparison macros for integer _key_s. */
201 #define INTEGER_HASH_1(KEY, RESULT) do { \
202 (RESULT) += ((unsigned long)(KEY)); \
203 } while (0)
204 #define return_INTEGER_HASH_1(KEY) do { \
205 unsigned long _result_ = 0; \
206 INTEGER_HASH_1 ((KEY), _result_); \
207 return _result_; \
208 } while (0)
210 #define INTEGER_HASH_2(KEY, RESULT) do { \
211 (RESULT) += ~((unsigned long)(KEY)); \
212 } while (0)
213 #define return_INTEGER_HASH_2(KEY) do { \
214 unsigned long _result_ = 0; \
215 INTEGER_HASH_2 ((KEY), _result_); \
216 return _result_; \
217 } while (0)
219 #define INTEGER_COMPARE(X, Y, RESULT) do { \
220 (RESULT) = X - Y; \
221 } while (0)
222 #define return_INTEGER_COMPARE(X, Y) do { \
223 int _result_; \
224 INTEGER_COMPARE (X, Y, _result_); \
225 return _result_; \
226 } while (0)
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_ */