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