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