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