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