1 | /* |
---|
2 | * HashTableFunctions.java |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2006 Peter Graves |
---|
5 | * $Id: HashTableFunctions.java 12513 2010-03-02 22:35:36Z ehuelsmann $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | public final class HashTableFunctions |
---|
39 | { |
---|
40 | static final LispObject FUNCTION_EQ = |
---|
41 | Symbol.EQ.getSymbolFunction(); |
---|
42 | static final LispObject FUNCTION_EQL = |
---|
43 | Symbol.EQL.getSymbolFunction(); |
---|
44 | static final LispObject FUNCTION_EQUAL = |
---|
45 | Symbol.EQUAL.getSymbolFunction(); |
---|
46 | static final LispObject FUNCTION_EQUALP = |
---|
47 | Symbol.EQUALP.getSymbolFunction(); |
---|
48 | |
---|
49 | // ### %make-hash-table |
---|
50 | private static final Primitive _MAKE_HASH_TABLE = |
---|
51 | new Primitive("%make-hash-table", PACKAGE_SYS, false) |
---|
52 | { |
---|
53 | @Override |
---|
54 | public LispObject execute(LispObject test, LispObject size, |
---|
55 | LispObject rehashSize, LispObject rehashThreshold) |
---|
56 | |
---|
57 | { |
---|
58 | final int n = Fixnum.getValue(size); |
---|
59 | if (test == FUNCTION_EQL || test == NIL) |
---|
60 | return new EqlHashTable(n, rehashSize, rehashThreshold); |
---|
61 | if (test == FUNCTION_EQ) |
---|
62 | return new EqHashTable(n, rehashSize, rehashThreshold); |
---|
63 | if (test == FUNCTION_EQUAL) |
---|
64 | return new EqualHashTable(n, rehashSize, rehashThreshold); |
---|
65 | if (test == FUNCTION_EQUALP) |
---|
66 | return new EqualpHashTable(n, rehashSize, rehashThreshold); |
---|
67 | return error(new LispError("Unsupported test for MAKE-HASH-TABLE: " + |
---|
68 | test.writeToString())); |
---|
69 | } |
---|
70 | }; |
---|
71 | |
---|
72 | // ### gethash key hash-table &optional default => value, present-p |
---|
73 | private static final Primitive GETHASH = |
---|
74 | new Primitive(Symbol.GETHASH, "key hash-table &optional default") |
---|
75 | { |
---|
76 | @Override |
---|
77 | public LispObject execute(LispObject key, LispObject ht) |
---|
78 | |
---|
79 | { |
---|
80 | return checkHashTable(ht).gethash(key); |
---|
81 | } |
---|
82 | |
---|
83 | @Override |
---|
84 | public LispObject execute(LispObject key, LispObject ht, |
---|
85 | LispObject defaultValue) |
---|
86 | |
---|
87 | { |
---|
88 | return checkHashTable(ht).gethash(key, defaultValue); |
---|
89 | } |
---|
90 | }; |
---|
91 | |
---|
92 | // ### gethash1 key hash-table => value |
---|
93 | private static final Primitive GETHASH1 = |
---|
94 | new Primitive(Symbol.GETHASH1, "key hash-table") |
---|
95 | { |
---|
96 | @Override |
---|
97 | public LispObject execute(LispObject first, LispObject second) |
---|
98 | |
---|
99 | { |
---|
100 | final HashTable ht = checkHashTable(second); |
---|
101 | synchronized (ht) |
---|
102 | { |
---|
103 | final LispObject value = ht.get(first); |
---|
104 | return value != null ? value : NIL; |
---|
105 | } |
---|
106 | } |
---|
107 | }; |
---|
108 | |
---|
109 | // ### puthash key hash-table new-value &optional default => value |
---|
110 | private static final Primitive PUTHASH = |
---|
111 | new Primitive(Symbol.PUTHASH, |
---|
112 | "key hash-table new-value &optional default") |
---|
113 | { |
---|
114 | @Override |
---|
115 | public LispObject execute(LispObject key, LispObject ht, |
---|
116 | LispObject value) |
---|
117 | |
---|
118 | { |
---|
119 | return checkHashTable(ht).puthash(key, value); |
---|
120 | } |
---|
121 | @Override |
---|
122 | public LispObject execute(LispObject key, LispObject ht, |
---|
123 | LispObject ignored, LispObject value) |
---|
124 | |
---|
125 | { |
---|
126 | return checkHashTable(ht).puthash(key, value); |
---|
127 | } |
---|
128 | }; |
---|
129 | |
---|
130 | // remhash key hash-table => generalized-boolean |
---|
131 | private static final Primitive REMHASH = |
---|
132 | new Primitive(Symbol.REMHASH, "key hash-table") |
---|
133 | { |
---|
134 | @Override |
---|
135 | public LispObject execute(LispObject key, LispObject ht) |
---|
136 | |
---|
137 | { |
---|
138 | return checkHashTable(ht).remhash(key); |
---|
139 | } |
---|
140 | }; |
---|
141 | |
---|
142 | // ### clrhash hash-table => hash-table |
---|
143 | private static final Primitive CLRHASH = |
---|
144 | new Primitive(Symbol.CLRHASH, "hash-table") |
---|
145 | { |
---|
146 | @Override |
---|
147 | public LispObject execute(LispObject ht) |
---|
148 | { |
---|
149 | checkHashTable(ht).clear(); |
---|
150 | return ht; |
---|
151 | } |
---|
152 | }; |
---|
153 | |
---|
154 | // ### hash-table-count |
---|
155 | private static final Primitive HASH_TABLE_COUNT = |
---|
156 | new Primitive(Symbol.HASH_TABLE_COUNT, "hash-table") |
---|
157 | { |
---|
158 | @Override |
---|
159 | public LispObject execute(LispObject arg) |
---|
160 | { |
---|
161 | return Fixnum.getInstance(checkHashTable(arg).getCount()); |
---|
162 | } |
---|
163 | }; |
---|
164 | |
---|
165 | // ### sxhash object => hash-code |
---|
166 | private static final Primitive SXHASH = |
---|
167 | new Primitive(Symbol.SXHASH, "object") |
---|
168 | { |
---|
169 | @Override |
---|
170 | public LispObject execute(LispObject arg) |
---|
171 | { |
---|
172 | return Fixnum.getInstance(arg.sxhash()); |
---|
173 | } |
---|
174 | }; |
---|
175 | |
---|
176 | // ### psxhash object => hash-code |
---|
177 | // For EQUALP hash tables. |
---|
178 | private static final Primitive PSXHASH = |
---|
179 | new Primitive("psxhash", PACKAGE_SYS, true, "object") |
---|
180 | { |
---|
181 | @Override |
---|
182 | public LispObject execute(LispObject arg) |
---|
183 | { |
---|
184 | return Fixnum.getInstance(arg.psxhash()); |
---|
185 | } |
---|
186 | }; |
---|
187 | |
---|
188 | // ### hash-table-p |
---|
189 | private static final Primitive HASH_TABLE_P = |
---|
190 | new Primitive(Symbol.HASH_TABLE_P,"object") |
---|
191 | { |
---|
192 | @Override |
---|
193 | public LispObject execute(LispObject arg) |
---|
194 | { |
---|
195 | return arg instanceof HashTable ? T : NIL; |
---|
196 | } |
---|
197 | }; |
---|
198 | |
---|
199 | // ### hash-table-entries |
---|
200 | private static final Primitive HASH_TABLE_ENTRIES = |
---|
201 | new Primitive("hash-table-entries", PACKAGE_SYS, false) |
---|
202 | { |
---|
203 | @Override |
---|
204 | public LispObject execute(LispObject arg) |
---|
205 | { |
---|
206 | return checkHashTable(arg).ENTRIES(); |
---|
207 | } |
---|
208 | }; |
---|
209 | |
---|
210 | // ### hash-table-test |
---|
211 | private static final Primitive HASH_TABLE_TEST = |
---|
212 | new Primitive(Symbol.HASH_TABLE_TEST, "hash-table") |
---|
213 | { |
---|
214 | @Override |
---|
215 | public LispObject execute(LispObject arg) |
---|
216 | { |
---|
217 | return checkHashTable(arg).getTest(); |
---|
218 | } |
---|
219 | }; |
---|
220 | |
---|
221 | // ### hash-table-size |
---|
222 | private static final Primitive HASH_TABLE_SIZE = |
---|
223 | new Primitive(Symbol.HASH_TABLE_SIZE, "hash-table") |
---|
224 | { |
---|
225 | @Override |
---|
226 | public LispObject execute(LispObject arg) |
---|
227 | { |
---|
228 | return Fixnum.getInstance(checkHashTable(arg).getSize()); |
---|
229 | } |
---|
230 | }; |
---|
231 | |
---|
232 | // ### hash-table-rehash-size |
---|
233 | private static final Primitive HASH_TABLE_REHASH_SIZE = |
---|
234 | new Primitive(Symbol.HASH_TABLE_REHASH_SIZE, "hash-table") |
---|
235 | { |
---|
236 | @Override |
---|
237 | public LispObject execute(LispObject arg) |
---|
238 | { |
---|
239 | return checkHashTable(arg).getRehashSize(); |
---|
240 | } |
---|
241 | }; |
---|
242 | |
---|
243 | // ### hash-table-rehash-threshold |
---|
244 | private static final Primitive HASH_TABLE_REHASH_THRESHOLD = |
---|
245 | new Primitive(Symbol.HASH_TABLE_REHASH_THRESHOLD, "hash-table") |
---|
246 | { |
---|
247 | @Override |
---|
248 | public LispObject execute(LispObject arg) |
---|
249 | { |
---|
250 | return checkHashTable(arg).getRehashThreshold(); |
---|
251 | } |
---|
252 | }; |
---|
253 | |
---|
254 | // ### maphash |
---|
255 | private static final Primitive MAPHASH = |
---|
256 | new Primitive(Symbol.MAPHASH, "function hash-table") |
---|
257 | { |
---|
258 | @Override |
---|
259 | public LispObject execute(LispObject first, LispObject second) |
---|
260 | |
---|
261 | { |
---|
262 | return checkHashTable(second).MAPHASH(first); |
---|
263 | } |
---|
264 | }; |
---|
265 | |
---|
266 | protected static HashTable checkHashTable(LispObject ht) { |
---|
267 | if (ht instanceof HashTable) return (HashTable)ht; |
---|
268 | type_error(ht, Symbol.HASH_TABLE); |
---|
269 | return null; |
---|
270 | } |
---|
271 | } |
---|