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