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