428
|
1 /* Implementation of the hash table lisp object type.
|
|
2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995, 1996 Ben Wing.
|
|
4 Copyright (C) 1997 Free Software Foundation, Inc.
|
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCNTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: Not in FSF. */
|
|
24
|
489
|
25 /* This file implements the hash table lisp object type.
|
|
26
|
504
|
27 This implementation was mostly written by Martin Buchholz in 1997.
|
|
28
|
|
29 The Lisp-level API (derived from Common Lisp) is almost completely
|
|
30 compatible with GNU Emacs 21, even though the implementations are
|
|
31 totally independent.
|
|
32
|
489
|
33 The hash table technique used is "linear probing". Collisions are
|
|
34 resolved by putting the item in the next empty place in the array
|
|
35 following the collision. Finding a hash entry performs a linear
|
|
36 search in the cluster starting at the hash value.
|
|
37
|
|
38 On deletions from the hash table, the entries immediately following
|
|
39 the deleted entry are re-entered in the hash table. We do not have
|
|
40 a special way to mark deleted entries (known as "tombstones").
|
|
41
|
|
42 At the end of the hash entries ("hentries"), we leave room for an
|
|
43 entry that is always empty (the "sentinel").
|
|
44
|
|
45 The traditional literature on hash table implementation
|
|
46 (e.g. Knuth) suggests that too much "primary clustering" occurs
|
|
47 with linear probing. However, this literature was written when
|
|
48 locality of reference was not a factor. The discrepancy between
|
|
49 CPU speeds and memory speeds is increasing, and the speed of access
|
|
50 to memory is highly dependent on memory caches which work best when
|
|
51 there is high locality of data reference. Random access to memory
|
|
52 is up to 20 times as expensive as access to the nearest address
|
|
53 (and getting worse). So linear probing makes sense.
|
|
54
|
|
55 But the representation doesn't actually matter that much with the
|
|
56 current elisp engine. Funcall is sufficiently slow that the choice
|
|
57 of hash table implementation is noise. */
|
|
58
|
428
|
59 #include <config.h>
|
|
60 #include "lisp.h"
|
|
61 #include "bytecode.h"
|
|
62 #include "elhash.h"
|
489
|
63 #include "opaque.h"
|
428
|
64
|
|
65 Lisp_Object Qhash_tablep;
|
|
66 static Lisp_Object Qhashtable, Qhash_table;
|
442
|
67 static Lisp_Object Qweakness, Qvalue, Qkey_or_value, Qkey_and_value;
|
428
|
68 static Lisp_Object Vall_weak_hash_tables;
|
|
69 static Lisp_Object Qrehash_size, Qrehash_threshold;
|
|
70 static Lisp_Object Q_size, Q_test, Q_weakness, Q_rehash_size, Q_rehash_threshold;
|
|
71
|
|
72 /* obsolete as of 19990901 in xemacs-21.2 */
|
442
|
73 static Lisp_Object Qweak, Qkey_weak, Qvalue_weak, Qkey_or_value_weak;
|
|
74 static Lisp_Object Qnon_weak, Q_type;
|
428
|
75
|
|
76 typedef struct hentry
|
|
77 {
|
|
78 Lisp_Object key;
|
|
79 Lisp_Object value;
|
|
80 } hentry;
|
|
81
|
|
82 struct Lisp_Hash_Table
|
|
83 {
|
|
84 struct lcrecord_header header;
|
|
85 size_t size;
|
|
86 size_t count;
|
|
87 size_t rehash_count;
|
|
88 double rehash_size;
|
|
89 double rehash_threshold;
|
|
90 size_t golden_ratio;
|
|
91 hash_table_hash_function_t hash_function;
|
|
92 hash_table_test_function_t test_function;
|
|
93 hentry *hentries;
|
|
94 enum hash_table_weakness weakness;
|
|
95 Lisp_Object next_weak; /* Used to chain together all of the weak
|
|
96 hash tables. Don't mark through this. */
|
|
97 };
|
|
98
|
|
99 #define HENTRY_CLEAR_P(hentry) ((*(EMACS_UINT*)(&((hentry)->key))) == 0)
|
|
100 #define CLEAR_HENTRY(hentry) \
|
|
101 ((*(EMACS_UINT*)(&((hentry)->key))) = 0, \
|
|
102 (*(EMACS_UINT*)(&((hentry)->value))) = 0)
|
|
103
|
|
104 #define HASH_TABLE_DEFAULT_SIZE 16
|
|
105 #define HASH_TABLE_DEFAULT_REHASH_SIZE 1.3
|
|
106 #define HASH_TABLE_MIN_SIZE 10
|
|
107
|
434
|
108 #define HASH_CODE(key, ht) \
|
444
|
109 ((((ht)->hash_function ? (ht)->hash_function (key) : LISP_HASH (key)) \
|
|
110 * (ht)->golden_ratio) \
|
|
111 % (ht)->size)
|
428
|
112
|
|
113 #define KEYS_EQUAL_P(key1, key2, testfun) \
|
434
|
114 (EQ (key1, key2) || ((testfun) && (testfun) (key1, key2)))
|
428
|
115
|
|
116 #define LINEAR_PROBING_LOOP(probe, entries, size) \
|
|
117 for (; \
|
|
118 !HENTRY_CLEAR_P (probe) || \
|
|
119 (probe == entries + size ? \
|
|
120 (probe = entries, !HENTRY_CLEAR_P (probe)) : 0); \
|
|
121 probe++)
|
|
122
|
|
123 #ifndef ERROR_CHECK_HASH_TABLE
|
|
124 # ifdef ERROR_CHECK_TYPECHECK
|
|
125 # define ERROR_CHECK_HASH_TABLE 1
|
|
126 # else
|
|
127 # define ERROR_CHECK_HASH_TABLE 0
|
|
128 # endif
|
|
129 #endif
|
|
130
|
|
131 #if ERROR_CHECK_HASH_TABLE
|
|
132 static void
|
|
133 check_hash_table_invariants (Lisp_Hash_Table *ht)
|
|
134 {
|
|
135 assert (ht->count < ht->size);
|
|
136 assert (ht->count <= ht->rehash_count);
|
|
137 assert (ht->rehash_count < ht->size);
|
|
138 assert ((double) ht->count * ht->rehash_threshold - 1 <= (double) ht->rehash_count);
|
|
139 assert (HENTRY_CLEAR_P (ht->hentries + ht->size));
|
|
140 }
|
|
141 #else
|
|
142 #define check_hash_table_invariants(ht)
|
|
143 #endif
|
|
144
|
|
145 /* Return a suitable size for a hash table, with at least SIZE slots. */
|
|
146 static size_t
|
|
147 hash_table_size (size_t requested_size)
|
|
148 {
|
|
149 /* Return some prime near, but greater than or equal to, SIZE.
|
|
150 Decades from the time of writing, someone will have a system large
|
|
151 enough that the list below will be too short... */
|
442
|
152 static const size_t primes [] =
|
428
|
153 {
|
|
154 19, 29, 41, 59, 79, 107, 149, 197, 263, 347, 457, 599, 787, 1031,
|
|
155 1361, 1777, 2333, 3037, 3967, 5167, 6719, 8737, 11369, 14783,
|
|
156 19219, 24989, 32491, 42257, 54941, 71429, 92861, 120721, 156941,
|
|
157 204047, 265271, 344857, 448321, 582821, 757693, 985003, 1280519,
|
|
158 1664681, 2164111, 2813353, 3657361, 4754591, 6180989, 8035301,
|
|
159 10445899, 13579681, 17653589, 22949669, 29834603, 38784989,
|
|
160 50420551, 65546729, 85210757, 110774011, 144006217, 187208107,
|
|
161 243370577, 316381771, 411296309, 534685237, 695090819, 903618083,
|
|
162 1174703521, 1527114613, 1985248999, 2580823717UL, 3355070839UL
|
|
163 };
|
|
164 /* We've heard of binary search. */
|
|
165 int low, high;
|
|
166 for (low = 0, high = countof (primes) - 1; high - low > 1;)
|
|
167 {
|
|
168 /* Loop Invariant: size < primes [high] */
|
|
169 int mid = (low + high) / 2;
|
|
170 if (primes [mid] < requested_size)
|
|
171 low = mid;
|
|
172 else
|
|
173 high = mid;
|
|
174 }
|
|
175 return primes [high];
|
|
176 }
|
|
177
|
|
178
|
|
179 #if 0 /* I don't think these are needed any more.
|
|
180 If using the general lisp_object_equal_*() functions
|
|
181 causes efficiency problems, these can be resurrected. --ben */
|
|
182 /* equality and hash functions for Lisp strings */
|
|
183 int
|
|
184 lisp_string_equal (Lisp_Object str1, Lisp_Object str2)
|
|
185 {
|
|
186 /* This is wrong anyway. You can't use strcmp() on Lisp strings,
|
|
187 because they can contain zero characters. */
|
|
188 return !strcmp ((char *) XSTRING_DATA (str1), (char *) XSTRING_DATA (str2));
|
|
189 }
|
|
190
|
|
191 static hashcode_t
|
|
192 lisp_string_hash (Lisp_Object obj)
|
|
193 {
|
|
194 return hash_string (XSTRING_DATA (str), XSTRING_LENGTH (str));
|
|
195 }
|
|
196
|
|
197 #endif /* 0 */
|
|
198
|
|
199 static int
|
|
200 lisp_object_eql_equal (Lisp_Object obj1, Lisp_Object obj2)
|
|
201 {
|
|
202 return EQ (obj1, obj2) || (FLOATP (obj1) && internal_equal (obj1, obj2, 0));
|
|
203 }
|
|
204
|
|
205 static hashcode_t
|
|
206 lisp_object_eql_hash (Lisp_Object obj)
|
|
207 {
|
|
208 return FLOATP (obj) ? internal_hash (obj, 0) : LISP_HASH (obj);
|
|
209 }
|
|
210
|
|
211 static int
|
|
212 lisp_object_equal_equal (Lisp_Object obj1, Lisp_Object obj2)
|
|
213 {
|
|
214 return internal_equal (obj1, obj2, 0);
|
|
215 }
|
|
216
|
|
217 static hashcode_t
|
|
218 lisp_object_equal_hash (Lisp_Object obj)
|
|
219 {
|
|
220 return internal_hash (obj, 0);
|
|
221 }
|
|
222
|
|
223
|
|
224 static Lisp_Object
|
|
225 mark_hash_table (Lisp_Object obj)
|
|
226 {
|
|
227 Lisp_Hash_Table *ht = XHASH_TABLE (obj);
|
|
228
|
|
229 /* If the hash table is weak, we don't want to mark the keys and
|
|
230 values (we scan over them after everything else has been marked,
|
|
231 and mark or remove them as necessary). */
|
|
232 if (ht->weakness == HASH_TABLE_NON_WEAK)
|
|
233 {
|
|
234 hentry *e, *sentinel;
|
|
235
|
|
236 for (e = ht->hentries, sentinel = e + ht->size; e < sentinel; e++)
|
|
237 if (!HENTRY_CLEAR_P (e))
|
|
238 {
|
|
239 mark_object (e->key);
|
|
240 mark_object (e->value);
|
|
241 }
|
|
242 }
|
|
243 return Qnil;
|
|
244 }
|
|
245
|
|
246 /* Equality of hash tables. Two hash tables are equal when they are of
|
|
247 the same weakness and test function, they have the same number of
|
|
248 elements, and for each key in the hash table, the values are `equal'.
|
|
249
|
|
250 This is similar to Common Lisp `equalp' of hash tables, with the
|
|
251 difference that CL requires the keys to be compared with the test
|
|
252 function, which we don't do. Doing that would require consing, and
|
|
253 consing is a bad idea in `equal'. Anyway, our method should provide
|
|
254 the same result -- if the keys are not equal according to the test
|
|
255 function, then Fgethash() in hash_table_equal_mapper() will fail. */
|
|
256 static int
|
|
257 hash_table_equal (Lisp_Object hash_table1, Lisp_Object hash_table2, int depth)
|
|
258 {
|
|
259 Lisp_Hash_Table *ht1 = XHASH_TABLE (hash_table1);
|
|
260 Lisp_Hash_Table *ht2 = XHASH_TABLE (hash_table2);
|
|
261 hentry *e, *sentinel;
|
|
262
|
|
263 if ((ht1->test_function != ht2->test_function) ||
|
|
264 (ht1->weakness != ht2->weakness) ||
|
|
265 (ht1->count != ht2->count))
|
|
266 return 0;
|
|
267
|
|
268 depth++;
|
|
269
|
|
270 for (e = ht1->hentries, sentinel = e + ht1->size; e < sentinel; e++)
|
|
271 if (!HENTRY_CLEAR_P (e))
|
|
272 /* Look up the key in the other hash table, and compare the values. */
|
|
273 {
|
|
274 Lisp_Object value_in_other = Fgethash (e->key, hash_table2, Qunbound);
|
|
275 if (UNBOUNDP (value_in_other) ||
|
|
276 !internal_equal (e->value, value_in_other, depth))
|
|
277 return 0; /* Give up */
|
|
278 }
|
|
279
|
|
280 return 1;
|
|
281 }
|
442
|
282
|
|
283 /* This is not a great hash function, but it _is_ correct and fast.
|
|
284 Examining all entries is too expensive, and examining a random
|
|
285 subset does not yield a correct hash function. */
|
|
286 static hashcode_t
|
|
287 hash_table_hash (Lisp_Object hash_table, int depth)
|
|
288 {
|
|
289 return XHASH_TABLE (hash_table)->count;
|
|
290 }
|
|
291
|
428
|
292
|
|
293 /* Printing hash tables.
|
|
294
|
|
295 This is non-trivial, because we use a readable structure-style
|
|
296 syntax for hash tables. This means that a typical hash table will be
|
|
297 readably printed in the form of:
|
|
298
|
|
299 #s(hash-table size 2 data (key1 value1 key2 value2))
|
|
300
|
|
301 The supported hash table structure keywords and their values are:
|
|
302 `test' (eql (or nil), eq or equal)
|
|
303 `size' (a natnum or nil)
|
|
304 `rehash-size' (a float)
|
|
305 `rehash-threshold' (a float)
|
442
|
306 `weakness' (nil, key, value, key-and-value, or key-or-value)
|
428
|
307 `data' (a list)
|
|
308
|
430
|
309 If `print-readably' is nil, then a simpler syntax is used, for example
|
428
|
310
|
|
311 #<hash-table size 2/13 data (key1 value1 key2 value2) 0x874d>
|
|
312
|
|
313 The data is truncated to four pairs, and the rest is shown with
|
|
314 `...'. This printer does not cons. */
|
|
315
|
|
316
|
|
317 /* Print the data of the hash table. This maps through a Lisp
|
|
318 hash table and prints key/value pairs using PRINTCHARFUN. */
|
|
319 static void
|
|
320 print_hash_table_data (Lisp_Hash_Table *ht, Lisp_Object printcharfun)
|
|
321 {
|
|
322 int count = 0;
|
|
323 hentry *e, *sentinel;
|
|
324
|
|
325 write_c_string (" data (", printcharfun);
|
|
326
|
|
327 for (e = ht->hentries, sentinel = e + ht->size; e < sentinel; e++)
|
|
328 if (!HENTRY_CLEAR_P (e))
|
|
329 {
|
|
330 if (count > 0)
|
|
331 write_c_string (" ", printcharfun);
|
|
332 if (!print_readably && count > 3)
|
|
333 {
|
|
334 write_c_string ("...", printcharfun);
|
|
335 break;
|
|
336 }
|
|
337 print_internal (e->key, printcharfun, 1);
|
|
338 write_c_string (" ", printcharfun);
|
|
339 print_internal (e->value, printcharfun, 1);
|
|
340 count++;
|
|
341 }
|
|
342
|
|
343 write_c_string (")", printcharfun);
|
|
344 }
|
|
345
|
|
346 static void
|
|
347 print_hash_table (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
348 {
|
|
349 Lisp_Hash_Table *ht = XHASH_TABLE (obj);
|
|
350 char buf[128];
|
|
351
|
|
352 write_c_string (print_readably ? "#s(hash-table" : "#<hash-table",
|
|
353 printcharfun);
|
|
354
|
|
355 /* These checks have a kludgy look to them, but they are safe.
|
|
356 Due to nature of hashing, you cannot use arbitrary
|
|
357 test functions anyway. */
|
|
358 if (!ht->test_function)
|
|
359 write_c_string (" test eq", printcharfun);
|
|
360 else if (ht->test_function == lisp_object_equal_equal)
|
|
361 write_c_string (" test equal", printcharfun);
|
|
362 else if (ht->test_function == lisp_object_eql_equal)
|
|
363 DO_NOTHING;
|
|
364 else
|
|
365 abort ();
|
|
366
|
|
367 if (ht->count || !print_readably)
|
|
368 {
|
|
369 if (print_readably)
|
|
370 sprintf (buf, " size %lu", (unsigned long) ht->count);
|
|
371 else
|
|
372 sprintf (buf, " size %lu/%lu",
|
|
373 (unsigned long) ht->count,
|
|
374 (unsigned long) ht->size);
|
|
375 write_c_string (buf, printcharfun);
|
|
376 }
|
|
377
|
|
378 if (ht->weakness != HASH_TABLE_NON_WEAK)
|
|
379 {
|
|
380 sprintf (buf, " weakness %s",
|
442
|
381 (ht->weakness == HASH_TABLE_WEAK ? "key-and-value" :
|
|
382 ht->weakness == HASH_TABLE_KEY_WEAK ? "key" :
|
|
383 ht->weakness == HASH_TABLE_VALUE_WEAK ? "value" :
|
|
384 ht->weakness == HASH_TABLE_KEY_VALUE_WEAK ? "key-or-value" :
|
428
|
385 "you-d-better-not-see-this"));
|
|
386 write_c_string (buf, printcharfun);
|
|
387 }
|
|
388
|
|
389 if (ht->count)
|
|
390 print_hash_table_data (ht, printcharfun);
|
|
391
|
|
392 if (print_readably)
|
|
393 write_c_string (")", printcharfun);
|
|
394 else
|
|
395 {
|
|
396 sprintf (buf, " 0x%x>", ht->header.uid);
|
|
397 write_c_string (buf, printcharfun);
|
|
398 }
|
|
399 }
|
|
400
|
|
401 static void
|
489
|
402 free_hentries (hentry *hentries, size_t size)
|
|
403 {
|
|
404 #if ERROR_CHECK_HASH_TABLE
|
|
405 /* Ensure a crash if other code uses the discarded entries afterwards. */
|
|
406 hentry *e, *sentinel;
|
|
407
|
|
408 for (e = hentries, sentinel = e + size; e < sentinel; e++)
|
|
409 * (unsigned long *) e = 0xdeadbeef;
|
|
410 #endif
|
|
411
|
|
412 if (!DUMPEDP (hentries))
|
|
413 xfree (hentries);
|
|
414 }
|
|
415
|
|
416 static void
|
428
|
417 finalize_hash_table (void *header, int for_disksave)
|
|
418 {
|
|
419 if (!for_disksave)
|
|
420 {
|
|
421 Lisp_Hash_Table *ht = (Lisp_Hash_Table *) header;
|
489
|
422 free_hentries (ht->hentries, ht->size);
|
428
|
423 ht->hentries = 0;
|
|
424 }
|
|
425 }
|
|
426
|
|
427 static const struct lrecord_description hentry_description_1[] = {
|
440
|
428 { XD_LISP_OBJECT, offsetof (hentry, key) },
|
|
429 { XD_LISP_OBJECT, offsetof (hentry, value) },
|
428
|
430 { XD_END }
|
|
431 };
|
|
432
|
|
433 static const struct struct_description hentry_description = {
|
440
|
434 sizeof (hentry),
|
428
|
435 hentry_description_1
|
|
436 };
|
|
437
|
|
438 const struct lrecord_description hash_table_description[] = {
|
440
|
439 { XD_SIZE_T, offsetof (Lisp_Hash_Table, size) },
|
|
440 { XD_STRUCT_PTR, offsetof (Lisp_Hash_Table, hentries), XD_INDIRECT(0, 1), &hentry_description },
|
|
441 { XD_LO_LINK, offsetof (Lisp_Hash_Table, next_weak) },
|
428
|
442 { XD_END }
|
|
443 };
|
|
444
|
|
445 DEFINE_LRECORD_IMPLEMENTATION ("hash-table", hash_table,
|
|
446 mark_hash_table, print_hash_table,
|
|
447 finalize_hash_table,
|
442
|
448 hash_table_equal, hash_table_hash,
|
428
|
449 hash_table_description,
|
|
450 Lisp_Hash_Table);
|
|
451
|
|
452 static Lisp_Hash_Table *
|
|
453 xhash_table (Lisp_Object hash_table)
|
|
454 {
|
|
455 if (!gc_in_progress)
|
|
456 CHECK_HASH_TABLE (hash_table);
|
|
457 check_hash_table_invariants (XHASH_TABLE (hash_table));
|
|
458 return XHASH_TABLE (hash_table);
|
|
459 }
|
|
460
|
|
461
|
|
462 /************************************************************************/
|
|
463 /* Creation of Hash Tables */
|
|
464 /************************************************************************/
|
|
465
|
|
466 /* Creation of hash tables, without error-checking. */
|
|
467 static void
|
|
468 compute_hash_table_derived_values (Lisp_Hash_Table *ht)
|
|
469 {
|
|
470 ht->rehash_count = (size_t)
|
438
|
471 ((double) ht->size * ht->rehash_threshold);
|
428
|
472 ht->golden_ratio = (size_t)
|
|
473 ((double) ht->size * (.6180339887 / (double) sizeof (Lisp_Object)));
|
|
474 }
|
|
475
|
|
476 Lisp_Object
|
450
|
477 make_standard_lisp_hash_table (enum hash_table_test test,
|
|
478 size_t size,
|
|
479 double rehash_size,
|
|
480 double rehash_threshold,
|
|
481 enum hash_table_weakness weakness)
|
|
482 {
|
462
|
483 hash_table_hash_function_t hash_function = 0;
|
450
|
484 hash_table_test_function_t test_function = 0;
|
|
485
|
|
486 switch (test)
|
|
487 {
|
|
488 case HASH_TABLE_EQ:
|
|
489 test_function = 0;
|
|
490 hash_function = 0;
|
|
491 break;
|
|
492
|
|
493 case HASH_TABLE_EQL:
|
|
494 test_function = lisp_object_eql_equal;
|
|
495 hash_function = lisp_object_eql_hash;
|
|
496 break;
|
|
497
|
|
498 case HASH_TABLE_EQUAL:
|
|
499 test_function = lisp_object_equal_equal;
|
|
500 hash_function = lisp_object_equal_hash;
|
|
501 break;
|
|
502
|
|
503 default:
|
|
504 abort ();
|
|
505 }
|
|
506
|
|
507 return make_general_lisp_hash_table (hash_function, test_function,
|
|
508 size, rehash_size, rehash_threshold,
|
|
509 weakness);
|
|
510 }
|
|
511
|
|
512 Lisp_Object
|
|
513 make_general_lisp_hash_table (hash_table_hash_function_t hash_function,
|
|
514 hash_table_test_function_t test_function,
|
428
|
515 size_t size,
|
|
516 double rehash_size,
|
|
517 double rehash_threshold,
|
|
518 enum hash_table_weakness weakness)
|
|
519 {
|
|
520 Lisp_Object hash_table;
|
|
521 Lisp_Hash_Table *ht = alloc_lcrecord_type (Lisp_Hash_Table, &lrecord_hash_table);
|
|
522
|
450
|
523 ht->test_function = test_function;
|
|
524 ht->hash_function = hash_function;
|
438
|
525 ht->weakness = weakness;
|
|
526
|
|
527 ht->rehash_size =
|
|
528 rehash_size > 1.0 ? rehash_size : HASH_TABLE_DEFAULT_REHASH_SIZE;
|
|
529
|
|
530 ht->rehash_threshold =
|
|
531 rehash_threshold > 0.0 ? rehash_threshold :
|
|
532 size > 4096 && !ht->test_function ? 0.7 : 0.6;
|
|
533
|
428
|
534 if (size < HASH_TABLE_MIN_SIZE)
|
|
535 size = HASH_TABLE_MIN_SIZE;
|
438
|
536 ht->size = hash_table_size ((size_t) (((double) size / ht->rehash_threshold)
|
|
537 + 1.0));
|
428
|
538 ht->count = 0;
|
438
|
539
|
428
|
540 compute_hash_table_derived_values (ht);
|
|
541
|
|
542 /* We leave room for one never-occupied sentinel hentry at the end. */
|
458
|
543 ht->hentries = xnew_array_and_zero (hentry, ht->size + 1);
|
428
|
544
|
|
545 XSETHASH_TABLE (hash_table, ht);
|
|
546
|
|
547 if (weakness == HASH_TABLE_NON_WEAK)
|
|
548 ht->next_weak = Qunbound;
|
|
549 else
|
|
550 ht->next_weak = Vall_weak_hash_tables, Vall_weak_hash_tables = hash_table;
|
|
551
|
|
552 return hash_table;
|
|
553 }
|
|
554
|
|
555 Lisp_Object
|
|
556 make_lisp_hash_table (size_t size,
|
|
557 enum hash_table_weakness weakness,
|
|
558 enum hash_table_test test)
|
|
559 {
|
450
|
560 return make_standard_lisp_hash_table (test, size, -1.0, -1.0, weakness);
|
428
|
561 }
|
|
562
|
|
563 /* Pretty reading of hash tables.
|
|
564
|
|
565 Here we use the existing structures mechanism (which is,
|
|
566 unfortunately, pretty cumbersome) for validating and instantiating
|
|
567 the hash tables. The idea is that the side-effect of reading a
|
|
568 #s(hash-table PLIST) object is creation of a hash table with desired
|
|
569 properties, and that the hash table is returned. */
|
|
570
|
|
571 /* Validation functions: each keyword provides its own validation
|
|
572 function. The errors should maybe be continuable, but it is
|
|
573 unclear how this would cope with ERRB. */
|
|
574 static int
|
|
575 hash_table_size_validate (Lisp_Object keyword, Lisp_Object value,
|
|
576 Error_behavior errb)
|
|
577 {
|
|
578 if (NATNUMP (value))
|
|
579 return 1;
|
|
580
|
|
581 maybe_signal_error (Qwrong_type_argument, list2 (Qnatnump, value),
|
|
582 Qhash_table, errb);
|
|
583 return 0;
|
|
584 }
|
|
585
|
|
586 static size_t
|
|
587 decode_hash_table_size (Lisp_Object obj)
|
|
588 {
|
|
589 return NILP (obj) ? HASH_TABLE_DEFAULT_SIZE : XINT (obj);
|
|
590 }
|
|
591
|
|
592 static int
|
|
593 hash_table_weakness_validate (Lisp_Object keyword, Lisp_Object value,
|
|
594 Error_behavior errb)
|
|
595 {
|
442
|
596 if (EQ (value, Qnil)) return 1;
|
|
597 if (EQ (value, Qt)) return 1;
|
|
598 if (EQ (value, Qkey)) return 1;
|
|
599 if (EQ (value, Qkey_and_value)) return 1;
|
|
600 if (EQ (value, Qkey_or_value)) return 1;
|
|
601 if (EQ (value, Qvalue)) return 1;
|
428
|
602
|
|
603 /* Following values are obsolete as of 19990901 in xemacs-21.2 */
|
442
|
604 if (EQ (value, Qnon_weak)) return 1;
|
|
605 if (EQ (value, Qweak)) return 1;
|
|
606 if (EQ (value, Qkey_weak)) return 1;
|
|
607 if (EQ (value, Qkey_or_value_weak)) return 1;
|
|
608 if (EQ (value, Qvalue_weak)) return 1;
|
428
|
609
|
|
610 maybe_signal_simple_error ("Invalid hash table weakness",
|
|
611 value, Qhash_table, errb);
|
|
612 return 0;
|
|
613 }
|
|
614
|
|
615 static enum hash_table_weakness
|
|
616 decode_hash_table_weakness (Lisp_Object obj)
|
|
617 {
|
442
|
618 if (EQ (obj, Qnil)) return HASH_TABLE_NON_WEAK;
|
|
619 if (EQ (obj, Qt)) return HASH_TABLE_WEAK;
|
|
620 if (EQ (obj, Qkey_and_value)) return HASH_TABLE_WEAK;
|
|
621 if (EQ (obj, Qkey)) return HASH_TABLE_KEY_WEAK;
|
|
622 if (EQ (obj, Qkey_or_value)) return HASH_TABLE_KEY_VALUE_WEAK;
|
|
623 if (EQ (obj, Qvalue)) return HASH_TABLE_VALUE_WEAK;
|
428
|
624
|
|
625 /* Following values are obsolete as of 19990901 in xemacs-21.2 */
|
442
|
626 if (EQ (obj, Qnon_weak)) return HASH_TABLE_NON_WEAK;
|
|
627 if (EQ (obj, Qweak)) return HASH_TABLE_WEAK;
|
|
628 if (EQ (obj, Qkey_weak)) return HASH_TABLE_KEY_WEAK;
|
|
629 if (EQ (obj, Qkey_or_value_weak)) return HASH_TABLE_KEY_VALUE_WEAK;
|
|
630 if (EQ (obj, Qvalue_weak)) return HASH_TABLE_VALUE_WEAK;
|
428
|
631
|
|
632 signal_simple_error ("Invalid hash table weakness", obj);
|
|
633 return HASH_TABLE_NON_WEAK; /* not reached */
|
|
634 }
|
|
635
|
|
636 static int
|
|
637 hash_table_test_validate (Lisp_Object keyword, Lisp_Object value,
|
|
638 Error_behavior errb)
|
|
639 {
|
|
640 if (EQ (value, Qnil)) return 1;
|
|
641 if (EQ (value, Qeq)) return 1;
|
|
642 if (EQ (value, Qequal)) return 1;
|
|
643 if (EQ (value, Qeql)) return 1;
|
|
644
|
|
645 maybe_signal_simple_error ("Invalid hash table test",
|
|
646 value, Qhash_table, errb);
|
|
647 return 0;
|
|
648 }
|
|
649
|
|
650 static enum hash_table_test
|
|
651 decode_hash_table_test (Lisp_Object obj)
|
|
652 {
|
|
653 if (EQ (obj, Qnil)) return HASH_TABLE_EQL;
|
|
654 if (EQ (obj, Qeq)) return HASH_TABLE_EQ;
|
|
655 if (EQ (obj, Qequal)) return HASH_TABLE_EQUAL;
|
|
656 if (EQ (obj, Qeql)) return HASH_TABLE_EQL;
|
|
657
|
|
658 signal_simple_error ("Invalid hash table test", obj);
|
|
659 return HASH_TABLE_EQ; /* not reached */
|
|
660 }
|
|
661
|
|
662 static int
|
|
663 hash_table_rehash_size_validate (Lisp_Object keyword, Lisp_Object value,
|
|
664 Error_behavior errb)
|
|
665 {
|
|
666 if (!FLOATP (value))
|
|
667 {
|
|
668 maybe_signal_error (Qwrong_type_argument, list2 (Qfloatp, value),
|
|
669 Qhash_table, errb);
|
|
670 return 0;
|
|
671 }
|
|
672
|
|
673 {
|
|
674 double rehash_size = XFLOAT_DATA (value);
|
|
675 if (rehash_size <= 1.0)
|
|
676 {
|
|
677 maybe_signal_simple_error
|
|
678 ("Hash table rehash size must be greater than 1.0",
|
|
679 value, Qhash_table, errb);
|
|
680 return 0;
|
|
681 }
|
|
682 }
|
|
683
|
|
684 return 1;
|
|
685 }
|
|
686
|
|
687 static double
|
|
688 decode_hash_table_rehash_size (Lisp_Object rehash_size)
|
|
689 {
|
|
690 return NILP (rehash_size) ? -1.0 : XFLOAT_DATA (rehash_size);
|
|
691 }
|
|
692
|
|
693 static int
|
|
694 hash_table_rehash_threshold_validate (Lisp_Object keyword, Lisp_Object value,
|
|
695 Error_behavior errb)
|
|
696 {
|
|
697 if (!FLOATP (value))
|
|
698 {
|
|
699 maybe_signal_error (Qwrong_type_argument, list2 (Qfloatp, value),
|
|
700 Qhash_table, errb);
|
|
701 return 0;
|
|
702 }
|
|
703
|
|
704 {
|
|
705 double rehash_threshold = XFLOAT_DATA (value);
|
|
706 if (rehash_threshold <= 0.0 || rehash_threshold >= 1.0)
|
|
707 {
|
|
708 maybe_signal_simple_error
|
|
709 ("Hash table rehash threshold must be between 0.0 and 1.0",
|
|
710 value, Qhash_table, errb);
|
|
711 return 0;
|
|
712 }
|
|
713 }
|
|
714
|
|
715 return 1;
|
|
716 }
|
|
717
|
|
718 static double
|
|
719 decode_hash_table_rehash_threshold (Lisp_Object rehash_threshold)
|
|
720 {
|
|
721 return NILP (rehash_threshold) ? -1.0 : XFLOAT_DATA (rehash_threshold);
|
|
722 }
|
|
723
|
|
724 static int
|
|
725 hash_table_data_validate (Lisp_Object keyword, Lisp_Object value,
|
|
726 Error_behavior errb)
|
|
727 {
|
|
728 int len;
|
|
729
|
|
730 GET_EXTERNAL_LIST_LENGTH (value, len);
|
|
731
|
|
732 if (len & 1)
|
|
733 {
|
|
734 maybe_signal_simple_error
|
|
735 ("Hash table data must have alternating key/value pairs",
|
|
736 value, Qhash_table, errb);
|
|
737 return 0;
|
|
738 }
|
|
739 return 1;
|
|
740 }
|
|
741
|
|
742 /* The actual instantiation of a hash table. This does practically no
|
|
743 error checking, because it relies on the fact that the paranoid
|
|
744 functions above have error-checked everything to the last details.
|
|
745 If this assumption is wrong, we will get a crash immediately (with
|
|
746 error-checking compiled in), and we'll know if there is a bug in
|
|
747 the structure mechanism. So there. */
|
|
748 static Lisp_Object
|
|
749 hash_table_instantiate (Lisp_Object plist)
|
|
750 {
|
|
751 Lisp_Object hash_table;
|
|
752 Lisp_Object test = Qnil;
|
|
753 Lisp_Object size = Qnil;
|
|
754 Lisp_Object rehash_size = Qnil;
|
|
755 Lisp_Object rehash_threshold = Qnil;
|
|
756 Lisp_Object weakness = Qnil;
|
|
757 Lisp_Object data = Qnil;
|
|
758
|
|
759 while (!NILP (plist))
|
|
760 {
|
|
761 Lisp_Object key, value;
|
|
762 key = XCAR (plist); plist = XCDR (plist);
|
|
763 value = XCAR (plist); plist = XCDR (plist);
|
|
764
|
|
765 if (EQ (key, Qtest)) test = value;
|
|
766 else if (EQ (key, Qsize)) size = value;
|
|
767 else if (EQ (key, Qrehash_size)) rehash_size = value;
|
|
768 else if (EQ (key, Qrehash_threshold)) rehash_threshold = value;
|
|
769 else if (EQ (key, Qweakness)) weakness = value;
|
|
770 else if (EQ (key, Qdata)) data = value;
|
|
771 else if (EQ (key, Qtype))/*obsolete*/ weakness = value;
|
|
772 else
|
|
773 abort ();
|
|
774 }
|
|
775
|
|
776 /* Create the hash table. */
|
450
|
777 hash_table = make_standard_lisp_hash_table
|
428
|
778 (decode_hash_table_test (test),
|
|
779 decode_hash_table_size (size),
|
|
780 decode_hash_table_rehash_size (rehash_size),
|
|
781 decode_hash_table_rehash_threshold (rehash_threshold),
|
|
782 decode_hash_table_weakness (weakness));
|
|
783
|
|
784 /* I'm not sure whether this can GC, but better safe than sorry. */
|
|
785 {
|
|
786 struct gcpro gcpro1;
|
|
787 GCPRO1 (hash_table);
|
|
788
|
|
789 /* And fill it with data. */
|
|
790 while (!NILP (data))
|
|
791 {
|
|
792 Lisp_Object key, value;
|
|
793 key = XCAR (data); data = XCDR (data);
|
|
794 value = XCAR (data); data = XCDR (data);
|
|
795 Fputhash (key, value, hash_table);
|
|
796 }
|
|
797 UNGCPRO;
|
|
798 }
|
|
799
|
|
800 return hash_table;
|
|
801 }
|
|
802
|
|
803 static void
|
|
804 structure_type_create_hash_table_structure_name (Lisp_Object structure_name)
|
|
805 {
|
|
806 struct structure_type *st;
|
|
807
|
|
808 st = define_structure_type (structure_name, 0, hash_table_instantiate);
|
|
809 define_structure_type_keyword (st, Qtest, hash_table_test_validate);
|
|
810 define_structure_type_keyword (st, Qsize, hash_table_size_validate);
|
|
811 define_structure_type_keyword (st, Qrehash_size, hash_table_rehash_size_validate);
|
|
812 define_structure_type_keyword (st, Qrehash_threshold, hash_table_rehash_threshold_validate);
|
|
813 define_structure_type_keyword (st, Qweakness, hash_table_weakness_validate);
|
|
814 define_structure_type_keyword (st, Qdata, hash_table_data_validate);
|
|
815
|
|
816 /* obsolete as of 19990901 in xemacs-21.2 */
|
|
817 define_structure_type_keyword (st, Qtype, hash_table_weakness_validate);
|
|
818 }
|
|
819
|
|
820 /* Create a built-in Lisp structure type named `hash-table'.
|
|
821 We make #s(hashtable ...) equivalent to #s(hash-table ...),
|
|
822 for backward compatibility.
|
|
823 This is called from emacs.c. */
|
|
824 void
|
|
825 structure_type_create_hash_table (void)
|
|
826 {
|
|
827 structure_type_create_hash_table_structure_name (Qhash_table);
|
|
828 structure_type_create_hash_table_structure_name (Qhashtable); /* compat */
|
|
829 }
|
|
830
|
|
831
|
|
832 /************************************************************************/
|
|
833 /* Definition of Lisp-visible methods */
|
|
834 /************************************************************************/
|
|
835
|
|
836 DEFUN ("hash-table-p", Fhash_table_p, 1, 1, 0, /*
|
|
837 Return t if OBJECT is a hash table, else nil.
|
|
838 */
|
|
839 (object))
|
|
840 {
|
|
841 return HASH_TABLEP (object) ? Qt : Qnil;
|
|
842 }
|
|
843
|
|
844 DEFUN ("make-hash-table", Fmake_hash_table, 0, MANY, 0, /*
|
|
845 Return a new empty hash table object.
|
|
846 Use Common Lisp style keywords to specify hash table properties.
|
|
847 (make-hash-table &key test size rehash-size rehash-threshold weakness)
|
|
848
|
|
849 Keyword :test can be `eq', `eql' (default) or `equal'.
|
|
850 Comparison between keys is done using this function.
|
|
851 If speed is important, consider using `eq'.
|
|
852 When storing strings in the hash table, you will likely need to use `equal'.
|
|
853
|
|
854 Keyword :size specifies the number of keys likely to be inserted.
|
|
855 This number of entries can be inserted without enlarging the hash table.
|
|
856
|
|
857 Keyword :rehash-size must be a float greater than 1.0, and specifies
|
|
858 the factor by which to increase the size of the hash table when enlarging.
|
|
859
|
|
860 Keyword :rehash-threshold must be a float between 0.0 and 1.0,
|
|
861 and specifies the load factor of the hash table which triggers enlarging.
|
|
862
|
442
|
863 Non-standard keyword :weakness can be `nil' (default), `t', `key-and-value',
|
|
864 `key', `value' or `key-or-value'. `t' is an alias for `key-and-value'.
|
428
|
865
|
442
|
866 A key-and-value-weak hash table, also known as a fully-weak or simply
|
|
867 as a weak hash table, is one whose pointers do not count as GC
|
|
868 referents: for any key-value pair in the hash table, if the only
|
|
869 remaining pointer to either the key or the value is in a weak hash
|
|
870 table, then the pair will be removed from the hash table, and the key
|
|
871 and value collected. A non-weak hash table (or any other pointer)
|
|
872 would prevent the object from being collected.
|
428
|
873
|
|
874 A key-weak hash table is similar to a fully-weak hash table except that
|
|
875 a key-value pair will be removed only if the key remains unmarked
|
|
876 outside of weak hash tables. The pair will remain in the hash table if
|
|
877 the key is pointed to by something other than a weak hash table, even
|
|
878 if the value is not.
|
|
879
|
|
880 A value-weak hash table is similar to a fully-weak hash table except
|
|
881 that a key-value pair will be removed only if the value remains
|
|
882 unmarked outside of weak hash tables. The pair will remain in the
|
|
883 hash table if the value is pointed to by something other than a weak
|
|
884 hash table, even if the key is not.
|
442
|
885
|
|
886 A key-or-value-weak hash table is similar to a fully-weak hash table except
|
|
887 that a key-value pair will be removed only if the value and the key remain
|
|
888 unmarked outside of weak hash tables. The pair will remain in the
|
|
889 hash table if the value or key are pointed to by something other than a weak
|
|
890 hash table, even if the other is not.
|
428
|
891 */
|
|
892 (int nargs, Lisp_Object *args))
|
|
893 {
|
|
894 int i = 0;
|
|
895 Lisp_Object test = Qnil;
|
|
896 Lisp_Object size = Qnil;
|
|
897 Lisp_Object rehash_size = Qnil;
|
|
898 Lisp_Object rehash_threshold = Qnil;
|
|
899 Lisp_Object weakness = Qnil;
|
|
900
|
|
901 while (i + 1 < nargs)
|
|
902 {
|
|
903 Lisp_Object keyword = args[i++];
|
|
904 Lisp_Object value = args[i++];
|
|
905
|
|
906 if (EQ (keyword, Q_test)) test = value;
|
|
907 else if (EQ (keyword, Q_size)) size = value;
|
|
908 else if (EQ (keyword, Q_rehash_size)) rehash_size = value;
|
|
909 else if (EQ (keyword, Q_rehash_threshold)) rehash_threshold = value;
|
|
910 else if (EQ (keyword, Q_weakness)) weakness = value;
|
|
911 else if (EQ (keyword, Q_type))/*obsolete*/ weakness = value;
|
|
912 else signal_simple_error ("Invalid hash table property keyword", keyword);
|
|
913 }
|
|
914
|
|
915 if (i < nargs)
|
|
916 signal_simple_error ("Hash table property requires a value", args[i]);
|
|
917
|
|
918 #define VALIDATE_VAR(var) \
|
|
919 if (!NILP (var)) hash_table_##var##_validate (Q##var, var, ERROR_ME);
|
|
920
|
|
921 VALIDATE_VAR (test);
|
|
922 VALIDATE_VAR (size);
|
|
923 VALIDATE_VAR (rehash_size);
|
|
924 VALIDATE_VAR (rehash_threshold);
|
|
925 VALIDATE_VAR (weakness);
|
|
926
|
450
|
927 return make_standard_lisp_hash_table
|
428
|
928 (decode_hash_table_test (test),
|
|
929 decode_hash_table_size (size),
|
|
930 decode_hash_table_rehash_size (rehash_size),
|
|
931 decode_hash_table_rehash_threshold (rehash_threshold),
|
|
932 decode_hash_table_weakness (weakness));
|
|
933 }
|
|
934
|
|
935 DEFUN ("copy-hash-table", Fcopy_hash_table, 1, 1, 0, /*
|
|
936 Return a new hash table containing the same keys and values as HASH-TABLE.
|
|
937 The keys and values will not themselves be copied.
|
|
938 */
|
|
939 (hash_table))
|
|
940 {
|
442
|
941 const Lisp_Hash_Table *ht_old = xhash_table (hash_table);
|
428
|
942 Lisp_Hash_Table *ht = alloc_lcrecord_type (Lisp_Hash_Table, &lrecord_hash_table);
|
|
943
|
|
944 copy_lcrecord (ht, ht_old);
|
|
945
|
|
946 ht->hentries = xnew_array (hentry, ht_old->size + 1);
|
|
947 memcpy (ht->hentries, ht_old->hentries, (ht_old->size + 1) * sizeof (hentry));
|
|
948
|
|
949 XSETHASH_TABLE (hash_table, ht);
|
|
950
|
|
951 if (! EQ (ht->next_weak, Qunbound))
|
|
952 {
|
|
953 ht->next_weak = Vall_weak_hash_tables;
|
|
954 Vall_weak_hash_tables = hash_table;
|
|
955 }
|
|
956
|
|
957 return hash_table;
|
|
958 }
|
|
959
|
|
960 static void
|
|
961 resize_hash_table (Lisp_Hash_Table *ht, size_t new_size)
|
|
962 {
|
440
|
963 hentry *old_entries, *new_entries, *sentinel, *e;
|
428
|
964 size_t old_size;
|
|
965
|
|
966 old_size = ht->size;
|
|
967 ht->size = new_size;
|
|
968
|
|
969 old_entries = ht->hentries;
|
|
970
|
440
|
971 ht->hentries = xnew_array_and_zero (hentry, new_size + 1);
|
428
|
972 new_entries = ht->hentries;
|
|
973
|
|
974 compute_hash_table_derived_values (ht);
|
|
975
|
440
|
976 for (e = old_entries, sentinel = e + old_size; e < sentinel; e++)
|
428
|
977 if (!HENTRY_CLEAR_P (e))
|
|
978 {
|
|
979 hentry *probe = new_entries + HASH_CODE (e->key, ht);
|
|
980 LINEAR_PROBING_LOOP (probe, new_entries, new_size)
|
|
981 ;
|
|
982 *probe = *e;
|
|
983 }
|
|
984
|
489
|
985 free_hentries (old_entries, old_size);
|
428
|
986 }
|
|
987
|
440
|
988 /* After a hash table has been saved to disk and later restored by the
|
|
989 portable dumper, it contains the same objects, but their addresses
|
|
990 and thus their HASH_CODEs have changed. */
|
428
|
991 void
|
440
|
992 pdump_reorganize_hash_table (Lisp_Object hash_table)
|
428
|
993 {
|
442
|
994 const Lisp_Hash_Table *ht = xhash_table (hash_table);
|
440
|
995 hentry *new_entries = xnew_array_and_zero (hentry, ht->size + 1);
|
|
996 hentry *e, *sentinel;
|
|
997
|
|
998 for (e = ht->hentries, sentinel = e + ht->size; e < sentinel; e++)
|
|
999 if (!HENTRY_CLEAR_P (e))
|
|
1000 {
|
|
1001 hentry *probe = new_entries + HASH_CODE (e->key, ht);
|
|
1002 LINEAR_PROBING_LOOP (probe, new_entries, ht->size)
|
|
1003 ;
|
|
1004 *probe = *e;
|
|
1005 }
|
|
1006
|
|
1007 memcpy (ht->hentries, new_entries, ht->size * sizeof (hentry));
|
|
1008
|
|
1009 xfree (new_entries);
|
428
|
1010 }
|
|
1011
|
|
1012 static void
|
|
1013 enlarge_hash_table (Lisp_Hash_Table *ht)
|
|
1014 {
|
432
|
1015 size_t new_size =
|
428
|
1016 hash_table_size ((size_t) ((double) ht->size * ht->rehash_size));
|
|
1017 resize_hash_table (ht, new_size);
|
|
1018 }
|
|
1019
|
|
1020 static hentry *
|
442
|
1021 find_hentry (Lisp_Object key, const Lisp_Hash_Table *ht)
|
428
|
1022 {
|
|
1023 hash_table_test_function_t test_function = ht->test_function;
|
|
1024 hentry *entries = ht->hentries;
|
|
1025 hentry *probe = entries + HASH_CODE (key, ht);
|
|
1026
|
|
1027 LINEAR_PROBING_LOOP (probe, entries, ht->size)
|
|
1028 if (KEYS_EQUAL_P (probe->key, key, test_function))
|
|
1029 break;
|
|
1030
|
|
1031 return probe;
|
|
1032 }
|
|
1033
|
|
1034 DEFUN ("gethash", Fgethash, 2, 3, 0, /*
|
|
1035 Find hash value for KEY in HASH-TABLE.
|
|
1036 If there is no corresponding value, return DEFAULT (which defaults to nil).
|
|
1037 */
|
|
1038 (key, hash_table, default_))
|
|
1039 {
|
442
|
1040 const Lisp_Hash_Table *ht = xhash_table (hash_table);
|
428
|
1041 hentry *e = find_hentry (key, ht);
|
|
1042
|
|
1043 return HENTRY_CLEAR_P (e) ? default_ : e->value;
|
|
1044 }
|
|
1045
|
|
1046 DEFUN ("puthash", Fputhash, 3, 3, 0, /*
|
|
1047 Hash KEY to VALUE in HASH-TABLE.
|
|
1048 */
|
|
1049 (key, value, hash_table))
|
|
1050 {
|
|
1051 Lisp_Hash_Table *ht = xhash_table (hash_table);
|
|
1052 hentry *e = find_hentry (key, ht);
|
|
1053
|
|
1054 if (!HENTRY_CLEAR_P (e))
|
|
1055 return e->value = value;
|
|
1056
|
|
1057 e->key = key;
|
|
1058 e->value = value;
|
|
1059
|
|
1060 if (++ht->count >= ht->rehash_count)
|
|
1061 enlarge_hash_table (ht);
|
|
1062
|
|
1063 return value;
|
|
1064 }
|
|
1065
|
|
1066 /* Remove hentry pointed at by PROBE.
|
|
1067 Subsequent entries are removed and reinserted.
|
|
1068 We don't use tombstones - too wasteful. */
|
|
1069 static void
|
|
1070 remhash_1 (Lisp_Hash_Table *ht, hentry *entries, hentry *probe)
|
|
1071 {
|
|
1072 size_t size = ht->size;
|
|
1073 CLEAR_HENTRY (probe);
|
|
1074 probe++;
|
|
1075 ht->count--;
|
|
1076
|
|
1077 LINEAR_PROBING_LOOP (probe, entries, size)
|
|
1078 {
|
|
1079 Lisp_Object key = probe->key;
|
|
1080 hentry *probe2 = entries + HASH_CODE (key, ht);
|
|
1081 LINEAR_PROBING_LOOP (probe2, entries, size)
|
|
1082 if (EQ (probe2->key, key))
|
|
1083 /* hentry at probe doesn't need to move. */
|
|
1084 goto continue_outer_loop;
|
|
1085 /* Move hentry from probe to new home at probe2. */
|
|
1086 *probe2 = *probe;
|
|
1087 CLEAR_HENTRY (probe);
|
|
1088 continue_outer_loop: continue;
|
|
1089 }
|
|
1090 }
|
|
1091
|
|
1092 DEFUN ("remhash", Fremhash, 2, 2, 0, /*
|
|
1093 Remove the entry for KEY from HASH-TABLE.
|
|
1094 Do nothing if there is no entry for KEY in HASH-TABLE.
|
|
1095 */
|
|
1096 (key, hash_table))
|
|
1097 {
|
|
1098 Lisp_Hash_Table *ht = xhash_table (hash_table);
|
|
1099 hentry *e = find_hentry (key, ht);
|
|
1100
|
|
1101 if (HENTRY_CLEAR_P (e))
|
|
1102 return Qnil;
|
|
1103
|
|
1104 remhash_1 (ht, ht->hentries, e);
|
|
1105 return Qt;
|
|
1106 }
|
|
1107
|
|
1108 DEFUN ("clrhash", Fclrhash, 1, 1, 0, /*
|
|
1109 Remove all entries from HASH-TABLE, leaving it empty.
|
|
1110 */
|
|
1111 (hash_table))
|
|
1112 {
|
|
1113 Lisp_Hash_Table *ht = xhash_table (hash_table);
|
|
1114 hentry *e, *sentinel;
|
|
1115
|
|
1116 for (e = ht->hentries, sentinel = e + ht->size; e < sentinel; e++)
|
|
1117 CLEAR_HENTRY (e);
|
|
1118 ht->count = 0;
|
|
1119
|
|
1120 return hash_table;
|
|
1121 }
|
|
1122
|
|
1123 /************************************************************************/
|
|
1124 /* Accessor Functions */
|
|
1125 /************************************************************************/
|
|
1126
|
|
1127 DEFUN ("hash-table-count", Fhash_table_count, 1, 1, 0, /*
|
|
1128 Return the number of entries in HASH-TABLE.
|
|
1129 */
|
|
1130 (hash_table))
|
|
1131 {
|
|
1132 return make_int (xhash_table (hash_table)->count);
|
|
1133 }
|
|
1134
|
|
1135 DEFUN ("hash-table-test", Fhash_table_test, 1, 1, 0, /*
|
|
1136 Return the test function of HASH-TABLE.
|
|
1137 This can be one of `eq', `eql' or `equal'.
|
|
1138 */
|
|
1139 (hash_table))
|
|
1140 {
|
|
1141 hash_table_test_function_t fun = xhash_table (hash_table)->test_function;
|
|
1142
|
|
1143 return (fun == lisp_object_eql_equal ? Qeql :
|
|
1144 fun == lisp_object_equal_equal ? Qequal :
|
|
1145 Qeq);
|
|
1146 }
|
|
1147
|
|
1148 DEFUN ("hash-table-size", Fhash_table_size, 1, 1, 0, /*
|
|
1149 Return the size of HASH-TABLE.
|
|
1150 This is the current number of slots in HASH-TABLE, whether occupied or not.
|
|
1151 */
|
|
1152 (hash_table))
|
|
1153 {
|
|
1154 return make_int (xhash_table (hash_table)->size);
|
|
1155 }
|
|
1156
|
|
1157 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size, 1, 1, 0, /*
|
|
1158 Return the current rehash size of HASH-TABLE.
|
|
1159 This is a float greater than 1.0; the factor by which HASH-TABLE
|
|
1160 is enlarged when the rehash threshold is exceeded.
|
|
1161 */
|
|
1162 (hash_table))
|
|
1163 {
|
|
1164 return make_float (xhash_table (hash_table)->rehash_size);
|
|
1165 }
|
|
1166
|
|
1167 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold, 1, 1, 0, /*
|
|
1168 Return the current rehash threshold of HASH-TABLE.
|
|
1169 This is a float between 0.0 and 1.0; the maximum `load factor' of HASH-TABLE,
|
|
1170 beyond which the HASH-TABLE is enlarged by rehashing.
|
|
1171 */
|
|
1172 (hash_table))
|
|
1173 {
|
438
|
1174 return make_float (xhash_table (hash_table)->rehash_threshold);
|
428
|
1175 }
|
|
1176
|
|
1177 DEFUN ("hash-table-weakness", Fhash_table_weakness, 1, 1, 0, /*
|
|
1178 Return the weakness of HASH-TABLE.
|
442
|
1179 This can be one of `nil', `key-and-value', `key-or-value', `key' or `value'.
|
428
|
1180 */
|
|
1181 (hash_table))
|
|
1182 {
|
|
1183 switch (xhash_table (hash_table)->weakness)
|
|
1184 {
|
442
|
1185 case HASH_TABLE_WEAK: return Qkey_and_value;
|
|
1186 case HASH_TABLE_KEY_WEAK: return Qkey;
|
|
1187 case HASH_TABLE_KEY_VALUE_WEAK: return Qkey_or_value;
|
|
1188 case HASH_TABLE_VALUE_WEAK: return Qvalue;
|
|
1189 default: return Qnil;
|
428
|
1190 }
|
|
1191 }
|
|
1192
|
|
1193 /* obsolete as of 19990901 in xemacs-21.2 */
|
|
1194 DEFUN ("hash-table-type", Fhash_table_type, 1, 1, 0, /*
|
|
1195 Return the type of HASH-TABLE.
|
|
1196 This can be one of `non-weak', `weak', `key-weak' or `value-weak'.
|
|
1197 */
|
|
1198 (hash_table))
|
|
1199 {
|
|
1200 switch (xhash_table (hash_table)->weakness)
|
|
1201 {
|
442
|
1202 case HASH_TABLE_WEAK: return Qweak;
|
|
1203 case HASH_TABLE_KEY_WEAK: return Qkey_weak;
|
|
1204 case HASH_TABLE_KEY_VALUE_WEAK: return Qkey_or_value_weak;
|
|
1205 case HASH_TABLE_VALUE_WEAK: return Qvalue_weak;
|
|
1206 default: return Qnon_weak;
|
428
|
1207 }
|
|
1208 }
|
|
1209
|
|
1210 /************************************************************************/
|
|
1211 /* Mapping Functions */
|
|
1212 /************************************************************************/
|
489
|
1213
|
|
1214 /* We need to be careful when mapping over hash tables because the
|
|
1215 hash table might be modified during the mapping operation:
|
|
1216 - by the mapping function
|
|
1217 - by gc (if the hash table is weak)
|
|
1218
|
|
1219 So we make a copy of the hentries at the beginning of the mapping
|
497
|
1220 operation, and iterate over the copy. Naturally, this is
|
|
1221 expensive, but not as expensive as you might think, because no
|
|
1222 actual memory has to be collected by our notoriously inefficient
|
|
1223 GC; we use an unwind-protect instead to free the memory directly.
|
|
1224
|
|
1225 We could avoid the copying by having the hash table modifiers
|
|
1226 puthash and remhash check for currently active mapping functions.
|
|
1227 Disadvantages: it's hard to get right, and IMO hash mapping
|
|
1228 functions are basically rare, and no extra space in the hash table
|
|
1229 object and no extra cpu in puthash or remhash should be wasted to
|
|
1230 make maphash 3% faster. From a design point of view, the basic
|
|
1231 functions gethash, puthash and remhash should be implementable
|
|
1232 without having to think about maphash.
|
|
1233
|
|
1234 Note: We don't (yet) have Common Lisp's with-hash-table-iterator.
|
|
1235 If you implement this naively, you cannot have more than one
|
|
1236 concurrently active iterator over the same hash table. The `each'
|
|
1237 function in perl has this limitation.
|
|
1238
|
|
1239 Note: We GCPRO memory on the heap, not on the stack. There is no
|
|
1240 obvious reason why this is bad, but as of this writing this is the
|
|
1241 only known occurrence of this technique in the code.
|
504
|
1242
|
|
1243 -- Martin
|
|
1244 */
|
|
1245
|
|
1246 /* Ben disagrees with the "copying hentries" design, and says:
|
|
1247
|
|
1248 Another solution is the same as I've already proposed -- when
|
|
1249 mapping, mark the table as "change-unsafe", and in this case, use a
|
|
1250 secondary table to maintain changes. this could be basically a
|
|
1251 standard hash table, but with entries only for added or deleted
|
|
1252 entries in the primary table, and a marker like Qunbound to
|
|
1253 indicate a deleted entry. puthash, gethash and remhash need a
|
|
1254 single extra check for this secondary table -- totally
|
|
1255 insignificant speedwise. if you really cared about making
|
|
1256 recursive maphashes completely correct, you'd have to do a bit of
|
|
1257 extra work here -- when maphashing, if the secondary table exists,
|
|
1258 make a copy of it, and use the copy in conjunction with the primary
|
|
1259 table when mapping. the advantages of this are
|
|
1260
|
|
1261 [a] easy to demonstrate correct, even with weak hashtables.
|
|
1262
|
|
1263 [b] no extra overhead in the general maphash case -- only when you
|
|
1264 modify the table while maphashing, and even then the overhead is
|
|
1265 very small.
|
497
|
1266 */
|
|
1267
|
489
|
1268 static Lisp_Object
|
|
1269 maphash_unwind (Lisp_Object unwind_obj)
|
|
1270 {
|
|
1271 void *ptr = (void *) get_opaque_ptr (unwind_obj);
|
|
1272 xfree (ptr);
|
|
1273 free_opaque_ptr (unwind_obj);
|
|
1274 return Qnil;
|
|
1275 }
|
|
1276
|
|
1277 /* Return a malloced array of alternating key/value pairs from HT. */
|
|
1278 static Lisp_Object *
|
|
1279 copy_compress_hentries (const Lisp_Hash_Table *ht)
|
|
1280 {
|
|
1281 Lisp_Object * const objs =
|
|
1282 /* If the hash table is empty, ht->count could be 0. */
|
|
1283 xnew_array (Lisp_Object, 2 * (ht->count > 0 ? ht->count : 1));
|
|
1284 const hentry *e, *sentinel;
|
|
1285 Lisp_Object *pobj;
|
|
1286
|
|
1287 for (e = ht->hentries, sentinel = e + ht->size, pobj = objs; e < sentinel; e++)
|
|
1288 if (!HENTRY_CLEAR_P (e))
|
|
1289 {
|
|
1290 *(pobj++) = e->key;
|
|
1291 *(pobj++) = e->value;
|
|
1292 }
|
|
1293
|
|
1294 type_checking_assert (pobj == objs + 2 * ht->count);
|
|
1295
|
|
1296 return objs;
|
|
1297 }
|
|
1298
|
428
|
1299 DEFUN ("maphash", Fmaphash, 2, 2, 0, /*
|
|
1300 Map FUNCTION over entries in HASH-TABLE, calling it with two args,
|
|
1301 each key and value in HASH-TABLE.
|
|
1302
|
489
|
1303 FUNCTION must not modify HASH-TABLE, with the one exception that FUNCTION
|
428
|
1304 may remhash or puthash the entry currently being processed by FUNCTION.
|
|
1305 */
|
|
1306 (function, hash_table))
|
|
1307 {
|
489
|
1308 const Lisp_Hash_Table * const ht = xhash_table (hash_table);
|
|
1309 Lisp_Object * const objs = copy_compress_hentries (ht);
|
|
1310 Lisp_Object args[3];
|
|
1311 const Lisp_Object *pobj, *end;
|
|
1312 int speccount = specpdl_depth ();
|
|
1313 struct gcpro gcpro1;
|
|
1314
|
|
1315 record_unwind_protect (maphash_unwind, make_opaque_ptr ((void *)objs));
|
|
1316 GCPRO1 (objs[0]);
|
|
1317 gcpro1.nvars = 2 * ht->count;
|
428
|
1318
|
489
|
1319 args[0] = function;
|
|
1320
|
|
1321 for (pobj = objs, end = pobj + 2 * ht->count; pobj < end; pobj += 2)
|
|
1322 {
|
|
1323 args[1] = pobj[0];
|
|
1324 args[2] = pobj[1];
|
|
1325 Ffuncall (countof (args), args);
|
|
1326 }
|
|
1327
|
|
1328 unbind_to (speccount, Qnil);
|
|
1329 UNGCPRO;
|
428
|
1330
|
|
1331 return Qnil;
|
|
1332 }
|
|
1333
|
489
|
1334 /* Map *C* function FUNCTION over the elements of a non-weak lisp hash table.
|
|
1335 FUNCTION must not modify HASH-TABLE, with the one exception that FUNCTION
|
|
1336 may puthash the entry currently being processed by FUNCTION.
|
|
1337 Mapping terminates if FUNCTION returns something other than 0. */
|
428
|
1338 void
|
489
|
1339 elisp_maphash_unsafe (maphash_function_t function,
|
428
|
1340 Lisp_Object hash_table, void *extra_arg)
|
|
1341 {
|
442
|
1342 const Lisp_Hash_Table *ht = XHASH_TABLE (hash_table);
|
|
1343 const hentry *e, *sentinel;
|
428
|
1344
|
|
1345 for (e = ht->hentries, sentinel = e + ht->size; e < sentinel; e++)
|
|
1346 if (!HENTRY_CLEAR_P (e))
|
489
|
1347 if (function (e->key, e->value, extra_arg))
|
|
1348 return;
|
428
|
1349 }
|
|
1350
|
489
|
1351 /* Map *C* function FUNCTION over the elements of a lisp hash table.
|
|
1352 It is safe for FUNCTION to modify HASH-TABLE.
|
|
1353 Mapping terminates if FUNCTION returns something other than 0. */
|
|
1354 void
|
|
1355 elisp_maphash (maphash_function_t function,
|
|
1356 Lisp_Object hash_table, void *extra_arg)
|
|
1357 {
|
|
1358 const Lisp_Hash_Table * const ht = xhash_table (hash_table);
|
|
1359 Lisp_Object * const objs = copy_compress_hentries (ht);
|
|
1360 const Lisp_Object *pobj, *end;
|
|
1361 int speccount = specpdl_depth ();
|
|
1362 struct gcpro gcpro1;
|
|
1363
|
|
1364 record_unwind_protect (maphash_unwind, make_opaque_ptr ((void *)objs));
|
|
1365 GCPRO1 (objs[0]);
|
|
1366 gcpro1.nvars = 2 * ht->count;
|
|
1367
|
|
1368 for (pobj = objs, end = pobj + 2 * ht->count; pobj < end; pobj += 2)
|
|
1369 if (function (pobj[0], pobj[1], extra_arg))
|
|
1370 break;
|
|
1371
|
|
1372 unbind_to (speccount, Qnil);
|
|
1373 UNGCPRO;
|
|
1374 }
|
|
1375
|
|
1376 /* Remove all elements of a lisp hash table satisfying *C* predicate PREDICATE.
|
|
1377 PREDICATE must not modify HASH-TABLE. */
|
428
|
1378 void
|
|
1379 elisp_map_remhash (maphash_function_t predicate,
|
|
1380 Lisp_Object hash_table, void *extra_arg)
|
|
1381 {
|
489
|
1382 const Lisp_Hash_Table * const ht = xhash_table (hash_table);
|
|
1383 Lisp_Object * const objs = copy_compress_hentries (ht);
|
|
1384 const Lisp_Object *pobj, *end;
|
|
1385 int speccount = specpdl_depth ();
|
|
1386 struct gcpro gcpro1;
|
428
|
1387
|
489
|
1388 record_unwind_protect (maphash_unwind, make_opaque_ptr ((void *)objs));
|
|
1389 GCPRO1 (objs[0]);
|
|
1390 gcpro1.nvars = 2 * ht->count;
|
|
1391
|
|
1392 for (pobj = objs, end = pobj + 2 * ht->count; pobj < end; pobj += 2)
|
|
1393 if (predicate (pobj[0], pobj[1], extra_arg))
|
|
1394 Fremhash (pobj[0], hash_table);
|
|
1395
|
|
1396 unbind_to (speccount, Qnil);
|
|
1397 UNGCPRO;
|
428
|
1398 }
|
|
1399
|
|
1400
|
|
1401 /************************************************************************/
|
|
1402 /* garbage collecting weak hash tables */
|
|
1403 /************************************************************************/
|
442
|
1404 #define MARK_OBJ(obj) do { \
|
|
1405 Lisp_Object mo_obj = (obj); \
|
|
1406 if (!marked_p (mo_obj)) \
|
|
1407 { \
|
|
1408 mark_object (mo_obj); \
|
|
1409 did_mark = 1; \
|
|
1410 } \
|
|
1411 } while (0)
|
|
1412
|
428
|
1413
|
|
1414 /* Complete the marking for semi-weak hash tables. */
|
|
1415 int
|
|
1416 finish_marking_weak_hash_tables (void)
|
|
1417 {
|
|
1418 Lisp_Object hash_table;
|
|
1419 int did_mark = 0;
|
|
1420
|
|
1421 for (hash_table = Vall_weak_hash_tables;
|
|
1422 !NILP (hash_table);
|
|
1423 hash_table = XHASH_TABLE (hash_table)->next_weak)
|
|
1424 {
|
442
|
1425 const Lisp_Hash_Table *ht = XHASH_TABLE (hash_table);
|
|
1426 const hentry *e = ht->hentries;
|
|
1427 const hentry *sentinel = e + ht->size;
|
428
|
1428
|
|
1429 if (! marked_p (hash_table))
|
|
1430 /* The hash table is probably garbage. Ignore it. */
|
|
1431 continue;
|
|
1432
|
|
1433 /* Now, scan over all the pairs. For all pairs that are
|
|
1434 half-marked, we may need to mark the other half if we're
|
|
1435 keeping this pair. */
|
|
1436 switch (ht->weakness)
|
|
1437 {
|
|
1438 case HASH_TABLE_KEY_WEAK:
|
|
1439 for (; e < sentinel; e++)
|
|
1440 if (!HENTRY_CLEAR_P (e))
|
|
1441 if (marked_p (e->key))
|
|
1442 MARK_OBJ (e->value);
|
|
1443 break;
|
|
1444
|
|
1445 case HASH_TABLE_VALUE_WEAK:
|
|
1446 for (; e < sentinel; e++)
|
|
1447 if (!HENTRY_CLEAR_P (e))
|
|
1448 if (marked_p (e->value))
|
|
1449 MARK_OBJ (e->key);
|
|
1450 break;
|
|
1451
|
442
|
1452 case HASH_TABLE_KEY_VALUE_WEAK:
|
|
1453 for (; e < sentinel; e++)
|
|
1454 if (!HENTRY_CLEAR_P (e))
|
|
1455 {
|
|
1456 if (marked_p (e->value))
|
|
1457 MARK_OBJ (e->key);
|
|
1458 else if (marked_p (e->key))
|
|
1459 MARK_OBJ (e->value);
|
|
1460 }
|
|
1461 break;
|
|
1462
|
428
|
1463 case HASH_TABLE_KEY_CAR_WEAK:
|
|
1464 for (; e < sentinel; e++)
|
|
1465 if (!HENTRY_CLEAR_P (e))
|
|
1466 if (!CONSP (e->key) || marked_p (XCAR (e->key)))
|
|
1467 {
|
|
1468 MARK_OBJ (e->key);
|
|
1469 MARK_OBJ (e->value);
|
|
1470 }
|
|
1471 break;
|
|
1472
|
450
|
1473 /* We seem to be sprouting new weakness types at an alarming
|
|
1474 rate. At least this is not externally visible - and in
|
|
1475 fact all of these KEY_CAR_* types are only used by the
|
|
1476 glyph code. */
|
|
1477 case HASH_TABLE_KEY_CAR_VALUE_WEAK:
|
|
1478 for (; e < sentinel; e++)
|
|
1479 if (!HENTRY_CLEAR_P (e))
|
|
1480 {
|
|
1481 if (!CONSP (e->key) || marked_p (XCAR (e->key)))
|
|
1482 {
|
|
1483 MARK_OBJ (e->key);
|
|
1484 MARK_OBJ (e->value);
|
|
1485 }
|
|
1486 else if (marked_p (e->value))
|
|
1487 MARK_OBJ (e->key);
|
|
1488 }
|
|
1489 break;
|
|
1490
|
428
|
1491 case HASH_TABLE_VALUE_CAR_WEAK:
|
|
1492 for (; e < sentinel; e++)
|
|
1493 if (!HENTRY_CLEAR_P (e))
|
|
1494 if (!CONSP (e->value) || marked_p (XCAR (e->value)))
|
|
1495 {
|
|
1496 MARK_OBJ (e->key);
|
|
1497 MARK_OBJ (e->value);
|
|
1498 }
|
|
1499 break;
|
|
1500
|
|
1501 default:
|
|
1502 break;
|
|
1503 }
|
|
1504 }
|
|
1505
|
|
1506 return did_mark;
|
|
1507 }
|
|
1508
|
|
1509 void
|
|
1510 prune_weak_hash_tables (void)
|
|
1511 {
|
|
1512 Lisp_Object hash_table, prev = Qnil;
|
|
1513 for (hash_table = Vall_weak_hash_tables;
|
|
1514 !NILP (hash_table);
|
|
1515 hash_table = XHASH_TABLE (hash_table)->next_weak)
|
|
1516 {
|
|
1517 if (! marked_p (hash_table))
|
|
1518 {
|
|
1519 /* This hash table itself is garbage. Remove it from the list. */
|
|
1520 if (NILP (prev))
|
|
1521 Vall_weak_hash_tables = XHASH_TABLE (hash_table)->next_weak;
|
|
1522 else
|
|
1523 XHASH_TABLE (prev)->next_weak = XHASH_TABLE (hash_table)->next_weak;
|
|
1524 }
|
|
1525 else
|
|
1526 {
|
|
1527 /* Now, scan over all the pairs. Remove all of the pairs
|
|
1528 in which the key or value, or both, is unmarked
|
|
1529 (depending on the weakness of the hash table). */
|
|
1530 Lisp_Hash_Table *ht = XHASH_TABLE (hash_table);
|
|
1531 hentry *entries = ht->hentries;
|
|
1532 hentry *sentinel = entries + ht->size;
|
|
1533 hentry *e;
|
|
1534
|
|
1535 for (e = entries; e < sentinel; e++)
|
|
1536 if (!HENTRY_CLEAR_P (e))
|
|
1537 {
|
|
1538 again:
|
|
1539 if (!marked_p (e->key) || !marked_p (e->value))
|
|
1540 {
|
|
1541 remhash_1 (ht, entries, e);
|
|
1542 if (!HENTRY_CLEAR_P (e))
|
|
1543 goto again;
|
|
1544 }
|
|
1545 }
|
|
1546
|
|
1547 prev = hash_table;
|
|
1548 }
|
|
1549 }
|
|
1550 }
|
|
1551
|
|
1552 /* Return a hash value for an array of Lisp_Objects of size SIZE. */
|
|
1553
|
|
1554 hashcode_t
|
|
1555 internal_array_hash (Lisp_Object *arr, int size, int depth)
|
|
1556 {
|
|
1557 int i;
|
442
|
1558 hashcode_t hash = 0;
|
|
1559 depth++;
|
428
|
1560
|
|
1561 if (size <= 5)
|
|
1562 {
|
|
1563 for (i = 0; i < size; i++)
|
442
|
1564 hash = HASH2 (hash, internal_hash (arr[i], depth));
|
428
|
1565 return hash;
|
|
1566 }
|
|
1567
|
|
1568 /* just pick five elements scattered throughout the array.
|
|
1569 A slightly better approach would be to offset by some
|
|
1570 noise factor from the points chosen below. */
|
|
1571 for (i = 0; i < 5; i++)
|
442
|
1572 hash = HASH2 (hash, internal_hash (arr[i*size/5], depth));
|
428
|
1573
|
|
1574 return hash;
|
|
1575 }
|
|
1576
|
|
1577 /* Return a hash value for a Lisp_Object. This is for use when hashing
|
|
1578 objects with the comparison being `equal' (for `eq', you can just
|
|
1579 use the Lisp_Object itself as the hash value). You need to make a
|
|
1580 tradeoff between the speed of the hash function and how good the
|
|
1581 hashing is. In particular, the hash function needs to be FAST,
|
|
1582 so you can't just traipse down the whole tree hashing everything
|
|
1583 together. Most of the time, objects will differ in the first
|
|
1584 few elements you hash. Thus, we only go to a short depth (5)
|
|
1585 and only hash at most 5 elements out of a vector. Theoretically
|
|
1586 we could still take 5^5 time (a big big number) to compute a
|
|
1587 hash, but practically this won't ever happen. */
|
|
1588
|
|
1589 hashcode_t
|
|
1590 internal_hash (Lisp_Object obj, int depth)
|
|
1591 {
|
|
1592 if (depth > 5)
|
|
1593 return 0;
|
|
1594 if (CONSP (obj))
|
|
1595 {
|
|
1596 /* no point in worrying about tail recursion, since we're not
|
|
1597 going very deep */
|
|
1598 return HASH2 (internal_hash (XCAR (obj), depth + 1),
|
|
1599 internal_hash (XCDR (obj), depth + 1));
|
|
1600 }
|
|
1601 if (STRINGP (obj))
|
|
1602 {
|
|
1603 return hash_string (XSTRING_DATA (obj), XSTRING_LENGTH (obj));
|
|
1604 }
|
|
1605 if (LRECORDP (obj))
|
|
1606 {
|
442
|
1607 const struct lrecord_implementation
|
428
|
1608 *imp = XRECORD_LHEADER_IMPLEMENTATION (obj);
|
|
1609 if (imp->hash)
|
|
1610 return imp->hash (obj, depth);
|
|
1611 }
|
|
1612
|
|
1613 return LISP_HASH (obj);
|
|
1614 }
|
|
1615
|
|
1616 DEFUN ("sxhash", Fsxhash, 1, 1, 0, /*
|
|
1617 Return a hash value for OBJECT.
|
444
|
1618 \(equal obj1 obj2) implies (= (sxhash obj1) (sxhash obj2)).
|
428
|
1619 */
|
|
1620 (object))
|
|
1621 {
|
|
1622 return make_int (internal_hash (object, 0));
|
|
1623 }
|
|
1624
|
|
1625 #if 0
|
|
1626 xxDEFUN ("internal-hash-value", Finternal_hash_value, 1, 1, 0, /*
|
|
1627 Hash value of OBJECT. For debugging.
|
|
1628 The value is returned as (HIGH . LOW).
|
|
1629 */
|
|
1630 (object))
|
|
1631 {
|
|
1632 /* This function is pretty 32bit-centric. */
|
442
|
1633 hashcode_t hash = internal_hash (object, 0);
|
428
|
1634 return Fcons (hash >> 16, hash & 0xffff);
|
|
1635 }
|
|
1636 #endif
|
|
1637
|
|
1638
|
|
1639 /************************************************************************/
|
|
1640 /* initialization */
|
|
1641 /************************************************************************/
|
|
1642
|
|
1643 void
|
|
1644 syms_of_elhash (void)
|
|
1645 {
|
442
|
1646 INIT_LRECORD_IMPLEMENTATION (hash_table);
|
|
1647
|
428
|
1648 DEFSUBR (Fhash_table_p);
|
|
1649 DEFSUBR (Fmake_hash_table);
|
|
1650 DEFSUBR (Fcopy_hash_table);
|
|
1651 DEFSUBR (Fgethash);
|
|
1652 DEFSUBR (Fremhash);
|
|
1653 DEFSUBR (Fputhash);
|
|
1654 DEFSUBR (Fclrhash);
|
|
1655 DEFSUBR (Fmaphash);
|
|
1656 DEFSUBR (Fhash_table_count);
|
|
1657 DEFSUBR (Fhash_table_test);
|
|
1658 DEFSUBR (Fhash_table_size);
|
|
1659 DEFSUBR (Fhash_table_rehash_size);
|
|
1660 DEFSUBR (Fhash_table_rehash_threshold);
|
|
1661 DEFSUBR (Fhash_table_weakness);
|
|
1662 DEFSUBR (Fhash_table_type); /* obsolete */
|
|
1663 DEFSUBR (Fsxhash);
|
|
1664 #if 0
|
|
1665 DEFSUBR (Finternal_hash_value);
|
|
1666 #endif
|
|
1667
|
|
1668 defsymbol (&Qhash_tablep, "hash-table-p");
|
|
1669 defsymbol (&Qhash_table, "hash-table");
|
|
1670 defsymbol (&Qhashtable, "hashtable");
|
|
1671 defsymbol (&Qweakness, "weakness");
|
|
1672 defsymbol (&Qvalue, "value");
|
442
|
1673 defsymbol (&Qkey_or_value, "key-or-value");
|
|
1674 defsymbol (&Qkey_and_value, "key-and-value");
|
428
|
1675 defsymbol (&Qrehash_size, "rehash-size");
|
|
1676 defsymbol (&Qrehash_threshold, "rehash-threshold");
|
|
1677
|
|
1678 defsymbol (&Qweak, "weak"); /* obsolete */
|
|
1679 defsymbol (&Qkey_weak, "key-weak"); /* obsolete */
|
442
|
1680 defsymbol (&Qkey_or_value_weak, "key-or-value-weak"); /* obsolete */
|
428
|
1681 defsymbol (&Qvalue_weak, "value-weak"); /* obsolete */
|
|
1682 defsymbol (&Qnon_weak, "non-weak"); /* obsolete */
|
|
1683
|
|
1684 defkeyword (&Q_test, ":test");
|
|
1685 defkeyword (&Q_size, ":size");
|
|
1686 defkeyword (&Q_rehash_size, ":rehash-size");
|
|
1687 defkeyword (&Q_rehash_threshold, ":rehash-threshold");
|
|
1688 defkeyword (&Q_weakness, ":weakness");
|
|
1689 defkeyword (&Q_type, ":type"); /* obsolete */
|
|
1690 }
|
|
1691
|
|
1692 void
|
|
1693 vars_of_elhash (void)
|
|
1694 {
|
|
1695 /* This must NOT be staticpro'd */
|
|
1696 Vall_weak_hash_tables = Qnil;
|
452
|
1697 dump_add_weak_object_chain (&Vall_weak_hash_tables);
|
428
|
1698 }
|