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