1 | /* |
---|
2 | * BasicVector_UnsignedByte32.java |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2006 Peter Graves |
---|
5 | * $Id: BasicVector_UnsignedByte32.java 12588 2010-04-10 17:17:08Z vvoutilainen $ |
---|
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 | // A basic vector is a specialized vector that is not displaced to another |
---|
39 | // array, has no fill pointer, and is not expressly adjustable. |
---|
40 | public final class BasicVector_UnsignedByte32 extends AbstractVector |
---|
41 | { |
---|
42 | private int capacity; |
---|
43 | |
---|
44 | private long[] elements; |
---|
45 | |
---|
46 | public BasicVector_UnsignedByte32(int capacity) |
---|
47 | { |
---|
48 | elements = new long[capacity]; |
---|
49 | this.capacity = capacity; |
---|
50 | } |
---|
51 | |
---|
52 | public BasicVector_UnsignedByte32(LispObject[] array) |
---|
53 | |
---|
54 | { |
---|
55 | capacity = array.length; |
---|
56 | elements = new long[capacity]; |
---|
57 | for (int i = array.length; i-- > 0;) |
---|
58 | elements[i] = array[i].longValue(); |
---|
59 | } |
---|
60 | |
---|
61 | @Override |
---|
62 | public LispObject typeOf() |
---|
63 | { |
---|
64 | return list(Symbol.SIMPLE_ARRAY, UNSIGNED_BYTE_32, |
---|
65 | new Cons(Fixnum.getInstance(capacity))); |
---|
66 | } |
---|
67 | |
---|
68 | @Override |
---|
69 | public LispObject classOf() |
---|
70 | { |
---|
71 | return BuiltInClass.VECTOR; |
---|
72 | } |
---|
73 | |
---|
74 | @Override |
---|
75 | public LispObject typep(LispObject type) |
---|
76 | { |
---|
77 | if (type == Symbol.SIMPLE_ARRAY) |
---|
78 | return T; |
---|
79 | if (type == BuiltInClass.SIMPLE_ARRAY) |
---|
80 | return T; |
---|
81 | return super.typep(type); |
---|
82 | } |
---|
83 | |
---|
84 | @Override |
---|
85 | public LispObject getElementType() |
---|
86 | { |
---|
87 | return UNSIGNED_BYTE_32; |
---|
88 | } |
---|
89 | |
---|
90 | @Override |
---|
91 | public boolean isSimpleVector() |
---|
92 | { |
---|
93 | return false; |
---|
94 | } |
---|
95 | |
---|
96 | @Override |
---|
97 | public boolean hasFillPointer() |
---|
98 | { |
---|
99 | return false; |
---|
100 | } |
---|
101 | |
---|
102 | @Override |
---|
103 | public boolean isAdjustable() |
---|
104 | { |
---|
105 | return false; |
---|
106 | } |
---|
107 | |
---|
108 | @Override |
---|
109 | public int capacity() |
---|
110 | { |
---|
111 | return capacity; |
---|
112 | } |
---|
113 | |
---|
114 | @Override |
---|
115 | public int length() |
---|
116 | { |
---|
117 | return capacity; |
---|
118 | } |
---|
119 | |
---|
120 | @Override |
---|
121 | public LispObject elt(int index) |
---|
122 | { |
---|
123 | try |
---|
124 | { |
---|
125 | return number(elements[index]); |
---|
126 | } |
---|
127 | catch (ArrayIndexOutOfBoundsException e) |
---|
128 | { |
---|
129 | badIndex(index, capacity); |
---|
130 | return NIL; // Not reached. |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | @Override |
---|
135 | public int aref(int index) |
---|
136 | { |
---|
137 | try |
---|
138 | { |
---|
139 | return (int) elements[index]; |
---|
140 | } |
---|
141 | catch (ArrayIndexOutOfBoundsException e) |
---|
142 | { |
---|
143 | badIndex(index, elements.length); |
---|
144 | return -1; // Not reached. |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | @Override |
---|
149 | public long aref_long(int index) |
---|
150 | { |
---|
151 | try |
---|
152 | { |
---|
153 | return elements[index]; |
---|
154 | } |
---|
155 | catch (ArrayIndexOutOfBoundsException e) |
---|
156 | { |
---|
157 | badIndex(index, elements.length); |
---|
158 | return -1; // Not reached. |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | @Override |
---|
163 | public LispObject AREF(int index) |
---|
164 | { |
---|
165 | try |
---|
166 | { |
---|
167 | return number(elements[index]); |
---|
168 | } |
---|
169 | catch (ArrayIndexOutOfBoundsException e) |
---|
170 | { |
---|
171 | badIndex(index, elements.length); |
---|
172 | return NIL; // Not reached. |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | @Override |
---|
177 | public void aset(int index, LispObject newValue) |
---|
178 | { |
---|
179 | try |
---|
180 | { |
---|
181 | elements[index] = newValue.longValue(); |
---|
182 | } |
---|
183 | catch (ArrayIndexOutOfBoundsException e) |
---|
184 | { |
---|
185 | badIndex(index, capacity); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | @Override |
---|
190 | public LispObject subseq(int start, int end) |
---|
191 | { |
---|
192 | BasicVector_UnsignedByte32 v = new BasicVector_UnsignedByte32(end - start); |
---|
193 | int i = start, j = 0; |
---|
194 | try |
---|
195 | { |
---|
196 | while (i < end) |
---|
197 | v.elements[j++] = elements[i++]; |
---|
198 | return v; |
---|
199 | } |
---|
200 | catch (ArrayIndexOutOfBoundsException e) |
---|
201 | { |
---|
202 | // FIXME |
---|
203 | return error(new TypeError("Array index out of bounds: " + i + ".")); |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | @Override |
---|
208 | public void fill(LispObject obj) |
---|
209 | { |
---|
210 | for (int i = capacity; i-- > 0;) |
---|
211 | elements[i] = obj.longValue(); |
---|
212 | } |
---|
213 | |
---|
214 | @Override |
---|
215 | public void shrink(int n) |
---|
216 | { |
---|
217 | if (n < capacity) |
---|
218 | { |
---|
219 | long[] newArray = new long[n]; |
---|
220 | System.arraycopy(elements, 0, newArray, 0, n); |
---|
221 | elements = newArray; |
---|
222 | capacity = n; |
---|
223 | return; |
---|
224 | } |
---|
225 | if (n == capacity) |
---|
226 | return; |
---|
227 | error(new LispError()); |
---|
228 | } |
---|
229 | |
---|
230 | @Override |
---|
231 | public LispObject reverse() |
---|
232 | { |
---|
233 | BasicVector_UnsignedByte32 result = new BasicVector_UnsignedByte32(capacity); |
---|
234 | int i, j; |
---|
235 | for (i = 0, j = capacity - 1; i < capacity; i++, j--) |
---|
236 | result.elements[i] = elements[j]; |
---|
237 | return result; |
---|
238 | } |
---|
239 | |
---|
240 | @Override |
---|
241 | public LispObject nreverse() |
---|
242 | { |
---|
243 | int i = 0; |
---|
244 | int j = capacity - 1; |
---|
245 | while (i < j) |
---|
246 | { |
---|
247 | long temp = elements[i]; |
---|
248 | elements[i] = elements[j]; |
---|
249 | elements[j] = temp; |
---|
250 | ++i; |
---|
251 | --j; |
---|
252 | } |
---|
253 | return this; |
---|
254 | } |
---|
255 | |
---|
256 | @Override |
---|
257 | public AbstractVector adjustArray(int newCapacity, |
---|
258 | LispObject initialElement, |
---|
259 | LispObject initialContents) |
---|
260 | |
---|
261 | { |
---|
262 | if (initialContents != null) |
---|
263 | { |
---|
264 | LispObject[] newElements = new LispObject[newCapacity]; |
---|
265 | if (initialContents.listp()) |
---|
266 | { |
---|
267 | LispObject list = initialContents; |
---|
268 | for (int i = 0; i < newCapacity; i++) |
---|
269 | { |
---|
270 | newElements[i] = list.car(); |
---|
271 | list = list.cdr(); |
---|
272 | } |
---|
273 | } |
---|
274 | else if (initialContents.vectorp()) |
---|
275 | { |
---|
276 | for (int i = 0; i < newCapacity; i++) |
---|
277 | newElements[i] = initialContents.elt(i); |
---|
278 | } |
---|
279 | else |
---|
280 | type_error(initialContents, Symbol.SEQUENCE); |
---|
281 | return new BasicVector_UnsignedByte32(newElements); |
---|
282 | } |
---|
283 | if (capacity != newCapacity) |
---|
284 | { |
---|
285 | LispObject[] newElements = new LispObject[newCapacity]; |
---|
286 | System.arraycopy(elements, 0, newElements, 0, |
---|
287 | Math.min(capacity, newCapacity)); |
---|
288 | if (initialElement != null) |
---|
289 | for (int i = capacity; i < newCapacity; i++) |
---|
290 | newElements[i] = initialElement; |
---|
291 | return new BasicVector_UnsignedByte32(newElements); |
---|
292 | } |
---|
293 | // No change. |
---|
294 | return this; |
---|
295 | } |
---|
296 | |
---|
297 | @Override |
---|
298 | public AbstractVector adjustArray(int newCapacity, |
---|
299 | AbstractArray displacedTo, |
---|
300 | int displacement) |
---|
301 | { |
---|
302 | return new ComplexVector(newCapacity, displacedTo, displacement); |
---|
303 | } |
---|
304 | } |
---|