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