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