1 | /* |
---|
2 | * ComplexVector_UnsignedByte32.java |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2005 Peter Graves |
---|
5 | * $Id: ComplexVector_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 specialized vector of element type (UNSIGNED-BYTE 32) that is displaced to |
---|
39 | // another array, has a fill pointer, and/or is expressly adjustable. |
---|
40 | public final class ComplexVector_UnsignedByte32 extends AbstractVector |
---|
41 | { |
---|
42 | private int capacity; |
---|
43 | private int fillPointer = -1; // -1 indicates no fill pointer. |
---|
44 | private boolean isDisplaced; |
---|
45 | |
---|
46 | // For non-displaced arrays. |
---|
47 | private LispObject[] elements; |
---|
48 | |
---|
49 | // For displaced arrays. |
---|
50 | private AbstractArray array; |
---|
51 | private int displacement; |
---|
52 | |
---|
53 | public ComplexVector_UnsignedByte32(int capacity) |
---|
54 | { |
---|
55 | elements = new LispObject[capacity]; |
---|
56 | for (int i = capacity; i-- > 0;) |
---|
57 | elements[i] = Fixnum.ZERO; |
---|
58 | this.capacity = capacity; |
---|
59 | } |
---|
60 | |
---|
61 | public ComplexVector_UnsignedByte32(int capacity, AbstractArray array, |
---|
62 | int displacement) |
---|
63 | { |
---|
64 | this.capacity = capacity; |
---|
65 | this.array = array; |
---|
66 | this.displacement = displacement; |
---|
67 | isDisplaced = true; |
---|
68 | } |
---|
69 | |
---|
70 | @Override |
---|
71 | public LispObject typeOf() |
---|
72 | { |
---|
73 | return list(Symbol.VECTOR, UNSIGNED_BYTE_32, Fixnum.getInstance(capacity)); |
---|
74 | } |
---|
75 | |
---|
76 | @Override |
---|
77 | public LispObject classOf() |
---|
78 | { |
---|
79 | return BuiltInClass.VECTOR; |
---|
80 | } |
---|
81 | |
---|
82 | @Override |
---|
83 | public boolean hasFillPointer() |
---|
84 | { |
---|
85 | return fillPointer >= 0; |
---|
86 | } |
---|
87 | |
---|
88 | @Override |
---|
89 | public int getFillPointer() |
---|
90 | { |
---|
91 | return fillPointer; |
---|
92 | } |
---|
93 | |
---|
94 | @Override |
---|
95 | public void setFillPointer(int n) |
---|
96 | { |
---|
97 | fillPointer = n; |
---|
98 | } |
---|
99 | |
---|
100 | @Override |
---|
101 | public void setFillPointer(LispObject obj) |
---|
102 | { |
---|
103 | if (obj == T) |
---|
104 | fillPointer = capacity(); |
---|
105 | else { |
---|
106 | int n = Fixnum.getValue(obj); |
---|
107 | if (n > capacity()) { |
---|
108 | StringBuffer sb = new StringBuffer("The new fill pointer ("); |
---|
109 | sb.append(n); |
---|
110 | sb.append(") exceeds the capacity of the vector ("); |
---|
111 | sb.append(capacity()); |
---|
112 | sb.append(")."); |
---|
113 | error(new LispError(sb.toString())); |
---|
114 | } else if (n < 0) { |
---|
115 | StringBuffer sb = new StringBuffer("The new fill pointer ("); |
---|
116 | sb.append(n); |
---|
117 | sb.append(") is negative."); |
---|
118 | error(new LispError(sb.toString())); |
---|
119 | } else |
---|
120 | fillPointer = n; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | @Override |
---|
125 | public boolean isDisplaced() |
---|
126 | { |
---|
127 | return isDisplaced; |
---|
128 | } |
---|
129 | |
---|
130 | @Override |
---|
131 | public LispObject arrayDisplacement() |
---|
132 | { |
---|
133 | LispObject value1, value2; |
---|
134 | if (array != null) { |
---|
135 | value1 = array; |
---|
136 | value2 = Fixnum.getInstance(displacement); |
---|
137 | } else { |
---|
138 | value1 = NIL; |
---|
139 | value2 = Fixnum.ZERO; |
---|
140 | } |
---|
141 | return LispThread.currentThread().setValues(value1, value2); |
---|
142 | } |
---|
143 | |
---|
144 | @Override |
---|
145 | public LispObject getElementType() |
---|
146 | { |
---|
147 | return UNSIGNED_BYTE_32; |
---|
148 | } |
---|
149 | |
---|
150 | @Override |
---|
151 | public boolean isSimpleVector() |
---|
152 | { |
---|
153 | return false; |
---|
154 | } |
---|
155 | |
---|
156 | @Override |
---|
157 | public int capacity() |
---|
158 | { |
---|
159 | return capacity; |
---|
160 | } |
---|
161 | |
---|
162 | @Override |
---|
163 | public int length() |
---|
164 | { |
---|
165 | return fillPointer >= 0 ? fillPointer : capacity; |
---|
166 | } |
---|
167 | |
---|
168 | @Override |
---|
169 | public LispObject elt(int index) |
---|
170 | { |
---|
171 | final int limit = length(); |
---|
172 | if (index < 0 || index >= limit) |
---|
173 | badIndex(index, limit); |
---|
174 | return AREF(index); |
---|
175 | } |
---|
176 | |
---|
177 | // Ignores fill pointer. |
---|
178 | @Override |
---|
179 | public LispObject AREF(int index) |
---|
180 | { |
---|
181 | if (elements != null) { |
---|
182 | try { |
---|
183 | return elements[index]; |
---|
184 | } |
---|
185 | catch (ArrayIndexOutOfBoundsException e) { |
---|
186 | badIndex(index, elements.length); |
---|
187 | return NIL; // Not reached. |
---|
188 | } |
---|
189 | } else { |
---|
190 | // Displaced array. |
---|
191 | if (index < 0 || index >= capacity) |
---|
192 | badIndex(index, capacity); |
---|
193 | return array.AREF(index + displacement); |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | @Override |
---|
198 | public void aset(int index, LispObject newValue) |
---|
199 | { |
---|
200 | if (elements != null) { |
---|
201 | try { |
---|
202 | elements[index] = newValue; |
---|
203 | } |
---|
204 | catch (ArrayIndexOutOfBoundsException e) { |
---|
205 | badIndex(index, elements.length); |
---|
206 | } |
---|
207 | } else { |
---|
208 | // Displaced array. |
---|
209 | if (index < 0 || index >= capacity) |
---|
210 | badIndex(index, capacity); |
---|
211 | else |
---|
212 | array.aset(index + displacement, newValue); |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | @Override |
---|
217 | public LispObject subseq(int start, int end) |
---|
218 | { |
---|
219 | SimpleVector v = new SimpleVector(end - start); |
---|
220 | int i = start, j = 0; |
---|
221 | try { |
---|
222 | while (i < end) |
---|
223 | v.aset(j++, AREF(i++)); |
---|
224 | return v; |
---|
225 | } |
---|
226 | catch (ArrayIndexOutOfBoundsException e) { |
---|
227 | return error(new TypeError("Array index out of bounds: " + i + ".")); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | @Override |
---|
232 | public void fill(LispObject obj) |
---|
233 | { |
---|
234 | for (int i = capacity; i-- > 0;) |
---|
235 | elements[i] = obj; |
---|
236 | } |
---|
237 | |
---|
238 | @Override |
---|
239 | public void shrink(int n) |
---|
240 | { |
---|
241 | if (elements != null) { |
---|
242 | if (n < elements.length) { |
---|
243 | LispObject[] newArray = new LispObject[n]; |
---|
244 | System.arraycopy(elements, 0, newArray, 0, n); |
---|
245 | elements = newArray; |
---|
246 | capacity = n; |
---|
247 | return; |
---|
248 | } |
---|
249 | if (n == elements.length) |
---|
250 | return; |
---|
251 | } |
---|
252 | error(new LispError()); |
---|
253 | } |
---|
254 | |
---|
255 | @Override |
---|
256 | public LispObject reverse() |
---|
257 | { |
---|
258 | int length = length(); |
---|
259 | SimpleVector result = new SimpleVector(length); |
---|
260 | int i, j; |
---|
261 | for (i = 0, j = length - 1; i < length; i++, j--) |
---|
262 | result.aset(i, AREF(j)); |
---|
263 | return result; |
---|
264 | } |
---|
265 | |
---|
266 | @Override |
---|
267 | public LispObject nreverse() |
---|
268 | { |
---|
269 | if (elements != null) { |
---|
270 | int i = 0; |
---|
271 | int j = length() - 1; |
---|
272 | while (i < j) { |
---|
273 | LispObject temp = elements[i]; |
---|
274 | elements[i] = elements[j]; |
---|
275 | elements[j] = temp; |
---|
276 | ++i; |
---|
277 | --j; |
---|
278 | } |
---|
279 | } else { |
---|
280 | // Displaced array. |
---|
281 | int length = length(); |
---|
282 | LispObject[] data = new LispObject[length]; |
---|
283 | int i, j; |
---|
284 | for (i = 0, j = length - 1; i < length; i++, j--) |
---|
285 | data[i] = AREF(j); |
---|
286 | elements = data; |
---|
287 | capacity = length; |
---|
288 | array = null; |
---|
289 | displacement = 0; |
---|
290 | isDisplaced = false; |
---|
291 | fillPointer = -1; |
---|
292 | } |
---|
293 | return this; |
---|
294 | } |
---|
295 | |
---|
296 | @Override |
---|
297 | public void vectorPushExtend(LispObject element) |
---|
298 | |
---|
299 | { |
---|
300 | if (fillPointer < 0) |
---|
301 | noFillPointer(); |
---|
302 | if (fillPointer >= capacity) { |
---|
303 | // Need to extend vector. |
---|
304 | ensureCapacity(capacity * 2 + 1); |
---|
305 | } |
---|
306 | aset(fillPointer, element); |
---|
307 | ++fillPointer; |
---|
308 | } |
---|
309 | |
---|
310 | @Override |
---|
311 | public LispObject VECTOR_PUSH_EXTEND(LispObject element) |
---|
312 | |
---|
313 | { |
---|
314 | vectorPushExtend(element); |
---|
315 | return Fixnum.getInstance(fillPointer - 1); |
---|
316 | } |
---|
317 | |
---|
318 | @Override |
---|
319 | public LispObject VECTOR_PUSH_EXTEND(LispObject element, LispObject extension) |
---|
320 | |
---|
321 | { |
---|
322 | int ext = Fixnum.getValue(extension); |
---|
323 | if (fillPointer < 0) |
---|
324 | noFillPointer(); |
---|
325 | if (fillPointer >= capacity) { |
---|
326 | // Need to extend vector. |
---|
327 | ext = Math.max(ext, capacity + 1); |
---|
328 | ensureCapacity(capacity + ext); |
---|
329 | } |
---|
330 | aset(fillPointer, element); |
---|
331 | return Fixnum.getInstance(fillPointer++); |
---|
332 | } |
---|
333 | |
---|
334 | private final void ensureCapacity(int minCapacity) |
---|
335 | { |
---|
336 | if (elements != null) { |
---|
337 | if (capacity < minCapacity) { |
---|
338 | LispObject[] newArray = new LispObject[minCapacity]; |
---|
339 | System.arraycopy(elements, 0, newArray, 0, capacity); |
---|
340 | elements = newArray; |
---|
341 | capacity = minCapacity; |
---|
342 | } |
---|
343 | } else { |
---|
344 | // Displaced array. |
---|
345 | Debug.assertTrue(array != null); |
---|
346 | if (capacity < minCapacity || |
---|
347 | array.getTotalSize() - displacement < minCapacity) |
---|
348 | { |
---|
349 | // Copy array. |
---|
350 | elements = new LispObject[minCapacity]; |
---|
351 | final int limit = |
---|
352 | Math.min(capacity, array.getTotalSize() - displacement); |
---|
353 | for (int i = 0; i < limit; i++) |
---|
354 | elements[i] = array.AREF(displacement + i); |
---|
355 | capacity = minCapacity; |
---|
356 | array = null; |
---|
357 | displacement = 0; |
---|
358 | isDisplaced = false; |
---|
359 | } |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | @Override |
---|
364 | public AbstractVector adjustArray(int newCapacity, |
---|
365 | LispObject initialElement, |
---|
366 | LispObject initialContents) |
---|
367 | |
---|
368 | { |
---|
369 | if (initialContents != null) { |
---|
370 | // "If INITIAL-CONTENTS is supplied, it is treated as for MAKE- |
---|
371 | // ARRAY. In this case none of the original contents of array |
---|
372 | // appears in the resulting array." |
---|
373 | LispObject[] newElements = new LispObject[newCapacity]; |
---|
374 | if (initialContents.listp()) { |
---|
375 | LispObject list = initialContents; |
---|
376 | for (int i = 0; i < newCapacity; i++) { |
---|
377 | newElements[i] = list.car(); |
---|
378 | list = list.cdr(); |
---|
379 | } |
---|
380 | } else if (initialContents.vectorp()) { |
---|
381 | for (int i = 0; i < newCapacity; i++) |
---|
382 | newElements[i] = initialContents.elt(i); |
---|
383 | } else |
---|
384 | error(new TypeError(initialContents, Symbol.SEQUENCE)); |
---|
385 | elements = newElements; |
---|
386 | } else { |
---|
387 | if (elements == null) { |
---|
388 | // Displaced array. Copy existing elements. |
---|
389 | elements = new LispObject[newCapacity]; |
---|
390 | final int limit = Math.min(capacity, newCapacity); |
---|
391 | for (int i = 0; i < limit; i++) |
---|
392 | elements[i] = array.AREF(displacement + i); |
---|
393 | } else if (capacity != newCapacity) { |
---|
394 | LispObject[] newElements = new LispObject[newCapacity]; |
---|
395 | System.arraycopy(elements, 0, newElements, 0, |
---|
396 | Math.min(capacity, newCapacity)); |
---|
397 | elements = newElements; |
---|
398 | } |
---|
399 | // Initialize new elements (if aapplicable). |
---|
400 | if (initialElement != null) { |
---|
401 | for (int i = capacity; i < newCapacity; i++) |
---|
402 | elements[i] = initialElement; |
---|
403 | } |
---|
404 | } |
---|
405 | capacity = newCapacity; |
---|
406 | array = null; |
---|
407 | displacement = 0; |
---|
408 | isDisplaced = false; |
---|
409 | return this; |
---|
410 | } |
---|
411 | |
---|
412 | @Override |
---|
413 | public AbstractVector adjustArray(int newCapacity, |
---|
414 | AbstractArray displacedTo, |
---|
415 | int displacement) |
---|
416 | |
---|
417 | { |
---|
418 | capacity = newCapacity; |
---|
419 | array = displacedTo; |
---|
420 | this.displacement = displacement; |
---|
421 | elements = null; |
---|
422 | isDisplaced = true; |
---|
423 | return this; |
---|
424 | } |
---|
425 | } |
---|