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