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