[5969] | 1 | /* |
---|
| 2 | * ZeroRankArray.java |
---|
| 3 | * |
---|
[9614] | 4 | * Copyright (C) 2004-2005 Peter Graves |
---|
[11297] | 5 | * $Id: ZeroRankArray.java 12288 2009-11-29 22:00:12Z vvoutilainen $ |
---|
[5969] | 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. |
---|
[11391] | 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. |
---|
[5969] | 32 | */ |
---|
| 33 | |
---|
| 34 | package org.armedbear.lisp; |
---|
| 35 | |
---|
[12288] | 36 | import static org.armedbear.lisp.Lisp.*; |
---|
| 37 | |
---|
[5969] | 38 | public final class ZeroRankArray extends AbstractArray |
---|
| 39 | { |
---|
| 40 | private final LispObject elementType; |
---|
| 41 | private final boolean adjustable; |
---|
| 42 | |
---|
| 43 | private LispObject data; |
---|
| 44 | |
---|
| 45 | public ZeroRankArray(LispObject elementType, LispObject data, |
---|
| 46 | boolean adjustable) |
---|
| 47 | { |
---|
| 48 | this.elementType = elementType; |
---|
| 49 | this.data = data; |
---|
| 50 | this.adjustable = adjustable; |
---|
| 51 | } |
---|
| 52 | |
---|
[11488] | 53 | @Override |
---|
[5969] | 54 | public LispObject typeOf() |
---|
| 55 | { |
---|
| 56 | if (adjustable) |
---|
[11711] | 57 | return list(Symbol.ARRAY, elementType, NIL); |
---|
[5969] | 58 | else |
---|
[11711] | 59 | return list(Symbol.SIMPLE_ARRAY, elementType, NIL); |
---|
[5969] | 60 | } |
---|
| 61 | |
---|
[11488] | 62 | @Override |
---|
[7968] | 63 | public LispObject classOf() |
---|
[5969] | 64 | { |
---|
| 65 | return BuiltInClass.ARRAY; |
---|
| 66 | } |
---|
| 67 | |
---|
[11488] | 68 | @Override |
---|
[12254] | 69 | public LispObject typep(LispObject type) |
---|
[5969] | 70 | { |
---|
| 71 | if (type == Symbol.SIMPLE_ARRAY) |
---|
| 72 | return adjustable ? NIL : T; |
---|
| 73 | return super.typep(type); |
---|
| 74 | } |
---|
| 75 | |
---|
[11488] | 76 | @Override |
---|
[5969] | 77 | public int getRank() |
---|
| 78 | { |
---|
| 79 | return 0; |
---|
| 80 | } |
---|
| 81 | |
---|
[11488] | 82 | @Override |
---|
[5969] | 83 | public LispObject getDimensions() |
---|
| 84 | { |
---|
| 85 | return NIL; |
---|
| 86 | } |
---|
| 87 | |
---|
[11488] | 88 | @Override |
---|
[12254] | 89 | public int getDimension(int n) |
---|
[5969] | 90 | { |
---|
[11158] | 91 | error(new TypeError("Bad array dimension (" + n + ") for array of rank 0.")); |
---|
[5969] | 92 | // Not reached. |
---|
| 93 | return -1; |
---|
| 94 | } |
---|
| 95 | |
---|
[11488] | 96 | @Override |
---|
[5969] | 97 | public LispObject getElementType() |
---|
| 98 | { |
---|
| 99 | return elementType; |
---|
| 100 | } |
---|
| 101 | |
---|
[11488] | 102 | @Override |
---|
[5969] | 103 | public int getTotalSize() |
---|
| 104 | { |
---|
| 105 | return 1; |
---|
| 106 | } |
---|
| 107 | |
---|
[11488] | 108 | @Override |
---|
[12254] | 109 | public LispObject AREF(int index) |
---|
[5969] | 110 | { |
---|
| 111 | if (index == 0) |
---|
| 112 | return data; |
---|
| 113 | else |
---|
[11158] | 114 | return error(new TypeError("Bad row major index " + index + ".")); |
---|
[5969] | 115 | } |
---|
| 116 | |
---|
[11488] | 117 | @Override |
---|
[12254] | 118 | public void aset(int index, LispObject obj) |
---|
[5969] | 119 | { |
---|
[9614] | 120 | if (obj.typep(elementType) == NIL) |
---|
[11158] | 121 | error(new TypeError(obj, elementType)); |
---|
[5969] | 122 | if (index == 0) |
---|
[9614] | 123 | data = obj; |
---|
[5969] | 124 | else |
---|
[11158] | 125 | error(new TypeError("Bad row major index " + index + ".")); |
---|
[5969] | 126 | } |
---|
[5982] | 127 | |
---|
[11488] | 128 | @Override |
---|
[12254] | 129 | public void fill(LispObject obj) |
---|
[5982] | 130 | { |
---|
| 131 | if (obj.typep(elementType) == NIL) |
---|
[11158] | 132 | error(new TypeError(obj, elementType)); |
---|
[5982] | 133 | data = obj; |
---|
| 134 | } |
---|
| 135 | |
---|
[11488] | 136 | @Override |
---|
[12254] | 137 | public String writeToString() |
---|
[5982] | 138 | { |
---|
[7874] | 139 | final LispThread thread = LispThread.currentThread(); |
---|
[10202] | 140 | boolean printReadably = (Symbol.PRINT_READABLY.symbolValue(thread) != NIL); |
---|
[8813] | 141 | if (printReadably) { |
---|
| 142 | if (elementType != T) { |
---|
[11711] | 143 | error(new PrintNotReadable(list(Keyword.OBJECT, this))); |
---|
[8813] | 144 | // Not reached. |
---|
| 145 | return null; |
---|
| 146 | } |
---|
| 147 | } |
---|
[10210] | 148 | if (printReadably || Symbol.PRINT_ARRAY.symbolValue(thread) != NIL) { |
---|
[6765] | 149 | StringBuffer sb = new StringBuffer("#0A"); |
---|
[10209] | 150 | if (data == this && Symbol.PRINT_CIRCLE.symbolValue(thread) != NIL) { |
---|
[7874] | 151 | StringOutputStream stream = new StringOutputStream(); |
---|
[8100] | 152 | thread.execute(Symbol.OUTPUT_OBJECT.getSymbolFunction(), |
---|
| 153 | data, stream); |
---|
[7874] | 154 | sb.append(stream.getString().getStringValue()); |
---|
| 155 | } else |
---|
| 156 | sb.append(data.writeToString()); |
---|
[6765] | 157 | return sb.toString(); |
---|
| 158 | } |
---|
[8813] | 159 | StringBuffer sb = new StringBuffer(); |
---|
| 160 | if (!adjustable) |
---|
| 161 | sb.append("SIMPLE-"); |
---|
| 162 | sb.append("ARRAY "); |
---|
| 163 | sb.append(elementType.writeToString()); |
---|
| 164 | sb.append(" NIL"); |
---|
| 165 | return unreadableString(sb.toString()); |
---|
[5982] | 166 | } |
---|
[11557] | 167 | |
---|
| 168 | @Override |
---|
| 169 | public AbstractArray adjustArray(int[] dims, |
---|
| 170 | LispObject initialElement, |
---|
| 171 | LispObject initialContents) |
---|
[12254] | 172 | { |
---|
[11557] | 173 | if (isAdjustable()) { |
---|
[11562] | 174 | // initial element doesn't matter: |
---|
| 175 | // we're not creating new elements |
---|
| 176 | if (initialContents != null) |
---|
[11557] | 177 | data = initialContents; |
---|
| 178 | return this; |
---|
| 179 | } else { |
---|
| 180 | return new ZeroRankArray(elementType, |
---|
[11562] | 181 | initialContents != null ? initialContents : |
---|
| 182 | initialElement != null ? initialElement : data, false); |
---|
[11557] | 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | @Override |
---|
| 187 | public AbstractArray adjustArray(int[] dims, |
---|
| 188 | AbstractArray displacedTo, |
---|
| 189 | int displacement) |
---|
[12254] | 190 | { |
---|
[11557] | 191 | error(new TypeError("Displacement not supported for array of rank 0.")); |
---|
| 192 | return null; |
---|
| 193 | } |
---|
[5969] | 194 | } |
---|