1 | /* |
---|
2 | * SimpleArray_IntBuffer.java |
---|
3 | * |
---|
4 | * Copyright (C) 2020 @easye |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the GNU General Public License |
---|
8 | * as published by the Free Software Foundation; either version 2 |
---|
9 | * of the License, or (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | * |
---|
20 | * As a special exception, the copyright holders of this library give you |
---|
21 | * permission to link this library with independent modules to produce an |
---|
22 | * executable, regardless of the license terms of these independent |
---|
23 | * modules, and to copy and distribute the resulting executable under |
---|
24 | * terms of your choice, provided that you also meet, for each linked |
---|
25 | * independent module, the terms and conditions of the license of that |
---|
26 | * module. An independent module is a module which is not derived from |
---|
27 | * or based on this library. If you modify this library, you may extend |
---|
28 | * this exception to your version of the library, but you are not |
---|
29 | * obligated to do so. If you do not wish to do so, delete this |
---|
30 | * exception statement from your version. |
---|
31 | */ |
---|
32 | |
---|
33 | package org.armedbear.lisp; |
---|
34 | |
---|
35 | import static org.armedbear.lisp.Lisp.*; |
---|
36 | |
---|
37 | import java.nio.ByteBuffer; |
---|
38 | import java.nio.IntBuffer; |
---|
39 | |
---|
40 | public final class SimpleArray_IntBuffer |
---|
41 | extends AbstractArray |
---|
42 | { |
---|
43 | private final int[] dimv; |
---|
44 | private final int totalSize; |
---|
45 | |
---|
46 | final IntBuffer data; |
---|
47 | private boolean directAllocation; |
---|
48 | |
---|
49 | public SimpleArray_IntBuffer(int[] dimv) { |
---|
50 | this(dimv, false); |
---|
51 | } |
---|
52 | |
---|
53 | public SimpleArray_IntBuffer(int [] dimv, boolean directAllocation) { |
---|
54 | this.dimv = dimv; |
---|
55 | this.directAllocation = directAllocation; |
---|
56 | totalSize = computeTotalSize(dimv); |
---|
57 | if (directAllocation) { |
---|
58 | ByteBuffer b = ByteBuffer.allocateDirect(totalSize * 4); |
---|
59 | data = b.asIntBuffer(); |
---|
60 | } else { |
---|
61 | data = IntBuffer.allocate(totalSize); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | public SimpleArray_IntBuffer(int[] dimv, LispObject initialContents) { |
---|
66 | this(dimv, initialContents, false); |
---|
67 | } |
---|
68 | |
---|
69 | public SimpleArray_IntBuffer(int[] dimv, LispObject initialContents, boolean directAllocation) { |
---|
70 | this.dimv = dimv; |
---|
71 | final int rank = dimv.length; |
---|
72 | this.directAllocation = directAllocation; |
---|
73 | LispObject rest = initialContents; |
---|
74 | for (int i = 0; i < rank; i++) { |
---|
75 | dimv[i] = rest.length(); |
---|
76 | rest = rest.elt(0); |
---|
77 | } |
---|
78 | totalSize = computeTotalSize(dimv); |
---|
79 | if (directAllocation) { |
---|
80 | ByteBuffer b = ByteBuffer.allocateDirect(totalSize * 4); |
---|
81 | data = b.asIntBuffer(); |
---|
82 | } else { |
---|
83 | data = IntBuffer.allocate(totalSize); |
---|
84 | } |
---|
85 | |
---|
86 | setInitialContents(0, dimv, initialContents, 0); |
---|
87 | } |
---|
88 | |
---|
89 | public SimpleArray_IntBuffer(int rank, LispObject initialContents) { |
---|
90 | this(rank, initialContents, false); |
---|
91 | } |
---|
92 | public SimpleArray_IntBuffer(int rank, LispObject initialContents, boolean directAllocation) { |
---|
93 | if (rank < 2) { |
---|
94 | Debug.assertTrue(false); |
---|
95 | } |
---|
96 | dimv = new int[rank]; |
---|
97 | LispObject rest = initialContents; |
---|
98 | for (int i = 0; i < rank; i++) { |
---|
99 | dimv[i] = rest.length(); |
---|
100 | if (rest == NIL || rest.length() == 0) |
---|
101 | break; |
---|
102 | rest = rest.elt(0); |
---|
103 | } |
---|
104 | totalSize = computeTotalSize(dimv); |
---|
105 | if (directAllocation) { |
---|
106 | ByteBuffer b = ByteBuffer.allocateDirect(totalSize * 4); |
---|
107 | data = b.asIntBuffer(); |
---|
108 | } else { |
---|
109 | data = IntBuffer.allocate(totalSize); |
---|
110 | } |
---|
111 | |
---|
112 | setInitialContents(0, dimv, initialContents, 0); |
---|
113 | } |
---|
114 | |
---|
115 | private int setInitialContents(int axis, int[] dims, LispObject contents, |
---|
116 | int index) { |
---|
117 | if (dims.length == 0) { |
---|
118 | try { |
---|
119 | data.put(index, (int)(contents.longValue() & 0xffffffffL)); |
---|
120 | } catch (IndexOutOfBoundsException e) { |
---|
121 | error(new LispError("Bad initial contents for array.")); |
---|
122 | return -1; |
---|
123 | } |
---|
124 | ++index; |
---|
125 | } else { |
---|
126 | int dim = dims[0]; |
---|
127 | if (dim != contents.length()) { |
---|
128 | error(new LispError("Bad initial contents for array.")); |
---|
129 | return -1; |
---|
130 | } |
---|
131 | int[] newDims = new int[dims.length-1]; |
---|
132 | for (int i = 1; i < dims.length; i++) |
---|
133 | newDims[i-1] = dims[i]; |
---|
134 | if (contents.listp()) { |
---|
135 | for (int i = contents.length();i-- > 0;) { |
---|
136 | LispObject content = contents.car(); |
---|
137 | index = |
---|
138 | setInitialContents(axis + 1, newDims, content, index); |
---|
139 | contents = contents.cdr(); |
---|
140 | } |
---|
141 | } else { |
---|
142 | AbstractVector v = checkVector(contents); |
---|
143 | final int length = v.length(); |
---|
144 | for (int i = 0; i < length; i++) { |
---|
145 | LispObject content = v.AREF(i); |
---|
146 | index = |
---|
147 | setInitialContents(axis + 1, newDims, content, index); |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | return index; |
---|
152 | } |
---|
153 | |
---|
154 | @Override |
---|
155 | public LispObject typeOf() { |
---|
156 | return list(Symbol.SIMPLE_ARRAY, UNSIGNED_BYTE_32, getDimensions()); |
---|
157 | } |
---|
158 | |
---|
159 | @Override |
---|
160 | public LispObject classOf() { |
---|
161 | return BuiltInClass.SIMPLE_ARRAY; |
---|
162 | } |
---|
163 | |
---|
164 | @Override |
---|
165 | public LispObject typep(LispObject typeSpecifier) { |
---|
166 | if (typeSpecifier == Symbol.SIMPLE_ARRAY) |
---|
167 | return T; |
---|
168 | if (typeSpecifier == BuiltInClass.SIMPLE_ARRAY) |
---|
169 | return T; |
---|
170 | return super.typep(typeSpecifier); |
---|
171 | } |
---|
172 | |
---|
173 | @Override |
---|
174 | public int getRank() { |
---|
175 | return dimv.length; |
---|
176 | } |
---|
177 | |
---|
178 | @Override |
---|
179 | public LispObject getDimensions() { |
---|
180 | LispObject result = NIL; |
---|
181 | for (int i = dimv.length; i-- > 0;) |
---|
182 | result = new Cons(Fixnum.getInstance(dimv[i]), result); |
---|
183 | return result; |
---|
184 | } |
---|
185 | |
---|
186 | @Override |
---|
187 | public int getDimension(int n) { |
---|
188 | try { |
---|
189 | return dimv[n]; |
---|
190 | } catch (ArrayIndexOutOfBoundsException e) { |
---|
191 | error(new TypeError("Bad array dimension " + n + ".")); |
---|
192 | return -1; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | @Override |
---|
197 | public LispObject getElementType() { |
---|
198 | return UNSIGNED_BYTE_32; |
---|
199 | } |
---|
200 | |
---|
201 | @Override |
---|
202 | public int getTotalSize() { |
---|
203 | return totalSize; |
---|
204 | } |
---|
205 | |
---|
206 | @Override |
---|
207 | public boolean isAdjustable() { |
---|
208 | return false; |
---|
209 | } |
---|
210 | |
---|
211 | @Override |
---|
212 | public LispObject AREF(int index) { |
---|
213 | try { |
---|
214 | return number(((long)data.get(index)) & 0xffffffffL); |
---|
215 | } catch (IndexOutOfBoundsException e) { |
---|
216 | return error(new TypeError("Bad row major index " + index + ".")); |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | @Override |
---|
221 | public void aset(int index, LispObject newValue) { |
---|
222 | try { |
---|
223 | if (newValue.isLessThan(Fixnum.ZERO) || newValue.isGreaterThan(UNSIGNED_BYTE_32_MAX_VALUE)) { |
---|
224 | type_error(newValue, UNSIGNED_BYTE_32); |
---|
225 | } |
---|
226 | data.put(index, (int)(newValue.longValue() & 0xffffffffL)); |
---|
227 | } catch (IndexOutOfBoundsException e) { |
---|
228 | error(new TypeError("Bad row major index " + index + ".")); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | @Override |
---|
233 | public int getRowMajorIndex(int[] subscripts) { |
---|
234 | final int rank = dimv.length; |
---|
235 | if (rank != subscripts.length) { |
---|
236 | StringBuffer sb = new StringBuffer("Wrong number of subscripts ("); |
---|
237 | sb.append(subscripts.length); |
---|
238 | sb.append(") for array of rank "); |
---|
239 | sb.append(rank); |
---|
240 | sb.append('.'); |
---|
241 | program_error(sb.toString()); |
---|
242 | } |
---|
243 | int sum = 0; |
---|
244 | int size = 1; |
---|
245 | for (int i = rank; i-- > 0;) { |
---|
246 | final int dim = dimv[i]; |
---|
247 | final int lastSize = size; |
---|
248 | size *= dim; |
---|
249 | int n = subscripts[i]; |
---|
250 | if (n < 0 || n >= dim) { |
---|
251 | StringBuffer sb = new StringBuffer("Invalid index "); |
---|
252 | sb.append(n); |
---|
253 | sb.append(" for array "); |
---|
254 | sb.append(this); |
---|
255 | sb.append('.'); |
---|
256 | program_error(sb.toString()); |
---|
257 | } |
---|
258 | sum += n * lastSize; |
---|
259 | } |
---|
260 | return sum; |
---|
261 | } |
---|
262 | |
---|
263 | @Override |
---|
264 | public LispObject get(int[] subscripts) { |
---|
265 | try { |
---|
266 | return number(((long)data.get(getRowMajorIndex(subscripts))) & 0xffffffffL); |
---|
267 | } catch (IndexOutOfBoundsException e) { |
---|
268 | return error(new TypeError("Bad row major index " + |
---|
269 | getRowMajorIndex(subscripts) + ".")); |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | @Override |
---|
274 | public void set(int[] subscripts, LispObject newValue) { |
---|
275 | try { |
---|
276 | if (newValue.isLessThan(Fixnum.ZERO) || newValue.isGreaterThan(UNSIGNED_BYTE_32_MAX_VALUE)) { |
---|
277 | type_error(newValue, UNSIGNED_BYTE_32); |
---|
278 | } |
---|
279 | data.put(getRowMajorIndex(subscripts), (int)(newValue.longValue() & 0xffffffffL)); |
---|
280 | } catch (IndexOutOfBoundsException e) { |
---|
281 | error(new TypeError("Bad row major index " + |
---|
282 | getRowMajorIndex(subscripts) + ".")); |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | @Override |
---|
287 | public void fill(LispObject obj) { |
---|
288 | if (!(obj instanceof LispInteger)) { |
---|
289 | type_error(obj, Symbol.INTEGER); |
---|
290 | // Not reached. |
---|
291 | return; |
---|
292 | } |
---|
293 | if (obj.isLessThan(Fixnum.ZERO) || obj.isGreaterThan(UNSIGNED_BYTE_32_MAX_VALUE)) { |
---|
294 | type_error(obj, UNSIGNED_BYTE_32); |
---|
295 | } |
---|
296 | for (int i = totalSize; i-- > 0;) |
---|
297 | data.put(i, (int) (obj.longValue() & 0xffffffffL));; |
---|
298 | } |
---|
299 | |
---|
300 | @Override |
---|
301 | public String printObject() { |
---|
302 | if (Symbol.PRINT_READABLY.symbolValue() != NIL) { |
---|
303 | error(new PrintNotReadable(list(Keyword.OBJECT, this))); |
---|
304 | // Not reached. |
---|
305 | return null; |
---|
306 | } |
---|
307 | return printObject(dimv); |
---|
308 | } |
---|
309 | |
---|
310 | public AbstractArray adjustArray(int[] dimv, LispObject initialElement, |
---|
311 | LispObject initialContents) { |
---|
312 | if (initialContents != null) { |
---|
313 | return new SimpleArray_IntBuffer(dimv, initialContents); |
---|
314 | } |
---|
315 | for (int i = 0; i < dimv.length; i++) { |
---|
316 | if (dimv[i] != this.dimv[i]) { |
---|
317 | SimpleArray_IntBuffer newArray |
---|
318 | = new SimpleArray_IntBuffer(dimv, directAllocation); |
---|
319 | if (initialElement != null) { |
---|
320 | newArray.fill(initialElement); |
---|
321 | } |
---|
322 | copyArray(this, newArray); |
---|
323 | |
---|
324 | return newArray; |
---|
325 | } |
---|
326 | } |
---|
327 | // New dimensions are identical to old dimensions. |
---|
328 | return this; |
---|
329 | } |
---|
330 | |
---|
331 | // Copy a1 to a2 for index tuples that are valid for both arrays. |
---|
332 | static void copyArray(AbstractArray a1, AbstractArray a2) { |
---|
333 | Debug.assertTrue(a1.getRank() == a2.getRank()); |
---|
334 | int[] subscripts = new int[a1.getRank()]; |
---|
335 | int axis = 0; |
---|
336 | copySubArray(a1, a2, subscripts, axis); |
---|
337 | } |
---|
338 | |
---|
339 | private static void copySubArray(AbstractArray a1, AbstractArray a2, |
---|
340 | int[] subscripts, int axis) { |
---|
341 | if (axis < subscripts.length) { |
---|
342 | final int limit |
---|
343 | = Math.min(a1.getDimension(axis), a2.getDimension(axis)); |
---|
344 | for (int i = 0; i < limit; i++) { |
---|
345 | subscripts[axis] = i; |
---|
346 | copySubArray(a1, a2, subscripts, axis + 1); |
---|
347 | } |
---|
348 | } else { |
---|
349 | int i1 = a1.getRowMajorIndex(subscripts); |
---|
350 | int i2 = a2.getRowMajorIndex(subscripts); |
---|
351 | a2.aset(i2, a1.AREF(i1)); |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | public AbstractArray adjustArray(int[] dimv, AbstractArray displacedTo, |
---|
356 | int displacement) { |
---|
357 | return new ComplexArray(dimv, displacedTo, displacement); |
---|
358 | } |
---|
359 | } |
---|