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