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.
6 This file is part of XEmacs.
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
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
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. */
23 /* Synched up with: Not in FSF. */
30 Lisp_Object Qhash_tablep;
31 static Lisp_Object Qhashtable, Qhash_table;
32 static Lisp_Object Qweakness, Qvalue;
33 static Lisp_Object Vall_weak_hash_tables;
34 static Lisp_Object Qrehash_size, Qrehash_threshold;
35 static Lisp_Object Q_size, Q_test, Q_weakness, Q_rehash_size, Q_rehash_threshold;
37 /* obsolete as of 19990901 in xemacs-21.2 */
38 static Lisp_Object Qweak, Qkey_weak, Qvalue_weak, Qnon_weak, Q_type;
46 struct Lisp_Hash_Table
48 struct lcrecord_header header;
53 double rehash_threshold;
55 hash_table_hash_function_t hash_function;
56 hash_table_test_function_t test_function;
58 enum hash_table_weakness weakness;
59 Lisp_Object next_weak; /* Used to chain together all of the weak
60 hash tables. Don't mark through this. */
62 typedef struct Lisp_Hash_Table Lisp_Hash_Table;
64 #define HENTRY_CLEAR_P(hentry) ((*(EMACS_UINT*)(&((hentry)->key))) == 0)
65 #define CLEAR_HENTRY(hentry) \
66 ((*(EMACS_UINT*)(&((hentry)->key))) = 0, \
67 (*(EMACS_UINT*)(&((hentry)->value))) = 0)
69 #define HASH_TABLE_DEFAULT_SIZE 16
70 #define HASH_TABLE_DEFAULT_REHASH_SIZE 1.3
71 #define HASH_TABLE_MIN_SIZE 10
73 #define HASH_CODE(key, ht) \
74 ((((ht)->hash_function ? (ht)->hash_function (key) : LISP_HASH (key)) \
75 * (ht)->golden_ratio) \
78 #define KEYS_EQUAL_P(key1, key2, testfun) \
79 (EQ (key1, key2) || ((testfun) && (testfun) (key1, key2)))
81 #define LINEAR_PROBING_LOOP(probe, entries, size) \
83 !HENTRY_CLEAR_P (probe) || \
84 (probe == entries + size ? \
85 (probe = entries, !HENTRY_CLEAR_P (probe)) : 0); \
88 #ifndef ERROR_CHECK_HASH_TABLE
89 # ifdef ERROR_CHECK_TYPECHECK
90 # define ERROR_CHECK_HASH_TABLE 1
92 # define ERROR_CHECK_HASH_TABLE 0
96 #if ERROR_CHECK_HASH_TABLE
98 check_hash_table_invariants (Lisp_Hash_Table *ht)
100 assert (ht->count < ht->size);
101 assert (ht->count <= ht->rehash_count);
102 assert (ht->rehash_count < ht->size);
103 assert ((double) ht->count * ht->rehash_threshold - 1 <= (double) ht->rehash_count);
104 assert (HENTRY_CLEAR_P (ht->hentries + ht->size));
107 #define check_hash_table_invariants(ht)
110 /* We use linear probing instead of double hashing, despite its lack
111 of blessing by Knuth and company, because, as a result of the
112 increasing discrepancy between CPU speeds and memory speeds, cache
113 behavior is becoming increasingly important, e.g:
115 For a trivial loop, the penalty for non-sequential access of an array is:
116 - a factor of 3-4 on Pentium Pro 200 Mhz
117 - a factor of 10 on Ultrasparc 300 Mhz */
119 /* Return a suitable size for a hash table, with at least SIZE slots. */
121 hash_table_size (size_t requested_size)
123 /* Return some prime near, but greater than or equal to, SIZE.
124 Decades from the time of writing, someone will have a system large
125 enough that the list below will be too short... */
126 static CONST size_t primes [] =
128 19, 29, 41, 59, 79, 107, 149, 197, 263, 347, 457, 599, 787, 1031,
129 1361, 1777, 2333, 3037, 3967, 5167, 6719, 8737, 11369, 14783,
130 19219, 24989, 32491, 42257, 54941, 71429, 92861, 120721, 156941,
131 204047, 265271, 344857, 448321, 582821, 757693, 985003, 1280519,
132 1664681, 2164111, 2813353, 3657361, 4754591, 6180989, 8035301,
133 10445899, 13579681, 17653589, 22949669, 29834603, 38784989,
134 50420551, 65546729, 85210757, 110774011, 144006217, 187208107,
135 243370577, 316381771, 411296309, 534685237, 695090819, 903618083,
136 1174703521, 1527114613, 1985248999, 2580823717UL, 3355070839UL
138 /* We've heard of binary search. */
140 for (low = 0, high = countof (primes) - 1; high - low > 1;)
142 /* Loop Invariant: size < primes [high] */
143 int mid = (low + high) / 2;
144 if (primes [mid] < requested_size)
149 return primes [high];
153 #if 0 /* I don't think these are needed any more.
154 If using the general lisp_object_equal_*() functions
155 causes efficiency problems, these can be resurrected. --ben */
156 /* equality and hash functions for Lisp strings */
158 lisp_string_equal (Lisp_Object str1, Lisp_Object str2)
160 /* This is wrong anyway. You can't use strcmp() on Lisp strings,
161 because they can contain zero characters. */
162 return !strcmp ((char *) XSTRING_DATA (str1), (char *) XSTRING_DATA (str2));
166 lisp_string_hash (Lisp_Object obj)
168 return hash_string (XSTRING_DATA (str), XSTRING_LENGTH (str));
174 lisp_object_eql_equal (Lisp_Object obj1, Lisp_Object obj2)
176 return EQ (obj1, obj2) || (FLOATP (obj1) && internal_equal (obj1, obj2, 0));
180 lisp_object_eql_hash (Lisp_Object obj)
182 return FLOATP (obj) ? internal_hash (obj, 0) : LISP_HASH (obj);
186 lisp_object_equal_equal (Lisp_Object obj1, Lisp_Object obj2)
188 return internal_equal (obj1, obj2, 0);
192 lisp_object_equal_hash (Lisp_Object obj)
194 return internal_hash (obj, 0);
199 mark_hash_table (Lisp_Object obj)
201 Lisp_Hash_Table *ht = XHASH_TABLE (obj);
203 /* If the hash table is weak, we don't want to mark the keys and
204 values (we scan over them after everything else has been marked,
205 and mark or remove them as necessary). */
206 if (ht->weakness == HASH_TABLE_NON_WEAK)
208 hentry *e, *sentinel;
210 for (e = ht->hentries, sentinel = e + ht->size; e < sentinel; e++)
211 if (!HENTRY_CLEAR_P (e))
213 mark_object (e->key);
214 mark_object (e->value);
220 /* Equality of hash tables. Two hash tables are equal when they are of
221 the same weakness and test function, they have the same number of
222 elements, and for each key in the hash table, the values are `equal'.
224 This is similar to Common Lisp `equalp' of hash tables, with the
225 difference that CL requires the keys to be compared with the test
226 function, which we don't do. Doing that would require consing, and
227 consing is a bad idea in `equal'. Anyway, our method should provide
228 the same result -- if the keys are not equal according to the test
229 function, then Fgethash() in hash_table_equal_mapper() will fail. */
231 hash_table_equal (Lisp_Object hash_table1, Lisp_Object hash_table2, int depth)
233 Lisp_Hash_Table *ht1 = XHASH_TABLE (hash_table1);
234 Lisp_Hash_Table *ht2 = XHASH_TABLE (hash_table2);
235 hentry *e, *sentinel;
237 if ((ht1->test_function != ht2->test_function) ||
238 (ht1->weakness != ht2->weakness) ||
239 (ht1->count != ht2->count))
244 for (e = ht1->hentries, sentinel = e + ht1->size; e < sentinel; e++)
245 if (!HENTRY_CLEAR_P (e))
246 /* Look up the key in the other hash table, and compare the values. */
248 Lisp_Object value_in_other = Fgethash (e->key, hash_table2, Qunbound);
249 if (UNBOUNDP (value_in_other) ||
250 !internal_equal (e->value, value_in_other, depth))
251 return 0; /* Give up */
257 /* Printing hash tables.
259 This is non-trivial, because we use a readable structure-style
260 syntax for hash tables. This means that a typical hash table will be
261 readably printed in the form of:
263 #s(hash-table size 2 data (key1 value1 key2 value2))
265 The supported hash table structure keywords and their values are:
266 `test' (eql (or nil), eq or equal)
267 `size' (a natnum or nil)
268 `rehash-size' (a float)
269 `rehash-threshold' (a float)
270 `weakness' (nil, t, key or value)
273 If `print-readably' is nil, then a simpler syntax is used, for example
275 #<hash-table size 2/13 data (key1 value1 key2 value2) 0x874d>
277 The data is truncated to four pairs, and the rest is shown with
278 `...'. This printer does not cons. */
281 /* Print the data of the hash table. This maps through a Lisp
282 hash table and prints key/value pairs using PRINTCHARFUN. */
284 print_hash_table_data (Lisp_Hash_Table *ht, Lisp_Object printcharfun)
287 hentry *e, *sentinel;
289 write_c_string (" data (", printcharfun);
291 for (e = ht->hentries, sentinel = e + ht->size; e < sentinel; e++)
292 if (!HENTRY_CLEAR_P (e))
295 write_c_string (" ", printcharfun);
296 if (!print_readably && count > 3)
298 write_c_string ("...", printcharfun);
301 print_internal (e->key, printcharfun, 1);
302 write_c_string (" ", printcharfun);
303 print_internal (e->value, printcharfun, 1);
307 write_c_string (")", printcharfun);
311 print_hash_table (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
313 Lisp_Hash_Table *ht = XHASH_TABLE (obj);
316 write_c_string (print_readably ? "#s(hash-table" : "#<hash-table",
319 /* These checks have a kludgy look to them, but they are safe.
320 Due to nature of hashing, you cannot use arbitrary
321 test functions anyway. */
322 if (!ht->test_function)
323 write_c_string (" test eq", printcharfun);
324 else if (ht->test_function == lisp_object_equal_equal)
325 write_c_string (" test equal", printcharfun);
326 else if (ht->test_function == lisp_object_eql_equal)
331 if (ht->count || !print_readably)
334 sprintf (buf, " size %lu", (unsigned long) ht->count);
336 sprintf (buf, " size %lu/%lu",
337 (unsigned long) ht->count,
338 (unsigned long) ht->size);
339 write_c_string (buf, printcharfun);
342 if (ht->weakness != HASH_TABLE_NON_WEAK)
344 sprintf (buf, " weakness %s",
345 (ht->weakness == HASH_TABLE_WEAK ? "t" :
346 ht->weakness == HASH_TABLE_KEY_WEAK ? "key" :
347 ht->weakness == HASH_TABLE_VALUE_WEAK ? "value" :
348 "you-d-better-not-see-this"));
349 write_c_string (buf, printcharfun);
353 print_hash_table_data (ht, printcharfun);
356 write_c_string (")", printcharfun);
359 sprintf (buf, " 0x%x>", ht->header.uid);
360 write_c_string (buf, printcharfun);
365 finalize_hash_table (void *header, int for_disksave)
369 Lisp_Hash_Table *ht = (Lisp_Hash_Table *) header;
371 xfree (ht->hentries);
376 static const struct lrecord_description hentry_description_1[] = {
377 { XD_LISP_OBJECT, offsetof(hentry, key), 2 },
381 static const struct struct_description hentry_description = {
386 const struct lrecord_description hash_table_description[] = {
387 { XD_SIZE_T, offsetof(Lisp_Hash_Table, size) },
388 { XD_STRUCT_PTR, offsetof(Lisp_Hash_Table, hentries), XD_INDIRECT(0, 1), &hentry_description },
389 { XD_LO_LINK, offsetof(Lisp_Hash_Table, next_weak) },
393 DEFINE_LRECORD_IMPLEMENTATION ("hash-table", hash_table,
394 mark_hash_table, print_hash_table,
396 /* #### Implement hash_table_hash()! */
398 hash_table_description,
401 static Lisp_Hash_Table *
402 xhash_table (Lisp_Object hash_table)
405 CHECK_HASH_TABLE (hash_table);
406 check_hash_table_invariants (XHASH_TABLE (hash_table));
407 return XHASH_TABLE (hash_table);
411 /************************************************************************/
412 /* Creation of Hash Tables */
413 /************************************************************************/
415 /* Creation of hash tables, without error-checking. */
417 compute_hash_table_derived_values (Lisp_Hash_Table *ht)
419 ht->rehash_count = (size_t)
420 ((double) ht->size * ht->rehash_threshold);
421 ht->golden_ratio = (size_t)
422 ((double) ht->size * (.6180339887 / (double) sizeof (Lisp_Object)));
426 make_general_lisp_hash_table (enum hash_table_test test,
429 double rehash_threshold,
430 enum hash_table_weakness weakness)
432 Lisp_Object hash_table;
433 Lisp_Hash_Table *ht = alloc_lcrecord_type (Lisp_Hash_Table, &lrecord_hash_table);
438 ht->test_function = 0;
439 ht->hash_function = 0;
443 ht->test_function = lisp_object_eql_equal;
444 ht->hash_function = lisp_object_eql_hash;
447 case HASH_TABLE_EQUAL:
448 ht->test_function = lisp_object_equal_equal;
449 ht->hash_function = lisp_object_equal_hash;
456 ht->weakness = weakness;
459 rehash_size > 1.0 ? rehash_size : HASH_TABLE_DEFAULT_REHASH_SIZE;
461 ht->rehash_threshold =
462 rehash_threshold > 0.0 ? rehash_threshold :
463 size > 4096 && !ht->test_function ? 0.7 : 0.6;
465 if (size < HASH_TABLE_MIN_SIZE)
466 size = HASH_TABLE_MIN_SIZE;
467 ht->size = hash_table_size ((size_t) (((double) size / ht->rehash_threshold)
471 compute_hash_table_derived_values (ht);
473 /* We leave room for one never-occupied sentinel hentry at the end. */
474 ht->hentries = xnew_array (hentry, ht->size + 1);
477 hentry *e, *sentinel;
478 for (e = ht->hentries, sentinel = e + ht->size; e <= sentinel; e++)
482 XSETHASH_TABLE (hash_table, ht);
484 if (weakness == HASH_TABLE_NON_WEAK)
485 ht->next_weak = Qunbound;
487 ht->next_weak = Vall_weak_hash_tables, Vall_weak_hash_tables = hash_table;
493 make_lisp_hash_table (size_t size,
494 enum hash_table_weakness weakness,
495 enum hash_table_test test)
497 return make_general_lisp_hash_table (test, size, -1.0, -1.0, weakness);
500 /* Pretty reading of hash tables.
502 Here we use the existing structures mechanism (which is,
503 unfortunately, pretty cumbersome) for validating and instantiating
504 the hash tables. The idea is that the side-effect of reading a
505 #s(hash-table PLIST) object is creation of a hash table with desired
506 properties, and that the hash table is returned. */
508 /* Validation functions: each keyword provides its own validation
509 function. The errors should maybe be continuable, but it is
510 unclear how this would cope with ERRB. */
512 hash_table_size_validate (Lisp_Object keyword, Lisp_Object value,
518 maybe_signal_error (Qwrong_type_argument, list2 (Qnatnump, value),
524 decode_hash_table_size (Lisp_Object obj)
526 return NILP (obj) ? HASH_TABLE_DEFAULT_SIZE : XINT (obj);
530 hash_table_weakness_validate (Lisp_Object keyword, Lisp_Object value,
533 if (EQ (value, Qnil)) return 1;
534 if (EQ (value, Qt)) return 1;
535 if (EQ (value, Qkey)) return 1;
536 if (EQ (value, Qvalue)) return 1;
538 /* Following values are obsolete as of 19990901 in xemacs-21.2 */
539 if (EQ (value, Qnon_weak)) return 1;
540 if (EQ (value, Qweak)) return 1;
541 if (EQ (value, Qkey_weak)) return 1;
542 if (EQ (value, Qvalue_weak)) return 1;
544 maybe_signal_simple_error ("Invalid hash table weakness",
545 value, Qhash_table, errb);
549 static enum hash_table_weakness
550 decode_hash_table_weakness (Lisp_Object obj)
552 if (EQ (obj, Qnil)) return HASH_TABLE_NON_WEAK;
553 if (EQ (obj, Qt)) return HASH_TABLE_WEAK;
554 if (EQ (obj, Qkey)) return HASH_TABLE_KEY_WEAK;
555 if (EQ (obj, Qvalue)) return HASH_TABLE_VALUE_WEAK;
557 /* Following values are obsolete as of 19990901 in xemacs-21.2 */
558 if (EQ (obj, Qnon_weak)) return HASH_TABLE_NON_WEAK;
559 if (EQ (obj, Qweak)) return HASH_TABLE_WEAK;
560 if (EQ (obj, Qkey_weak)) return HASH_TABLE_KEY_WEAK;
561 if (EQ (obj, Qvalue_weak)) return HASH_TABLE_VALUE_WEAK;
563 signal_simple_error ("Invalid hash table weakness", obj);
564 return HASH_TABLE_NON_WEAK; /* not reached */
568 hash_table_test_validate (Lisp_Object keyword, Lisp_Object value,
571 if (EQ (value, Qnil)) return 1;
572 if (EQ (value, Qeq)) return 1;
573 if (EQ (value, Qequal)) return 1;
574 if (EQ (value, Qeql)) return 1;
576 maybe_signal_simple_error ("Invalid hash table test",
577 value, Qhash_table, errb);
581 static enum hash_table_test
582 decode_hash_table_test (Lisp_Object obj)
584 if (EQ (obj, Qnil)) return HASH_TABLE_EQL;
585 if (EQ (obj, Qeq)) return HASH_TABLE_EQ;
586 if (EQ (obj, Qequal)) return HASH_TABLE_EQUAL;
587 if (EQ (obj, Qeql)) return HASH_TABLE_EQL;
589 signal_simple_error ("Invalid hash table test", obj);
590 return HASH_TABLE_EQ; /* not reached */
594 hash_table_rehash_size_validate (Lisp_Object keyword, Lisp_Object value,
599 maybe_signal_error (Qwrong_type_argument, list2 (Qfloatp, value),
605 double rehash_size = XFLOAT_DATA (value);
606 if (rehash_size <= 1.0)
608 maybe_signal_simple_error
609 ("Hash table rehash size must be greater than 1.0",
610 value, Qhash_table, errb);
619 decode_hash_table_rehash_size (Lisp_Object rehash_size)
621 return NILP (rehash_size) ? -1.0 : XFLOAT_DATA (rehash_size);
625 hash_table_rehash_threshold_validate (Lisp_Object keyword, Lisp_Object value,
630 maybe_signal_error (Qwrong_type_argument, list2 (Qfloatp, value),
636 double rehash_threshold = XFLOAT_DATA (value);
637 if (rehash_threshold <= 0.0 || rehash_threshold >= 1.0)
639 maybe_signal_simple_error
640 ("Hash table rehash threshold must be between 0.0 and 1.0",
641 value, Qhash_table, errb);
650 decode_hash_table_rehash_threshold (Lisp_Object rehash_threshold)
652 return NILP (rehash_threshold) ? -1.0 : XFLOAT_DATA (rehash_threshold);
656 hash_table_data_validate (Lisp_Object keyword, Lisp_Object value,
661 GET_EXTERNAL_LIST_LENGTH (value, len);
665 maybe_signal_simple_error
666 ("Hash table data must have alternating key/value pairs",
667 value, Qhash_table, errb);
673 /* The actual instantiation of a hash table. This does practically no
674 error checking, because it relies on the fact that the paranoid
675 functions above have error-checked everything to the last details.
676 If this assumption is wrong, we will get a crash immediately (with
677 error-checking compiled in), and we'll know if there is a bug in
678 the structure mechanism. So there. */
680 hash_table_instantiate (Lisp_Object plist)
682 Lisp_Object hash_table;
683 Lisp_Object test = Qnil;
684 Lisp_Object size = Qnil;
685 Lisp_Object rehash_size = Qnil;
686 Lisp_Object rehash_threshold = Qnil;
687 Lisp_Object weakness = Qnil;
688 Lisp_Object data = Qnil;
690 while (!NILP (plist))
692 Lisp_Object key, value;
693 key = XCAR (plist); plist = XCDR (plist);
694 value = XCAR (plist); plist = XCDR (plist);
696 if (EQ (key, Qtest)) test = value;
697 else if (EQ (key, Qsize)) size = value;
698 else if (EQ (key, Qrehash_size)) rehash_size = value;
699 else if (EQ (key, Qrehash_threshold)) rehash_threshold = value;
700 else if (EQ (key, Qweakness)) weakness = value;
701 else if (EQ (key, Qdata)) data = value;
702 else if (EQ (key, Qtype))/*obsolete*/ weakness = value;
707 /* Create the hash table. */
708 hash_table = make_general_lisp_hash_table
709 (decode_hash_table_test (test),
710 decode_hash_table_size (size),
711 decode_hash_table_rehash_size (rehash_size),
712 decode_hash_table_rehash_threshold (rehash_threshold),
713 decode_hash_table_weakness (weakness));
715 /* I'm not sure whether this can GC, but better safe than sorry. */
720 /* And fill it with data. */
723 Lisp_Object key, value;
724 key = XCAR (data); data = XCDR (data);
725 value = XCAR (data); data = XCDR (data);
726 Fputhash (key, value, hash_table);
735 structure_type_create_hash_table_structure_name (Lisp_Object structure_name)
737 struct structure_type *st;
739 st = define_structure_type (structure_name, 0, hash_table_instantiate);
740 define_structure_type_keyword (st, Qtest, hash_table_test_validate);
741 define_structure_type_keyword (st, Qsize, hash_table_size_validate);
742 define_structure_type_keyword (st, Qrehash_size, hash_table_rehash_size_validate);
743 define_structure_type_keyword (st, Qrehash_threshold, hash_table_rehash_threshold_validate);
744 define_structure_type_keyword (st, Qweakness, hash_table_weakness_validate);
745 define_structure_type_keyword (st, Qdata, hash_table_data_validate);
747 /* obsolete as of 19990901 in xemacs-21.2 */
748 define_structure_type_keyword (st, Qtype, hash_table_weakness_validate);
751 /* Create a built-in Lisp structure type named `hash-table'.
752 We make #s(hashtable ...) equivalent to #s(hash-table ...),
753 for backward compatibility.
754 This is called from emacs.c. */
756 structure_type_create_hash_table (void)
758 structure_type_create_hash_table_structure_name (Qhash_table);
759 structure_type_create_hash_table_structure_name (Qhashtable); /* compat */
763 /************************************************************************/
764 /* Definition of Lisp-visible methods */
765 /************************************************************************/
767 DEFUN ("hash-table-p", Fhash_table_p, 1, 1, 0, /*
768 Return t if OBJECT is a hash table, else nil.
772 return HASH_TABLEP (object) ? Qt : Qnil;
775 DEFUN ("make-hash-table", Fmake_hash_table, 0, MANY, 0, /*
776 Return a new empty hash table object.
777 Use Common Lisp style keywords to specify hash table properties.
778 (make-hash-table &key test size rehash-size rehash-threshold weakness)
780 Keyword :test can be `eq', `eql' (default) or `equal'.
781 Comparison between keys is done using this function.
782 If speed is important, consider using `eq'.
783 When storing strings in the hash table, you will likely need to use `equal'.
785 Keyword :size specifies the number of keys likely to be inserted.
786 This number of entries can be inserted without enlarging the hash table.
788 Keyword :rehash-size must be a float greater than 1.0, and specifies
789 the factor by which to increase the size of the hash table when enlarging.
791 Keyword :rehash-threshold must be a float between 0.0 and 1.0,
792 and specifies the load factor of the hash table which triggers enlarging.
794 Non-standard keyword :weakness can be `nil' (default), `t', `key' or `value'.
796 A weak hash table is one whose pointers do not count as GC referents:
797 for any key-value pair in the hash table, if the only remaining pointer
798 to either the key or the value is in a weak hash table, then the pair
799 will be removed from the hash table, and the key and value collected.
800 A non-weak hash table (or any other pointer) would prevent the object
801 from being collected.
803 A key-weak hash table is similar to a fully-weak hash table except that
804 a key-value pair will be removed only if the key remains unmarked
805 outside of weak hash tables. The pair will remain in the hash table if
806 the key is pointed to by something other than a weak hash table, even
809 A value-weak hash table is similar to a fully-weak hash table except
810 that a key-value pair will be removed only if the value remains
811 unmarked outside of weak hash tables. The pair will remain in the
812 hash table if the value is pointed to by something other than a weak
813 hash table, even if the key is not.
815 (int nargs, Lisp_Object *args))
818 Lisp_Object test = Qnil;
819 Lisp_Object size = Qnil;
820 Lisp_Object rehash_size = Qnil;
821 Lisp_Object rehash_threshold = Qnil;
822 Lisp_Object weakness = Qnil;
824 while (i + 1 < nargs)
826 Lisp_Object keyword = args[i++];
827 Lisp_Object value = args[i++];
829 if (EQ (keyword, Q_test)) test = value;
830 else if (EQ (keyword, Q_size)) size = value;
831 else if (EQ (keyword, Q_rehash_size)) rehash_size = value;
832 else if (EQ (keyword, Q_rehash_threshold)) rehash_threshold = value;
833 else if (EQ (keyword, Q_weakness)) weakness = value;
834 else if (EQ (keyword, Q_type))/*obsolete*/ weakness = value;
835 else signal_simple_error ("Invalid hash table property keyword", keyword);
839 signal_simple_error ("Hash table property requires a value", args[i]);
841 #define VALIDATE_VAR(var) \
842 if (!NILP (var)) hash_table_##var##_validate (Q##var, var, ERROR_ME);
846 VALIDATE_VAR (rehash_size);
847 VALIDATE_VAR (rehash_threshold);
848 VALIDATE_VAR (weakness);
850 return make_general_lisp_hash_table
851 (decode_hash_table_test (test),
852 decode_hash_table_size (size),
853 decode_hash_table_rehash_size (rehash_size),
854 decode_hash_table_rehash_threshold (rehash_threshold),
855 decode_hash_table_weakness (weakness));
858 DEFUN ("copy-hash-table", Fcopy_hash_table, 1, 1, 0, /*
859 Return a new hash table containing the same keys and values as HASH-TABLE.
860 The keys and values will not themselves be copied.
864 CONST Lisp_Hash_Table *ht_old = xhash_table (hash_table);
865 Lisp_Hash_Table *ht = alloc_lcrecord_type (Lisp_Hash_Table, &lrecord_hash_table);
867 copy_lcrecord (ht, ht_old);
869 ht->hentries = xnew_array (hentry, ht_old->size + 1);
870 memcpy (ht->hentries, ht_old->hentries, (ht_old->size + 1) * sizeof (hentry));
872 XSETHASH_TABLE (hash_table, ht);
874 if (! EQ (ht->next_weak, Qunbound))
876 ht->next_weak = Vall_weak_hash_tables;
877 Vall_weak_hash_tables = hash_table;
884 resize_hash_table (Lisp_Hash_Table *ht, size_t new_size)
886 hentry *old_entries, *new_entries, *old_sentinel, *new_sentinel, *e;
892 old_entries = ht->hentries;
894 ht->hentries = xnew_array (hentry, new_size + 1);
895 new_entries = ht->hentries;
897 old_sentinel = old_entries + old_size;
898 new_sentinel = new_entries + new_size;
900 for (e = new_entries; e <= new_sentinel; e++)
903 compute_hash_table_derived_values (ht);
905 for (e = old_entries; e < old_sentinel; e++)
906 if (!HENTRY_CLEAR_P (e))
908 hentry *probe = new_entries + HASH_CODE (e->key, ht);
909 LINEAR_PROBING_LOOP (probe, new_entries, new_size)
914 if (!DUMPEDP (old_entries))
919 reorganize_hash_table (Lisp_Hash_Table *ht)
921 resize_hash_table (ht, ht->size);
925 enlarge_hash_table (Lisp_Hash_Table *ht)
928 hash_table_size ((size_t) ((double) ht->size * ht->rehash_size));
929 resize_hash_table (ht, new_size);
933 find_hentry (Lisp_Object key, CONST Lisp_Hash_Table *ht)
935 hash_table_test_function_t test_function = ht->test_function;
936 hentry *entries = ht->hentries;
937 hentry *probe = entries + HASH_CODE (key, ht);
939 LINEAR_PROBING_LOOP (probe, entries, ht->size)
940 if (KEYS_EQUAL_P (probe->key, key, test_function))
946 DEFUN ("gethash", Fgethash, 2, 3, 0, /*
947 Find hash value for KEY in HASH-TABLE.
948 If there is no corresponding value, return DEFAULT (which defaults to nil).
950 (key, hash_table, default_))
952 CONST Lisp_Hash_Table *ht = xhash_table (hash_table);
953 hentry *e = find_hentry (key, ht);
955 return HENTRY_CLEAR_P (e) ? default_ : e->value;
958 DEFUN ("puthash", Fputhash, 3, 3, 0, /*
959 Hash KEY to VALUE in HASH-TABLE.
961 (key, value, hash_table))
963 Lisp_Hash_Table *ht = xhash_table (hash_table);
964 hentry *e = find_hentry (key, ht);
966 if (!HENTRY_CLEAR_P (e))
967 return e->value = value;
972 if (++ht->count >= ht->rehash_count)
973 enlarge_hash_table (ht);
978 /* Remove hentry pointed at by PROBE.
979 Subsequent entries are removed and reinserted.
980 We don't use tombstones - too wasteful. */
982 remhash_1 (Lisp_Hash_Table *ht, hentry *entries, hentry *probe)
984 size_t size = ht->size;
985 CLEAR_HENTRY (probe);
989 LINEAR_PROBING_LOOP (probe, entries, size)
991 Lisp_Object key = probe->key;
992 hentry *probe2 = entries + HASH_CODE (key, ht);
993 LINEAR_PROBING_LOOP (probe2, entries, size)
994 if (EQ (probe2->key, key))
995 /* hentry at probe doesn't need to move. */
996 goto continue_outer_loop;
997 /* Move hentry from probe to new home at probe2. */
999 CLEAR_HENTRY (probe);
1000 continue_outer_loop: continue;
1004 DEFUN ("remhash", Fremhash, 2, 2, 0, /*
1005 Remove the entry for KEY from HASH-TABLE.
1006 Do nothing if there is no entry for KEY in HASH-TABLE.
1010 Lisp_Hash_Table *ht = xhash_table (hash_table);
1011 hentry *e = find_hentry (key, ht);
1013 if (HENTRY_CLEAR_P (e))
1016 remhash_1 (ht, ht->hentries, e);
1020 DEFUN ("clrhash", Fclrhash, 1, 1, 0, /*
1021 Remove all entries from HASH-TABLE, leaving it empty.
1025 Lisp_Hash_Table *ht = xhash_table (hash_table);
1026 hentry *e, *sentinel;
1028 for (e = ht->hentries, sentinel = e + ht->size; e < sentinel; e++)
1035 /************************************************************************/
1036 /* Accessor Functions */
1037 /************************************************************************/
1039 DEFUN ("hash-table-count", Fhash_table_count, 1, 1, 0, /*
1040 Return the number of entries in HASH-TABLE.
1044 return make_int (xhash_table (hash_table)->count);
1047 DEFUN ("hash-table-test", Fhash_table_test, 1, 1, 0, /*
1048 Return the test function of HASH-TABLE.
1049 This can be one of `eq', `eql' or `equal'.
1053 hash_table_test_function_t fun = xhash_table (hash_table)->test_function;
1055 return (fun == lisp_object_eql_equal ? Qeql :
1056 fun == lisp_object_equal_equal ? Qequal :
1060 DEFUN ("hash-table-size", Fhash_table_size, 1, 1, 0, /*
1061 Return the size of HASH-TABLE.
1062 This is the current number of slots in HASH-TABLE, whether occupied or not.
1066 return make_int (xhash_table (hash_table)->size);
1069 DEFUN ("hash-table-rehash-size", Fhash_table_rehash_size, 1, 1, 0, /*
1070 Return the current rehash size of HASH-TABLE.
1071 This is a float greater than 1.0; the factor by which HASH-TABLE
1072 is enlarged when the rehash threshold is exceeded.
1076 return make_float (xhash_table (hash_table)->rehash_size);
1079 DEFUN ("hash-table-rehash-threshold", Fhash_table_rehash_threshold, 1, 1, 0, /*
1080 Return the current rehash threshold of HASH-TABLE.
1081 This is a float between 0.0 and 1.0; the maximum `load factor' of HASH-TABLE,
1082 beyond which the HASH-TABLE is enlarged by rehashing.
1086 return make_float (xhash_table (hash_table)->rehash_threshold);
1089 DEFUN ("hash-table-weakness", Fhash_table_weakness, 1, 1, 0, /*
1090 Return the weakness of HASH-TABLE.
1091 This can be one of `nil', `t', `key' or `value'.
1095 switch (xhash_table (hash_table)->weakness)
1097 case HASH_TABLE_WEAK: return Qt;
1098 case HASH_TABLE_KEY_WEAK: return Qkey;
1099 case HASH_TABLE_VALUE_WEAK: return Qvalue;
1100 default: return Qnil;
1104 /* obsolete as of 19990901 in xemacs-21.2 */
1105 DEFUN ("hash-table-type", Fhash_table_type, 1, 1, 0, /*
1106 Return the type of HASH-TABLE.
1107 This can be one of `non-weak', `weak', `key-weak' or `value-weak'.
1111 switch (xhash_table (hash_table)->weakness)
1113 case HASH_TABLE_WEAK: return Qweak;
1114 case HASH_TABLE_KEY_WEAK: return Qkey_weak;
1115 case HASH_TABLE_VALUE_WEAK: return Qvalue_weak;
1116 default: return Qnon_weak;
1120 /************************************************************************/
1121 /* Mapping Functions */
1122 /************************************************************************/
1123 DEFUN ("maphash", Fmaphash, 2, 2, 0, /*
1124 Map FUNCTION over entries in HASH-TABLE, calling it with two args,
1125 each key and value in HASH-TABLE.
1127 FUNCTION may not modify HASH-TABLE, with the one exception that FUNCTION
1128 may remhash or puthash the entry currently being processed by FUNCTION.
1130 (function, hash_table))
1132 CONST Lisp_Hash_Table *ht = xhash_table (hash_table);
1133 CONST hentry *e, *sentinel;
1135 for (e = ht->hentries, sentinel = e + ht->size; e < sentinel; e++)
1136 if (!HENTRY_CLEAR_P (e))
1138 Lisp_Object args[3], key;
1144 Ffuncall (countof (args), args);
1145 /* Has FUNCTION done a remhash? */
1146 if (!EQ (key, e->key) && !HENTRY_CLEAR_P (e))
1153 /* Map *C* function FUNCTION over the elements of a lisp hash table. */
1155 elisp_maphash (maphash_function_t function,
1156 Lisp_Object hash_table, void *extra_arg)
1158 CONST Lisp_Hash_Table *ht = XHASH_TABLE (hash_table);
1159 CONST hentry *e, *sentinel;
1161 for (e = ht->hentries, sentinel = e + ht->size; e < sentinel; e++)
1162 if (!HENTRY_CLEAR_P (e))
1167 if (function (key, e->value, extra_arg))
1169 /* Has FUNCTION done a remhash? */
1170 if (!EQ (key, e->key) && !HENTRY_CLEAR_P (e))
1175 /* Remove all elements of a lisp hash table satisfying *C* predicate PREDICATE. */
1177 elisp_map_remhash (maphash_function_t predicate,
1178 Lisp_Object hash_table, void *extra_arg)
1180 Lisp_Hash_Table *ht = XHASH_TABLE (hash_table);
1181 hentry *e, *entries, *sentinel;
1183 for (e = entries = ht->hentries, sentinel = e + ht->size; e < sentinel; e++)
1184 if (!HENTRY_CLEAR_P (e))
1187 if (predicate (e->key, e->value, extra_arg))
1189 remhash_1 (ht, entries, e);
1190 if (!HENTRY_CLEAR_P (e))
1197 /************************************************************************/
1198 /* garbage collecting weak hash tables */
1199 /************************************************************************/
1201 /* Complete the marking for semi-weak hash tables. */
1203 finish_marking_weak_hash_tables (void)
1205 Lisp_Object hash_table;
1208 for (hash_table = Vall_weak_hash_tables;
1210 hash_table = XHASH_TABLE (hash_table)->next_weak)
1212 CONST Lisp_Hash_Table *ht = XHASH_TABLE (hash_table);
1213 CONST hentry *e = ht->hentries;
1214 CONST hentry *sentinel = e + ht->size;
1216 if (! marked_p (hash_table))
1217 /* The hash table is probably garbage. Ignore it. */
1220 /* Now, scan over all the pairs. For all pairs that are
1221 half-marked, we may need to mark the other half if we're
1222 keeping this pair. */
1223 #define MARK_OBJ(obj) \
1224 do { if (!marked_p (obj)) mark_object (obj), did_mark = 1; } while (0)
1226 switch (ht->weakness)
1228 case HASH_TABLE_KEY_WEAK:
1229 for (; e < sentinel; e++)
1230 if (!HENTRY_CLEAR_P (e))
1231 if (marked_p (e->key))
1232 MARK_OBJ (e->value);
1235 case HASH_TABLE_VALUE_WEAK:
1236 for (; e < sentinel; e++)
1237 if (!HENTRY_CLEAR_P (e))
1238 if (marked_p (e->value))
1242 case HASH_TABLE_KEY_CAR_WEAK:
1243 for (; e < sentinel; e++)
1244 if (!HENTRY_CLEAR_P (e))
1245 if (!CONSP (e->key) || marked_p (XCAR (e->key)))
1248 MARK_OBJ (e->value);
1252 case HASH_TABLE_VALUE_CAR_WEAK:
1253 for (; e < sentinel; e++)
1254 if (!HENTRY_CLEAR_P (e))
1255 if (!CONSP (e->value) || marked_p (XCAR (e->value)))
1258 MARK_OBJ (e->value);
1271 prune_weak_hash_tables (void)
1273 Lisp_Object hash_table, prev = Qnil;
1274 for (hash_table = Vall_weak_hash_tables;
1276 hash_table = XHASH_TABLE (hash_table)->next_weak)
1278 if (! marked_p (hash_table))
1280 /* This hash table itself is garbage. Remove it from the list. */
1282 Vall_weak_hash_tables = XHASH_TABLE (hash_table)->next_weak;
1284 XHASH_TABLE (prev)->next_weak = XHASH_TABLE (hash_table)->next_weak;
1288 /* Now, scan over all the pairs. Remove all of the pairs
1289 in which the key or value, or both, is unmarked
1290 (depending on the weakness of the hash table). */
1291 Lisp_Hash_Table *ht = XHASH_TABLE (hash_table);
1292 hentry *entries = ht->hentries;
1293 hentry *sentinel = entries + ht->size;
1296 for (e = entries; e < sentinel; e++)
1297 if (!HENTRY_CLEAR_P (e))
1300 if (!marked_p (e->key) || !marked_p (e->value))
1302 remhash_1 (ht, entries, e);
1303 if (!HENTRY_CLEAR_P (e))
1313 /* Return a hash value for an array of Lisp_Objects of size SIZE. */
1316 internal_array_hash (Lisp_Object *arr, int size, int depth)
1319 unsigned long hash = 0;
1323 for (i = 0; i < size; i++)
1324 hash = HASH2 (hash, internal_hash (arr[i], depth + 1));
1328 /* just pick five elements scattered throughout the array.
1329 A slightly better approach would be to offset by some
1330 noise factor from the points chosen below. */
1331 for (i = 0; i < 5; i++)
1332 hash = HASH2 (hash, internal_hash (arr[i*size/5], depth + 1));
1337 /* Return a hash value for a Lisp_Object. This is for use when hashing
1338 objects with the comparison being `equal' (for `eq', you can just
1339 use the Lisp_Object itself as the hash value). You need to make a
1340 tradeoff between the speed of the hash function and how good the
1341 hashing is. In particular, the hash function needs to be FAST,
1342 so you can't just traipse down the whole tree hashing everything
1343 together. Most of the time, objects will differ in the first
1344 few elements you hash. Thus, we only go to a short depth (5)
1345 and only hash at most 5 elements out of a vector. Theoretically
1346 we could still take 5^5 time (a big big number) to compute a
1347 hash, but practically this won't ever happen. */
1350 internal_hash (Lisp_Object obj, int depth)
1356 /* no point in worrying about tail recursion, since we're not
1358 return HASH2 (internal_hash (XCAR (obj), depth + 1),
1359 internal_hash (XCDR (obj), depth + 1));
1363 return hash_string (XSTRING_DATA (obj), XSTRING_LENGTH (obj));
1367 return HASH2 (XVECTOR_LENGTH (obj),
1368 internal_array_hash (XVECTOR_DATA (obj),
1369 XVECTOR_LENGTH (obj),
1374 CONST struct lrecord_implementation
1375 *imp = XRECORD_LHEADER_IMPLEMENTATION (obj);
1377 return imp->hash (obj, depth);
1380 return LISP_HASH (obj);
1383 DEFUN ("sxhash", Fsxhash, 1, 1, 0, /*
1384 Return a hash value for OBJECT.
1385 (equal obj1 obj2) implies (= (sxhash obj1) (sxhash obj2)).
1389 return make_int (internal_hash (object, 0));
1393 xxDEFUN ("internal-hash-value", Finternal_hash_value, 1, 1, 0, /*
1394 Hash value of OBJECT. For debugging.
1395 The value is returned as (HIGH . LOW).
1399 /* This function is pretty 32bit-centric. */
1400 unsigned long hash = internal_hash (object, 0);
1401 return Fcons (hash >> 16, hash & 0xffff);
1406 /************************************************************************/
1407 /* initialization */
1408 /************************************************************************/
1411 syms_of_elhash (void)
1413 DEFSUBR (Fhash_table_p);
1414 DEFSUBR (Fmake_hash_table);
1415 DEFSUBR (Fcopy_hash_table);
1421 DEFSUBR (Fhash_table_count);
1422 DEFSUBR (Fhash_table_test);
1423 DEFSUBR (Fhash_table_size);
1424 DEFSUBR (Fhash_table_rehash_size);
1425 DEFSUBR (Fhash_table_rehash_threshold);
1426 DEFSUBR (Fhash_table_weakness);
1427 DEFSUBR (Fhash_table_type); /* obsolete */
1430 DEFSUBR (Finternal_hash_value);
1433 defsymbol (&Qhash_tablep, "hash-table-p");
1434 defsymbol (&Qhash_table, "hash-table");
1435 defsymbol (&Qhashtable, "hashtable");
1436 defsymbol (&Qweakness, "weakness");
1437 defsymbol (&Qvalue, "value");
1438 defsymbol (&Qrehash_size, "rehash-size");
1439 defsymbol (&Qrehash_threshold, "rehash-threshold");
1441 defsymbol (&Qweak, "weak"); /* obsolete */
1442 defsymbol (&Qkey_weak, "key-weak"); /* obsolete */
1443 defsymbol (&Qvalue_weak, "value-weak"); /* obsolete */
1444 defsymbol (&Qnon_weak, "non-weak"); /* obsolete */
1446 defkeyword (&Q_test, ":test");
1447 defkeyword (&Q_size, ":size");
1448 defkeyword (&Q_rehash_size, ":rehash-size");
1449 defkeyword (&Q_rehash_threshold, ":rehash-threshold");
1450 defkeyword (&Q_weakness, ":weakness");
1451 defkeyword (&Q_type, ":type"); /* obsolete */
1455 vars_of_elhash (void)
1457 /* This must NOT be staticpro'd */
1458 Vall_weak_hash_tables = Qnil;
1459 pdump_wire_list (&Vall_weak_hash_tables);