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