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
|
0
|
92 string_eq (CONST void *st1v, CONST void *st2v)
|
|
93 {
|
|
94 CONST char *st1 = (CONST char *)st1v;
|
|
95 CONST char *st2 = (CONST char *)st2v;
|
|
96
|
|
97 if (!st1)
|
|
98 return (st2)?0:1;
|
|
99 else if (!st2)
|
|
100 return 0;
|
|
101 else
|
|
102 return !strcmp (st1, st2);
|
|
103 }
|
|
104
|
|
105
|
173
|
106 static unsigned int
|
0
|
107 prime_size (unsigned int size)
|
|
108 {
|
|
109 unsigned int i;
|
|
110 CONST int lim = countof (primes);
|
|
111 for (i = 0; i < lim; i++)
|
|
112 if (size <= primes [i]) return primes [i];
|
|
113 return primes [lim - 1];
|
|
114 }
|
|
115
|
|
116 static void rehash (hentry *harray, c_hashtable ht, unsigned int size);
|
|
117
|
|
118 #define KEYS_DIFFER_P(old, new, testfun) \
|
|
119 ((testfun)?(((old) == (new))?0:(!(testfun ((old), new)))):((old) != (new)))
|
|
120
|
|
121 CONST void *
|
|
122 gethash (CONST void *key, c_hashtable hash, CONST void **ret_value)
|
|
123 {
|
|
124 hentry *harray = hash->harray;
|
|
125 int (*test_function) (CONST void *, CONST void *) = hash->test_function;
|
|
126 unsigned int hsize = hash->size;
|
173
|
127 unsigned int hcode_initial =
|
0
|
128 (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
|
|
129 unsigned int hcode = hcode_initial % hsize;
|
|
130 hentry *e = &harray [hcode];
|
|
131 CONST void *e_key = e->key;
|
|
132
|
173
|
133 if (!key)
|
0
|
134 {
|
|
135 *ret_value = hash->zero_entry;
|
|
136 return (void *) hash->zero_set;
|
|
137 }
|
173
|
138
|
0
|
139 if ((e_key)?
|
|
140 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
141 (e->contents == NULL_ENTRY))
|
|
142 {
|
|
143 unsigned int h2 = hsize - 2;
|
|
144 unsigned int incr = 1 + (hcode_initial % h2);
|
|
145 do
|
|
146 {
|
|
147 hcode = hcode + incr;
|
|
148 if (hcode >= hsize) hcode = hcode - hsize;
|
|
149 e = &harray [hcode];
|
|
150 e_key = e->key;
|
173
|
151 }
|
0
|
152 while ((e_key)?
|
|
153 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
154 (e->contents == NULL_ENTRY));
|
|
155 }
|
|
156
|
|
157 *ret_value = e->contents;
|
|
158 return e->key;
|
|
159 }
|
|
160
|
173
|
161 void
|
0
|
162 clrhash (c_hashtable hash)
|
|
163 {
|
|
164 memset (hash->harray, 0, sizeof (hentry) * hash->size);
|
|
165 hash->zero_entry = 0;
|
|
166 hash->zero_set = 0;
|
|
167 hash->fullness = 0;
|
|
168 }
|
|
169
|
|
170 void
|
|
171 free_hashtable (c_hashtable hash)
|
|
172 {
|
|
173 #ifdef emacs
|
|
174 if (!NILP (hash->elisp_table))
|
|
175 return;
|
|
176 #endif
|
|
177 xfree (hash->harray);
|
|
178 xfree (hash);
|
|
179 }
|
|
180
|
|
181 c_hashtable
|
|
182 make_hashtable (unsigned int hsize)
|
|
183 {
|
|
184 c_hashtable res = (c_hashtable) xmalloc (sizeof (struct _C_hashtable));
|
|
185 memset (res, 0, sizeof (struct _C_hashtable));
|
|
186 res->size = prime_size ((13 * hsize) / 10);
|
|
187 res->harray = (hentry *) xmalloc (sizeof (hentry) * res->size);
|
|
188 #ifdef emacs
|
|
189 res->elisp_table = Qnil;
|
|
190 #endif
|
|
191 clrhash (res);
|
|
192 return res;
|
|
193 }
|
|
194
|
|
195 c_hashtable
|
|
196 make_general_hashtable (unsigned int hsize,
|
|
197 unsigned long (*hash_function) (CONST void *),
|
|
198 int (*test_function) (CONST void *, CONST void *))
|
|
199 {
|
|
200 c_hashtable res = (c_hashtable) xmalloc (sizeof (struct _C_hashtable));
|
|
201 memset (res, 0, sizeof (struct _C_hashtable));
|
|
202 res->size = prime_size ((13 * hsize) / 10);
|
|
203 res->harray = (hentry *) xmalloc (sizeof (hentry) * res->size);
|
|
204 res->hash_function = hash_function;
|
|
205 res->test_function = test_function;
|
|
206 #ifdef emacs
|
|
207 res->elisp_table = Qnil;
|
|
208 #endif
|
|
209 clrhash (res);
|
|
210 return res;
|
|
211 }
|
|
212
|
173
|
213 c_hashtable
|
0
|
214 make_strings_hashtable (unsigned int hsize)
|
|
215 {
|
|
216 return make_general_hashtable (hsize, string_hash, string_eq);
|
|
217 }
|
|
218
|
|
219 #ifdef emacs
|
|
220 unsigned int
|
|
221 compute_harray_size (unsigned int hsize)
|
|
222 {
|
|
223 return prime_size ((13 * hsize) / 10);
|
|
224 }
|
|
225 #endif
|
|
226
|
|
227 void
|
|
228 copy_hash (c_hashtable dest, c_hashtable src)
|
|
229 {
|
|
230 #ifdef emacs
|
|
231 /* if these are not the same, then we are losing here */
|
|
232 if ((NILP (dest->elisp_table)) != (NILP (src->elisp_table)))
|
|
233 {
|
|
234 error ("Incompatible hashtable types to copy_hash.");
|
|
235 return;
|
|
236 }
|
|
237 #endif
|
|
238
|
|
239 if (dest->size != src->size)
|
|
240 {
|
|
241 #ifdef emacs
|
|
242 if (!NILP (dest->elisp_table))
|
|
243 elisp_hvector_free (dest->harray, dest->elisp_table);
|
|
244 else
|
|
245 #endif
|
|
246 xfree (dest->harray);
|
|
247
|
|
248 dest->size = src->size;
|
|
249 #ifdef emacs
|
|
250 if (!NILP (dest->elisp_table))
|
173
|
251 dest->harray = (hentry *)
|
|
252 elisp_hvector_malloc (sizeof (hentry) * dest->size,
|
|
253 dest->elisp_table);
|
0
|
254 else
|
|
255 #endif
|
|
256 dest->harray = (hentry *) xmalloc (sizeof (hentry) * dest->size);
|
|
257 }
|
|
258 dest->fullness = src->fullness;
|
|
259 dest->zero_entry = src->zero_entry;
|
|
260 dest->zero_set = src->zero_set;
|
|
261 dest->hash_function = src->hash_function;
|
|
262 dest->test_function = src->test_function;
|
|
263 memcpy (dest->harray, src->harray, sizeof (hentry) * dest->size);
|
|
264 }
|
173
|
265
|
0
|
266 static void
|
|
267 grow_hashtable (c_hashtable hash, unsigned int new_size)
|
|
268 {
|
|
269 unsigned int old_hsize = hash->size;
|
|
270 hentry *old_harray = hash->harray;
|
|
271 unsigned int new_hsize = prime_size (new_size);
|
|
272 hentry *new_harray;
|
|
273
|
|
274 #ifdef emacs
|
|
275 /* We test for Qzero to facilitate free-hook.c. That module creates
|
|
276 a hashtable very very early, at which point Qnil has not yet
|
|
277 been set and is thus all zeroes. Qzero is "automatically"
|
|
278 initialized at startup because its correct value is also all
|
|
279 zeroes. */
|
|
280 if (!NILP (hash->elisp_table) && !ZEROP (hash->elisp_table))
|
|
281 new_harray = (hentry *) elisp_hvector_malloc (sizeof (hentry) * new_hsize,
|
|
282 hash->elisp_table);
|
|
283 else
|
|
284 #endif
|
173
|
285 new_harray = (hentry *) xmalloc (sizeof (hentry) * new_hsize);
|
0
|
286
|
|
287 hash->size = new_hsize;
|
|
288 hash->harray = new_harray;
|
|
289
|
|
290 /* do the rehash on the "grown" table */
|
|
291 {
|
|
292 long old_zero_set = hash->zero_set;
|
|
293 void *old_zero_entry = hash->zero_entry;
|
|
294 clrhash (hash);
|
|
295 hash->zero_set = old_zero_set;
|
|
296 hash->zero_entry = old_zero_entry;
|
|
297 rehash (old_harray, hash, old_hsize);
|
|
298 }
|
|
299
|
|
300 #ifdef emacs
|
|
301 if (!NILP (hash->elisp_table) && !ZEROP (hash->elisp_table))
|
|
302 elisp_hvector_free (old_harray, hash->elisp_table);
|
|
303 else
|
|
304 #endif
|
|
305 xfree (old_harray);
|
|
306 }
|
|
307
|
|
308 void
|
|
309 expand_hashtable (c_hashtable hash, unsigned int needed_size)
|
|
310 {
|
|
311 unsigned int hsize = hash->size;
|
|
312 int comfortable_size = (13 * needed_size) / 10;
|
|
313 if (hsize < comfortable_size)
|
|
314 grow_hashtable (hash, comfortable_size + 1);
|
|
315 }
|
|
316
|
173
|
317 void
|
0
|
318 puthash (CONST void *key, void *cont, c_hashtable hash)
|
|
319 {
|
|
320 unsigned int hsize = hash->size;
|
|
321 int (*test_function) (CONST void *, CONST void *) = hash->test_function;
|
|
322 unsigned int fullness = hash->fullness;
|
|
323 hentry *harray;
|
|
324 CONST void *e_key;
|
|
325 hentry *e;
|
173
|
326 unsigned int hcode_initial =
|
0
|
327 (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
|
|
328 unsigned int hcode;
|
|
329 unsigned int incr = 0;
|
|
330 unsigned int h2;
|
|
331 CONST void *oldcontents;
|
|
332
|
173
|
333 if (!key)
|
0
|
334 {
|
173
|
335 hash->zero_entry = cont;
|
0
|
336 hash->zero_set = 1;
|
|
337 return;
|
|
338 }
|
|
339
|
|
340 if (hsize < (1 + ((13 * fullness) / 10)))
|
|
341 {
|
|
342 grow_hashtable (hash, hsize + 1);
|
|
343 hsize = hash->size;
|
|
344 fullness = hash->fullness;
|
|
345 }
|
|
346
|
|
347 harray= hash->harray;
|
|
348 h2 = hsize - 2;
|
|
349
|
|
350 hcode = hcode_initial % hsize;
|
173
|
351
|
0
|
352 e_key = harray [hcode].key;
|
|
353 if (e_key && (KEYS_DIFFER_P (e_key, key, test_function)))
|
|
354 {
|
|
355 h2 = hsize - 2;
|
|
356 incr = 1 + (hcode_initial % h2);
|
|
357 do
|
|
358 {
|
|
359 hcode = hcode + incr;
|
|
360 if (hcode >= hsize) hcode = hcode - hsize;
|
|
361 e_key = harray [hcode].key;
|
173
|
362 }
|
0
|
363 while (e_key && (KEYS_DIFFER_P (e_key, key, test_function)));
|
|
364 }
|
|
365 oldcontents = harray [hcode].contents;
|
|
366 harray [hcode].key = key;
|
|
367 harray [hcode].contents = cont;
|
|
368 /* if the entry that we used was a deleted entry,
|
|
369 check for a non deleted entry of the same key,
|
|
370 then delete it */
|
|
371 if (!e_key && (oldcontents == NULL_ENTRY))
|
|
372 {
|
|
373 if (!incr) incr = 1 + ((unsigned long) key % h2);
|
|
374
|
|
375 do
|
|
376 {
|
|
377 hcode = hcode + incr;
|
|
378 if (hcode >= hsize) hcode = hcode - hsize;
|
|
379 e = &harray [hcode];
|
|
380 e_key = e->key;
|
|
381 }
|
|
382 while ((e_key)?
|
|
383 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
384 (e->contents == NULL_ENTRY));
|
|
385
|
|
386 if (e_key)
|
|
387 {
|
|
388 e->key = 0;
|
|
389 e->contents = NULL_ENTRY;
|
|
390 }
|
|
391 }
|
|
392
|
|
393 /* only increment the fullness when we used up a new hentry */
|
|
394 if (!e_key || (KEYS_DIFFER_P (e_key, key, test_function)))
|
|
395 hash->fullness++;
|
|
396 }
|
|
397
|
|
398 static void
|
|
399 rehash (hentry *harray, c_hashtable hash, unsigned int size)
|
|
400 {
|
|
401 hentry *limit = harray + size;
|
|
402 hentry *e;
|
|
403 for (e = harray; e < limit; e++)
|
|
404 {
|
|
405 if (e->key)
|
|
406 puthash (e->key, e->contents, hash);
|
|
407 }
|
|
408 }
|
|
409
|
173
|
410 void
|
0
|
411 remhash (CONST void *key, c_hashtable hash)
|
|
412 {
|
|
413 hentry *harray = hash->harray;
|
|
414 int (*test_function) (CONST void*, CONST void*) = hash->test_function;
|
|
415 unsigned int hsize = hash->size;
|
173
|
416 unsigned int hcode_initial =
|
0
|
417 (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
|
|
418 unsigned int hcode = hcode_initial % hsize;
|
|
419 hentry *e = &harray [hcode];
|
|
420 CONST void *e_key = e->key;
|
|
421
|
173
|
422 if (!key)
|
0
|
423 {
|
|
424 hash->zero_entry = 0;
|
|
425 hash->zero_set = 0;
|
|
426 return;
|
|
427 }
|
|
428
|
|
429 if ((e_key)?
|
|
430 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
431 (e->contents == NULL_ENTRY))
|
|
432 {
|
|
433 unsigned int h2 = hsize - 2;
|
|
434 unsigned int incr = 1 + (hcode_initial % h2);
|
|
435 do
|
|
436 {
|
|
437 hcode = hcode + incr;
|
|
438 if (hcode >= hsize) hcode = hcode - hsize;
|
|
439 e = &harray [hcode];
|
|
440 e_key = e->key;
|
|
441 }
|
|
442 while ((e_key)?
|
|
443 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
444 (e->contents == NULL_ENTRY));
|
|
445 }
|
|
446 if (e_key)
|
|
447 {
|
|
448 e->key = 0;
|
|
449 e->contents = NULL_ENTRY;
|
|
450 /* Note: you can't do fullness-- here, it breaks the world. */
|
|
451 }
|
|
452 }
|
|
453
|
173
|
454 void
|
0
|
455 maphash (maphash_function mf, c_hashtable hash, void *arg)
|
|
456 {
|
|
457 hentry *e;
|
|
458 hentry *limit;
|
173
|
459
|
|
460 if (hash->zero_set)
|
0
|
461 ((*mf) (0, hash->zero_entry, arg));
|
|
462
|
|
463 for (e = hash->harray, limit = e + hash->size; e < limit; e++)
|
|
464 {
|
|
465 if (e->key)
|
|
466 ((*mf) (e->key, e->contents, arg));
|
|
467 }
|
|
468 }
|
|
469
|
173
|
470 void
|
0
|
471 map_remhash (remhash_predicate predicate, c_hashtable hash, void *arg)
|
|
472 {
|
|
473 hentry *e;
|
|
474 hentry *limit;
|
173
|
475
|
0
|
476 if (hash->zero_set && ((*predicate) (0, hash->zero_entry, arg)))
|
|
477 {
|
|
478 hash->zero_set = 0;
|
|
479 hash->zero_entry = 0;
|
|
480 }
|
|
481
|
|
482 for (e = hash->harray, limit = e + hash->size; e < limit; e++)
|
|
483 if ((*predicate) (e->key, e->contents, arg))
|
|
484 {
|
|
485 e->key = 0;
|
|
486 e->contents = NULL_ENTRY;
|
|
487 }
|
|
488 }
|