annotate src/elhash.c @ 242:fc816b73a05f

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