1 | /* |
---|
2 | * make_array.java |
---|
3 | * |
---|
4 | * Copyright (C) 2003-2005 Peter Graves |
---|
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 org.armedbear.lisp.Java.Buffers.AllocationPolicy; |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | // ### %make-array dimensions element-type initial-element initial-element-p initial-contents adjustable fill-pointer displaced-to displaced-index-offset nio-buffer nio-buffer-provided-p |
---|
39 | // => new-array |
---|
40 | public final class make_array |
---|
41 | extends Primitive |
---|
42 | { |
---|
43 | public make_array() { |
---|
44 | super("%make-array", PACKAGE_SYS, false); |
---|
45 | } |
---|
46 | |
---|
47 | @Override |
---|
48 | public LispObject execute(LispObject[] args) { |
---|
49 | // What a mess without keywords, but it still works⊠|
---|
50 | if (args.length != 13) { |
---|
51 | return error(new WrongNumberOfArgumentsException(this, 13)); |
---|
52 | } |
---|
53 | LispObject dimensions = args[0]; |
---|
54 | LispObject elementType = args[1]; |
---|
55 | LispObject initialElement = args[2]; |
---|
56 | LispObject initialElementProvided = args[3]; |
---|
57 | LispObject initialContents = args[4]; |
---|
58 | LispObject adjustable = args[5]; |
---|
59 | LispObject fillPointer = args[6]; |
---|
60 | LispObject displacedTo = args[7]; |
---|
61 | LispObject displacedIndexOffset = args[8]; |
---|
62 | LispObject direct = args[9]; // boolean whether to do a direct allocation for nio capable vectors |
---|
63 | // LispObject directProvided = args[10]; // UNUSED but included for consistency |
---|
64 | boolean directAllocation = direct.equals(NIL) ? false : true; |
---|
65 | LispObject nioBuffer = args[11]; |
---|
66 | LispObject nioBufferProvided = args[12]; |
---|
67 | |
---|
68 | if (initialElementProvided != NIL && initialContents != NIL) { |
---|
69 | return error(new LispError("MAKE-ARRAY: cannot specify both " + |
---|
70 | "initial element and initial contents.")); |
---|
71 | } |
---|
72 | final int rank = dimensions.listp() ? dimensions.length() : 1; |
---|
73 | int[] dimv = new int[rank]; |
---|
74 | if (dimensions.listp()) { |
---|
75 | for (int i = 0; i < rank; i++) { |
---|
76 | LispObject dim = dimensions.car(); |
---|
77 | dimv[i] = Fixnum.getValue(dim); |
---|
78 | dimensions = dimensions.cdr(); |
---|
79 | } |
---|
80 | } else { |
---|
81 | dimv[0] = Fixnum.getValue(dimensions); |
---|
82 | } |
---|
83 | if (displacedTo != NIL) { |
---|
84 | // FIXME Make sure element type (if specified) is compatible with |
---|
85 | // displaced-to array. |
---|
86 | final AbstractArray array = checkArray(displacedTo); |
---|
87 | if (initialElementProvided != NIL) { |
---|
88 | return error(new LispError("Initial element must not be specified for a displaced array.")); |
---|
89 | } |
---|
90 | if (initialContents != NIL) { |
---|
91 | return error(new LispError("Initial contents must not be specified for a displaced array.")); |
---|
92 | } |
---|
93 | final int displacement; |
---|
94 | if (displacedIndexOffset != NIL) { |
---|
95 | displacement = Fixnum.getValue(displacedIndexOffset); |
---|
96 | } else { |
---|
97 | displacement = 0; |
---|
98 | } |
---|
99 | if (rank == 1) |
---|
100 | { |
---|
101 | AbstractVector v = null; // FIXME needed to get the compiler not to warn |
---|
102 | LispObject arrayElementType = array.getElementType(); |
---|
103 | if (arrayElementType == Symbol.CHARACTER) { |
---|
104 | v = new ComplexString(dimv[0], array, displacement); |
---|
105 | } else if (arrayElementType == Symbol.BIT) { |
---|
106 | v = new ComplexBitVector(dimv[0], array, displacement); |
---|
107 | } else if (arrayElementType.equal(UNSIGNED_BYTE_8)) { |
---|
108 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
109 | // an abstract array doesn't have a directAllocation ??? |
---|
110 | v = new ComplexVector_ByteBuffer(dimv[0], array, displacement, directAllocation); |
---|
111 | } else { // if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) |
---|
112 | v = new ComplexVector_UnsignedByte8(dimv[0], array, displacement); |
---|
113 | } |
---|
114 | } else if (arrayElementType.equal(UNSIGNED_BYTE_32)) { |
---|
115 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
116 | // an abstract array doesn't have a directAllocation ??? |
---|
117 | v = new ComplexVector_IntBuffer(dimv[0], array, displacement, directAllocation); |
---|
118 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) |
---|
119 | v = new ComplexVector_UnsignedByte32(dimv[0], array, displacement); |
---|
120 | } |
---|
121 | } else { |
---|
122 | v = new ComplexVector(dimv[0], array, displacement); |
---|
123 | } |
---|
124 | if (fillPointer != NIL) { |
---|
125 | v.setFillPointer(fillPointer); |
---|
126 | } |
---|
127 | return v; |
---|
128 | } |
---|
129 | return new ComplexArray(dimv, array, displacement); |
---|
130 | } |
---|
131 | |
---|
132 | LispObject upgradedType = getUpgradedArrayElementType(elementType); |
---|
133 | if (rank == 0) { |
---|
134 | LispObject data; |
---|
135 | if (initialElementProvided != NIL) { |
---|
136 | data = initialElement; |
---|
137 | } else { |
---|
138 | data = initialContents; |
---|
139 | } |
---|
140 | return new ZeroRankArray(upgradedType, data, adjustable != NIL); |
---|
141 | } else if (rank == 1) { |
---|
142 | final int size = dimv[0]; |
---|
143 | if (size < 0 || size >= ARRAY_DIMENSION_MAX) { |
---|
144 | StringBuilder sb = new StringBuilder(); |
---|
145 | sb.append("The size specified for this array ("); |
---|
146 | sb.append(size); |
---|
147 | sb.append(')'); |
---|
148 | if (size >= ARRAY_DIMENSION_MAX) { |
---|
149 | sb.append(" is >= ARRAY-DIMENSION-LIMIT ("); |
---|
150 | sb.append(ARRAY_DIMENSION_MAX); |
---|
151 | sb.append(")."); |
---|
152 | } else { |
---|
153 | sb.append(" is negative.");} |
---|
154 | return error(new LispError(sb.toString())); |
---|
155 | } |
---|
156 | final AbstractVector v; |
---|
157 | final LispObject defaultInitialElement; |
---|
158 | if (upgradedType == Symbol.CHARACTER) { |
---|
159 | if (fillPointer != NIL || adjustable != NIL) { |
---|
160 | v = new ComplexString(size); |
---|
161 | } else { |
---|
162 | v = new SimpleString(size); |
---|
163 | } |
---|
164 | defaultInitialElement = LispCharacter.getInstance('\0'); |
---|
165 | } else if (upgradedType == Symbol.BIT) { |
---|
166 | if (fillPointer != NIL || adjustable != NIL) { |
---|
167 | v = new ComplexBitVector(size); |
---|
168 | } else { |
---|
169 | v = new SimpleBitVector(size); |
---|
170 | } |
---|
171 | defaultInitialElement = Fixnum.ZERO; |
---|
172 | } else if (upgradedType.equal(UNSIGNED_BYTE_8)) { |
---|
173 | if (fillPointer != NIL || adjustable != NIL) { |
---|
174 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
175 | v = new ComplexVector_ByteBuffer(size, directAllocation); |
---|
176 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
177 | v = new ComplexVector_UnsignedByte8(size); |
---|
178 | } |
---|
179 | } else { |
---|
180 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
181 | if (!nioBuffer.equals(NIL)) { |
---|
182 | v = new BasicVector_ByteBuffer((java.nio.ByteBuffer)(((JavaObject)nioBuffer).getObject()), |
---|
183 | directAllocation); |
---|
184 | } else { |
---|
185 | v = new BasicVector_ByteBuffer(size, directAllocation); |
---|
186 | } |
---|
187 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
188 | v = new BasicVector_UnsignedByte8(size); |
---|
189 | } |
---|
190 | } |
---|
191 | defaultInitialElement = Fixnum.ZERO; |
---|
192 | } else if (upgradedType.equal(UNSIGNED_BYTE_16) |
---|
193 | && fillPointer == NIL && adjustable == NIL) { |
---|
194 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
195 | if (!nioBuffer.equals(NIL)) { |
---|
196 | Object o = ((JavaObject)nioBuffer).getObject(); |
---|
197 | if (o instanceof java.nio.CharBuffer) { |
---|
198 | v = new BasicVector_CharBuffer((java.nio.CharBuffer) o, directAllocation); |
---|
199 | } else if (o instanceof java.nio.ByteBuffer) { |
---|
200 | v = new BasicVector_CharBuffer((java.nio.ByteBuffer)o, directAllocation); // FIXME warn on coercion? |
---|
201 | } else { |
---|
202 | return type_error(nioBuffer, JavaObject.getInstance(java.nio.CharBuffer.class)); |
---|
203 | } |
---|
204 | } else { |
---|
205 | v = new BasicVector_CharBuffer(size, directAllocation); |
---|
206 | } |
---|
207 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
208 | v = new BasicVector_UnsignedByte16(size); |
---|
209 | } |
---|
210 | defaultInitialElement = Fixnum.ZERO; |
---|
211 | } else if (upgradedType.equal(UNSIGNED_BYTE_32)) { |
---|
212 | if (fillPointer != NIL || adjustable != NIL) { |
---|
213 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
214 | v = new ComplexVector_IntBuffer(size); |
---|
215 | } else { |
---|
216 | v = new ComplexVector_UnsignedByte32(size); |
---|
217 | } |
---|
218 | } else { |
---|
219 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
220 | if (!nioBuffer.equals(NIL)) { |
---|
221 | Object o = ((JavaObject)nioBuffer).getObject(); |
---|
222 | if (o instanceof java.nio.IntBuffer) { |
---|
223 | v = new BasicVector_IntBuffer((java.nio.IntBuffer)o, directAllocation); |
---|
224 | } else if (o instanceof java.nio.ByteBuffer) { |
---|
225 | v = new BasicVector_IntBuffer((java.nio.ByteBuffer)o, directAllocation); |
---|
226 | } else { |
---|
227 | return type_error(nioBuffer, JavaObject.getInstance(java.nio.IntBuffer.class)); |
---|
228 | } |
---|
229 | } else { |
---|
230 | v = new BasicVector_IntBuffer(size, directAllocation); |
---|
231 | } |
---|
232 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
233 | v = new BasicVector_UnsignedByte32(size); |
---|
234 | } |
---|
235 | } |
---|
236 | defaultInitialElement = Fixnum.ZERO; |
---|
237 | } else if (upgradedType == NIL) { |
---|
238 | v = new NilVector(size); |
---|
239 | defaultInitialElement = null; |
---|
240 | } else { |
---|
241 | if (fillPointer != NIL || adjustable != NIL) { |
---|
242 | v = new ComplexVector(size); |
---|
243 | } else { |
---|
244 | v = new SimpleVector(size); |
---|
245 | } |
---|
246 | defaultInitialElement = NIL; |
---|
247 | } |
---|
248 | |
---|
249 | if (nioBuffer != NIL) { |
---|
250 | // v should have been allocated with a nioBuffer reference⊠|
---|
251 | ; |
---|
252 | } else if (initialElementProvided != NIL) { |
---|
253 | // Initial element was specified. |
---|
254 | v.fill(initialElement); |
---|
255 | } else if (initialContents != NIL) { |
---|
256 | if (initialContents.listp()) { |
---|
257 | LispObject list = initialContents; |
---|
258 | for (int i = 0; i < size; i++) { |
---|
259 | v.aset(i, list.car()); |
---|
260 | list = list.cdr(); |
---|
261 | } |
---|
262 | } else if (initialContents.vectorp()) { |
---|
263 | for (int i = 0; i < size; i++) { |
---|
264 | v.aset(i, initialContents.elt(i)); |
---|
265 | } |
---|
266 | } else { |
---|
267 | return type_error(initialContents, Symbol.SEQUENCE); |
---|
268 | } |
---|
269 | } else { |
---|
270 | if (defaultInitialElement != null) { |
---|
271 | v.fill(defaultInitialElement); |
---|
272 | } |
---|
273 | } |
---|
274 | if (fillPointer != NIL) { |
---|
275 | v.setFillPointer(fillPointer); |
---|
276 | } |
---|
277 | return v; |
---|
278 | } else { // rank > 1 |
---|
279 | AbstractArray array; |
---|
280 | if (adjustable == NIL) { |
---|
281 | if (upgradedType.equal(UNSIGNED_BYTE_8)) { |
---|
282 | if (initialContents != NIL) { |
---|
283 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
284 | array = new SimpleArray_ByteBuffer(dimv, initialContents, directAllocation); |
---|
285 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
286 | array = new SimpleArray_UnsignedByte8(dimv, initialContents); |
---|
287 | } |
---|
288 | } else { |
---|
289 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
290 | array = new SimpleArray_ByteBuffer(dimv, directAllocation); |
---|
291 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
292 | array = new SimpleArray_UnsignedByte8(dimv); |
---|
293 | } |
---|
294 | if (initialElementProvided != NIL) { |
---|
295 | array.fill(initialElement); |
---|
296 | } else { |
---|
297 | array.fill(Fixnum.ZERO); |
---|
298 | } |
---|
299 | } |
---|
300 | } else if (upgradedType.equal(UNSIGNED_BYTE_16)) { |
---|
301 | if (initialContents != NIL) { |
---|
302 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
303 | array = new SimpleArray_CharBuffer(dimv, initialContents, directAllocation); |
---|
304 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
305 | array = new SimpleArray_UnsignedByte16(dimv, initialContents); |
---|
306 | } |
---|
307 | } else { |
---|
308 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
309 | array = new SimpleArray_CharBuffer(dimv, directAllocation); |
---|
310 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
311 | array = new SimpleArray_UnsignedByte16(dimv); |
---|
312 | } |
---|
313 | if (initialElementProvided != NIL) { |
---|
314 | array.fill(initialElement); |
---|
315 | } else { |
---|
316 | array.fill(Fixnum.ZERO); |
---|
317 | } |
---|
318 | } |
---|
319 | } else if (upgradedType.equal(UNSIGNED_BYTE_32)) { |
---|
320 | if (initialContents != NIL) { |
---|
321 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
322 | array = new SimpleArray_IntBuffer(dimv, initialContents, directAllocation); |
---|
323 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
324 | array = new SimpleArray_UnsignedByte32(dimv, initialContents); |
---|
325 | } |
---|
326 | } else { |
---|
327 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
328 | array = new SimpleArray_IntBuffer(dimv, directAllocation); |
---|
329 | } else { |
---|
330 | array = new SimpleArray_UnsignedByte32(dimv); |
---|
331 | } |
---|
332 | if (initialElementProvided != NIL) { |
---|
333 | array.fill(initialElement); |
---|
334 | } else { |
---|
335 | array.fill(Fixnum.ZERO); |
---|
336 | } |
---|
337 | } |
---|
338 | } else { |
---|
339 | if (initialContents != NIL) { |
---|
340 | array = new SimpleArray_T(dimv, upgradedType, initialContents); |
---|
341 | } else { |
---|
342 | array = new SimpleArray_T(dimv, upgradedType); |
---|
343 | if (initialElementProvided != NIL) { |
---|
344 | array.fill(initialElement); |
---|
345 | } else { |
---|
346 | array.fill(NIL); |
---|
347 | } |
---|
348 | } |
---|
349 | } |
---|
350 | } else { // Adjustable. |
---|
351 | if (upgradedType.equal(UNSIGNED_BYTE_8)) { |
---|
352 | if (initialContents != NIL) { |
---|
353 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
354 | array = new ComplexArray_ByteBuffer(dimv, initialContents); |
---|
355 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
356 | array = new ComplexArray_UnsignedByte8(dimv, initialContents); |
---|
357 | } |
---|
358 | } else { |
---|
359 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
360 | array = new ComplexArray_ByteBuffer(dimv); |
---|
361 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
362 | array = new ComplexArray_UnsignedByte8(dimv); |
---|
363 | } |
---|
364 | if (initialElementProvided != NIL) { |
---|
365 | array.fill(initialElement); |
---|
366 | } else { |
---|
367 | array.fill(Fixnum.ZERO); |
---|
368 | } |
---|
369 | } // FIXME add specialization on (unsigned-byte 16) |
---|
370 | } else if (upgradedType.equal(UNSIGNED_BYTE_32)) { |
---|
371 | if (initialContents != NIL) { |
---|
372 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
373 | array = new ComplexArray_IntBuffer(dimv, initialContents, directAllocation); |
---|
374 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
375 | array = new ComplexArray_UnsignedByte32(dimv, initialContents); |
---|
376 | } |
---|
377 | } else { |
---|
378 | if (Java.Buffers.active.equals(AllocationPolicy.NIO)) { |
---|
379 | array = new ComplexArray_IntBuffer(dimv, directAllocation); |
---|
380 | } else { //if (Java.Buffers.active.equals(AllocationPolicy.PRIMITIVE_ARRAY)) { |
---|
381 | array = new ComplexArray_UnsignedByte32(dimv); |
---|
382 | } |
---|
383 | if (initialElementProvided != NIL) { |
---|
384 | array.fill(initialElement); |
---|
385 | } else { |
---|
386 | array.fill(Fixnum.ZERO); |
---|
387 | } |
---|
388 | } |
---|
389 | } else { |
---|
390 | if (initialContents != NIL) { |
---|
391 | array = new ComplexArray(dimv, upgradedType, initialContents); |
---|
392 | } else { |
---|
393 | array = new ComplexArray(dimv, upgradedType); |
---|
394 | if (initialElementProvided != NIL) { |
---|
395 | array.fill(initialElement); |
---|
396 | } else { |
---|
397 | array.fill(NIL); |
---|
398 | } |
---|
399 | } |
---|
400 | } |
---|
401 | } |
---|
402 | return array; |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | private static final Primitive _MAKE_ARRAY = new make_array(); |
---|
407 | } |
---|