1 | /* |
---|
2 | * HashTableFunctions.java |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2006 Peter Graves |
---|
5 | * $Id: HashTableFunctions.java 12254 2009-11-06 20:07:54Z 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 | public final class HashTableFunctions extends Lisp |
---|
37 | { |
---|
38 | private static final LispObject FUNCTION_EQ = |
---|
39 | Symbol.EQ.getSymbolFunction(); |
---|
40 | private static final LispObject FUNCTION_EQL = |
---|
41 | Symbol.EQL.getSymbolFunction(); |
---|
42 | private static final LispObject FUNCTION_EQUAL = |
---|
43 | Symbol.EQUAL.getSymbolFunction(); |
---|
44 | private static final LispObject FUNCTION_EQUALP = |
---|
45 | Symbol.EQUALP.getSymbolFunction(); |
---|
46 | |
---|
47 | // ### %make-hash-table |
---|
48 | private static final Primitive _MAKE_HASH_TABLE = |
---|
49 | new Primitive("%make-hash-table", PACKAGE_SYS, false) |
---|
50 | { |
---|
51 | @Override |
---|
52 | public LispObject execute(LispObject test, LispObject size, |
---|
53 | LispObject rehashSize, LispObject rehashThreshold) |
---|
54 | |
---|
55 | { |
---|
56 | final int n = Fixnum.getValue(size); |
---|
57 | if (test == FUNCTION_EQL || test == NIL) |
---|
58 | return new EqlHashTable(n, rehashSize, rehashThreshold); |
---|
59 | if (test == FUNCTION_EQ) |
---|
60 | return new EqHashTable(n, rehashSize, rehashThreshold); |
---|
61 | if (test == FUNCTION_EQUAL) |
---|
62 | return new EqualHashTable(n, rehashSize, rehashThreshold); |
---|
63 | if (test == FUNCTION_EQUALP) |
---|
64 | return new EqualpHashTable(n, rehashSize, rehashThreshold); |
---|
65 | return error(new LispError("Unsupported test for MAKE-HASH-TABLE: " + |
---|
66 | test.writeToString())); |
---|
67 | } |
---|
68 | }; |
---|
69 | |
---|
70 | // ### gethash key hash-table &optional default => value, present-p |
---|
71 | private static final Primitive GETHASH = |
---|
72 | new Primitive(Symbol.GETHASH, "key hash-table &optional default") |
---|
73 | { |
---|
74 | @Override |
---|
75 | public LispObject execute(LispObject key, LispObject ht) |
---|
76 | |
---|
77 | { |
---|
78 | return checkHashTable(ht).gethash(key); |
---|
79 | } |
---|
80 | |
---|
81 | @Override |
---|
82 | public LispObject execute(LispObject key, LispObject ht, |
---|
83 | LispObject defaultValue) |
---|
84 | |
---|
85 | { |
---|
86 | return checkHashTable(ht).gethash(key, defaultValue); |
---|
87 | } |
---|
88 | }; |
---|
89 | |
---|
90 | // ### gethash1 key hash-table => value |
---|
91 | private static final Primitive GETHASH1 = |
---|
92 | new Primitive(Symbol.GETHASH1, "key hash-table") |
---|
93 | { |
---|
94 | @Override |
---|
95 | public LispObject execute(LispObject first, LispObject second) |
---|
96 | |
---|
97 | { |
---|
98 | final HashTable ht = checkHashTable(second); |
---|
99 | synchronized (ht) |
---|
100 | { |
---|
101 | final LispObject value = ht.get(first); |
---|
102 | return value != null ? value : NIL; |
---|
103 | } |
---|
104 | } |
---|
105 | }; |
---|
106 | |
---|
107 | // ### puthash key hash-table new-value &optional default => value |
---|
108 | private static final Primitive PUTHASH = |
---|
109 | new Primitive(Symbol.PUTHASH, |
---|
110 | "key hash-table new-value &optional default") |
---|
111 | { |
---|
112 | @Override |
---|
113 | public LispObject execute(LispObject key, LispObject ht, |
---|
114 | LispObject value) |
---|
115 | |
---|
116 | { |
---|
117 | return checkHashTable(ht).puthash(key, value); |
---|
118 | } |
---|
119 | @Override |
---|
120 | public LispObject execute(LispObject key, LispObject ht, |
---|
121 | LispObject ignored, LispObject value) |
---|
122 | |
---|
123 | { |
---|
124 | return checkHashTable(ht).puthash(key, value); |
---|
125 | } |
---|
126 | }; |
---|
127 | |
---|
128 | // remhash key hash-table => generalized-boolean |
---|
129 | private static final Primitive REMHASH = |
---|
130 | new Primitive(Symbol.REMHASH, "key hash-table") |
---|
131 | { |
---|
132 | @Override |
---|
133 | public LispObject execute(LispObject key, LispObject ht) |
---|
134 | |
---|
135 | { |
---|
136 | return checkHashTable(ht).remhash(key); |
---|
137 | } |
---|
138 | }; |
---|
139 | |
---|
140 | // ### clrhash hash-table => hash-table |
---|
141 | private static final Primitive CLRHASH = |
---|
142 | new Primitive(Symbol.CLRHASH, "hash-table") |
---|
143 | { |
---|
144 | @Override |
---|
145 | public LispObject execute(LispObject ht) |
---|
146 | { |
---|
147 | checkHashTable(ht).clear(); |
---|
148 | return ht; |
---|
149 | } |
---|
150 | }; |
---|
151 | |
---|
152 | // ### hash-table-count |
---|
153 | private static final Primitive HASH_TABLE_COUNT = |
---|
154 | new Primitive(Symbol.HASH_TABLE_COUNT, "hash-table") |
---|
155 | { |
---|
156 | @Override |
---|
157 | public LispObject execute(LispObject arg) |
---|
158 | { |
---|
159 | return Fixnum.getInstance(checkHashTable(arg).getCount()); |
---|
160 | } |
---|
161 | }; |
---|
162 | |
---|
163 | // ### sxhash object => hash-code |
---|
164 | private static final Primitive SXHASH = |
---|
165 | new Primitive(Symbol.SXHASH, "object") |
---|
166 | { |
---|
167 | @Override |
---|
168 | public LispObject execute(LispObject arg) |
---|
169 | { |
---|
170 | return Fixnum.getInstance(arg.sxhash()); |
---|
171 | } |
---|
172 | }; |
---|
173 | |
---|
174 | // ### psxhash object => hash-code |
---|
175 | // For EQUALP hash tables. |
---|
176 | private static final Primitive PSXHASH = |
---|
177 | new Primitive("psxhash", PACKAGE_SYS, true, "object") |
---|
178 | { |
---|
179 | @Override |
---|
180 | public LispObject execute(LispObject arg) |
---|
181 | { |
---|
182 | return Fixnum.getInstance(arg.psxhash()); |
---|
183 | } |
---|
184 | }; |
---|
185 | |
---|
186 | // ### hash-table-p |
---|
187 | private static final Primitive HASH_TABLE_P = |
---|
188 | new Primitive(Symbol.HASH_TABLE_P,"object") |
---|
189 | { |
---|
190 | @Override |
---|
191 | public LispObject execute(LispObject arg) |
---|
192 | { |
---|
193 | return arg instanceof HashTable ? T : NIL; |
---|
194 | } |
---|
195 | }; |
---|
196 | |
---|
197 | // ### hash-table-entries |
---|
198 | private static final Primitive HASH_TABLE_ENTRIES = |
---|
199 | new Primitive("hash-table-entries", PACKAGE_SYS, false) |
---|
200 | { |
---|
201 | @Override |
---|
202 | public LispObject execute(LispObject arg) |
---|
203 | { |
---|
204 | return checkHashTable(arg).ENTRIES(); |
---|
205 | } |
---|
206 | }; |
---|
207 | |
---|
208 | // ### hash-table-test |
---|
209 | private static final Primitive HASH_TABLE_TEST = |
---|
210 | new Primitive(Symbol.HASH_TABLE_TEST, "hash-table") |
---|
211 | { |
---|
212 | @Override |
---|
213 | public LispObject execute(LispObject arg) |
---|
214 | { |
---|
215 | return checkHashTable(arg).getTest(); |
---|
216 | } |
---|
217 | }; |
---|
218 | |
---|
219 | // ### hash-table-size |
---|
220 | private static final Primitive HASH_TABLE_SIZE = |
---|
221 | new Primitive(Symbol.HASH_TABLE_SIZE, "hash-table") |
---|
222 | { |
---|
223 | @Override |
---|
224 | public LispObject execute(LispObject arg) |
---|
225 | { |
---|
226 | return Fixnum.getInstance(checkHashTable(arg).getSize()); |
---|
227 | } |
---|
228 | }; |
---|
229 | |
---|
230 | // ### hash-table-rehash-size |
---|
231 | private static final Primitive HASH_TABLE_REHASH_SIZE = |
---|
232 | new Primitive(Symbol.HASH_TABLE_REHASH_SIZE, "hash-table") |
---|
233 | { |
---|
234 | @Override |
---|
235 | public LispObject execute(LispObject arg) |
---|
236 | { |
---|
237 | return checkHashTable(arg).getRehashSize(); |
---|
238 | } |
---|
239 | }; |
---|
240 | |
---|
241 | // ### hash-table-rehash-threshold |
---|
242 | private static final Primitive HASH_TABLE_REHASH_THRESHOLD = |
---|
243 | new Primitive(Symbol.HASH_TABLE_REHASH_THRESHOLD, "hash-table") |
---|
244 | { |
---|
245 | @Override |
---|
246 | public LispObject execute(LispObject arg) |
---|
247 | { |
---|
248 | return checkHashTable(arg).getRehashThreshold(); |
---|
249 | } |
---|
250 | }; |
---|
251 | |
---|
252 | // ### maphash |
---|
253 | private static final Primitive MAPHASH = |
---|
254 | new Primitive(Symbol.MAPHASH, "function hash-table") |
---|
255 | { |
---|
256 | @Override |
---|
257 | public LispObject execute(LispObject first, LispObject second) |
---|
258 | |
---|
259 | { |
---|
260 | return checkHashTable(second).MAPHASH(first); |
---|
261 | } |
---|
262 | }; |
---|
263 | |
---|
264 | protected static HashTable checkHashTable(LispObject ht) { |
---|
265 | if (ht instanceof HashTable) return (HashTable)ht; |
---|
266 | type_error(ht, Symbol.HASH_TABLE); |
---|
267 | return null; |
---|
268 | } |
---|
269 | } |
---|