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. */
|
267
|
275 if (!EQ (hash->elisp_table, Qnull_pointer) &&
|
|
276 !NILP (hash->elisp_table) &&
|
|
277 !ZEROP (hash->elisp_table))
|
0
|
278 new_harray = (hentry *) elisp_hvector_malloc (sizeof (hentry) * new_hsize,
|
|
279 hash->elisp_table);
|
|
280 else
|
|
281 #endif
|
173
|
282 new_harray = (hentry *) xmalloc (sizeof (hentry) * new_hsize);
|
0
|
283
|
|
284 hash->size = new_hsize;
|
|
285 hash->harray = new_harray;
|
|
286
|
|
287 /* do the rehash on the "grown" table */
|
|
288 {
|
|
289 long old_zero_set = hash->zero_set;
|
|
290 void *old_zero_entry = hash->zero_entry;
|
|
291 clrhash (hash);
|
|
292 hash->zero_set = old_zero_set;
|
|
293 hash->zero_entry = old_zero_entry;
|
|
294 rehash (old_harray, hash, old_hsize);
|
|
295 }
|
|
296
|
|
297 #ifdef emacs
|
267
|
298 if (!EQ (hash->elisp_table, Qnull_pointer) &&
|
|
299 !NILP (hash->elisp_table) &&
|
|
300 !ZEROP (hash->elisp_table))
|
0
|
301 elisp_hvector_free (old_harray, hash->elisp_table);
|
|
302 else
|
|
303 #endif
|
|
304 xfree (old_harray);
|
|
305 }
|
|
306
|
|
307 void
|
|
308 expand_hashtable (c_hashtable hash, unsigned int needed_size)
|
|
309 {
|
|
310 unsigned int hsize = hash->size;
|
|
311 int comfortable_size = (13 * needed_size) / 10;
|
|
312 if (hsize < comfortable_size)
|
|
313 grow_hashtable (hash, comfortable_size + 1);
|
|
314 }
|
|
315
|
173
|
316 void
|
0
|
317 puthash (CONST void *key, void *cont, c_hashtable hash)
|
|
318 {
|
|
319 unsigned int hsize = hash->size;
|
|
320 int (*test_function) (CONST void *, CONST void *) = hash->test_function;
|
|
321 unsigned int fullness = hash->fullness;
|
|
322 hentry *harray;
|
|
323 CONST void *e_key;
|
|
324 hentry *e;
|
173
|
325 unsigned int hcode_initial =
|
0
|
326 (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
|
|
327 unsigned int hcode;
|
|
328 unsigned int incr = 0;
|
|
329 unsigned int h2;
|
|
330 CONST void *oldcontents;
|
|
331
|
173
|
332 if (!key)
|
0
|
333 {
|
173
|
334 hash->zero_entry = cont;
|
0
|
335 hash->zero_set = 1;
|
|
336 return;
|
|
337 }
|
|
338
|
|
339 if (hsize < (1 + ((13 * fullness) / 10)))
|
|
340 {
|
|
341 grow_hashtable (hash, hsize + 1);
|
|
342 hsize = hash->size;
|
|
343 fullness = hash->fullness;
|
|
344 }
|
|
345
|
|
346 harray= hash->harray;
|
|
347 h2 = hsize - 2;
|
|
348
|
|
349 hcode = hcode_initial % hsize;
|
173
|
350
|
0
|
351 e_key = harray [hcode].key;
|
|
352 if (e_key && (KEYS_DIFFER_P (e_key, key, test_function)))
|
|
353 {
|
|
354 h2 = hsize - 2;
|
|
355 incr = 1 + (hcode_initial % h2);
|
|
356 do
|
|
357 {
|
|
358 hcode = hcode + incr;
|
|
359 if (hcode >= hsize) hcode = hcode - hsize;
|
|
360 e_key = harray [hcode].key;
|
173
|
361 }
|
0
|
362 while (e_key && (KEYS_DIFFER_P (e_key, key, test_function)));
|
|
363 }
|
|
364 oldcontents = harray [hcode].contents;
|
|
365 harray [hcode].key = key;
|
|
366 harray [hcode].contents = cont;
|
|
367 /* if the entry that we used was a deleted entry,
|
|
368 check for a non deleted entry of the same key,
|
|
369 then delete it */
|
|
370 if (!e_key && (oldcontents == NULL_ENTRY))
|
|
371 {
|
|
372 if (!incr) incr = 1 + ((unsigned long) key % h2);
|
|
373
|
|
374 do
|
|
375 {
|
|
376 hcode = hcode + incr;
|
|
377 if (hcode >= hsize) hcode = hcode - hsize;
|
|
378 e = &harray [hcode];
|
|
379 e_key = e->key;
|
|
380 }
|
|
381 while ((e_key)?
|
|
382 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
383 (e->contents == NULL_ENTRY));
|
|
384
|
|
385 if (e_key)
|
|
386 {
|
|
387 e->key = 0;
|
|
388 e->contents = NULL_ENTRY;
|
|
389 }
|
|
390 }
|
|
391
|
|
392 /* only increment the fullness when we used up a new hentry */
|
|
393 if (!e_key || (KEYS_DIFFER_P (e_key, key, test_function)))
|
|
394 hash->fullness++;
|
|
395 }
|
|
396
|
|
397 static void
|
|
398 rehash (hentry *harray, c_hashtable hash, unsigned int size)
|
|
399 {
|
|
400 hentry *limit = harray + size;
|
|
401 hentry *e;
|
|
402 for (e = harray; e < limit; e++)
|
|
403 {
|
|
404 if (e->key)
|
|
405 puthash (e->key, e->contents, hash);
|
|
406 }
|
|
407 }
|
|
408
|
173
|
409 void
|
0
|
410 remhash (CONST void *key, c_hashtable hash)
|
|
411 {
|
|
412 hentry *harray = hash->harray;
|
|
413 int (*test_function) (CONST void*, CONST void*) = hash->test_function;
|
|
414 unsigned int hsize = hash->size;
|
173
|
415 unsigned int hcode_initial =
|
0
|
416 (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
|
|
417 unsigned int hcode = hcode_initial % hsize;
|
|
418 hentry *e = &harray [hcode];
|
|
419 CONST void *e_key = e->key;
|
|
420
|
173
|
421 if (!key)
|
0
|
422 {
|
|
423 hash->zero_entry = 0;
|
|
424 hash->zero_set = 0;
|
|
425 return;
|
|
426 }
|
|
427
|
|
428 if ((e_key)?
|
|
429 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
430 (e->contents == NULL_ENTRY))
|
|
431 {
|
|
432 unsigned int h2 = hsize - 2;
|
|
433 unsigned int incr = 1 + (hcode_initial % h2);
|
|
434 do
|
|
435 {
|
|
436 hcode = hcode + incr;
|
|
437 if (hcode >= hsize) hcode = hcode - hsize;
|
|
438 e = &harray [hcode];
|
|
439 e_key = e->key;
|
|
440 }
|
|
441 while ((e_key)?
|
|
442 (KEYS_DIFFER_P (e_key, key, test_function)):
|
|
443 (e->contents == NULL_ENTRY));
|
|
444 }
|
|
445 if (e_key)
|
|
446 {
|
|
447 e->key = 0;
|
|
448 e->contents = NULL_ENTRY;
|
|
449 /* Note: you can't do fullness-- here, it breaks the world. */
|
|
450 }
|
|
451 }
|
|
452
|
173
|
453 void
|
0
|
454 maphash (maphash_function mf, c_hashtable hash, void *arg)
|
|
455 {
|
|
456 hentry *e;
|
|
457 hentry *limit;
|
173
|
458
|
|
459 if (hash->zero_set)
|
241
|
460 {
|
|
461 if (((*mf) (0, hash->zero_entry, arg)))
|
|
462 return;
|
|
463 }
|
0
|
464
|
|
465 for (e = hash->harray, limit = e + hash->size; e < limit; e++)
|
|
466 {
|
|
467 if (e->key)
|
241
|
468 {
|
|
469 if (((*mf) (e->key, e->contents, arg)))
|
|
470 return;
|
|
471 }
|
0
|
472 }
|
|
473 }
|
|
474
|
173
|
475 void
|
0
|
476 map_remhash (remhash_predicate predicate, c_hashtable hash, void *arg)
|
|
477 {
|
|
478 hentry *e;
|
|
479 hentry *limit;
|
173
|
480
|
0
|
481 if (hash->zero_set && ((*predicate) (0, hash->zero_entry, arg)))
|
|
482 {
|
|
483 hash->zero_set = 0;
|
|
484 hash->zero_entry = 0;
|
|
485 }
|
|
486
|
|
487 for (e = hash->harray, limit = e + hash->size; e < limit; e++)
|
|
488 if ((*predicate) (e->key, e->contents, arg))
|
|
489 {
|
|
490 e->key = 0;
|
|
491 e->contents = NULL_ENTRY;
|
|
492 }
|
|
493 }
|