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