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