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