1 | /* |
---|
2 | * ComplexArray_UnsignedByte32.java |
---|
3 | * |
---|
4 | * Copyright (C) 2003-2005 Peter Graves |
---|
5 | * $Id: ComplexArray_UnsignedByte32.java 12254 2009-11-06 20:07:54Z ehuelsmann $ |
---|
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 | public final class ComplexArray_UnsignedByte32 extends AbstractArray |
---|
37 | { |
---|
38 | private final int[] dimv; |
---|
39 | private int totalSize; |
---|
40 | |
---|
41 | // For non-displaced arrays. |
---|
42 | // FIXME We should really use an array of unboxed values! |
---|
43 | private LispObject[] data; |
---|
44 | |
---|
45 | // For displaced arrays. |
---|
46 | private AbstractArray array; |
---|
47 | private int displacement; |
---|
48 | |
---|
49 | public ComplexArray_UnsignedByte32(int[] dimv) |
---|
50 | { |
---|
51 | this.dimv = dimv; |
---|
52 | totalSize = computeTotalSize(dimv); |
---|
53 | data = new LispObject[totalSize]; |
---|
54 | for (int i = totalSize; i-- > 0;) |
---|
55 | data[i] = NIL; |
---|
56 | } |
---|
57 | |
---|
58 | public ComplexArray_UnsignedByte32(int[] dimv, LispObject initialContents) |
---|
59 | |
---|
60 | { |
---|
61 | this.dimv = dimv; |
---|
62 | final int rank = dimv.length; |
---|
63 | LispObject rest = initialContents; |
---|
64 | for (int i = 0; i < rank; i++) { |
---|
65 | dimv[i] = rest.length(); |
---|
66 | rest = rest.elt(0); |
---|
67 | } |
---|
68 | totalSize = computeTotalSize(dimv); |
---|
69 | data = new LispObject[totalSize]; |
---|
70 | setInitialContents(0, dimv, initialContents, 0); |
---|
71 | } |
---|
72 | |
---|
73 | public ComplexArray_UnsignedByte32(int[] dimv, AbstractArray array, |
---|
74 | int displacement) |
---|
75 | { |
---|
76 | this.dimv = dimv; |
---|
77 | this.array = array; |
---|
78 | this.displacement = displacement; |
---|
79 | totalSize = computeTotalSize(dimv); |
---|
80 | } |
---|
81 | |
---|
82 | private int setInitialContents(int axis, int[] dims, LispObject contents, |
---|
83 | int index) |
---|
84 | |
---|
85 | { |
---|
86 | if (dims.length == 0) { |
---|
87 | try { |
---|
88 | data[index] = contents; |
---|
89 | } |
---|
90 | catch (ArrayIndexOutOfBoundsException e) { |
---|
91 | error(new LispError("Bad initial contents for array.")); |
---|
92 | return -1; |
---|
93 | } |
---|
94 | ++index; |
---|
95 | } else { |
---|
96 | int dim = dims[0]; |
---|
97 | if (dim != contents.length()) { |
---|
98 | error(new LispError("Bad initial contents for array.")); |
---|
99 | return -1; |
---|
100 | } |
---|
101 | int[] newDims = new int[dims.length-1]; |
---|
102 | for (int i = 1; i < dims.length; i++) |
---|
103 | newDims[i-1] = dims[i]; |
---|
104 | if (contents.listp()) { |
---|
105 | for (int i = contents.length();i-- > 0;) { |
---|
106 | LispObject content = contents.car(); |
---|
107 | index = |
---|
108 | setInitialContents(axis + 1, newDims, content, index); |
---|
109 | contents = contents.cdr(); |
---|
110 | } |
---|
111 | } else { |
---|
112 | AbstractVector v = checkVector(contents); |
---|
113 | final int length = v.length(); |
---|
114 | for (int i = 0; i < length; i++) { |
---|
115 | LispObject content = v.AREF(i); |
---|
116 | index = |
---|
117 | setInitialContents(axis + 1, newDims, content, index); |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | return index; |
---|
122 | } |
---|
123 | |
---|
124 | @Override |
---|
125 | public LispObject typeOf() |
---|
126 | { |
---|
127 | return list(Symbol.ARRAY, UNSIGNED_BYTE_32, getDimensions()); |
---|
128 | } |
---|
129 | |
---|
130 | @Override |
---|
131 | public LispObject classOf() |
---|
132 | { |
---|
133 | return BuiltInClass.ARRAY; |
---|
134 | } |
---|
135 | |
---|
136 | @Override |
---|
137 | public int getRank() |
---|
138 | { |
---|
139 | return dimv.length; |
---|
140 | } |
---|
141 | |
---|
142 | @Override |
---|
143 | public LispObject getDimensions() |
---|
144 | { |
---|
145 | LispObject result = NIL; |
---|
146 | for (int i = dimv.length; i-- > 0;) |
---|
147 | result = new Cons(Fixnum.getInstance(dimv[i]), result); |
---|
148 | return result; |
---|
149 | } |
---|
150 | |
---|
151 | @Override |
---|
152 | public int getDimension(int n) |
---|
153 | { |
---|
154 | try { |
---|
155 | return dimv[n]; |
---|
156 | } |
---|
157 | catch (ArrayIndexOutOfBoundsException e) { |
---|
158 | error(new TypeError("Bad array dimension " + n + ".")); |
---|
159 | return -1; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | @Override |
---|
164 | public LispObject getElementType() |
---|
165 | { |
---|
166 | return UNSIGNED_BYTE_32; |
---|
167 | } |
---|
168 | |
---|
169 | @Override |
---|
170 | public int getTotalSize() |
---|
171 | { |
---|
172 | return totalSize; |
---|
173 | } |
---|
174 | |
---|
175 | @Override |
---|
176 | public LispObject arrayDisplacement() |
---|
177 | { |
---|
178 | LispObject value1, value2; |
---|
179 | if (array != null) { |
---|
180 | value1 = array; |
---|
181 | value2 = Fixnum.getInstance(displacement); |
---|
182 | } else { |
---|
183 | value1 = NIL; |
---|
184 | value2 = Fixnum.ZERO; |
---|
185 | } |
---|
186 | return LispThread.currentThread().setValues(value1, value2); |
---|
187 | } |
---|
188 | |
---|
189 | @Override |
---|
190 | public LispObject AREF(int index) |
---|
191 | { |
---|
192 | if (data != null) { |
---|
193 | try { |
---|
194 | return data[index]; |
---|
195 | } |
---|
196 | catch (ArrayIndexOutOfBoundsException e) { |
---|
197 | return error(new TypeError("Bad row major index " + index + ".")); |
---|
198 | } |
---|
199 | } else |
---|
200 | return array.AREF(index + displacement); |
---|
201 | } |
---|
202 | |
---|
203 | @Override |
---|
204 | public void aset(int index, LispObject newValue) |
---|
205 | { |
---|
206 | if (data != null) { |
---|
207 | try { |
---|
208 | data[index] = newValue; |
---|
209 | } |
---|
210 | catch (ArrayIndexOutOfBoundsException e) { |
---|
211 | error(new TypeError("Bad row major index " + index + ".")); |
---|
212 | } |
---|
213 | } else |
---|
214 | array.aset(index + displacement, newValue); |
---|
215 | } |
---|
216 | |
---|
217 | @Override |
---|
218 | public void fill(LispObject obj) |
---|
219 | { |
---|
220 | if (data != null) { |
---|
221 | for (int i = data.length; i-- > 0;) |
---|
222 | data[i] = obj; |
---|
223 | } else { |
---|
224 | for (int i = totalSize; i-- > 0;) |
---|
225 | aset(i, obj); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | @Override |
---|
230 | public String writeToString() |
---|
231 | { |
---|
232 | return writeToString(dimv); |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | @Override |
---|
237 | public AbstractArray adjustArray(int[] dims, |
---|
238 | LispObject initialElement, |
---|
239 | LispObject initialContents) |
---|
240 | { |
---|
241 | if (isAdjustable()) { |
---|
242 | if (initialContents != null) |
---|
243 | setInitialContents(0, dims, initialContents, 0); |
---|
244 | else { |
---|
245 | //### FIXME Take the easy way out: we don't want to reorganize |
---|
246 | // all of the array code yet |
---|
247 | SimpleArray_UnsignedByte32 tempArray = new SimpleArray_UnsignedByte32(dims); |
---|
248 | if (initialElement != null) |
---|
249 | tempArray.fill(initialElement); |
---|
250 | SimpleArray_UnsignedByte32.copyArray(this, tempArray); |
---|
251 | this.data = tempArray.data; |
---|
252 | |
---|
253 | for (int i = 0; i < dims.length; i++) |
---|
254 | dimv[i] = dims[i]; |
---|
255 | } |
---|
256 | return this; |
---|
257 | } else { |
---|
258 | if (initialContents != null) |
---|
259 | return new ComplexArray_UnsignedByte32(dims, initialContents); |
---|
260 | else { |
---|
261 | ComplexArray_UnsignedByte32 newArray = new ComplexArray_UnsignedByte32(dims); |
---|
262 | if (initialElement != null) |
---|
263 | newArray.fill(initialElement); |
---|
264 | return newArray; |
---|
265 | } |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | @Override |
---|
270 | public AbstractArray adjustArray(int[] dims, |
---|
271 | AbstractArray displacedTo, |
---|
272 | int displacement) |
---|
273 | { |
---|
274 | if (isAdjustable()) { |
---|
275 | for (int i = 0; i < dims.length; i++) |
---|
276 | dimv[i] = dims[i]; |
---|
277 | |
---|
278 | this.data = null; |
---|
279 | this.array = displacedTo; |
---|
280 | this.displacement = displacement; |
---|
281 | this.totalSize = computeTotalSize(dims); |
---|
282 | |
---|
283 | return this; |
---|
284 | } else { |
---|
285 | ComplexArray_UnsignedByte32 a = new ComplexArray_UnsignedByte32(dims, displacedTo, displacement); |
---|
286 | |
---|
287 | return a; |
---|
288 | } |
---|
289 | } |
---|
290 | } |
---|