| 1 | /* |
|---|
| 2 | * make_array.java |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2003-2005 Peter Graves |
|---|
| 5 | * $Id: make_array.java 12431 2010-02-08 08:05:15Z mevenson $ |
|---|
| 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)); |
|---|
| 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 | if (upgradedType == Symbol.CHARACTER) |
|---|
| 145 | { |
|---|
| 146 | if (fillPointer != NIL || adjustable != NIL) |
|---|
| 147 | v = new ComplexString(size); |
|---|
| 148 | else |
|---|
| 149 | v = new SimpleString(size); |
|---|
| 150 | } |
|---|
| 151 | else if (upgradedType == Symbol.BIT) |
|---|
| 152 | { |
|---|
| 153 | if (fillPointer != NIL || adjustable != NIL) |
|---|
| 154 | v = new ComplexBitVector(size); |
|---|
| 155 | else |
|---|
| 156 | v = new SimpleBitVector(size); |
|---|
| 157 | } |
|---|
| 158 | else if (upgradedType.equal(UNSIGNED_BYTE_8)) |
|---|
| 159 | { |
|---|
| 160 | if (fillPointer != NIL || adjustable != NIL) |
|---|
| 161 | v = new ComplexVector_UnsignedByte8(size); |
|---|
| 162 | else |
|---|
| 163 | v = new BasicVector_UnsignedByte8(size); |
|---|
| 164 | } |
|---|
| 165 | else if (upgradedType.equal(UNSIGNED_BYTE_16) && |
|---|
| 166 | fillPointer == NIL && adjustable == NIL) |
|---|
| 167 | { |
|---|
| 168 | v = new BasicVector_UnsignedByte16(size); |
|---|
| 169 | } |
|---|
| 170 | else if (upgradedType.equal(UNSIGNED_BYTE_32)) |
|---|
| 171 | { |
|---|
| 172 | if (fillPointer != NIL || adjustable != NIL) |
|---|
| 173 | v = new ComplexVector_UnsignedByte32(size); |
|---|
| 174 | else |
|---|
| 175 | v = new BasicVector_UnsignedByte32(size); |
|---|
| 176 | } |
|---|
| 177 | else if (upgradedType == NIL) |
|---|
| 178 | v = new NilVector(size); |
|---|
| 179 | else |
|---|
| 180 | { |
|---|
| 181 | if (fillPointer != NIL || adjustable != NIL) |
|---|
| 182 | v = new ComplexVector(size); |
|---|
| 183 | else |
|---|
| 184 | v = new SimpleVector(size); |
|---|
| 185 | } |
|---|
| 186 | if (initialElementProvided != NIL) |
|---|
| 187 | { |
|---|
| 188 | // Initial element was specified. |
|---|
| 189 | v.fill(initialElement); |
|---|
| 190 | } |
|---|
| 191 | else if (initialContents != NIL) |
|---|
| 192 | { |
|---|
| 193 | if (initialContents.listp()) |
|---|
| 194 | { |
|---|
| 195 | LispObject list = initialContents; |
|---|
| 196 | for (int i = 0; i < size; i++) |
|---|
| 197 | { |
|---|
| 198 | v.aset(i, list.car()); |
|---|
| 199 | list = list.cdr(); |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | else if (initialContents.vectorp()) |
|---|
| 203 | { |
|---|
| 204 | for (int i = 0; i < size; i++) |
|---|
| 205 | v.aset(i, initialContents.elt(i)); |
|---|
| 206 | } |
|---|
| 207 | else |
|---|
| 208 | return type_error(initialContents, Symbol.SEQUENCE); |
|---|
| 209 | } |
|---|
| 210 | if (fillPointer != NIL) |
|---|
| 211 | v.setFillPointer(fillPointer); |
|---|
| 212 | return v; |
|---|
| 213 | } |
|---|
| 214 | // rank > 1 |
|---|
| 215 | AbstractArray array; |
|---|
| 216 | if (adjustable == NIL) |
|---|
| 217 | { |
|---|
| 218 | if (upgradedType.equal(UNSIGNED_BYTE_8)) |
|---|
| 219 | { |
|---|
| 220 | if (initialContents != NIL) |
|---|
| 221 | { |
|---|
| 222 | array = new SimpleArray_UnsignedByte8(dimv, initialContents); |
|---|
| 223 | } |
|---|
| 224 | else |
|---|
| 225 | { |
|---|
| 226 | array = new SimpleArray_UnsignedByte8(dimv); |
|---|
| 227 | if (initialElementProvided != NIL) |
|---|
| 228 | array.fill(initialElement); |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | else if (upgradedType.equal(UNSIGNED_BYTE_16)) |
|---|
| 232 | { |
|---|
| 233 | if (initialContents != NIL) |
|---|
| 234 | { |
|---|
| 235 | array = new SimpleArray_UnsignedByte16(dimv, initialContents); |
|---|
| 236 | } |
|---|
| 237 | else |
|---|
| 238 | { |
|---|
| 239 | array = new SimpleArray_UnsignedByte16(dimv); |
|---|
| 240 | if (initialElementProvided != NIL) |
|---|
| 241 | array.fill(initialElement); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | else if (upgradedType.equal(UNSIGNED_BYTE_32)) |
|---|
| 245 | { |
|---|
| 246 | if (initialContents != NIL) |
|---|
| 247 | { |
|---|
| 248 | array = new SimpleArray_UnsignedByte32(dimv, initialContents); |
|---|
| 249 | } |
|---|
| 250 | else |
|---|
| 251 | { |
|---|
| 252 | array = new SimpleArray_UnsignedByte32(dimv); |
|---|
| 253 | if (initialElementProvided != NIL) |
|---|
| 254 | array.fill(initialElement); |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | else |
|---|
| 258 | { |
|---|
| 259 | if (initialContents != NIL) |
|---|
| 260 | { |
|---|
| 261 | array = new SimpleArray_T(dimv, upgradedType, initialContents); |
|---|
| 262 | } |
|---|
| 263 | else |
|---|
| 264 | { |
|---|
| 265 | array = new SimpleArray_T(dimv, upgradedType); |
|---|
| 266 | if (initialElementProvided != NIL) |
|---|
| 267 | array.fill(initialElement); |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | } |
|---|
| 271 | else |
|---|
| 272 | { |
|---|
| 273 | // Adjustable. |
|---|
| 274 | if (upgradedType.equal(UNSIGNED_BYTE_8)) |
|---|
| 275 | { |
|---|
| 276 | if (initialContents != NIL) |
|---|
| 277 | { |
|---|
| 278 | array = new ComplexArray_UnsignedByte8(dimv, initialContents); |
|---|
| 279 | } |
|---|
| 280 | else |
|---|
| 281 | { |
|---|
| 282 | array = new ComplexArray_UnsignedByte8(dimv); |
|---|
| 283 | if (initialElementProvided != NIL) |
|---|
| 284 | array.fill(initialElement); |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | else if (upgradedType.equal(UNSIGNED_BYTE_32)) |
|---|
| 288 | { |
|---|
| 289 | if (initialContents != NIL) |
|---|
| 290 | { |
|---|
| 291 | array = new ComplexArray_UnsignedByte32(dimv, initialContents); |
|---|
| 292 | } |
|---|
| 293 | else |
|---|
| 294 | { |
|---|
| 295 | array = new ComplexArray_UnsignedByte32(dimv); |
|---|
| 296 | if (initialElementProvided != NIL) |
|---|
| 297 | array.fill(initialElement); |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | else |
|---|
| 301 | { |
|---|
| 302 | if (initialContents != NIL) |
|---|
| 303 | { |
|---|
| 304 | array = new ComplexArray(dimv, upgradedType, initialContents); |
|---|
| 305 | } |
|---|
| 306 | else |
|---|
| 307 | { |
|---|
| 308 | array = new ComplexArray(dimv, upgradedType); |
|---|
| 309 | if (initialElementProvided != NIL) |
|---|
| 310 | array.fill(initialElement); |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | return array; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | private static final Primitive _MAKE_ARRAY = new make_array(); |
|---|
| 318 | } |
|---|