2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of XEmacs.
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Synched up with: Not in FSF. */
27 #define NULL_ENTRY ((void *) 0xdeadbeef)
29 #define COMFORTABLE_SIZE(size) (21 * (size) / 16)
31 #define KEYS_DIFFER_P(old, new, testfun) \
32 (((old) != (new)) && (!(testfun) || !(testfun) ((old),(new))))
34 static void rehash (hentry *harray, struct hash_table *ht, hash_size_t size);
37 memory_hash (CONST void *xv, size_t size)
40 unsigned CONST char *x = (unsigned CONST char *) xv;
48 if ((g = h & 0xf0000000) != 0)
49 h = (h ^ (g >> 24)) ^ g;
55 /* Return a suitable size for a hash table, with at least SIZE slots. */
57 hash_table_size (size_t requested_size)
59 /* Return some prime near, but greater than or equal to, SIZE.
60 Decades from the time of writing, someone will have a system large
61 enough that the list below will be too short... */
62 static CONST size_t primes [] =
64 19, 29, 41, 59, 79, 107, 149, 197, 263, 347, 457, 599, 787, 1031,
65 1361, 1777, 2333, 3037, 3967, 5167, 6719, 8737, 11369, 14783,
66 19219, 24989, 32491, 42257, 54941, 71429, 92861, 120721, 156941,
67 204047, 265271, 344857, 448321, 582821, 757693, 985003, 1280519,
68 1664681, 2164111, 2813353, 3657361, 4754591, 6180989, 8035301,
69 10445899, 13579681, 17653589, 22949669, 29834603, 38784989,
70 50420551, 65546729, 85210757, 110774011, 144006217, 187208107,
71 243370577, 316381771, 411296309, 534685237, 695090819, 903618083,
72 1174703521, 1527114613, 1985248999, 2580823717UL, 3355070839UL
74 /* We've heard of binary search. */
76 for (low = 0, high = countof (primes) - 1; high - low > 1;)
78 /* Loop Invariant: size < primes [high] */
79 int mid = (low + high) / 2;
80 if (primes [mid] < requested_size)
89 gethash (CONST void *key, struct hash_table *hash_table, CONST void **ret_value)
93 *ret_value = hash_table->zero_entry;
94 return (void *) hash_table->zero_set;
98 hentry *harray = hash_table->harray;
99 hash_table_test_function test_function = hash_table->test_function;
100 hash_size_t size = hash_table->size;
101 unsigned int hcode_initial =
102 hash_table->hash_function ?
103 hash_table->hash_function (key) :
105 unsigned int hcode = hcode_initial % size;
106 hentry *e = &harray [hcode];
107 CONST void *e_key = e->key;
110 KEYS_DIFFER_P (e_key, key, test_function) :
111 e->contents == NULL_ENTRY)
113 size_t h2 = size - 2;
114 unsigned int incr = 1 + (hcode_initial % h2);
117 hcode += incr; if (hcode >= size) hcode -= size;
122 KEYS_DIFFER_P (e_key, key, test_function) :
123 e->contents == NULL_ENTRY);
126 *ret_value = e->contents;
132 clrhash (struct hash_table *hash_table)
134 memset (hash_table->harray, 0, sizeof (hentry) * hash_table->size);
135 hash_table->zero_entry = 0;
136 hash_table->zero_set = 0;
137 hash_table->fullness = 0;
141 free_hash_table (struct hash_table *hash_table)
143 xfree (hash_table->harray);
148 make_hash_table (hash_size_t size)
150 struct hash_table *hash_table = xnew_and_zero (struct hash_table);
151 hash_table->size = hash_table_size (COMFORTABLE_SIZE (size));
152 hash_table->harray = xnew_array (hentry, hash_table->size);
153 clrhash (hash_table);
158 make_general_hash_table (hash_size_t size,
159 hash_table_hash_function hash_function,
160 hash_table_test_function test_function)
162 struct hash_table* hash_table = make_hash_table (size);
163 hash_table->hash_function = hash_function;
164 hash_table->test_function = test_function;
169 grow_hash_table (struct hash_table *hash_table, hash_size_t new_size)
171 hash_size_t old_size = hash_table->size;
172 hentry *old_harray = hash_table->harray;
174 hash_table->size = hash_table_size (new_size);
175 hash_table->harray = xnew_array (hentry, hash_table->size);
177 /* do the rehash on the "grown" table */
179 long old_zero_set = hash_table->zero_set;
180 void *old_zero_entry = hash_table->zero_entry;
181 clrhash (hash_table);
182 hash_table->zero_set = old_zero_set;
183 hash_table->zero_entry = old_zero_entry;
184 rehash (old_harray, hash_table, old_size);
191 puthash (CONST void *key, void *contents, struct hash_table *hash_table)
195 hash_table->zero_entry = contents;
196 hash_table->zero_set = 1;
200 hash_table_test_function test_function = hash_table->test_function;
201 hash_size_t size = hash_table->size;
202 hentry *harray = hash_table->harray;
203 unsigned int hcode_initial =
204 hash_table->hash_function ?
205 hash_table->hash_function (key) :
207 unsigned int hcode = hcode_initial % size;
208 size_t h2 = size - 2;
209 unsigned int incr = 1 + (hcode_initial % h2);
210 CONST void *e_key = harray [hcode].key;
211 CONST void *oldcontents;
213 if (e_key && KEYS_DIFFER_P (e_key, key, test_function))
217 hcode += incr; if (hcode >= size) hcode -= size;
218 e_key = harray [hcode].key;
220 while (e_key && KEYS_DIFFER_P (e_key, key, test_function));
222 oldcontents = harray [hcode].contents;
223 harray [hcode].key = key;
224 harray [hcode].contents = contents;
225 /* If the entry that we used was a deleted entry,
226 check for a non deleted entry of the same key,
228 if (!e_key && oldcontents == NULL_ENTRY)
234 hcode += incr; if (hcode >= size) hcode -= size;
239 KEYS_DIFFER_P (e_key, key, test_function):
240 e->contents == NULL_ENTRY);
245 e->contents = NULL_ENTRY;
249 /* only increment the fullness when we used up a new hentry */
250 if (!e_key || KEYS_DIFFER_P (e_key, key, test_function))
252 hash_size_t comfortable_size = COMFORTABLE_SIZE (++(hash_table->fullness));
253 if (hash_table->size < comfortable_size)
254 grow_hash_table (hash_table, comfortable_size + 1);
260 rehash (hentry *harray, struct hash_table *hash_table, hash_size_t size)
262 hentry *limit = harray + size;
264 for (e = harray; e < limit; e++)
267 puthash (e->key, e->contents, hash_table);
272 remhash (CONST void *key, struct hash_table *hash_table)
276 hash_table->zero_entry = 0;
277 hash_table->zero_set = 0;
281 hentry *harray = hash_table->harray;
282 hash_table_test_function test_function = hash_table->test_function;
283 hash_size_t size = hash_table->size;
284 unsigned int hcode_initial =
285 (hash_table->hash_function) ?
286 (hash_table->hash_function (key)) :
287 ((unsigned long) key);
288 unsigned int hcode = hcode_initial % size;
289 hentry *e = &harray [hcode];
290 CONST void *e_key = e->key;
293 KEYS_DIFFER_P (e_key, key, test_function) :
294 e->contents == NULL_ENTRY)
296 size_t h2 = size - 2;
297 unsigned int incr = 1 + (hcode_initial % h2);
300 hcode += incr; if (hcode >= size) hcode -= size;
305 KEYS_DIFFER_P (e_key, key, test_function):
306 e->contents == NULL_ENTRY);
311 e->contents = NULL_ENTRY;
312 /* Note: you can't do fullness-- here, it breaks the world. */
318 maphash (maphash_function mf, struct hash_table *hash_table, void *arg)
323 if (hash_table->zero_set)
325 if (mf (0, hash_table->zero_entry, arg))
329 for (e = hash_table->harray, limit = e + hash_table->size; e < limit; e++)
331 if (e->key && mf (e->key, e->contents, arg))
337 map_remhash (remhash_predicate predicate, struct hash_table *hash_table, void *arg)
342 if (hash_table->zero_set && predicate (0, hash_table->zero_entry, arg))
344 hash_table->zero_set = 0;
345 hash_table->zero_entry = 0;
348 for (e = hash_table->harray, limit = e + hash_table->size; e < limit; e++)
349 if (predicate (e->key, e->contents, arg))
352 e->contents = NULL_ENTRY;