0
|
1 /* Hash tables.
|
|
2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
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
|
|
9 later version.
|
|
10
|
|
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
|
|
14 for more details.
|
|
15
|
|
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. */
|
|
20
|
|
21 /* Synched up with: Not in FSF. */
|
|
22
|
|
23 #ifdef emacs
|
|
24 #include <config.h>
|
|
25 #include "lisp.h"
|
|
26
|
|
27 #define NULL_ENTRY (LISP_TO_VOID (Qnil))
|
|
28
|
|
29 #else /* !emacs */
|
|
30
|
|
31 #define NULL_ENTRY ((void *) 1)
|
|
32
|
|
33 #endif /* !emacs */
|
|
34
|
|
35 #include "hash.h"
|
|
36 #include "elhash.h"
|
|
37
|
173
|
38 static CONST int
|
0
|
39 primes []={
|
|
40 13,
|
173
|
41 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631,
|
|
42 761, 919, 1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013,
|
|
43 8419, 10103, 12143, 14591, 17519, 21023, 25229, 30293, 36353, 43627, 52361,
|
|
44 62851, 75431, 90523, 108631, 130363, 156437, 187751, 225307, 270371, 324449,
|
0
|
45 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, 1674319,
|
|
46 2009191, 2411033, 2893249
|
|
47 };
|
|
48
|
|
49 /* strings code */
|
|
50
|
|
51 /* from base/generic-hash.cc, and hence from Dragon book, p436 */
|
|
52 unsigned long
|
|
53 string_hash (CONST void *xv)
|
173
|
54 {
|
0
|
55 unsigned int h = 0;
|
|
56 unsigned int g;
|
|
57 unsigned CONST char *x = (unsigned CONST char *) xv;
|
|
58
|
|
59 if (!x) return 0;
|
|
60
|
|
61 while (*x != 0)
|
|
62 {
|
|
63 h = (h << 4) + *x++;
|
|
64 if ((g = h & 0xf0000000) != 0)
|
|
65 h = (h ^ (g >> 24)) ^ g;
|
|
66 }
|
|
67
|
|
68 return h;
|
|
69 }
|
|
70
|
|
71 unsigned long
|
|
72 memory_hash (CONST void *xv, int size)
|
173
|
73 {
|
0
|
74 unsigned int h = 0;
|
|
75 unsigned int g;
|
|
76 unsigned CONST char *x = (unsigned CONST char *) xv;
|
|
77
|
|
78 if (!x) return 0;
|
|
79
|
|
80 while (size > 0)
|
|
81 {
|
|
82 h = (h << 4) + *x++;
|
|
83 if ((g = h & 0xf0000000) != 0)
|
|
84 h = (h ^ (g >> 24)) ^ g;
|
|
85 size--;
|
|
86 }
|
|
87
|
|
88 return h;
|
|
89 }
|
|
90
|
173
|
91 static int
|
185
|
92 string_eq (CONST void *st1, CONST void *st2)
|
0
|
93 {
|
|
94 if (!st1)
|
185
|
95 return st2 ? 0 : 1;
|
0
|
96 else if (!st2)
|
|
97 return 0;
|
|
98 else
|
185
|
99 return !strcmp ( (CONST char *) st1, (CONST char *) st2);
|
0
|
100 }
|
|
101
|
|
102
|
173
|
103 static unsigned int
|
0
|
104 prime_size (unsigned int size)
|
|
105 {
|
|
106 unsigned int i;
|
|
107 CONST int lim = countof (primes);
|
|
108 for (i = 0; i < lim; i++)
|
|
109 if (size <= primes [i]) return primes [i];
|
|
110 return primes [lim - 1];
|
|
111 }
|
|
112
|
|
113 static void rehash (hentry *harray, c_hashtable ht, unsigned int size);
|
|
114
|
|
115 #define KEYS_DIFFER_P(old, new, testfun) \
|
|
116 ((testfun)?(((old) == (new))?0:(!(testfun ((old), new)))):((old) != (new)))
|
|
117
|
|
118 CONST void *
|
|
119 gethash (CONST void *key, c_hashtable hash, CONST void **ret_value)
|
|
120 {
|
|
121 hentry *harray = hash->harray;
|
|
122 int (*test_function) (CONST void *, CONST void *) = hash->test_function;
|
|
123 unsigned int hsize = hash->size;
|
173
|
124 unsigned int hcode_initial =
|
0
|
125 (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
|
|
126 unsigned int hcode = hcode_initial % hsize;
|
|
127 hentry *e = &harray [hcode];
|
|
128 CONST void *e_key = e->key;
|
|
129
|
173
|
130 if (!key)
|
0
|
131 {
|
|
132 *ret_value = hash->zero_entry;
|
|
133 return (void *) hash->zero_set;
|
|
134 }
|
173
|
135
|
0
|
136 if ((e_key)?
|
|
137 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
138 (e->contents == NULL_ENTRY))
|
|
139 {
|
|
140 unsigned int h2 = hsize - 2;
|
|
141 unsigned int incr = 1 + (hcode_initial % h2);
|
|
142 do
|
|
143 {
|
|
144 hcode = hcode + incr;
|
|
145 if (hcode >= hsize) hcode = hcode - hsize;
|
|
146 e = &harray [hcode];
|
|
147 e_key = e->key;
|
173
|
148 }
|
0
|
149 while ((e_key)?
|
|
150 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
151 (e->contents == NULL_ENTRY));
|
|
152 }
|
|
153
|
|
154 *ret_value = e->contents;
|
|
155 return e->key;
|
|
156 }
|
|
157
|
173
|
158 void
|
0
|
159 clrhash (c_hashtable hash)
|
|
160 {
|
|
161 memset (hash->harray, 0, sizeof (hentry) * hash->size);
|
|
162 hash->zero_entry = 0;
|
|
163 hash->zero_set = 0;
|
|
164 hash->fullness = 0;
|
|
165 }
|
|
166
|
|
167 void
|
|
168 free_hashtable (c_hashtable hash)
|
|
169 {
|
|
170 #ifdef emacs
|
|
171 if (!NILP (hash->elisp_table))
|
|
172 return;
|
|
173 #endif
|
|
174 xfree (hash->harray);
|
|
175 xfree (hash);
|
|
176 }
|
|
177
|
|
178 c_hashtable
|
|
179 make_hashtable (unsigned int hsize)
|
|
180 {
|
185
|
181 c_hashtable res = xnew_and_zero (struct _C_hashtable);
|
0
|
182 res->size = prime_size ((13 * hsize) / 10);
|
185
|
183 res->harray = xnew_array (hentry, res->size);
|
0
|
184 #ifdef emacs
|
|
185 res->elisp_table = Qnil;
|
|
186 #endif
|
|
187 clrhash (res);
|
|
188 return res;
|
|
189 }
|
|
190
|
|
191 c_hashtable
|
|
192 make_general_hashtable (unsigned int hsize,
|
|
193 unsigned long (*hash_function) (CONST void *),
|
|
194 int (*test_function) (CONST void *, CONST void *))
|
|
195 {
|
185
|
196 c_hashtable res = xnew_and_zero (struct _C_hashtable);
|
0
|
197 res->size = prime_size ((13 * hsize) / 10);
|
185
|
198 res->harray = xnew_array (hentry, res->size);
|
0
|
199 res->hash_function = hash_function;
|
|
200 res->test_function = test_function;
|
|
201 #ifdef emacs
|
|
202 res->elisp_table = Qnil;
|
|
203 #endif
|
|
204 clrhash (res);
|
|
205 return res;
|
|
206 }
|
|
207
|
173
|
208 c_hashtable
|
0
|
209 make_strings_hashtable (unsigned int hsize)
|
|
210 {
|
|
211 return make_general_hashtable (hsize, string_hash, string_eq);
|
|
212 }
|
|
213
|
|
214 #ifdef emacs
|
|
215 unsigned int
|
|
216 compute_harray_size (unsigned int hsize)
|
|
217 {
|
|
218 return prime_size ((13 * hsize) / 10);
|
|
219 }
|
|
220 #endif
|
|
221
|
|
222 void
|
|
223 copy_hash (c_hashtable dest, c_hashtable src)
|
|
224 {
|
|
225 #ifdef emacs
|
|
226 /* if these are not the same, then we are losing here */
|
|
227 if ((NILP (dest->elisp_table)) != (NILP (src->elisp_table)))
|
|
228 {
|
|
229 error ("Incompatible hashtable types to copy_hash.");
|
|
230 return;
|
|
231 }
|
|
232 #endif
|
|
233
|
|
234 if (dest->size != src->size)
|
|
235 {
|
|
236 #ifdef emacs
|
|
237 if (!NILP (dest->elisp_table))
|
|
238 elisp_hvector_free (dest->harray, dest->elisp_table);
|
|
239 else
|
|
240 #endif
|
|
241 xfree (dest->harray);
|
|
242
|
|
243 dest->size = src->size;
|
|
244 #ifdef emacs
|
|
245 if (!NILP (dest->elisp_table))
|
173
|
246 dest->harray = (hentry *)
|
|
247 elisp_hvector_malloc (sizeof (hentry) * dest->size,
|
|
248 dest->elisp_table);
|
0
|
249 else
|
|
250 #endif
|
185
|
251 dest->harray = xnew_array (hentry, dest->size);
|
0
|
252 }
|
|
253 dest->fullness = src->fullness;
|
|
254 dest->zero_entry = src->zero_entry;
|
|
255 dest->zero_set = src->zero_set;
|
|
256 dest->hash_function = src->hash_function;
|
|
257 dest->test_function = src->test_function;
|
|
258 memcpy (dest->harray, src->harray, sizeof (hentry) * dest->size);
|
|
259 }
|
173
|
260
|
0
|
261 static void
|
|
262 grow_hashtable (c_hashtable hash, unsigned int new_size)
|
|
263 {
|
|
264 unsigned int old_hsize = hash->size;
|
|
265 hentry *old_harray = hash->harray;
|
|
266 unsigned int new_hsize = prime_size (new_size);
|
|
267 hentry *new_harray;
|
|
268
|
|
269 #ifdef emacs
|
|
270 /* We test for Qzero to facilitate free-hook.c. That module creates
|
|
271 a hashtable very very early, at which point Qnil has not yet
|
|
272 been set and is thus all zeroes. Qzero is "automatically"
|
|
273 initialized at startup because its correct value is also all
|
|
274 zeroes. */
|
|
275 if (!NILP (hash->elisp_table) && !ZEROP (hash->elisp_table))
|
|
276 new_harray = (hentry *) elisp_hvector_malloc (sizeof (hentry) * new_hsize,
|
|
277 hash->elisp_table);
|
|
278 else
|
|
279 #endif
|
173
|
280 new_harray = (hentry *) xmalloc (sizeof (hentry) * new_hsize);
|
0
|
281
|
|
282 hash->size = new_hsize;
|
|
283 hash->harray = new_harray;
|
|
284
|
|
285 /* do the rehash on the "grown" table */
|
|
286 {
|
|
287 long old_zero_set = hash->zero_set;
|
|
288 void *old_zero_entry = hash->zero_entry;
|
|
289 clrhash (hash);
|
|
290 hash->zero_set = old_zero_set;
|
|
291 hash->zero_entry = old_zero_entry;
|
|
292 rehash (old_harray, hash, old_hsize);
|
|
293 }
|
|
294
|
|
295 #ifdef emacs
|
|
296 if (!NILP (hash->elisp_table) && !ZEROP (hash->elisp_table))
|
|
297 elisp_hvector_free (old_harray, hash->elisp_table);
|
|
298 else
|
|
299 #endif
|
|
300 xfree (old_harray);
|
|
301 }
|
|
302
|
|
303 void
|
|
304 expand_hashtable (c_hashtable hash, unsigned int needed_size)
|
|
305 {
|
|
306 unsigned int hsize = hash->size;
|
|
307 int comfortable_size = (13 * needed_size) / 10;
|
|
308 if (hsize < comfortable_size)
|
|
309 grow_hashtable (hash, comfortable_size + 1);
|
|
310 }
|
|
311
|
173
|
312 void
|
0
|
313 puthash (CONST void *key, void *cont, c_hashtable hash)
|
|
314 {
|
|
315 unsigned int hsize = hash->size;
|
|
316 int (*test_function) (CONST void *, CONST void *) = hash->test_function;
|
|
317 unsigned int fullness = hash->fullness;
|
|
318 hentry *harray;
|
|
319 CONST void *e_key;
|
|
320 hentry *e;
|
173
|
321 unsigned int hcode_initial =
|
0
|
322 (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
|
|
323 unsigned int hcode;
|
|
324 unsigned int incr = 0;
|
|
325 unsigned int h2;
|
|
326 CONST void *oldcontents;
|
|
327
|
173
|
328 if (!key)
|
0
|
329 {
|
173
|
330 hash->zero_entry = cont;
|
0
|
331 hash->zero_set = 1;
|
|
332 return;
|
|
333 }
|
|
334
|
|
335 if (hsize < (1 + ((13 * fullness) / 10)))
|
|
336 {
|
|
337 grow_hashtable (hash, hsize + 1);
|
|
338 hsize = hash->size;
|
|
339 fullness = hash->fullness;
|
|
340 }
|
|
341
|
|
342 harray= hash->harray;
|
|
343 h2 = hsize - 2;
|
|
344
|
|
345 hcode = hcode_initial % hsize;
|
173
|
346
|
0
|
347 e_key = harray [hcode].key;
|
|
348 if (e_key && (KEYS_DIFFER_P (e_key, key, test_function)))
|
|
349 {
|
|
350 h2 = hsize - 2;
|
|
351 incr = 1 + (hcode_initial % h2);
|
|
352 do
|
|
353 {
|
|
354 hcode = hcode + incr;
|
|
355 if (hcode >= hsize) hcode = hcode - hsize;
|
|
356 e_key = harray [hcode].key;
|
173
|
357 }
|
0
|
358 while (e_key && (KEYS_DIFFER_P (e_key, key, test_function)));
|
|
359 }
|
|
360 oldcontents = harray [hcode].contents;
|
|
361 harray [hcode].key = key;
|
|
362 harray [hcode].contents = cont;
|
|
363 /* if the entry that we used was a deleted entry,
|
|
364 check for a non deleted entry of the same key,
|
|
365 then delete it */
|
|
366 if (!e_key && (oldcontents == NULL_ENTRY))
|
|
367 {
|
|
368 if (!incr) incr = 1 + ((unsigned long) key % h2);
|
|
369
|
|
370 do
|
|
371 {
|
|
372 hcode = hcode + incr;
|
|
373 if (hcode >= hsize) hcode = hcode - hsize;
|
|
374 e = &harray [hcode];
|
|
375 e_key = e->key;
|
|
376 }
|
|
377 while ((e_key)?
|
|
378 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
379 (e->contents == NULL_ENTRY));
|
|
380
|
|
381 if (e_key)
|
|
382 {
|
|
383 e->key = 0;
|
|
384 e->contents = NULL_ENTRY;
|
|
385 }
|
|
386 }
|
|
387
|
|
388 /* only increment the fullness when we used up a new hentry */
|
|
389 if (!e_key || (KEYS_DIFFER_P (e_key, key, test_function)))
|
|
390 hash->fullness++;
|
|
391 }
|
|
392
|
|
393 static void
|
|
394 rehash (hentry *harray, c_hashtable hash, unsigned int size)
|
|
395 {
|
|
396 hentry *limit = harray + size;
|
|
397 hentry *e;
|
|
398 for (e = harray; e < limit; e++)
|
|
399 {
|
|
400 if (e->key)
|
|
401 puthash (e->key, e->contents, hash);
|
|
402 }
|
|
403 }
|
|
404
|
173
|
405 void
|
0
|
406 remhash (CONST void *key, c_hashtable hash)
|
|
407 {
|
|
408 hentry *harray = hash->harray;
|
|
409 int (*test_function) (CONST void*, CONST void*) = hash->test_function;
|
|
410 unsigned int hsize = hash->size;
|
173
|
411 unsigned int hcode_initial =
|
0
|
412 (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
|
|
413 unsigned int hcode = hcode_initial % hsize;
|
|
414 hentry *e = &harray [hcode];
|
|
415 CONST void *e_key = e->key;
|
|
416
|
173
|
417 if (!key)
|
0
|
418 {
|
|
419 hash->zero_entry = 0;
|
|
420 hash->zero_set = 0;
|
|
421 return;
|
|
422 }
|
|
423
|
|
424 if ((e_key)?
|
|
425 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
426 (e->contents == NULL_ENTRY))
|
|
427 {
|
|
428 unsigned int h2 = hsize - 2;
|
|
429 unsigned int incr = 1 + (hcode_initial % h2);
|
|
430 do
|
|
431 {
|
|
432 hcode = hcode + incr;
|
|
433 if (hcode >= hsize) hcode = hcode - hsize;
|
|
434 e = &harray [hcode];
|
|
435 e_key = e->key;
|
|
436 }
|
|
437 while ((e_key)?
|
|
438 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
439 (e->contents == NULL_ENTRY));
|
|
440 }
|
|
441 if (e_key)
|
|
442 {
|
|
443 e->key = 0;
|
|
444 e->contents = NULL_ENTRY;
|
|
445 /* Note: you can't do fullness-- here, it breaks the world. */
|
|
446 }
|
|
447 }
|
|
448
|
173
|
449 void
|
0
|
450 maphash (maphash_function mf, c_hashtable hash, void *arg)
|
|
451 {
|
|
452 hentry *e;
|
|
453 hentry *limit;
|
173
|
454
|
|
455 if (hash->zero_set)
|
0
|
456 ((*mf) (0, hash->zero_entry, arg));
|
|
457
|
|
458 for (e = hash->harray, limit = e + hash->size; e < limit; e++)
|
|
459 {
|
|
460 if (e->key)
|
|
461 ((*mf) (e->key, e->contents, arg));
|
|
462 }
|
|
463 }
|
|
464
|
173
|
465 void
|
0
|
466 map_remhash (remhash_predicate predicate, c_hashtable hash, void *arg)
|
|
467 {
|
|
468 hentry *e;
|
|
469 hentry *limit;
|
173
|
470
|
0
|
471 if (hash->zero_set && ((*predicate) (0, hash->zero_entry, arg)))
|
|
472 {
|
|
473 hash->zero_set = 0;
|
|
474 hash->zero_entry = 0;
|
|
475 }
|
|
476
|
|
477 for (e = hash->harray, limit = e + hash->size; e < limit; e++)
|
|
478 if ((*predicate) (e->key, e->contents, arg))
|
|
479 {
|
|
480 e->key = 0;
|
|
481 e->contents = NULL_ENTRY;
|
|
482 }
|
|
483 }
|