0
|
1 /* Lisp interface to hash tables.
|
|
2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995, 1996 Ben Wing.
|
223
|
4 Copyright (C) 1997 Free Software Foundation, Inc.
|
0
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: Not in FSF. */
|
|
24
|
|
25 #include <config.h>
|
|
26 #include "lisp.h"
|
|
27 #include "hash.h"
|
|
28 #include "elhash.h"
|
|
29 #include "bytecode.h"
|
|
30
|
223
|
31 Lisp_Object Qhashtablep, Qhashtable;
|
|
32 Lisp_Object Qweak, Qkey_weak, Qvalue_weak, Qnon_weak;
|
0
|
33
|
|
34 #define LISP_OBJECTS_PER_HENTRY (sizeof (hentry) / sizeof (Lisp_Object))/* 2 */
|
|
35
|
185
|
36 struct hashtable
|
0
|
37 {
|
|
38 struct lcrecord_header header;
|
|
39 unsigned int fullness;
|
|
40 unsigned long (*hash_function) (CONST void *);
|
|
41 int (*test_function) (CONST void *, CONST void *);
|
|
42 Lisp_Object zero_entry;
|
|
43 Lisp_Object harray;
|
|
44 enum hashtable_type type; /* whether and how this hashtable is weak */
|
|
45 Lisp_Object next_weak; /* Used to chain together all of the weak
|
|
46 hashtables. Don't mark through this. */
|
|
47 };
|
|
48
|
|
49 static Lisp_Object Vall_weak_hashtables;
|
|
50
|
|
51 static Lisp_Object mark_hashtable (Lisp_Object, void (*) (Lisp_Object));
|
|
52 static void print_hashtable (Lisp_Object, Lisp_Object, int);
|
|
53 DEFINE_LRECORD_IMPLEMENTATION ("hashtable", hashtable,
|
|
54 mark_hashtable, print_hashtable, 0, 0, 0,
|
185
|
55 struct hashtable);
|
0
|
56
|
|
57 static Lisp_Object
|
|
58 mark_hashtable (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
59 {
|
185
|
60 struct hashtable *table = XHASHTABLE (obj);
|
0
|
61
|
|
62 if (table->type != HASHTABLE_NONWEAK)
|
|
63 {
|
|
64 /* If the table is weak, we don't want to mark the keys and values
|
|
65 (we scan over them after everything else has been marked,
|
|
66 and mark or remove them as necessary). Note that we will mark
|
|
67 the table->harray itself at the same time; it's hard to mark
|
|
68 that here without also marking its contents. */
|
|
69 return Qnil;
|
|
70 }
|
|
71 ((markobj) (table->zero_entry));
|
173
|
72 return table->harray;
|
0
|
73 }
|
223
|
74
|
|
75 /* Printing hashtables.
|
|
76
|
|
77 This is non-trivial, because we use a readable structure-style
|
|
78 syntax for hashtables. This means that a typical hashtable will be
|
|
79 readably printed in the form of:
|
|
80
|
|
81 #s(hashtable size 2 data (key1 value1 key2 value2))
|
|
82
|
|
83 The supported keywords are `type' (non-weak (or nil), weak,
|
|
84 key-weak and value-weak), `test' (eql (or nil), eq or equal),
|
|
85 `size' (a natnum or nil) and `data' (a list).
|
|
86
|
|
87 If `print-readably' is non-nil, then a simpler syntax is used; for
|
|
88 instance:
|
|
89
|
|
90 #<hashtable size 2/13 data (key1 value1 key2 value2) 0x874d>
|
|
91
|
|
92 The data is truncated to four pairs, and the rest is shown with
|
|
93 `...'. The actual printer is non-consing. */
|
|
94
|
|
95 struct print_mapper_arg {
|
|
96 EMACS_INT count; /* Used to implement the truncation
|
|
97 for non-readable printing, as well
|
|
98 as to avoid the unnecessary space
|
|
99 at the beginning. */
|
|
100 Lisp_Object printcharfun;
|
|
101 };
|
|
102
|
|
103 static void
|
|
104 print_hashtable_data_mapper (void *key, void *contents, void *arg)
|
|
105 {
|
|
106 Lisp_Object keytem, valuetem;
|
|
107 struct print_mapper_arg *closure = (struct print_mapper_arg *)arg;
|
|
108
|
|
109 if (closure->count < 4 || print_readably)
|
|
110 {
|
|
111 CVOID_TO_LISP (keytem, key);
|
|
112 CVOID_TO_LISP (valuetem, contents);
|
|
113
|
|
114 if (closure->count)
|
|
115 write_c_string (" ", closure->printcharfun);
|
|
116
|
|
117 print_internal (keytem, closure->printcharfun, 1);
|
|
118 write_c_string (" ", closure->printcharfun);
|
|
119 print_internal (valuetem, closure->printcharfun, 1);
|
|
120 }
|
|
121 ++closure->count;
|
|
122 }
|
|
123
|
|
124 /* Print the data of the hashtable. This maps through a Lisp
|
|
125 hashtable and prints key/value pairs using PRINTCHARFUN. */
|
|
126 static void
|
|
127 print_hashtable_data (Lisp_Object hashtable, Lisp_Object printcharfun)
|
|
128 {
|
|
129 struct print_mapper_arg closure;
|
|
130 closure.count = 0;
|
|
131 closure.printcharfun = printcharfun;
|
|
132
|
|
133 write_c_string (" data (", printcharfun);
|
|
134 elisp_maphash (print_hashtable_data_mapper, hashtable, &closure);
|
|
135 write_c_string ((!print_readably && closure.count > 4) ? " ...)" : ")",
|
|
136 printcharfun);
|
|
137 }
|
|
138
|
|
139 /* Needed for tests. */
|
|
140 static int lisp_object_eql_equal (CONST void *x1, CONST void *x2);
|
|
141 static int lisp_object_equal_equal (CONST void *x1, CONST void *x2);
|
185
|
142
|
0
|
143 static void
|
|
144 print_hashtable (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
145 {
|
185
|
146 struct hashtable *table = XHASHTABLE (obj);
|
223
|
147 char buf[128];
|
|
148
|
|
149 write_c_string (print_readably ? "#s(hashtable" : "#<hashtable",
|
|
150 printcharfun);
|
|
151 if (table->type != HASHTABLE_NONWEAK)
|
|
152 {
|
|
153 sprintf (buf, " type %s",
|
|
154 (table->type == HASHTABLE_WEAK ? "weak" :
|
|
155 table->type == HASHTABLE_KEY_WEAK ? "key-weak" :
|
|
156 table->type == HASHTABLE_VALUE_WEAK ? "value-weak" :
|
|
157 "you-d-better-not-see-this"));
|
|
158 write_c_string (buf, printcharfun);
|
|
159 }
|
|
160 /* These checks are way kludgy... */
|
|
161 if (table->test_function == NULL)
|
|
162 write_c_string (" test eq", printcharfun);
|
|
163 else if (table->test_function == lisp_object_equal_equal)
|
|
164 write_c_string (" test equal", printcharfun);
|
|
165 else if (table->test_function == lisp_object_eql_equal)
|
|
166 ;
|
|
167 else
|
|
168 abort ();
|
|
169 if (table->fullness || !print_readably)
|
|
170 {
|
|
171 if (print_readably)
|
|
172 sprintf (buf, " size %d", table->fullness);
|
|
173 else
|
|
174 sprintf (buf, " size %u/%ld", table->fullness,
|
|
175 XVECTOR_LENGTH (table->harray) / LISP_OBJECTS_PER_HENTRY);
|
|
176 write_c_string (buf, printcharfun);
|
|
177 }
|
|
178 if (table->fullness)
|
|
179 print_hashtable_data (obj, printcharfun);
|
0
|
180 if (print_readably)
|
223
|
181 write_c_string (")", printcharfun);
|
|
182 else
|
|
183 {
|
|
184 sprintf (buf, " 0x%x>", table->header.uid);
|
|
185 write_c_string (buf, printcharfun);
|
|
186 }
|
|
187 }
|
|
188
|
|
189
|
|
190 /* Pretty reading of hashtables.
|
|
191
|
|
192 Here we use the existing structures mechanism (which is,
|
|
193 unfortunately, pretty cumbersome) for validating and instantiating
|
|
194 the hashtables. The idea is that the side-effect of reading a
|
|
195 #s(hashtable PLIST) object is creation of a hashtable with desired
|
|
196 properties, and that the hashtable is returned. */
|
|
197
|
|
198 /* Validation functions: each keyword provides its own validation
|
|
199 function. The errors should maybe be continuable, but it is
|
|
200 unclear how this would cope with ERRB. */
|
|
201 static int
|
|
202 hashtable_type_validate (Lisp_Object keyword, Lisp_Object value,
|
|
203 Error_behavior errb)
|
|
204 {
|
|
205 if (!(NILP (value)
|
|
206 || EQ (value, Qnon_weak)
|
|
207 || EQ (value, Qweak)
|
|
208 || EQ (value, Qkey_weak)
|
|
209 || EQ (value, Qvalue_weak)))
|
|
210 {
|
|
211 maybe_signal_simple_error ("Invalid hashtable type", value,
|
|
212 Qhashtable, errb);
|
|
213 return 0;
|
|
214 }
|
|
215 return 1;
|
|
216 }
|
|
217
|
|
218 static int
|
|
219 hashtable_test_validate (Lisp_Object keyword, Lisp_Object value,
|
|
220 Error_behavior errb)
|
|
221 {
|
|
222 if (!(NILP (value)
|
|
223 || EQ (value, Qeq)
|
|
224 || EQ (value, Qeql)
|
|
225 || EQ (value, Qequal)))
|
|
226 {
|
|
227 maybe_signal_simple_error ("Invalid hashtable test", value,
|
|
228 Qhashtable, errb);
|
|
229 return 0;
|
|
230 }
|
|
231 return 1;
|
|
232 }
|
|
233
|
|
234 static int
|
|
235 hashtable_size_validate (Lisp_Object keyword, Lisp_Object value,
|
|
236 Error_behavior errb)
|
|
237 {
|
|
238 if (!NATNUMP (value))
|
|
239 {
|
|
240 maybe_signal_error (Qwrong_type_argument, list2 (Qnatnump, value),
|
|
241 Qhashtable, errb);
|
|
242 return 0;
|
|
243 }
|
|
244 return 1;
|
0
|
245 }
|
|
246
|
223
|
247 static int
|
|
248 hashtable_data_validate (Lisp_Object keyword, Lisp_Object value,
|
|
249 Error_behavior errb)
|
|
250 {
|
|
251 int num = 0;
|
|
252 Lisp_Object tail;
|
|
253
|
|
254 /* #### Doesn't respect ERRB! */
|
|
255 EXTERNAL_LIST_LOOP (tail, value)
|
|
256 {
|
|
257 ++num;
|
|
258 QUIT;
|
|
259 }
|
|
260 if (num & 1)
|
|
261 {
|
|
262 maybe_signal_simple_error
|
|
263 ("Hashtable data must have alternating keyword/value pairs", value,
|
|
264 Qhashtable, errb);
|
|
265 return 0;
|
|
266 }
|
|
267 return 1;
|
|
268 }
|
|
269
|
|
270 /* The actual instantiation of hashtable. This does practically no
|
|
271 error checking, because it relies on the fact that the paranoid
|
|
272 functions above have error-checked everything to the last details.
|
|
273 If this assumption is wrong, we will get a crash immediately (with
|
|
274 error-checking compiled in), and we'll know if there is a bug in
|
|
275 the structure mechanism. So there. */
|
|
276 static Lisp_Object
|
|
277 hashtable_instantiate (Lisp_Object plist)
|
|
278 {
|
|
279 /* I'm not sure whether this can GC, but better safe than sorry. */
|
|
280 Lisp_Object hashtab = Qnil;
|
|
281 Lisp_Object type = Qnil, test = Qnil, size = Qnil, data = Qnil;
|
|
282 Lisp_Object key, value;
|
|
283 struct gcpro gcpro1;
|
|
284 GCPRO1 (hashtab);
|
|
285
|
|
286 while (!NILP (plist))
|
|
287 {
|
|
288 key = XCAR (plist);
|
|
289 plist = XCDR (plist);
|
|
290 value = XCAR (plist);
|
|
291 plist = XCDR (plist);
|
|
292 if (EQ (key, Qtype))
|
|
293 type = value;
|
|
294 else if (EQ (key, Qtest))
|
|
295 test = value;
|
|
296 else if (EQ (key, Qsize))
|
|
297 size = value;
|
|
298 else if (EQ (key, Qdata))
|
|
299 data = value;
|
|
300 else
|
|
301 abort ();
|
|
302 }
|
|
303 if (NILP (type))
|
|
304 type = Qnon_weak;
|
|
305 if (NILP (size))
|
|
306 {
|
|
307 /* Divide by two, because data is a plist. */
|
|
308 XSETINT (size, XINT (Flength (data)) / 2);
|
|
309 }
|
|
310
|
|
311 /* Create the hashtable. */
|
|
312 if (EQ (type, Qnon_weak))
|
|
313 hashtab = Fmake_hashtable (size, test);
|
|
314 else if (EQ (type, Qweak))
|
|
315 hashtab = Fmake_weak_hashtable (size, test);
|
|
316 else if (EQ (type, Qkey_weak))
|
|
317 hashtab = Fmake_key_weak_hashtable (size, test);
|
|
318 else if (EQ (type, Qvalue_weak))
|
|
319 hashtab = Fmake_value_weak_hashtable (size, test);
|
|
320 else
|
|
321 abort ();
|
|
322
|
|
323 /* And fill it with data. */
|
|
324 while (!NILP (data))
|
|
325 {
|
|
326 key = XCAR (data);
|
|
327 data = XCDR (data);
|
|
328 value = XCAR (data);
|
|
329 data = XCDR (data);
|
|
330 Fputhash (key, value, hashtab);
|
|
331 }
|
|
332
|
|
333 UNGCPRO;
|
|
334 return hashtab;
|
|
335 }
|
|
336
|
|
337 /* Initialize the hashtable as a structure type. This is called from
|
|
338 emacs.c. */
|
|
339 void
|
|
340 structure_type_create_hashtable (void)
|
|
341 {
|
|
342 struct structure_type *st;
|
|
343
|
|
344 st = define_structure_type (Qhashtable, 0, hashtable_instantiate);
|
|
345 define_structure_type_keyword (st, Qtype, hashtable_type_validate);
|
|
346 define_structure_type_keyword (st, Qtest, hashtable_test_validate);
|
|
347 define_structure_type_keyword (st, Qsize, hashtable_size_validate);
|
|
348 define_structure_type_keyword (st, Qdata, hashtable_data_validate);
|
|
349 }
|
|
350
|
|
351 /* Basic conversion and allocation functions. */
|
|
352
|
|
353 /* Create a C hashtable from the data in the Lisp hashtable. The
|
|
354 actual vector is not copied, nor are the keys or values copied. */
|
0
|
355 static void
|
185
|
356 ht_copy_to_c (struct hashtable *ht, c_hashtable c_table)
|
0
|
357 {
|
173
|
358 int len = XVECTOR_LENGTH (ht->harray);
|
0
|
359
|
185
|
360 c_table->harray = (hentry *) XVECTOR_DATA (ht->harray);
|
0
|
361 c_table->zero_set = (!GC_UNBOUNDP (ht->zero_entry));
|
|
362 c_table->zero_entry = LISP_TO_VOID (ht->zero_entry);
|
207
|
363 #ifndef LRECORD_VECTOR
|
0
|
364 if (len < 0)
|
|
365 {
|
|
366 /* #### if alloc.c mark_object() changes, this must change too. */
|
|
367 /* barf gag retch. When a vector is marked, its len is
|
|
368 made less than 0. In the prune_weak_hashtables() stage,
|
|
369 we are called on vectors that are like this, and we must
|
|
370 be able to deal. */
|
|
371 assert (gc_in_progress);
|
|
372 len = -1 - len;
|
|
373 }
|
207
|
374 #endif
|
173
|
375 c_table->size = len/LISP_OBJECTS_PER_HENTRY;
|
|
376 c_table->fullness = ht->fullness;
|
0
|
377 c_table->hash_function = ht->hash_function;
|
|
378 c_table->test_function = ht->test_function;
|
|
379 XSETHASHTABLE (c_table->elisp_table, ht);
|
|
380 }
|
|
381
|
|
382 static void
|
185
|
383 ht_copy_from_c (c_hashtable c_table, struct hashtable *ht)
|
0
|
384 {
|
|
385 struct Lisp_Vector dummy;
|
|
386 /* C is truly hateful */
|
|
387 void *vec_addr
|
185
|
388 = ((char *) c_table->harray
|
173
|
389 - ((char *) &(dummy.contents[0]) - (char *) &dummy));
|
0
|
390
|
|
391 XSETVECTOR (ht->harray, vec_addr);
|
|
392 if (c_table->zero_set)
|
|
393 VOID_TO_LISP (ht->zero_entry, c_table->zero_entry);
|
|
394 else
|
|
395 ht->zero_entry = Qunbound;
|
|
396 ht->fullness = c_table->fullness;
|
|
397 }
|
|
398
|
|
399
|
185
|
400 static struct hashtable *
|
0
|
401 allocate_hashtable (void)
|
|
402 {
|
185
|
403 struct hashtable *table =
|
|
404 alloc_lcrecord_type (struct hashtable, lrecord_hashtable);
|
173
|
405 table->harray = Qnil;
|
|
406 table->zero_entry = Qunbound;
|
|
407 table->fullness = 0;
|
0
|
408 table->hash_function = 0;
|
|
409 table->test_function = 0;
|
173
|
410 return table;
|
0
|
411 }
|
|
412
|
173
|
413 void *
|
0
|
414 elisp_hvector_malloc (unsigned int bytes, Lisp_Object table)
|
|
415 {
|
|
416 Lisp_Object new_vector;
|
185
|
417 struct hashtable *ht = XHASHTABLE (table);
|
0
|
418
|
173
|
419 assert (bytes > XVECTOR_LENGTH (ht->harray) * sizeof (Lisp_Object));
|
207
|
420 new_vector = make_vector ((bytes / sizeof (Lisp_Object)), Qnull_pointer);
|
173
|
421 return (void *) XVECTOR_DATA (new_vector);
|
0
|
422 }
|
|
423
|
|
424 void
|
|
425 elisp_hvector_free (void *ptr, Lisp_Object table)
|
|
426 {
|
185
|
427 struct hashtable *ht = XHASHTABLE (table);
|
0
|
428 #if defined (USE_ASSERTIONS) || defined (DEBUG_XEMACS)
|
|
429 Lisp_Object current_vector = ht->harray;
|
|
430 #endif
|
|
431
|
173
|
432 assert (((void *) XVECTOR_DATA (current_vector)) == ptr);
|
0
|
433 ht->harray = Qnil; /* Let GC do its job */
|
|
434 }
|
|
435
|
|
436
|
20
|
437 DEFUN ("hashtablep", Fhashtablep, 1, 1, 0, /*
|
0
|
438 Return t if OBJ is a hashtable, else nil.
|
20
|
439 */
|
|
440 (obj))
|
0
|
441 {
|
173
|
442 return HASHTABLEP (obj) ? Qt : Qnil;
|
0
|
443 }
|
|
444
|
|
445
|
|
446
|
|
447
|
|
448 #if 0 /* I don't think these are needed any more.
|
|
449 If using the general lisp_object_equal_*() functions
|
|
450 causes efficiency problems, these can be resurrected. --ben */
|
|
451 /* equality and hash functions for Lisp strings */
|
|
452 int
|
|
453 lisp_string_equal (CONST void *x1, CONST void *x2)
|
|
454 {
|
|
455 Lisp_Object str1, str2;
|
|
456 CVOID_TO_LISP (str1, x1);
|
|
457 CVOID_TO_LISP (str2, x2);
|
14
|
458 return !strcmp ((char *) XSTRING_DATA (str1), (char *) XSTRING_DATA (str2));
|
0
|
459 }
|
|
460
|
|
461 unsigned long
|
|
462 lisp_string_hash (CONST void *x)
|
|
463 {
|
|
464 Lisp_Object str;
|
|
465 CVOID_TO_LISP (str, x);
|
14
|
466 return hash_string (XSTRING_DATA (str), XSTRING_LENGTH (str));
|
0
|
467 }
|
|
468
|
|
469 #endif /* 0 */
|
|
470
|
|
471 static int
|
|
472 lisp_object_eql_equal (CONST void *x1, CONST void *x2)
|
|
473 {
|
|
474 Lisp_Object obj1, obj2;
|
|
475 CVOID_TO_LISP (obj1, x1);
|
|
476 CVOID_TO_LISP (obj2, x2);
|
195
|
477 return FLOATP (obj1) ? internal_equal (obj1, obj2, 0) : EQ (obj1, obj2);
|
0
|
478 }
|
|
479
|
|
480 static unsigned long
|
|
481 lisp_object_eql_hash (CONST void *x)
|
|
482 {
|
|
483 Lisp_Object obj;
|
|
484 CVOID_TO_LISP (obj, x);
|
|
485 if (FLOATP (obj))
|
|
486 return internal_hash (obj, 0);
|
|
487 else
|
|
488 return LISP_HASH (obj);
|
|
489 }
|
|
490
|
|
491 static int
|
|
492 lisp_object_equal_equal (CONST void *x1, CONST void *x2)
|
|
493 {
|
|
494 Lisp_Object obj1, obj2;
|
|
495 CVOID_TO_LISP (obj1, x1);
|
|
496 CVOID_TO_LISP (obj2, x2);
|
195
|
497 return internal_equal (obj1, obj2, 0);
|
0
|
498 }
|
|
499
|
|
500 static unsigned long
|
|
501 lisp_object_equal_hash (CONST void *x)
|
|
502 {
|
|
503 Lisp_Object obj;
|
|
504 CVOID_TO_LISP (obj, x);
|
|
505 return internal_hash (obj, 0);
|
|
506 }
|
|
507
|
|
508 Lisp_Object
|
|
509 make_lisp_hashtable (int size,
|
|
510 enum hashtable_type type,
|
|
511 enum hashtable_test_fun test)
|
|
512 {
|
|
513 Lisp_Object result;
|
185
|
514 struct hashtable *table = allocate_hashtable ();
|
0
|
515
|
|
516 table->harray = make_vector ((compute_harray_size (size)
|
|
517 * LISP_OBJECTS_PER_HENTRY),
|
207
|
518 Qnull_pointer);
|
0
|
519 switch (test)
|
|
520 {
|
|
521 case HASHTABLE_EQ:
|
187
|
522 table->test_function = NULL;
|
|
523 table->hash_function = NULL;
|
0
|
524 break;
|
|
525
|
|
526 case HASHTABLE_EQL:
|
|
527 table->test_function = lisp_object_eql_equal;
|
|
528 table->hash_function = lisp_object_eql_hash;
|
|
529 break;
|
|
530
|
|
531 case HASHTABLE_EQUAL:
|
|
532 table->test_function = lisp_object_equal_equal;
|
|
533 table->hash_function = lisp_object_equal_hash;
|
|
534 break;
|
|
535
|
|
536 default:
|
|
537 abort ();
|
|
538 }
|
|
539
|
|
540 table->type = type;
|
|
541 XSETHASHTABLE (result, table);
|
|
542
|
|
543 if (table->type != HASHTABLE_NONWEAK)
|
|
544 {
|
|
545 table->next_weak = Vall_weak_hashtables;
|
|
546 Vall_weak_hashtables = result;
|
|
547 }
|
|
548 else
|
|
549 table->next_weak = Qunbound;
|
|
550
|
173
|
551 return result;
|
0
|
552 }
|
|
553
|
|
554 static enum hashtable_test_fun
|
|
555 decode_hashtable_test_fun (Lisp_Object sym)
|
|
556 {
|
187
|
557 if (NILP (sym)) return HASHTABLE_EQL;
|
2
|
558 if (EQ (sym, Qeq)) return HASHTABLE_EQ;
|
|
559 if (EQ (sym, Qequal)) return HASHTABLE_EQUAL;
|
|
560 if (EQ (sym, Qeql)) return HASHTABLE_EQL;
|
185
|
561
|
0
|
562 signal_simple_error ("Invalid hashtable test fun", sym);
|
2
|
563 return HASHTABLE_EQ; /* not reached */
|
0
|
564 }
|
|
565
|
20
|
566 DEFUN ("make-hashtable", Fmake_hashtable, 1, 2, 0, /*
|
0
|
567 Make a hashtable of initial size SIZE.
|
|
568 Comparison between keys is done with TEST-FUN, which must be one of
|
|
569 `eq', `eql', or `equal'. The default is `eql'; i.e. two keys must
|
|
570 be the same object (or have the same floating-point value, for floats)
|
|
571 to be considered equivalent.
|
|
572
|
|
573 See also `make-weak-hashtable', `make-key-weak-hashtable', and
|
|
574 `make-value-weak-hashtable'.
|
20
|
575 */
|
|
576 (size, test_fun))
|
0
|
577 {
|
|
578 CHECK_NATNUM (size);
|
|
579 return make_lisp_hashtable (XINT (size), HASHTABLE_NONWEAK,
|
|
580 decode_hashtable_test_fun (test_fun));
|
|
581 }
|
|
582
|
20
|
583 DEFUN ("copy-hashtable", Fcopy_hashtable, 1, 1, 0, /*
|
0
|
584 Make a new hashtable which contains the same keys and values
|
|
585 as the given table. The keys and values will not themselves be copied.
|
20
|
586 */
|
|
587 (old_table))
|
0
|
588 {
|
|
589 struct _C_hashtable old_htbl;
|
|
590 struct _C_hashtable new_htbl;
|
185
|
591 struct hashtable *old_ht;
|
|
592 struct hashtable *new_ht;
|
0
|
593 Lisp_Object result;
|
|
594
|
|
595 CHECK_HASHTABLE (old_table);
|
|
596 old_ht = XHASHTABLE (old_table);
|
|
597 ht_copy_to_c (old_ht, &old_htbl);
|
|
598
|
|
599 /* we can't just call Fmake_hashtable() here because that will make a
|
|
600 table that is slightly larger than the one we're trying to copy,
|
|
601 which will make copy_hash() blow up. */
|
|
602 new_ht = allocate_hashtable ();
|
|
603 new_ht->fullness = 0;
|
|
604 new_ht->zero_entry = Qunbound;
|
|
605 new_ht->hash_function = old_ht->hash_function;
|
|
606 new_ht->test_function = old_ht->test_function;
|
207
|
607 new_ht->harray = Fmake_vector (Flength (old_ht->harray), Qnull_pointer);
|
0
|
608 ht_copy_to_c (new_ht, &new_htbl);
|
|
609 copy_hash (&new_htbl, &old_htbl);
|
|
610 ht_copy_from_c (&new_htbl, new_ht);
|
|
611 new_ht->type = old_ht->type;
|
|
612 XSETHASHTABLE (result, new_ht);
|
|
613
|
|
614 if (UNBOUNDP (old_ht->next_weak))
|
|
615 new_ht->next_weak = Qunbound;
|
|
616 else
|
|
617 {
|
|
618 new_ht->next_weak = Vall_weak_hashtables;
|
|
619 Vall_weak_hashtables = result;
|
|
620 }
|
|
621
|
173
|
622 return result;
|
0
|
623 }
|
|
624
|
|
625
|
20
|
626 DEFUN ("gethash", Fgethash, 2, 3, 0, /*
|
187
|
627 Find hash value for KEY in HASHTABLE.
|
0
|
628 If there is no corresponding value, return DEFAULT (defaults to nil).
|
20
|
629 */
|
187
|
630 (key, hashtable, default_))
|
0
|
631 {
|
|
632 CONST void *vval;
|
|
633 struct _C_hashtable htbl;
|
|
634 if (!gc_in_progress)
|
187
|
635 CHECK_HASHTABLE (hashtable);
|
|
636 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
0
|
637 if (gethash (LISP_TO_VOID (key), &htbl, &vval))
|
|
638 {
|
|
639 Lisp_Object val;
|
|
640 CVOID_TO_LISP (val, vval);
|
|
641 return val;
|
|
642 }
|
185
|
643 else
|
173
|
644 return default_;
|
0
|
645 }
|
|
646
|
|
647
|
20
|
648 DEFUN ("remhash", Fremhash, 2, 2, 0, /*
|
187
|
649 Remove hash value for KEY in HASHTABLE.
|
20
|
650 */
|
187
|
651 (key, hashtable))
|
0
|
652 {
|
|
653 struct _C_hashtable htbl;
|
187
|
654 CHECK_HASHTABLE (hashtable);
|
0
|
655
|
187
|
656 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
0
|
657 remhash (LISP_TO_VOID (key), &htbl);
|
187
|
658 ht_copy_from_c (&htbl, XHASHTABLE (hashtable));
|
0
|
659 return Qnil;
|
|
660 }
|
|
661
|
|
662
|
20
|
663 DEFUN ("puthash", Fputhash, 3, 3, 0, /*
|
187
|
664 Hash KEY to VAL in HASHTABLE.
|
20
|
665 */
|
187
|
666 (key, val, hashtable))
|
0
|
667 {
|
185
|
668 struct hashtable *ht;
|
0
|
669 void *vkey = LISP_TO_VOID (key);
|
|
670
|
187
|
671 CHECK_HASHTABLE (hashtable);
|
|
672 ht = XHASHTABLE (hashtable);
|
0
|
673 if (!vkey)
|
|
674 ht->zero_entry = val;
|
|
675 else
|
|
676 {
|
|
677 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
678 struct _C_hashtable htbl;
|
|
679
|
187
|
680 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
|
681 GCPRO3 (key, val, hashtable);
|
0
|
682 puthash (vkey, LISP_TO_VOID (val), &htbl);
|
187
|
683 ht_copy_from_c (&htbl, XHASHTABLE (hashtable));
|
0
|
684 UNGCPRO;
|
|
685 }
|
173
|
686 return val;
|
0
|
687 }
|
|
688
|
20
|
689 DEFUN ("clrhash", Fclrhash, 1, 1, 0, /*
|
187
|
690 Remove all entries from HASHTABLE.
|
20
|
691 */
|
187
|
692 (hashtable))
|
0
|
693 {
|
|
694 struct _C_hashtable htbl;
|
187
|
695 CHECK_HASHTABLE (hashtable);
|
|
696 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
0
|
697 clrhash (&htbl);
|
187
|
698 ht_copy_from_c (&htbl, XHASHTABLE (hashtable));
|
0
|
699 return Qnil;
|
|
700 }
|
|
701
|
20
|
702 DEFUN ("hashtable-fullness", Fhashtable_fullness, 1, 1, 0, /*
|
187
|
703 Return number of entries in HASHTABLE.
|
20
|
704 */
|
187
|
705 (hashtable))
|
0
|
706 {
|
|
707 struct _C_hashtable htbl;
|
187
|
708 CHECK_HASHTABLE (hashtable);
|
|
709 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
173
|
710 return make_int (htbl.fullness);
|
0
|
711 }
|
|
712
|
|
713
|
|
714 static void
|
|
715 verify_function (Lisp_Object function, CONST char *description)
|
|
716 {
|
223
|
717 /* #### Unused DESCRIPTION? */
|
0
|
718 if (SYMBOLP (function))
|
|
719 {
|
|
720 if (NILP (function))
|
|
721 return;
|
|
722 else
|
|
723 function = indirect_function (function, 1);
|
|
724 }
|
|
725 if (SUBRP (function) || COMPILED_FUNCTIONP (function))
|
|
726 return;
|
|
727 else if (CONSP (function))
|
|
728 {
|
223
|
729 Lisp_Object funcar = XCAR (function);
|
187
|
730 if ((SYMBOLP (funcar)) && (EQ (funcar, Qlambda) ||
|
|
731 EQ (funcar, Qautoload)))
|
0
|
732 return;
|
|
733 }
|
|
734 signal_error (Qinvalid_function, list1 (function));
|
|
735 }
|
|
736
|
|
737 static void
|
|
738 lisp_maphash_function (CONST void *void_key,
|
|
739 void *void_val,
|
|
740 void *void_fn)
|
|
741 {
|
|
742 /* This function can GC */
|
|
743 Lisp_Object key, val, fn;
|
|
744 CVOID_TO_LISP (key, void_key);
|
|
745 VOID_TO_LISP (val, void_val);
|
|
746 VOID_TO_LISP (fn, void_fn);
|
|
747 call2 (fn, key, val);
|
|
748 }
|
|
749
|
|
750
|
20
|
751 DEFUN ("maphash", Fmaphash, 2, 2, 0, /*
|
187
|
752 Map FUNCTION over entries in HASHTABLE, calling it with two args,
|
0
|
753 each key and value in the table.
|
20
|
754 */
|
187
|
755 (function, hashtable))
|
0
|
756 {
|
|
757 struct _C_hashtable htbl;
|
|
758 struct gcpro gcpro1, gcpro2;
|
|
759
|
|
760 verify_function (function, GETTEXT ("hashtable mapping function"));
|
187
|
761 CHECK_HASHTABLE (hashtable);
|
|
762 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
|
763 GCPRO2 (hashtable, function);
|
0
|
764 maphash (lisp_maphash_function, &htbl, LISP_TO_VOID (function));
|
|
765 UNGCPRO;
|
|
766 return Qnil;
|
|
767 }
|
|
768
|
|
769
|
|
770 /* This function is for mapping a *C* function over the elements of a
|
|
771 lisp hashtable.
|
|
772 */
|
|
773 void
|
187
|
774 elisp_maphash (maphash_function function, Lisp_Object hashtable, void *closure)
|
0
|
775 {
|
|
776 struct _C_hashtable htbl;
|
|
777
|
187
|
778 if (!gc_in_progress) CHECK_HASHTABLE (hashtable);
|
|
779 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
0
|
780 maphash (function, &htbl, closure);
|
|
781 }
|
|
782
|
|
783 void
|
187
|
784 elisp_map_remhash (remhash_predicate function, Lisp_Object hashtable,
|
|
785 void *closure)
|
0
|
786 {
|
|
787 struct _C_hashtable htbl;
|
|
788
|
187
|
789 if (!gc_in_progress) CHECK_HASHTABLE (hashtable);
|
|
790 ht_copy_to_c (XHASHTABLE (hashtable), &htbl);
|
0
|
791 map_remhash (function, &htbl, closure);
|
187
|
792 ht_copy_from_c (&htbl, XHASHTABLE (hashtable));
|
0
|
793 }
|
|
794
|
|
795 #if 0
|
|
796 void
|
|
797 elisp_table_op (Lisp_Object table, generic_hashtable_op op, void *arg1,
|
|
798 void *arg2, void *arg3)
|
|
799 {
|
|
800 struct _C_hashtable htbl;
|
|
801 CHECK_HASHTABLE (table);
|
|
802 ht_copy_to_c (XHASHTABLE (table), &htbl);
|
|
803 (*op) (&htbl, arg1, arg2, arg3);
|
|
804 ht_copy_from_c (&htbl, XHASHTABLE (table));
|
|
805 }
|
|
806 #endif /* 0 */
|
|
807
|
|
808
|
|
809
|
20
|
810 DEFUN ("make-weak-hashtable", Fmake_weak_hashtable, 1, 2, 0, /*
|
0
|
811 Make a fully weak hashtable of initial size SIZE.
|
|
812 A weak hashtable is one whose pointers do not count as GC referents:
|
|
813 for any key-value pair in the hashtable, if the only remaining pointer
|
|
814 to either the key or the value is in a weak hash table, then the pair
|
|
815 will be removed from the table, and the key and value collected. A
|
|
816 non-weak hash table (or any other pointer) would prevent the object
|
|
817 from being collected.
|
|
818
|
|
819 You can also create semi-weak hashtables; see `make-key-weak-hashtable'
|
|
820 and `make-value-weak-hashtable'.
|
20
|
821 */
|
|
822 (size, test_fun))
|
0
|
823 {
|
|
824 CHECK_NATNUM (size);
|
|
825 return make_lisp_hashtable (XINT (size), HASHTABLE_WEAK,
|
|
826 decode_hashtable_test_fun (test_fun));
|
|
827 }
|
|
828
|
20
|
829 DEFUN ("make-key-weak-hashtable", Fmake_key_weak_hashtable, 1, 2, 0, /*
|
0
|
830 Make a key-weak hashtable of initial size SIZE.
|
|
831 A key-weak hashtable is similar to a fully-weak hashtable (see
|
|
832 `make-weak-hashtable') except that a key-value pair will be removed
|
|
833 only if the key remains unmarked outside of weak hashtables. The pair
|
|
834 will remain in the hashtable if the key is pointed to by something other
|
|
835 than a weak hashtable, even if the value is not.
|
20
|
836 */
|
|
837 (size, test_fun))
|
0
|
838 {
|
|
839 CHECK_NATNUM (size);
|
|
840 return make_lisp_hashtable (XINT (size), HASHTABLE_KEY_WEAK,
|
|
841 decode_hashtable_test_fun (test_fun));
|
|
842 }
|
|
843
|
20
|
844 DEFUN ("make-value-weak-hashtable", Fmake_value_weak_hashtable, 1, 2, 0, /*
|
0
|
845 Make a value-weak hashtable of initial size SIZE.
|
|
846 A value-weak hashtable is similar to a fully-weak hashtable (see
|
|
847 `make-weak-hashtable') except that a key-value pair will be removed only
|
|
848 if the value remains unmarked outside of weak hashtables. The pair will
|
|
849 remain in the hashtable if the value is pointed to by something other
|
|
850 than a weak hashtable, even if the key is not.
|
20
|
851 */
|
|
852 (size, test_fun))
|
0
|
853 {
|
|
854 CHECK_NATNUM (size);
|
|
855 return make_lisp_hashtable (XINT (size), HASHTABLE_VALUE_WEAK,
|
|
856 decode_hashtable_test_fun (test_fun));
|
|
857 }
|
|
858
|
|
859 struct marking_closure
|
|
860 {
|
|
861 int (*obj_marked_p) (Lisp_Object);
|
|
862 void (*markobj) (Lisp_Object);
|
|
863 enum hashtable_type type;
|
|
864 int did_mark;
|
|
865 };
|
|
866
|
|
867 static void
|
|
868 marking_mapper (CONST void *key, void *contents, void *closure)
|
|
869 {
|
|
870 Lisp_Object keytem, valuetem;
|
|
871 struct marking_closure *fmh =
|
|
872 (struct marking_closure *) closure;
|
|
873
|
|
874 /* This function is called over each pair in the hashtable.
|
|
875 We complete the marking for semi-weak hashtables. */
|
|
876 CVOID_TO_LISP (keytem, key);
|
|
877 CVOID_TO_LISP (valuetem, contents);
|
185
|
878
|
0
|
879 switch (fmh->type)
|
|
880 {
|
|
881 case HASHTABLE_KEY_WEAK:
|
|
882 if ((fmh->obj_marked_p) (keytem) &&
|
|
883 !(fmh->obj_marked_p) (valuetem))
|
|
884 {
|
|
885 (fmh->markobj) (valuetem);
|
|
886 fmh->did_mark = 1;
|
|
887 }
|
|
888 break;
|
|
889
|
|
890 case HASHTABLE_VALUE_WEAK:
|
|
891 if ((fmh->obj_marked_p) (valuetem) &&
|
|
892 !(fmh->obj_marked_p) (keytem))
|
|
893 {
|
|
894 (fmh->markobj) (keytem);
|
|
895 fmh->did_mark = 1;
|
|
896 }
|
|
897 break;
|
|
898
|
|
899 case HASHTABLE_KEY_CAR_WEAK:
|
|
900 if (!CONSP (keytem) || (fmh->obj_marked_p) (XCAR (keytem)))
|
|
901 {
|
|
902 if (!(fmh->obj_marked_p) (keytem))
|
|
903 {
|
|
904 (fmh->markobj) (keytem);
|
|
905 fmh->did_mark = 1;
|
|
906 }
|
|
907 if (!(fmh->obj_marked_p) (valuetem))
|
|
908 {
|
|
909 (fmh->markobj) (valuetem);
|
|
910 fmh->did_mark = 1;
|
|
911 }
|
|
912 }
|
|
913 break;
|
|
914
|
|
915 case HASHTABLE_VALUE_CAR_WEAK:
|
|
916 if (!CONSP (valuetem) || (fmh->obj_marked_p) (XCAR (valuetem)))
|
|
917 {
|
|
918 if (!(fmh->obj_marked_p) (keytem))
|
|
919 {
|
|
920 (fmh->markobj) (keytem);
|
|
921 fmh->did_mark = 1;
|
|
922 }
|
|
923 if (!(fmh->obj_marked_p) (valuetem))
|
|
924 {
|
|
925 (fmh->markobj) (valuetem);
|
|
926 fmh->did_mark = 1;
|
|
927 }
|
|
928 }
|
|
929 break;
|
|
930
|
|
931 default:
|
|
932 abort (); /* Huh? */
|
|
933 }
|
185
|
934
|
0
|
935 return;
|
|
936 }
|
|
937
|
|
938 int
|
|
939 finish_marking_weak_hashtables (int (*obj_marked_p) (Lisp_Object),
|
|
940 void (*markobj) (Lisp_Object))
|
|
941 {
|
|
942 Lisp_Object rest;
|
|
943 int did_mark = 0;
|
|
944
|
|
945 for (rest = Vall_weak_hashtables;
|
|
946 !GC_NILP (rest);
|
|
947 rest = XHASHTABLE (rest)->next_weak)
|
|
948 {
|
|
949 enum hashtable_type type;
|
|
950
|
|
951 if (! ((*obj_marked_p) (rest)))
|
|
952 /* The hashtable is probably garbage. Ignore it. */
|
|
953 continue;
|
|
954 type = XHASHTABLE (rest)->type;
|
187
|
955 if (type == HASHTABLE_KEY_WEAK ||
|
|
956 type == HASHTABLE_VALUE_WEAK ||
|
|
957 type == HASHTABLE_KEY_CAR_WEAK ||
|
|
958 type == HASHTABLE_VALUE_CAR_WEAK)
|
0
|
959 {
|
|
960 struct marking_closure fmh;
|
|
961
|
|
962 fmh.obj_marked_p = obj_marked_p;
|
|
963 fmh.markobj = markobj;
|
|
964 fmh.type = type;
|
|
965 fmh.did_mark = 0;
|
|
966 /* Now, scan over all the pairs. For all pairs that are
|
|
967 half-marked, we may need to mark the other half if we're
|
|
968 keeping this pair. */
|
|
969 elisp_maphash (marking_mapper, rest, &fmh);
|
|
970 if (fmh.did_mark)
|
|
971 did_mark = 1;
|
|
972 }
|
|
973
|
|
974 /* #### If alloc.c mark_object changes, this must change also... */
|
|
975 {
|
|
976 /* Now mark the vector itself. (We don't need to call markobj
|
|
977 here because we know that everything *in* it is already marked,
|
|
978 we just need to prevent the vector itself from disappearing.)
|
|
979 (The remhash above has taken care of zero_entry.)
|
|
980 */
|
|
981 struct Lisp_Vector *ptr = XVECTOR (XHASHTABLE (rest)->harray);
|
207
|
982 #ifdef LRECORD_VECTOR
|
|
983 if (! MARKED_RECORD_P(XHASHTABLE(rest)->harray))
|
|
984 {
|
|
985 MARK_RECORD_HEADER(&(ptr->header.lheader));
|
|
986 did_mark = 1;
|
|
987 }
|
|
988 #else
|
223
|
989 int len = vector_length (ptr);
|
0
|
990 if (len >= 0)
|
|
991 {
|
|
992 ptr->size = -1 - len;
|
|
993 did_mark = 1;
|
|
994 }
|
207
|
995 #endif
|
0
|
996 /* else it's already marked (remember, this function is iterated
|
|
997 until marking stops) */
|
|
998 }
|
|
999 }
|
|
1000
|
|
1001 return did_mark;
|
|
1002 }
|
|
1003
|
|
1004 struct pruning_closure
|
|
1005 {
|
|
1006 int (*obj_marked_p) (Lisp_Object);
|
|
1007 };
|
|
1008
|
|
1009 static int
|
|
1010 pruning_mapper (CONST void *key, CONST void *contents, void *closure)
|
|
1011 {
|
|
1012 Lisp_Object keytem, valuetem;
|
185
|
1013 struct pruning_closure *fmh = (struct pruning_closure *) closure;
|
0
|
1014
|
|
1015 /* This function is called over each pair in the hashtable.
|
|
1016 We remove the pairs that aren't completely marked (everything
|
|
1017 that is going to stay ought to have been marked already
|
|
1018 by the finish_marking stage). */
|
|
1019 CVOID_TO_LISP (keytem, key);
|
|
1020 CVOID_TO_LISP (valuetem, contents);
|
|
1021
|
173
|
1022 return ! ((*fmh->obj_marked_p) (keytem) &&
|
|
1023 (*fmh->obj_marked_p) (valuetem));
|
0
|
1024 }
|
|
1025
|
|
1026 void
|
|
1027 prune_weak_hashtables (int (*obj_marked_p) (Lisp_Object))
|
|
1028 {
|
|
1029 Lisp_Object rest, prev = Qnil;
|
|
1030 for (rest = Vall_weak_hashtables;
|
|
1031 !GC_NILP (rest);
|
|
1032 rest = XHASHTABLE (rest)->next_weak)
|
|
1033 {
|
|
1034 if (! ((*obj_marked_p) (rest)))
|
|
1035 {
|
|
1036 /* This table itself is garbage. Remove it from the list. */
|
|
1037 if (GC_NILP (prev))
|
|
1038 Vall_weak_hashtables = XHASHTABLE (rest)->next_weak;
|
|
1039 else
|
|
1040 XHASHTABLE (prev)->next_weak = XHASHTABLE (rest)->next_weak;
|
|
1041 }
|
|
1042 else
|
|
1043 {
|
|
1044 struct pruning_closure fmh;
|
|
1045 fmh.obj_marked_p = obj_marked_p;
|
|
1046 /* Now, scan over all the pairs. Remove all of the pairs
|
|
1047 in which the key or value, or both, is unmarked
|
|
1048 (depending on the type of weak hashtable). */
|
|
1049 elisp_map_remhash (pruning_mapper, rest, &fmh);
|
|
1050 prev = rest;
|
|
1051 }
|
|
1052 }
|
|
1053 }
|
|
1054
|
|
1055 /* Return a hash value for an array of Lisp_Objects of size SIZE. */
|
|
1056
|
|
1057 unsigned long
|
|
1058 internal_array_hash (Lisp_Object *arr, int size, int depth)
|
|
1059 {
|
|
1060 int i;
|
|
1061 unsigned long hash = 0;
|
|
1062
|
|
1063 if (size <= 5)
|
|
1064 {
|
|
1065 for (i = 0; i < size; i++)
|
|
1066 hash = HASH2 (hash, internal_hash (arr[i], depth + 1));
|
|
1067 return hash;
|
|
1068 }
|
185
|
1069
|
0
|
1070 /* just pick five elements scattered throughout the array.
|
|
1071 A slightly better approach would be to offset by some
|
|
1072 noise factor from the points chosen below. */
|
|
1073 for (i = 0; i < 5; i++)
|
|
1074 hash = HASH2 (hash, internal_hash (arr[i*size/5], depth + 1));
|
185
|
1075
|
0
|
1076 return hash;
|
|
1077 }
|
|
1078
|
|
1079 /* Return a hash value for a Lisp_Object. This is for use when hashing
|
|
1080 objects with the comparison being `equal' (for `eq', you can just
|
|
1081 use the Lisp_Object itself as the hash value). You need to make a
|
|
1082 tradeoff between the speed of the hash function and how good the
|
|
1083 hashing is. In particular, the hash function needs to be FAST,
|
|
1084 so you can't just traipse down the whole tree hashing everything
|
|
1085 together. Most of the time, objects will differ in the first
|
|
1086 few elements you hash. Thus, we only go to a short depth (5)
|
|
1087 and only hash at most 5 elements out of a vector. Theoretically
|
|
1088 we could still take 5^5 time (a big big number) to compute a
|
|
1089 hash, but practically this won't ever happen. */
|
|
1090
|
|
1091 unsigned long
|
|
1092 internal_hash (Lisp_Object obj, int depth)
|
|
1093 {
|
|
1094 if (depth > 5)
|
|
1095 return 0;
|
|
1096 if (CONSP (obj))
|
|
1097 {
|
|
1098 /* no point in worrying about tail recursion, since we're not
|
|
1099 going very deep */
|
|
1100 return HASH2 (internal_hash (XCAR (obj), depth + 1),
|
|
1101 internal_hash (XCDR (obj), depth + 1));
|
|
1102 }
|
|
1103 else if (STRINGP (obj))
|
14
|
1104 return hash_string (XSTRING_DATA (obj), XSTRING_LENGTH (obj));
|
0
|
1105 else if (VECTORP (obj))
|
|
1106 {
|
|
1107 struct Lisp_Vector *v = XVECTOR (obj);
|
|
1108 return HASH2 (vector_length (v),
|
|
1109 internal_array_hash (v->contents, vector_length (v),
|
|
1110 depth + 1));
|
|
1111 }
|
|
1112 else if (LRECORDP (obj))
|
|
1113 {
|
|
1114 CONST struct lrecord_implementation
|
211
|
1115 *imp = XRECORD_LHEADER_IMPLEMENTATION (obj);
|
0
|
1116 if (imp->hash)
|
173
|
1117 return (imp->hash) (obj, depth);
|
0
|
1118 }
|
|
1119
|
|
1120 return LISP_HASH (obj);
|
|
1121 }
|
|
1122
|
|
1123
|
|
1124 /************************************************************************/
|
|
1125 /* initialization */
|
|
1126 /************************************************************************/
|
|
1127
|
|
1128 void
|
|
1129 syms_of_elhash (void)
|
|
1130 {
|
20
|
1131 DEFSUBR (Fmake_hashtable);
|
|
1132 DEFSUBR (Fcopy_hashtable);
|
|
1133 DEFSUBR (Fhashtablep);
|
|
1134 DEFSUBR (Fgethash);
|
|
1135 DEFSUBR (Fputhash);
|
|
1136 DEFSUBR (Fremhash);
|
|
1137 DEFSUBR (Fclrhash);
|
|
1138 DEFSUBR (Fmaphash);
|
|
1139 DEFSUBR (Fhashtable_fullness);
|
|
1140 DEFSUBR (Fmake_weak_hashtable);
|
|
1141 DEFSUBR (Fmake_key_weak_hashtable);
|
|
1142 DEFSUBR (Fmake_value_weak_hashtable);
|
0
|
1143 defsymbol (&Qhashtablep, "hashtablep");
|
223
|
1144 defsymbol (&Qhashtable, "hashtable");
|
|
1145 defsymbol (&Qweak, "weak");
|
|
1146 defsymbol (&Qkey_weak, "key-weak");
|
|
1147 defsymbol (&Qvalue_weak, "value-weak");
|
|
1148 defsymbol (&Qnon_weak, "non-weak");
|
0
|
1149 }
|
|
1150
|
|
1151 void
|
|
1152 vars_of_elhash (void)
|
|
1153 {
|
2
|
1154 /* This must NOT be staticpro'd */
|
0
|
1155 Vall_weak_hashtables = Qnil;
|
|
1156 }
|