source: branches/0.17.x/abcl/src/org/armedbear/lisp/make_array.java

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.9 KB
Line 
1/*
2 * make_array.java
3 *
4 * Copyright (C) 2003-2005 Peter Graves
5 * $Id: make_array.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
34package org.armedbear.lisp;
35
36// ### %make-array dimensions element-type initial-element initial-element-p
37// initial-contents adjustable fill-pointer displaced-to displaced-index-offset
38// => new-array
39public final class make_array extends Primitive
40{
41  public make_array()
42  {
43    super("%make-array", PACKAGE_SYS, false);
44  }
45
46  @Override
47  public LispObject execute(LispObject[] args)
48  {
49    if (args.length != 9)
50      return error(new WrongNumberOfArgumentsException(this));
51    LispObject dimensions = args[0];
52    LispObject elementType = args[1];
53    LispObject initialElement = args[2];
54    LispObject initialElementProvided = args[3];
55    LispObject initialContents = args[4];
56    LispObject adjustable = args[5];
57    LispObject fillPointer = args[6];
58    LispObject displacedTo = args[7];
59    LispObject displacedIndexOffset = args[8];
60    if (initialElementProvided != NIL && initialContents != NIL)
61      {
62        return error(new LispError("MAKE-ARRAY: cannot specify both " +
63                                    "initial element and initial contents."));
64      }
65    final int rank = dimensions.listp() ? dimensions.length() : 1;
66    int[] dimv = new int[rank];
67    if (dimensions.listp())
68      {
69        for (int i = 0; i < rank; i++)
70          {
71            LispObject dim = dimensions.car();
72            dimv[i] = Fixnum.getValue(dim);
73            dimensions = dimensions.cdr();
74          }
75      }
76    else
77      dimv[0] = Fixnum.getValue(dimensions);
78    if (displacedTo != NIL)
79      {
80        // FIXME Make sure element type (if specified) is compatible with
81        // displaced-to array.
82        final AbstractArray array = checkArray(displacedTo);
83        if (initialElementProvided != NIL)
84          return error(new LispError("Initial element must not be specified for a displaced array."));
85        if (initialContents != NIL)
86          return error(new LispError("Initial contents must not be specified for a displaced array."));
87        final int displacement;
88        if (displacedIndexOffset != NIL)
89          displacement = Fixnum.getValue(displacedIndexOffset);
90        else
91          displacement = 0;
92        if (rank == 1)
93          {
94            AbstractVector v;
95            LispObject arrayElementType = array.getElementType();
96            if (arrayElementType == Symbol.CHARACTER)
97              v = new ComplexString(dimv[0], array, displacement);
98            else if (arrayElementType == Symbol.BIT)
99              v = new ComplexBitVector(dimv[0], array, displacement);
100            else if (arrayElementType.equal(UNSIGNED_BYTE_8))
101              v = new ComplexVector_UnsignedByte8(dimv[0], array, displacement);
102            else if (arrayElementType.equal(UNSIGNED_BYTE_32))
103              v = new ComplexVector_UnsignedByte32(dimv[0], array, displacement);
104            else
105              v = new ComplexVector(dimv[0], array, displacement);
106            if (fillPointer != NIL)
107              v.setFillPointer(fillPointer);
108            return v;
109          }
110        return new ComplexArray(dimv, array, displacement);
111      }
112    LispObject upgradedType = getUpgradedArrayElementType(elementType);
113    if (rank == 0)
114      {
115        LispObject data;
116        if (initialElementProvided != NIL)
117          data = initialElement;
118        else
119          data = initialContents;
120        return new ZeroRankArray(upgradedType, data, adjustable != NIL);
121      }
122    if (rank == 1)
123      {
124        final int size = dimv[0];
125        if (size < 0 || size >= ARRAY_DIMENSION_MAX)
126          {
127            FastStringBuffer sb = new FastStringBuffer();
128            sb.append("The size specified for this array (");
129            sb.append(size);
130            sb.append(')');
131            if (size >= ARRAY_DIMENSION_MAX)
132              {
133                sb.append(" is >= ARRAY-DIMENSION-LIMIT (");
134                sb.append(ARRAY_DIMENSION_MAX);
135                sb.append(").");
136              }
137            else
138              sb.append(" is negative.");
139            return error(new LispError(sb.toString()));
140          }
141        final AbstractVector v;
142        if (upgradedType == Symbol.CHARACTER)
143          {
144            if (fillPointer != NIL || adjustable != NIL)
145              v = new ComplexString(size);
146            else
147              v = new SimpleString(size);
148          }
149        else if (upgradedType == Symbol.BIT)
150          {
151            if (fillPointer != NIL || adjustable != NIL)
152              v = new ComplexBitVector(size);
153            else
154              v = new SimpleBitVector(size);
155          }
156        else if (upgradedType.equal(UNSIGNED_BYTE_8))
157          {
158            if (fillPointer != NIL || adjustable != NIL)
159              v = new ComplexVector_UnsignedByte8(size);
160            else
161              v = new BasicVector_UnsignedByte8(size);
162          }
163        else if (upgradedType.equal(UNSIGNED_BYTE_16) &&
164                 fillPointer == NIL && adjustable == NIL)
165          {
166            v = new BasicVector_UnsignedByte16(size);
167          }
168        else if (upgradedType.equal(UNSIGNED_BYTE_32))
169          {
170            if (fillPointer != NIL || adjustable != NIL)
171              v = new ComplexVector_UnsignedByte32(size);
172            else
173              v = new BasicVector_UnsignedByte32(size);
174          }
175        else if (upgradedType == NIL)
176          v = new NilVector(size);
177        else
178          {
179            if (fillPointer != NIL || adjustable != NIL)
180              v = new ComplexVector(size);
181            else
182              v = new SimpleVector(size);
183          }
184        if (initialElementProvided != NIL)
185          {
186            // Initial element was specified.
187            v.fill(initialElement);
188          }
189        else if (initialContents != NIL)
190          {
191            if (initialContents.listp())
192              {
193                LispObject list = initialContents;
194                for (int i = 0; i < size; i++)
195                  {
196                    v.aset(i, list.car());
197                    list = list.cdr();
198                  }
199              }
200            else if (initialContents.vectorp())
201              {
202                for (int i = 0; i < size; i++)
203                  v.aset(i, initialContents.elt(i));
204              }
205            else
206              return type_error(initialContents, Symbol.SEQUENCE);
207          }
208        if (fillPointer != NIL)
209          v.setFillPointer(fillPointer);
210        return v;
211      }
212    // rank > 1
213    AbstractArray array;
214    if (adjustable == NIL)
215      {
216        if (upgradedType.equal(UNSIGNED_BYTE_8))
217          {
218            if (initialContents != NIL)
219              {
220                array = new SimpleArray_UnsignedByte8(dimv, initialContents);
221              }
222            else
223              {
224                array = new SimpleArray_UnsignedByte8(dimv);
225                if (initialElementProvided != NIL)
226                  array.fill(initialElement);
227              }
228          }
229        else if (upgradedType.equal(UNSIGNED_BYTE_16))
230          {
231            if (initialContents != NIL)
232              {
233                array = new SimpleArray_UnsignedByte16(dimv, initialContents);
234              }
235            else
236              {
237                array = new SimpleArray_UnsignedByte16(dimv);
238                if (initialElementProvided != NIL)
239                  array.fill(initialElement);
240              }
241          }
242        else if (upgradedType.equal(UNSIGNED_BYTE_32))
243          {
244            if (initialContents != NIL)
245              {
246                array = new SimpleArray_UnsignedByte32(dimv, initialContents);
247              }
248            else
249              {
250                array = new SimpleArray_UnsignedByte32(dimv);
251                if (initialElementProvided != NIL)
252                  array.fill(initialElement);
253              }
254          }
255        else
256          {
257            if (initialContents != NIL)
258              {
259                array = new SimpleArray_T(dimv, upgradedType, initialContents);
260              }
261            else
262              {
263                array = new SimpleArray_T(dimv, upgradedType);
264                if (initialElementProvided != NIL)
265                  array.fill(initialElement);
266              }
267          }
268      }
269    else
270      {
271        // Adjustable.
272        if (upgradedType.equal(UNSIGNED_BYTE_8))
273          {
274            if (initialContents != NIL)
275              {
276                array = new ComplexArray_UnsignedByte8(dimv, initialContents);
277              }
278            else
279              {
280                array = new ComplexArray_UnsignedByte8(dimv);
281                if (initialElementProvided != NIL)
282                  array.fill(initialElement);
283              }
284          }
285        else if (upgradedType.equal(UNSIGNED_BYTE_32))
286          {
287            if (initialContents != NIL)
288              {
289                array = new ComplexArray_UnsignedByte32(dimv, initialContents);
290              }
291            else
292              {
293                array = new ComplexArray_UnsignedByte32(dimv);
294                if (initialElementProvided != NIL)
295                  array.fill(initialElement);
296              }
297          }
298        else
299          {
300            if (initialContents != NIL)
301              {
302                array = new ComplexArray(dimv, upgradedType, initialContents);
303              }
304            else
305              {
306                array = new ComplexArray(dimv, upgradedType);
307                if (initialElementProvided != NIL)
308                  array.fill(initialElement);
309              }
310          }
311      }
312    return array;
313  }
314
315  private static final Primitive _MAKE_ARRAY = new make_array();
316}
Note: See TracBrowser for help on using the repository browser.