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