0
|
1 /* Lisp interface to hash tables.
|
|
2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995, 1996 Ben Wing.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 #include <config.h>
|
|
25 #include "lisp.h"
|
|
26 #include "hash.h"
|
|
27 #include "elhash.h"
|
|
28 #include "bytecode.h"
|
|
29
|
|
30 Lisp_Object Qhashtablep;
|
|
31
|
|
32 #define LISP_OBJECTS_PER_HENTRY (sizeof (hentry) / sizeof (Lisp_Object))/* 2 */
|
|
33
|
|
34 struct hashtable_struct
|
|
35 {
|
|
36 struct lcrecord_header header;
|
|
37 unsigned int fullness;
|
|
38 unsigned long (*hash_function) (CONST void *);
|
|
39 int (*test_function) (CONST void *, CONST void *);
|
|
40 Lisp_Object zero_entry;
|
|
41 Lisp_Object harray;
|
|
42 enum hashtable_type type; /* whether and how this hashtable is weak */
|
|
43 Lisp_Object next_weak; /* Used to chain together all of the weak
|
|
44 hashtables. Don't mark through this. */
|
|
45 };
|
|
46
|
|
47 static Lisp_Object Vall_weak_hashtables;
|
|
48
|
|
49 static Lisp_Object mark_hashtable (Lisp_Object, void (*) (Lisp_Object));
|
|
50 static void print_hashtable (Lisp_Object, Lisp_Object, int);
|
|
51 DEFINE_LRECORD_IMPLEMENTATION ("hashtable", hashtable,
|
|
52 mark_hashtable, print_hashtable, 0, 0, 0,
|
|
53 struct hashtable_struct);
|
|
54
|
|
55 static Lisp_Object
|
|
56 mark_hashtable (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
57 {
|
|
58 struct hashtable_struct *table = XHASHTABLE (obj);
|
|
59
|
|
60 if (table->type != HASHTABLE_NONWEAK)
|
|
61 {
|
|
62 /* If the table is weak, we don't want to mark the keys and values
|
|
63 (we scan over them after everything else has been marked,
|
|
64 and mark or remove them as necessary). Note that we will mark
|
|
65 the table->harray itself at the same time; it's hard to mark
|
|
66 that here without also marking its contents. */
|
|
67 return Qnil;
|
|
68 }
|
|
69 ((markobj) (table->zero_entry));
|
|
70 return (table->harray);
|
|
71 }
|
|
72
|
|
73 static void
|
|
74 print_hashtable (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
75 {
|
|
76 struct hashtable_struct *table = XHASHTABLE (obj);
|
|
77 char buf[200];
|
|
78 if (print_readably)
|
|
79 error ("printing unreadable object #<hashtable 0x%x>",
|
|
80 table->header.uid);
|
|
81 sprintf (buf, GETTEXT ("#<%shashtable %d/%ld 0x%x>"),
|
|
82 (table->type == HASHTABLE_WEAK ? "weak " :
|
|
83 table->type == HASHTABLE_KEY_WEAK ? "key-weak " :
|
|
84 table->type == HASHTABLE_VALUE_WEAK ? "value-weak " :
|
|
85 table->type == HASHTABLE_KEY_CAR_WEAK ? "key-car-weak " :
|
|
86 table->type == HASHTABLE_VALUE_CAR_WEAK ? "value-car-weak " :
|
|
87 ""),
|
|
88 table->fullness,
|
|
89 (vector_length (XVECTOR (table->harray)) / LISP_OBJECTS_PER_HENTRY),
|
|
90 table->header.uid);
|
|
91 write_c_string (buf, printcharfun);
|
|
92 }
|
|
93
|
|
94 static void
|
|
95 ht_copy_to_c (struct hashtable_struct *ht,
|
|
96 c_hashtable c_table)
|
|
97 {
|
|
98 int len;
|
|
99
|
|
100 c_table->harray = (void *) vector_data (XVECTOR (ht->harray));
|
|
101 c_table->zero_set = (!GC_UNBOUNDP (ht->zero_entry));
|
|
102 c_table->zero_entry = LISP_TO_VOID (ht->zero_entry);
|
|
103 len = vector_length (XVECTOR (ht->harray));
|
|
104 if (len < 0)
|
|
105 {
|
|
106 /* #### if alloc.c mark_object() changes, this must change too. */
|
|
107 /* barf gag retch. When a vector is marked, its len is
|
|
108 made less than 0. In the prune_weak_hashtables() stage,
|
|
109 we are called on vectors that are like this, and we must
|
|
110 be able to deal. */
|
|
111 assert (gc_in_progress);
|
|
112 len = -1 - len;
|
|
113 }
|
|
114 c_table->size = len/LISP_OBJECTS_PER_HENTRY;
|
|
115 c_table->fullness = ht->fullness;
|
|
116 c_table->hash_function = ht->hash_function;
|
|
117 c_table->test_function = ht->test_function;
|
|
118 XSETHASHTABLE (c_table->elisp_table, ht);
|
|
119 }
|
|
120
|
|
121 static void
|
|
122 ht_copy_from_c (c_hashtable c_table,
|
|
123 struct hashtable_struct *ht)
|
|
124 {
|
|
125 struct Lisp_Vector dummy;
|
|
126 /* C is truly hateful */
|
|
127 void *vec_addr
|
|
128 = ((char *) c_table->harray
|
|
129 - ((char *) &(dummy.contents) - (char *) &dummy));
|
|
130
|
|
131 XSETVECTOR (ht->harray, vec_addr);
|
|
132 if (c_table->zero_set)
|
|
133 VOID_TO_LISP (ht->zero_entry, c_table->zero_entry);
|
|
134 else
|
|
135 ht->zero_entry = Qunbound;
|
|
136 ht->fullness = c_table->fullness;
|
|
137 }
|
|
138
|
|
139
|
|
140 static struct hashtable_struct *
|
|
141 allocate_hashtable (void)
|
|
142 {
|
|
143 struct hashtable_struct *table
|
|
144 = alloc_lcrecord (sizeof (struct hashtable_struct), lrecord_hashtable);
|
|
145 table->harray = Qnil;
|
|
146 table->zero_entry = Qunbound;
|
|
147 table->fullness = 0;
|
|
148 table->hash_function = 0;
|
|
149 table->test_function = 0;
|
|
150 return (table);
|
|
151 }
|
|
152
|
|
153 char *
|
|
154 elisp_hvector_malloc (unsigned int bytes, Lisp_Object table)
|
|
155 {
|
|
156 Lisp_Object new_vector;
|
|
157 struct hashtable_struct *ht;
|
|
158
|
|
159 ht = XHASHTABLE (table);
|
|
160 assert (bytes > vector_length (XVECTOR (ht->harray)) * sizeof (Lisp_Object));
|
|
161 new_vector = make_vector ((bytes / sizeof (Lisp_Object)), Qzero);
|
|
162 return ((char *) (vector_data (XVECTOR (new_vector))));
|
|
163 }
|
|
164
|
|
165 void
|
|
166 elisp_hvector_free (void *ptr, Lisp_Object table)
|
|
167 {
|
|
168 struct hashtable_struct *ht = XHASHTABLE (table);
|
|
169 #if defined (USE_ASSERTIONS) || defined (DEBUG_XEMACS)
|
|
170 Lisp_Object current_vector = ht->harray;
|
|
171 #endif
|
|
172
|
|
173 assert (((void *) vector_data (XVECTOR (current_vector))) == ptr);
|
|
174 ht->harray = Qnil; /* Let GC do its job */
|
|
175 return;
|
|
176 }
|
|
177
|
|
178
|
20
|
179 DEFUN ("hashtablep", Fhashtablep, 1, 1, 0, /*
|
0
|
180 Return t if OBJ is a hashtable, else nil.
|
20
|
181 */
|
|
182 (obj))
|
0
|
183 {
|
|
184 return ((HASHTABLEP (obj)) ? Qt : Qnil);
|
|
185 }
|
|
186
|
|
187
|
|
188
|
|
189
|
|
190 #if 0 /* I don't think these are needed any more.
|
|
191 If using the general lisp_object_equal_*() functions
|
|
192 causes efficiency problems, these can be resurrected. --ben */
|
|
193 /* equality and hash functions for Lisp strings */
|
|
194 int
|
|
195 lisp_string_equal (CONST void *x1, CONST void *x2)
|
|
196 {
|
|
197 Lisp_Object str1, str2;
|
|
198 CVOID_TO_LISP (str1, x1);
|
|
199 CVOID_TO_LISP (str2, x2);
|
14
|
200 return !strcmp ((char *) XSTRING_DATA (str1), (char *) XSTRING_DATA (str2));
|
0
|
201 }
|
|
202
|
|
203 unsigned long
|
|
204 lisp_string_hash (CONST void *x)
|
|
205 {
|
|
206 Lisp_Object str;
|
|
207 CVOID_TO_LISP (str, x);
|
14
|
208 return hash_string (XSTRING_DATA (str), XSTRING_LENGTH (str));
|
0
|
209 }
|
|
210
|
|
211 #endif /* 0 */
|
|
212
|
|
213 static int
|
|
214 lisp_object_eql_equal (CONST void *x1, CONST void *x2)
|
|
215 {
|
|
216 Lisp_Object obj1, obj2;
|
|
217 CVOID_TO_LISP (obj1, x1);
|
|
218 CVOID_TO_LISP (obj2, x2);
|
|
219 return
|
|
220 (FLOATP (obj1) ? !NILP (Fequal (obj1, obj2)) : EQ (obj1, obj2));
|
|
221 }
|
|
222
|
|
223 static unsigned long
|
|
224 lisp_object_eql_hash (CONST void *x)
|
|
225 {
|
|
226 Lisp_Object obj;
|
|
227 CVOID_TO_LISP (obj, x);
|
|
228 if (FLOATP (obj))
|
|
229 return internal_hash (obj, 0);
|
|
230 else
|
|
231 return LISP_HASH (obj);
|
|
232 }
|
|
233
|
|
234 static int
|
|
235 lisp_object_equal_equal (CONST void *x1, CONST void *x2)
|
|
236 {
|
|
237 Lisp_Object obj1, obj2;
|
|
238 CVOID_TO_LISP (obj1, x1);
|
|
239 CVOID_TO_LISP (obj2, x2);
|
|
240 return !NILP (Fequal (obj1, obj2));
|
|
241 }
|
|
242
|
|
243 static unsigned long
|
|
244 lisp_object_equal_hash (CONST void *x)
|
|
245 {
|
|
246 Lisp_Object obj;
|
|
247 CVOID_TO_LISP (obj, x);
|
|
248 return internal_hash (obj, 0);
|
|
249 }
|
|
250
|
|
251 Lisp_Object
|
|
252 make_lisp_hashtable (int size,
|
|
253 enum hashtable_type type,
|
|
254 enum hashtable_test_fun test)
|
|
255 {
|
|
256 Lisp_Object result;
|
|
257 struct hashtable_struct *table = allocate_hashtable ();
|
|
258
|
|
259 table->harray = make_vector ((compute_harray_size (size)
|
|
260 * LISP_OBJECTS_PER_HENTRY),
|
|
261 Qzero);
|
|
262 switch (test)
|
|
263 {
|
|
264 case HASHTABLE_EQ:
|
|
265 table->test_function = 0;
|
|
266 table->hash_function = 0;
|
|
267 break;
|
|
268
|
|
269 case HASHTABLE_EQL:
|
|
270 table->test_function = lisp_object_eql_equal;
|
|
271 table->hash_function = lisp_object_eql_hash;
|
|
272 break;
|
|
273
|
|
274 case HASHTABLE_EQUAL:
|
|
275 table->test_function = lisp_object_equal_equal;
|
|
276 table->hash_function = lisp_object_equal_hash;
|
|
277 break;
|
|
278
|
|
279 default:
|
|
280 abort ();
|
|
281 }
|
|
282
|
|
283 table->type = type;
|
|
284 XSETHASHTABLE (result, table);
|
|
285
|
|
286 if (table->type != HASHTABLE_NONWEAK)
|
|
287 {
|
|
288 table->next_weak = Vall_weak_hashtables;
|
|
289 Vall_weak_hashtables = result;
|
|
290 }
|
|
291 else
|
|
292 table->next_weak = Qunbound;
|
|
293
|
|
294 return (result);
|
|
295 }
|
|
296
|
|
297 static enum hashtable_test_fun
|
|
298 decode_hashtable_test_fun (Lisp_Object sym)
|
|
299 {
|
|
300 if (NILP (sym))
|
|
301 return HASHTABLE_EQL;
|
|
302
|
|
303 CHECK_SYMBOL (sym);
|
|
304
|
2
|
305 if (EQ (sym, Qeq)) return HASHTABLE_EQ;
|
|
306 if (EQ (sym, Qequal)) return HASHTABLE_EQUAL;
|
|
307 if (EQ (sym, Qeql)) return HASHTABLE_EQL;
|
|
308
|
0
|
309 signal_simple_error ("Invalid hashtable test fun", sym);
|
2
|
310 return HASHTABLE_EQ; /* not reached */
|
0
|
311 }
|
|
312
|
20
|
313 DEFUN ("make-hashtable", Fmake_hashtable, 1, 2, 0, /*
|
0
|
314 Make a hashtable of initial size SIZE.
|
|
315 Comparison between keys is done with TEST-FUN, which must be one of
|
|
316 `eq', `eql', or `equal'. The default is `eql'; i.e. two keys must
|
|
317 be the same object (or have the same floating-point value, for floats)
|
|
318 to be considered equivalent.
|
|
319
|
|
320 See also `make-weak-hashtable', `make-key-weak-hashtable', and
|
|
321 `make-value-weak-hashtable'.
|
20
|
322 */
|
|
323 (size, test_fun))
|
0
|
324 {
|
|
325 CHECK_NATNUM (size);
|
|
326 return make_lisp_hashtable (XINT (size), HASHTABLE_NONWEAK,
|
|
327 decode_hashtable_test_fun (test_fun));
|
|
328 }
|
|
329
|
20
|
330 DEFUN ("copy-hashtable", Fcopy_hashtable, 1, 1, 0, /*
|
0
|
331 Make a new hashtable which contains the same keys and values
|
|
332 as the given table. The keys and values will not themselves be copied.
|
20
|
333 */
|
|
334 (old_table))
|
0
|
335 {
|
|
336 struct _C_hashtable old_htbl;
|
|
337 struct _C_hashtable new_htbl;
|
|
338 struct hashtable_struct *old_ht;
|
|
339 struct hashtable_struct *new_ht;
|
|
340 Lisp_Object result;
|
|
341
|
|
342 CHECK_HASHTABLE (old_table);
|
|
343 old_ht = XHASHTABLE (old_table);
|
|
344 ht_copy_to_c (old_ht, &old_htbl);
|
|
345
|
|
346 /* we can't just call Fmake_hashtable() here because that will make a
|
|
347 table that is slightly larger than the one we're trying to copy,
|
|
348 which will make copy_hash() blow up. */
|
|
349 new_ht = allocate_hashtable ();
|
|
350 new_ht->fullness = 0;
|
|
351 new_ht->zero_entry = Qunbound;
|
|
352 new_ht->hash_function = old_ht->hash_function;
|
|
353 new_ht->test_function = old_ht->test_function;
|
|
354 new_ht->harray = Fmake_vector (Flength (old_ht->harray), Qzero);
|
|
355 ht_copy_to_c (new_ht, &new_htbl);
|
|
356 copy_hash (&new_htbl, &old_htbl);
|
|
357 ht_copy_from_c (&new_htbl, new_ht);
|
|
358 new_ht->type = old_ht->type;
|
|
359 XSETHASHTABLE (result, new_ht);
|
|
360
|
|
361 if (UNBOUNDP (old_ht->next_weak))
|
|
362 new_ht->next_weak = Qunbound;
|
|
363 else
|
|
364 {
|
|
365 new_ht->next_weak = Vall_weak_hashtables;
|
|
366 Vall_weak_hashtables = result;
|
|
367 }
|
|
368
|
|
369 return (result);
|
|
370 }
|
|
371
|
|
372
|
20
|
373 DEFUN ("gethash", Fgethash, 2, 3, 0, /*
|
0
|
374 Find hash value for KEY in TABLE.
|
|
375 If there is no corresponding value, return DEFAULT (defaults to nil).
|
20
|
376 */
|
|
377 (key, table, defalt))
|
0
|
378 {
|
|
379 CONST void *vval;
|
|
380 struct _C_hashtable htbl;
|
|
381 if (!gc_in_progress)
|
|
382 CHECK_HASHTABLE (table);
|
|
383 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
384 if (gethash (LISP_TO_VOID (key), &htbl, &vval))
|
|
385 {
|
|
386 Lisp_Object val;
|
|
387 CVOID_TO_LISP (val, vval);
|
|
388 return val;
|
|
389 }
|
|
390 else
|
|
391 return defalt;
|
|
392 }
|
|
393
|
|
394
|
20
|
395 DEFUN ("remhash", Fremhash, 2, 2, 0, /*
|
0
|
396 Remove hash value for KEY in TABLE.
|
20
|
397 */
|
|
398 (key, table))
|
0
|
399 {
|
|
400 struct _C_hashtable htbl;
|
|
401 CHECK_HASHTABLE (table);
|
|
402
|
|
403 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
404 remhash (LISP_TO_VOID (key), &htbl);
|
|
405 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
406 return Qnil;
|
|
407 }
|
|
408
|
|
409
|
20
|
410 DEFUN ("puthash", Fputhash, 3, 3, 0, /*
|
0
|
411 Hash KEY to VAL in TABLE.
|
20
|
412 */
|
|
413 (key, val, table))
|
0
|
414 {
|
|
415 struct hashtable_struct *ht;
|
|
416 void *vkey = LISP_TO_VOID (key);
|
|
417
|
|
418 CHECK_HASHTABLE (table);
|
|
419 ht = XHASHTABLE (table);
|
|
420 if (!vkey)
|
|
421 ht->zero_entry = val;
|
|
422 else
|
|
423 {
|
|
424 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
425 struct _C_hashtable htbl;
|
|
426
|
|
427 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
428 GCPRO3 (key, val, table);
|
|
429 puthash (vkey, LISP_TO_VOID (val), &htbl);
|
|
430 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
431 UNGCPRO;
|
|
432 }
|
|
433 return (val);
|
|
434 }
|
|
435
|
20
|
436 DEFUN ("clrhash", Fclrhash, 1, 1, 0, /*
|
0
|
437 Flush TABLE.
|
20
|
438 */
|
|
439 (table))
|
0
|
440 {
|
|
441 struct _C_hashtable htbl;
|
|
442 CHECK_HASHTABLE (table);
|
|
443 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
444 clrhash (&htbl);
|
|
445 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
446 return Qnil;
|
|
447 }
|
|
448
|
20
|
449 DEFUN ("hashtable-fullness", Fhashtable_fullness, 1, 1, 0, /*
|
0
|
450 Return number of entries in TABLE.
|
20
|
451 */
|
|
452 (table))
|
0
|
453 {
|
|
454 struct _C_hashtable htbl;
|
|
455 CHECK_HASHTABLE (table);
|
|
456 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
457 return (make_int (htbl.fullness));
|
|
458 }
|
|
459
|
|
460
|
|
461 static void
|
|
462 verify_function (Lisp_Object function, CONST char *description)
|
|
463 {
|
|
464 if (SYMBOLP (function))
|
|
465 {
|
|
466 if (NILP (function))
|
|
467 return;
|
|
468 else
|
|
469 function = indirect_function (function, 1);
|
|
470 }
|
|
471 if (SUBRP (function) || COMPILED_FUNCTIONP (function))
|
|
472 return;
|
|
473 else if (CONSP (function))
|
|
474 {
|
|
475 Lisp_Object funcar = Fcar (function);
|
|
476 if ((SYMBOLP (funcar))
|
|
477 && (EQ (funcar, Qlambda)
|
|
478 #ifdef MOCKLISP_SUPPORT
|
|
479 || EQ (funcar, Qmocklisp)
|
|
480 #endif
|
|
481 || EQ (funcar, Qautoload)))
|
|
482 return;
|
|
483 }
|
|
484 signal_error (Qinvalid_function, list1 (function));
|
|
485 }
|
|
486
|
|
487 static void
|
|
488 lisp_maphash_function (CONST void *void_key,
|
|
489 void *void_val,
|
|
490 void *void_fn)
|
|
491 {
|
|
492 /* This function can GC */
|
|
493 Lisp_Object key, val, fn;
|
|
494 CVOID_TO_LISP (key, void_key);
|
|
495 VOID_TO_LISP (val, void_val);
|
|
496 VOID_TO_LISP (fn, void_fn);
|
|
497 call2 (fn, key, val);
|
|
498 }
|
|
499
|
|
500
|
20
|
501 DEFUN ("maphash", Fmaphash, 2, 2, 0, /*
|
0
|
502 Map FUNCTION over entries in TABLE, calling it with two args,
|
|
503 each key and value in the table.
|
20
|
504 */
|
|
505 (function, table))
|
0
|
506 {
|
|
507 struct _C_hashtable htbl;
|
|
508 struct gcpro gcpro1, gcpro2;
|
|
509
|
|
510 verify_function (function, GETTEXT ("hashtable mapping function"));
|
|
511 CHECK_HASHTABLE (table);
|
|
512 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
513 GCPRO2 (table, function);
|
|
514 maphash (lisp_maphash_function, &htbl, LISP_TO_VOID (function));
|
|
515 UNGCPRO;
|
|
516 return Qnil;
|
|
517 }
|
|
518
|
|
519
|
|
520 /* This function is for mapping a *C* function over the elements of a
|
|
521 lisp hashtable.
|
|
522 */
|
|
523 void
|
|
524 elisp_maphash (maphash_function function, Lisp_Object table, void *closure)
|
|
525 {
|
|
526 struct _C_hashtable htbl;
|
|
527
|
|
528 if (!gc_in_progress) CHECK_HASHTABLE (table);
|
|
529 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
530 maphash (function, &htbl, closure);
|
|
531 }
|
|
532
|
|
533 void
|
|
534 elisp_map_remhash (remhash_predicate function,
|
|
535 Lisp_Object table,
|
|
536 void *closure)
|
|
537 {
|
|
538 struct _C_hashtable htbl;
|
|
539
|
|
540 if (!gc_in_progress) CHECK_HASHTABLE (table);
|
|
541 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
542 map_remhash (function, &htbl, closure);
|
|
543 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
544 }
|
|
545
|
|
546 #if 0
|
|
547 void
|
|
548 elisp_table_op (Lisp_Object table, generic_hashtable_op op, void *arg1,
|
|
549 void *arg2, void *arg3)
|
|
550 {
|
|
551 struct _C_hashtable htbl;
|
|
552 CHECK_HASHTABLE (table);
|
|
553 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
554 (*op) (&htbl, arg1, arg2, arg3);
|
|
555 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
556 }
|
|
557 #endif /* 0 */
|
|
558
|
|
559
|
|
560
|
20
|
561 DEFUN ("make-weak-hashtable", Fmake_weak_hashtable, 1, 2, 0, /*
|
0
|
562 Make a fully weak hashtable of initial size SIZE.
|
|
563 A weak hashtable is one whose pointers do not count as GC referents:
|
|
564 for any key-value pair in the hashtable, if the only remaining pointer
|
|
565 to either the key or the value is in a weak hash table, then the pair
|
|
566 will be removed from the table, and the key and value collected. A
|
|
567 non-weak hash table (or any other pointer) would prevent the object
|
|
568 from being collected.
|
|
569
|
|
570 You can also create semi-weak hashtables; see `make-key-weak-hashtable'
|
|
571 and `make-value-weak-hashtable'.
|
20
|
572 */
|
|
573 (size, test_fun))
|
0
|
574 {
|
|
575 CHECK_NATNUM (size);
|
|
576 return make_lisp_hashtable (XINT (size), HASHTABLE_WEAK,
|
|
577 decode_hashtable_test_fun (test_fun));
|
|
578 }
|
|
579
|
20
|
580 DEFUN ("make-key-weak-hashtable", Fmake_key_weak_hashtable, 1, 2, 0, /*
|
0
|
581 Make a key-weak hashtable of initial size SIZE.
|
|
582 A key-weak hashtable is similar to a fully-weak hashtable (see
|
|
583 `make-weak-hashtable') except that a key-value pair will be removed
|
|
584 only if the key remains unmarked outside of weak hashtables. The pair
|
|
585 will remain in the hashtable if the key is pointed to by something other
|
|
586 than a weak hashtable, even if the value is not.
|
20
|
587 */
|
|
588 (size, test_fun))
|
0
|
589 {
|
|
590 CHECK_NATNUM (size);
|
|
591 return make_lisp_hashtable (XINT (size), HASHTABLE_KEY_WEAK,
|
|
592 decode_hashtable_test_fun (test_fun));
|
|
593 }
|
|
594
|
20
|
595 DEFUN ("make-value-weak-hashtable", Fmake_value_weak_hashtable, 1, 2, 0, /*
|
0
|
596 Make a value-weak hashtable of initial size SIZE.
|
|
597 A value-weak hashtable is similar to a fully-weak hashtable (see
|
|
598 `make-weak-hashtable') except that a key-value pair will be removed only
|
|
599 if the value remains unmarked outside of weak hashtables. The pair will
|
|
600 remain in the hashtable if the value is pointed to by something other
|
|
601 than a weak hashtable, even if the key is not.
|
20
|
602 */
|
|
603 (size, test_fun))
|
0
|
604 {
|
|
605 CHECK_NATNUM (size);
|
|
606 return make_lisp_hashtable (XINT (size), HASHTABLE_VALUE_WEAK,
|
|
607 decode_hashtable_test_fun (test_fun));
|
|
608 }
|
|
609
|
|
610 struct marking_closure
|
|
611 {
|
|
612 int (*obj_marked_p) (Lisp_Object);
|
|
613 void (*markobj) (Lisp_Object);
|
|
614 enum hashtable_type type;
|
|
615 int did_mark;
|
|
616 };
|
|
617
|
|
618 static void
|
|
619 marking_mapper (CONST void *key, void *contents, void *closure)
|
|
620 {
|
|
621 Lisp_Object keytem, valuetem;
|
|
622 struct marking_closure *fmh =
|
|
623 (struct marking_closure *) closure;
|
|
624
|
|
625 /* This function is called over each pair in the hashtable.
|
|
626 We complete the marking for semi-weak hashtables. */
|
|
627 CVOID_TO_LISP (keytem, key);
|
|
628 CVOID_TO_LISP (valuetem, contents);
|
|
629
|
|
630 switch (fmh->type)
|
|
631 {
|
|
632 case HASHTABLE_KEY_WEAK:
|
|
633 if ((fmh->obj_marked_p) (keytem) &&
|
|
634 !(fmh->obj_marked_p) (valuetem))
|
|
635 {
|
|
636 (fmh->markobj) (valuetem);
|
|
637 fmh->did_mark = 1;
|
|
638 }
|
|
639 break;
|
|
640
|
|
641 case HASHTABLE_VALUE_WEAK:
|
|
642 if ((fmh->obj_marked_p) (valuetem) &&
|
|
643 !(fmh->obj_marked_p) (keytem))
|
|
644 {
|
|
645 (fmh->markobj) (keytem);
|
|
646 fmh->did_mark = 1;
|
|
647 }
|
|
648 break;
|
|
649
|
|
650 case HASHTABLE_KEY_CAR_WEAK:
|
|
651 if (!CONSP (keytem) || (fmh->obj_marked_p) (XCAR (keytem)))
|
|
652 {
|
|
653 if (!(fmh->obj_marked_p) (keytem))
|
|
654 {
|
|
655 (fmh->markobj) (keytem);
|
|
656 fmh->did_mark = 1;
|
|
657 }
|
|
658 if (!(fmh->obj_marked_p) (valuetem))
|
|
659 {
|
|
660 (fmh->markobj) (valuetem);
|
|
661 fmh->did_mark = 1;
|
|
662 }
|
|
663 }
|
|
664 break;
|
|
665
|
|
666 case HASHTABLE_VALUE_CAR_WEAK:
|
|
667 if (!CONSP (valuetem) || (fmh->obj_marked_p) (XCAR (valuetem)))
|
|
668 {
|
|
669 if (!(fmh->obj_marked_p) (keytem))
|
|
670 {
|
|
671 (fmh->markobj) (keytem);
|
|
672 fmh->did_mark = 1;
|
|
673 }
|
|
674 if (!(fmh->obj_marked_p) (valuetem))
|
|
675 {
|
|
676 (fmh->markobj) (valuetem);
|
|
677 fmh->did_mark = 1;
|
|
678 }
|
|
679 }
|
|
680 break;
|
|
681
|
|
682 default:
|
|
683 abort (); /* Huh? */
|
|
684 }
|
|
685
|
|
686 return;
|
|
687 }
|
|
688
|
|
689 int
|
|
690 finish_marking_weak_hashtables (int (*obj_marked_p) (Lisp_Object),
|
|
691 void (*markobj) (Lisp_Object))
|
|
692 {
|
|
693 Lisp_Object rest;
|
|
694 int did_mark = 0;
|
|
695
|
|
696 for (rest = Vall_weak_hashtables;
|
|
697 !GC_NILP (rest);
|
|
698 rest = XHASHTABLE (rest)->next_weak)
|
|
699 {
|
|
700 enum hashtable_type type;
|
|
701
|
|
702 if (! ((*obj_marked_p) (rest)))
|
|
703 /* The hashtable is probably garbage. Ignore it. */
|
|
704 continue;
|
|
705 type = XHASHTABLE (rest)->type;
|
|
706 if (type == HASHTABLE_KEY_WEAK || type == HASHTABLE_VALUE_WEAK
|
|
707 || type == HASHTABLE_KEY_CAR_WEAK
|
|
708 || type == HASHTABLE_VALUE_CAR_WEAK)
|
|
709 {
|
|
710 struct marking_closure fmh;
|
|
711
|
|
712 fmh.obj_marked_p = obj_marked_p;
|
|
713 fmh.markobj = markobj;
|
|
714 fmh.type = type;
|
|
715 fmh.did_mark = 0;
|
|
716 /* Now, scan over all the pairs. For all pairs that are
|
|
717 half-marked, we may need to mark the other half if we're
|
|
718 keeping this pair. */
|
|
719 elisp_maphash (marking_mapper, rest, &fmh);
|
|
720 if (fmh.did_mark)
|
|
721 did_mark = 1;
|
|
722 }
|
|
723
|
|
724 /* #### If alloc.c mark_object changes, this must change also... */
|
|
725 {
|
|
726 /* Now mark the vector itself. (We don't need to call markobj
|
|
727 here because we know that everything *in* it is already marked,
|
|
728 we just need to prevent the vector itself from disappearing.)
|
|
729 (The remhash above has taken care of zero_entry.)
|
|
730 */
|
|
731 struct Lisp_Vector *ptr = XVECTOR (XHASHTABLE (rest)->harray);
|
|
732 int len = vector_length (ptr);
|
|
733 if (len >= 0)
|
|
734 {
|
|
735 ptr->size = -1 - len;
|
|
736 did_mark = 1;
|
|
737 }
|
|
738 /* else it's already marked (remember, this function is iterated
|
|
739 until marking stops) */
|
|
740 }
|
|
741 }
|
|
742
|
|
743 return did_mark;
|
|
744 }
|
|
745
|
|
746 struct pruning_closure
|
|
747 {
|
|
748 int (*obj_marked_p) (Lisp_Object);
|
|
749 };
|
|
750
|
|
751 static int
|
|
752 pruning_mapper (CONST void *key, CONST void *contents, void *closure)
|
|
753 {
|
|
754 Lisp_Object keytem, valuetem;
|
|
755 struct pruning_closure *fmh =
|
|
756 (struct pruning_closure *) closure;
|
|
757
|
|
758 /* This function is called over each pair in the hashtable.
|
|
759 We remove the pairs that aren't completely marked (everything
|
|
760 that is going to stay ought to have been marked already
|
|
761 by the finish_marking stage). */
|
|
762 CVOID_TO_LISP (keytem, key);
|
|
763 CVOID_TO_LISP (valuetem, contents);
|
|
764
|
|
765 return (! ((*fmh->obj_marked_p) (keytem) &&
|
|
766 (*fmh->obj_marked_p) (valuetem)));
|
|
767 }
|
|
768
|
|
769 void
|
|
770 prune_weak_hashtables (int (*obj_marked_p) (Lisp_Object))
|
|
771 {
|
|
772 Lisp_Object rest, prev = Qnil;
|
|
773 for (rest = Vall_weak_hashtables;
|
|
774 !GC_NILP (rest);
|
|
775 rest = XHASHTABLE (rest)->next_weak)
|
|
776 {
|
|
777 if (! ((*obj_marked_p) (rest)))
|
|
778 {
|
|
779 /* This table itself is garbage. Remove it from the list. */
|
|
780 if (GC_NILP (prev))
|
|
781 Vall_weak_hashtables = XHASHTABLE (rest)->next_weak;
|
|
782 else
|
|
783 XHASHTABLE (prev)->next_weak = XHASHTABLE (rest)->next_weak;
|
|
784 }
|
|
785 else
|
|
786 {
|
|
787 struct pruning_closure fmh;
|
|
788 fmh.obj_marked_p = obj_marked_p;
|
|
789 /* Now, scan over all the pairs. Remove all of the pairs
|
|
790 in which the key or value, or both, is unmarked
|
|
791 (depending on the type of weak hashtable). */
|
|
792 elisp_map_remhash (pruning_mapper, rest, &fmh);
|
|
793 prev = rest;
|
|
794 }
|
|
795 }
|
|
796 }
|
|
797
|
|
798 /* Return a hash value for an array of Lisp_Objects of size SIZE. */
|
|
799
|
|
800 unsigned long
|
|
801 internal_array_hash (Lisp_Object *arr, int size, int depth)
|
|
802 {
|
|
803 int i;
|
|
804 unsigned long hash = 0;
|
|
805
|
|
806 if (size <= 5)
|
|
807 {
|
|
808 for (i = 0; i < size; i++)
|
|
809 hash = HASH2 (hash, internal_hash (arr[i], depth + 1));
|
|
810 return hash;
|
|
811 }
|
|
812
|
|
813 /* just pick five elements scattered throughout the array.
|
|
814 A slightly better approach would be to offset by some
|
|
815 noise factor from the points chosen below. */
|
|
816 for (i = 0; i < 5; i++)
|
|
817 hash = HASH2 (hash, internal_hash (arr[i*size/5], depth + 1));
|
|
818
|
|
819 return hash;
|
|
820 }
|
|
821
|
|
822 /* Return a hash value for a Lisp_Object. This is for use when hashing
|
|
823 objects with the comparison being `equal' (for `eq', you can just
|
|
824 use the Lisp_Object itself as the hash value). You need to make a
|
|
825 tradeoff between the speed of the hash function and how good the
|
|
826 hashing is. In particular, the hash function needs to be FAST,
|
|
827 so you can't just traipse down the whole tree hashing everything
|
|
828 together. Most of the time, objects will differ in the first
|
|
829 few elements you hash. Thus, we only go to a short depth (5)
|
|
830 and only hash at most 5 elements out of a vector. Theoretically
|
|
831 we could still take 5^5 time (a big big number) to compute a
|
|
832 hash, but practically this won't ever happen. */
|
|
833
|
|
834 unsigned long
|
|
835 internal_hash (Lisp_Object obj, int depth)
|
|
836 {
|
|
837 if (depth > 5)
|
|
838 return 0;
|
|
839 if (CONSP (obj))
|
|
840 {
|
|
841 /* no point in worrying about tail recursion, since we're not
|
|
842 going very deep */
|
|
843 return HASH2 (internal_hash (XCAR (obj), depth + 1),
|
|
844 internal_hash (XCDR (obj), depth + 1));
|
|
845 }
|
|
846 else if (STRINGP (obj))
|
14
|
847 return hash_string (XSTRING_DATA (obj), XSTRING_LENGTH (obj));
|
0
|
848 #ifndef LRECORD_VECTOR
|
|
849 else if (VECTORP (obj))
|
|
850 {
|
|
851 struct Lisp_Vector *v = XVECTOR (obj);
|
|
852 return HASH2 (vector_length (v),
|
|
853 internal_array_hash (v->contents, vector_length (v),
|
|
854 depth + 1));
|
|
855 }
|
|
856 #endif /* !LRECORD_VECTOR */
|
|
857 else if (LRECORDP (obj))
|
|
858 {
|
|
859 CONST struct lrecord_implementation
|
|
860 *imp = XRECORD_LHEADER (obj)->implementation;
|
|
861 if (imp->hash)
|
|
862 return ((imp->hash) (obj, depth));
|
|
863 }
|
|
864
|
|
865 return LISP_HASH (obj);
|
|
866 }
|
|
867
|
|
868
|
|
869 /************************************************************************/
|
|
870 /* initialization */
|
|
871 /************************************************************************/
|
|
872
|
|
873 void
|
|
874 syms_of_elhash (void)
|
|
875 {
|
20
|
876 DEFSUBR (Fmake_hashtable);
|
|
877 DEFSUBR (Fcopy_hashtable);
|
|
878 DEFSUBR (Fhashtablep);
|
|
879 DEFSUBR (Fgethash);
|
|
880 DEFSUBR (Fputhash);
|
|
881 DEFSUBR (Fremhash);
|
|
882 DEFSUBR (Fclrhash);
|
|
883 DEFSUBR (Fmaphash);
|
|
884 DEFSUBR (Fhashtable_fullness);
|
|
885 DEFSUBR (Fmake_weak_hashtable);
|
|
886 DEFSUBR (Fmake_key_weak_hashtable);
|
|
887 DEFSUBR (Fmake_value_weak_hashtable);
|
0
|
888 defsymbol (&Qhashtablep, "hashtablep");
|
|
889 }
|
|
890
|
|
891 void
|
|
892 vars_of_elhash (void)
|
|
893 {
|
2
|
894 /* This must NOT be staticpro'd */
|
0
|
895 Vall_weak_hashtables = Qnil;
|
|
896 }
|