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