1 | /* |
---|
2 | * SimpleArray_UnsignedByte8.java |
---|
3 | * |
---|
4 | * Copyright (C) 2003-2005 Peter Graves |
---|
5 | * $Id: SimpleArray_UnsignedByte8.java 14465 2013-04-24 12:50:37Z rschlatte $ |
---|
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 | public final class SimpleArray_UnsignedByte8 extends AbstractArray |
---|
39 | { |
---|
40 | private final int[] dimv; |
---|
41 | private final int totalSize; |
---|
42 | final byte[] data; |
---|
43 | |
---|
44 | public SimpleArray_UnsignedByte8(int[] dimv) |
---|
45 | { |
---|
46 | this.dimv = dimv; |
---|
47 | totalSize = computeTotalSize(dimv); |
---|
48 | data = new byte[totalSize]; |
---|
49 | } |
---|
50 | |
---|
51 | public SimpleArray_UnsignedByte8(int[] dimv, LispObject initialContents) |
---|
52 | |
---|
53 | { |
---|
54 | this.dimv = dimv; |
---|
55 | final int rank = dimv.length; |
---|
56 | LispObject rest = initialContents; |
---|
57 | for (int i = 0; i < rank; i++) { |
---|
58 | dimv[i] = rest.length(); |
---|
59 | rest = rest.elt(0); |
---|
60 | } |
---|
61 | totalSize = computeTotalSize(dimv); |
---|
62 | data = new byte[totalSize]; |
---|
63 | setInitialContents(0, dimv, initialContents, 0); |
---|
64 | } |
---|
65 | |
---|
66 | public SimpleArray_UnsignedByte8(int rank, LispObject initialContents) |
---|
67 | |
---|
68 | { |
---|
69 | if (rank < 2) |
---|
70 | Debug.assertTrue(false); |
---|
71 | dimv = new int[rank]; |
---|
72 | LispObject rest = initialContents; |
---|
73 | for (int i = 0; i < rank; i++) { |
---|
74 | dimv[i] = rest.length(); |
---|
75 | if (rest == NIL || rest.length() == 0) |
---|
76 | break; |
---|
77 | rest = rest.elt(0); |
---|
78 | } |
---|
79 | totalSize = computeTotalSize(dimv); |
---|
80 | data = new byte[totalSize]; |
---|
81 | setInitialContents(0, dimv, initialContents, 0); |
---|
82 | } |
---|
83 | |
---|
84 | private int setInitialContents(int axis, int[] dims, LispObject contents, |
---|
85 | int index) |
---|
86 | |
---|
87 | { |
---|
88 | if (dims.length == 0) { |
---|
89 | try { |
---|
90 | data[index] = coerceLispObjectToJavaByte(contents); |
---|
91 | } |
---|
92 | catch (ArrayIndexOutOfBoundsException e) { |
---|
93 | error(new LispError("Bad initial contents for array.")); |
---|
94 | return -1; |
---|
95 | } |
---|
96 | ++index; |
---|
97 | } else { |
---|
98 | int dim = dims[0]; |
---|
99 | if (dim != contents.length()) { |
---|
100 | error(new LispError("Bad initial contents for array.")); |
---|
101 | return -1; |
---|
102 | } |
---|
103 | int[] newDims = new int[dims.length-1]; |
---|
104 | for (int i = 1; i < dims.length; i++) |
---|
105 | newDims[i-1] = dims[i]; |
---|
106 | if (contents.listp()) { |
---|
107 | for (int i = contents.length();i-- > 0;) { |
---|
108 | LispObject content = contents.car(); |
---|
109 | index = |
---|
110 | setInitialContents(axis + 1, newDims, content, index); |
---|
111 | contents = contents.cdr(); |
---|
112 | } |
---|
113 | } else { |
---|
114 | AbstractVector v = checkVector(contents); |
---|
115 | final int length = v.length(); |
---|
116 | for (int i = 0; i < length; i++) { |
---|
117 | LispObject content = v.AREF(i); |
---|
118 | index = |
---|
119 | setInitialContents(axis + 1, newDims, content, index); |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|
123 | return index; |
---|
124 | } |
---|
125 | |
---|
126 | @Override |
---|
127 | public LispObject typeOf() |
---|
128 | { |
---|
129 | return list(Symbol.SIMPLE_ARRAY, UNSIGNED_BYTE_8, getDimensions()); |
---|
130 | } |
---|
131 | |
---|
132 | @Override |
---|
133 | public LispObject classOf() |
---|
134 | { |
---|
135 | return BuiltInClass.SIMPLE_ARRAY; |
---|
136 | } |
---|
137 | |
---|
138 | @Override |
---|
139 | public LispObject typep(LispObject typeSpecifier) |
---|
140 | { |
---|
141 | if (typeSpecifier == Symbol.SIMPLE_ARRAY) |
---|
142 | return T; |
---|
143 | if (typeSpecifier == BuiltInClass.SIMPLE_ARRAY) |
---|
144 | return T; |
---|
145 | return super.typep(typeSpecifier); |
---|
146 | } |
---|
147 | |
---|
148 | @Override |
---|
149 | public int getRank() |
---|
150 | { |
---|
151 | return dimv.length; |
---|
152 | } |
---|
153 | |
---|
154 | @Override |
---|
155 | public LispObject getDimensions() |
---|
156 | { |
---|
157 | LispObject result = NIL; |
---|
158 | for (int i = dimv.length; i-- > 0;) |
---|
159 | result = new Cons(Fixnum.getInstance(dimv[i]), result); |
---|
160 | return result; |
---|
161 | } |
---|
162 | |
---|
163 | @Override |
---|
164 | public int getDimension(int n) |
---|
165 | { |
---|
166 | try { |
---|
167 | return dimv[n]; |
---|
168 | } |
---|
169 | catch (ArrayIndexOutOfBoundsException e) { |
---|
170 | error(new TypeError("Bad array dimension " + n + ".")); |
---|
171 | return -1; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | @Override |
---|
176 | public LispObject getElementType() |
---|
177 | { |
---|
178 | return UNSIGNED_BYTE_8; |
---|
179 | } |
---|
180 | |
---|
181 | @Override |
---|
182 | public int getTotalSize() |
---|
183 | { |
---|
184 | return totalSize; |
---|
185 | } |
---|
186 | |
---|
187 | @Override |
---|
188 | public boolean isAdjustable() |
---|
189 | { |
---|
190 | return false; |
---|
191 | } |
---|
192 | |
---|
193 | @Override |
---|
194 | public LispObject AREF(int index) |
---|
195 | { |
---|
196 | try { |
---|
197 | return coerceJavaByteToLispObject(data[index]); |
---|
198 | } |
---|
199 | catch (ArrayIndexOutOfBoundsException e) { |
---|
200 | return error(new TypeError("Bad row major index " + index + ".")); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | @Override |
---|
205 | public void aset(int index, LispObject newValue) |
---|
206 | { |
---|
207 | try { |
---|
208 | data[index] = coerceLispObjectToJavaByte(newValue); |
---|
209 | } |
---|
210 | catch (ArrayIndexOutOfBoundsException e) { |
---|
211 | error(new TypeError("Bad row major index " + index + ".")); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | @Override |
---|
216 | public int getRowMajorIndex(int[] subscripts) |
---|
217 | { |
---|
218 | final int rank = dimv.length; |
---|
219 | if (rank != subscripts.length) { |
---|
220 | StringBuffer sb = new StringBuffer("Wrong number of subscripts ("); |
---|
221 | sb.append(subscripts.length); |
---|
222 | sb.append(") for array of rank "); |
---|
223 | sb.append(rank); |
---|
224 | sb.append('.'); |
---|
225 | program_error(sb.toString()); |
---|
226 | } |
---|
227 | int sum = 0; |
---|
228 | int size = 1; |
---|
229 | for (int i = rank; i-- > 0;) { |
---|
230 | final int dim = dimv[i]; |
---|
231 | final int lastSize = size; |
---|
232 | size *= dim; |
---|
233 | int n = subscripts[i]; |
---|
234 | if (n < 0 || n >= dim) { |
---|
235 | StringBuffer sb = new StringBuffer("Invalid index "); |
---|
236 | sb.append(n); |
---|
237 | sb.append(" for array "); |
---|
238 | sb.append(this); |
---|
239 | sb.append('.'); |
---|
240 | program_error(sb.toString()); |
---|
241 | } |
---|
242 | sum += n * lastSize; |
---|
243 | } |
---|
244 | return sum; |
---|
245 | } |
---|
246 | |
---|
247 | @Override |
---|
248 | public LispObject get(int[] subscripts) |
---|
249 | { |
---|
250 | try { |
---|
251 | return coerceJavaByteToLispObject(data[getRowMajorIndex(subscripts)]); |
---|
252 | } |
---|
253 | catch (ArrayIndexOutOfBoundsException e) { |
---|
254 | return error(new TypeError("Bad row major index " + |
---|
255 | getRowMajorIndex(subscripts) + ".")); |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | @Override |
---|
260 | public void set(int[] subscripts, LispObject newValue) |
---|
261 | |
---|
262 | { |
---|
263 | try { |
---|
264 | data[getRowMajorIndex(subscripts)] = coerceLispObjectToJavaByte(newValue); |
---|
265 | } |
---|
266 | catch (ArrayIndexOutOfBoundsException e) { |
---|
267 | error(new TypeError("Bad row major index " + |
---|
268 | getRowMajorIndex(subscripts) + ".")); |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | @Override |
---|
273 | public void fill(LispObject obj) |
---|
274 | { |
---|
275 | byte b = coerceLispObjectToJavaByte(obj); |
---|
276 | for (int i = totalSize; i-- > 0;) |
---|
277 | data[i] = b; |
---|
278 | } |
---|
279 | |
---|
280 | @Override |
---|
281 | public String printObject() |
---|
282 | { |
---|
283 | if (Symbol.PRINT_READABLY.symbolValue() != NIL) { |
---|
284 | error(new PrintNotReadable(list(Keyword.OBJECT, this))); |
---|
285 | // Not reached. |
---|
286 | return null; |
---|
287 | } |
---|
288 | return printObject(dimv); |
---|
289 | } |
---|
290 | |
---|
291 | public AbstractArray adjustArray(int[] dimv, LispObject initialElement, |
---|
292 | LispObject initialContents) |
---|
293 | |
---|
294 | { |
---|
295 | if (initialContents != null) |
---|
296 | return new SimpleArray_UnsignedByte8(dimv, initialContents); |
---|
297 | for (int i = 0; i < dimv.length; i++) { |
---|
298 | if (dimv[i] != this.dimv[i]) { |
---|
299 | SimpleArray_UnsignedByte8 newArray = |
---|
300 | new SimpleArray_UnsignedByte8(dimv); |
---|
301 | if (initialElement != null) |
---|
302 | newArray.fill(initialElement); |
---|
303 | copyArray(this, newArray); |
---|
304 | return newArray; |
---|
305 | } |
---|
306 | } |
---|
307 | // New dimensions are identical to old dimensions. |
---|
308 | return this; |
---|
309 | } |
---|
310 | |
---|
311 | // Copy a1 to a2 for index tuples that are valid for both arrays. |
---|
312 | static void copyArray(AbstractArray a1, AbstractArray a2) |
---|
313 | |
---|
314 | { |
---|
315 | Debug.assertTrue(a1.getRank() == a2.getRank()); |
---|
316 | int[] subscripts = new int[a1.getRank()]; |
---|
317 | int axis = 0; |
---|
318 | copySubArray(a1, a2, subscripts, axis); |
---|
319 | } |
---|
320 | |
---|
321 | private static void copySubArray(AbstractArray a1, AbstractArray a2, |
---|
322 | int[] subscripts, int axis) |
---|
323 | |
---|
324 | { |
---|
325 | if (axis < subscripts.length) { |
---|
326 | final int limit = |
---|
327 | Math.min(a1.getDimension(axis), a2.getDimension(axis)); |
---|
328 | for (int i = 0; i < limit; i++) { |
---|
329 | subscripts[axis] = i; |
---|
330 | copySubArray(a1, a2, subscripts, axis + 1); |
---|
331 | } |
---|
332 | } else { |
---|
333 | int i1 = a1.getRowMajorIndex(subscripts); |
---|
334 | int i2 = a2.getRowMajorIndex(subscripts); |
---|
335 | a2.aset(i2, a1.AREF(i1)); |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | public AbstractArray adjustArray(int[] dimv, AbstractArray displacedTo, |
---|
340 | int displacement) |
---|
341 | { |
---|
342 | return new ComplexArray(dimv, displacedTo, displacement); |
---|
343 | } |
---|
344 | } |
---|