001: /*
002: * AbstractVector.java
003: *
004: * Copyright (C) 2003-2004 Peter Graves
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: package org.armedbear.lisp;
022:
023: public abstract class AbstractVector extends AbstractArray {
024: public LispObject typep(LispObject type) throws ConditionThrowable {
025: if (type == Symbol.VECTOR)
026: return T;
027: if (type == BuiltInClass.VECTOR)
028: return T;
029: if (type == Symbol.SEQUENCE)
030: return T;
031: if (type == BuiltInClass.SEQUENCE)
032: return T;
033: return super .typep(type);
034: }
035:
036: public final LispObject VECTORP() {
037: return T;
038: }
039:
040: public final boolean vectorp() {
041: return true;
042: }
043:
044: public boolean equalp(LispObject obj) throws ConditionThrowable {
045: if (obj instanceof AbstractVector) {
046: if (length() != obj.length())
047: return false;
048: AbstractVector v = (AbstractVector) obj;
049: for (int i = length(); i-- > 0;)
050: if (!getRowMajor(i).equalp(v.getRowMajor(i)))
051: return false;
052: return true;
053: }
054: return false;
055: }
056:
057: public int getRank() {
058: return 1;
059: }
060:
061: public final LispObject getDimensions() {
062: return new Cons(new Fixnum(capacity()));
063: }
064:
065: public final int getDimension(int n) throws ConditionThrowable {
066: if (n != 0) {
067: signal(new TypeError("bad dimension for vector"));
068: // Not reached.
069: return 0;
070: }
071: return capacity();
072: }
073:
074: public final int getTotalSize() {
075: return capacity();
076: }
077:
078: public abstract int capacity();
079:
080: public abstract LispObject subseq(int start, int end)
081: throws ConditionThrowable;
082:
083: public abstract void shrink(int n) throws ConditionThrowable;
084:
085: public int checkIndex(int index) throws ConditionThrowable {
086: if (index < 0 || index >= capacity())
087: badIndex(index, capacity());
088: return index;
089: }
090:
091: public int checkIndex(LispObject index) throws ConditionThrowable {
092: int i = Fixnum.getValue(index);
093: if (i < 0 || i >= capacity())
094: badIndex(i, capacity());
095: return i;
096: }
097:
098: protected void badIndex(int index, int limit)
099: throws ConditionThrowable {
100: StringBuffer sb = new StringBuffer("Invalid array index ");
101: sb.append(index);
102: sb.append(" for ");
103: sb.append(writeToString());
104: if (limit > 0) {
105: sb.append(" (should be >= 0 and < ");
106: sb.append(limit);
107: sb.append(").");
108: }
109: signal(new TypeError(sb.toString()));
110: }
111:
112: public void setFillPointer(int n) throws ConditionThrowable {
113: noFillPointer();
114: }
115:
116: public void setFillPointer(LispObject obj)
117: throws ConditionThrowable {
118: noFillPointer();
119: }
120:
121: public boolean isSimpleVector() {
122: return false;
123: }
124:
125: public abstract LispObject reverse() throws ConditionThrowable;
126:
127: public LispObject nreverse() throws ConditionThrowable {
128: int i = 0;
129: int j = length() - 1;
130: while (i < j) {
131: LispObject temp = getRowMajor(i);
132: setRowMajor(i, getRowMajor(j));
133: setRowMajor(j, temp);
134: ++i;
135: --j;
136: }
137: return this ;
138: }
139:
140: public String writeToString() throws ConditionThrowable {
141: if (_PRINT_READABLY_.symbolValue() != NIL) {
142: StringBuffer sb = new StringBuffer("#(");
143: final int limit = length();
144: for (int i = 0; i < limit; i++) {
145: if (i > 0)
146: sb.append(' ');
147: sb.append(getRowMajor(i).writeToString());
148: }
149: sb.append(')');
150: return sb.toString();
151: } else if (_PRINT_ARRAY_.symbolValue() != NIL) {
152: StringBuffer sb = new StringBuffer("#(");
153: final LispObject printLength = _PRINT_LENGTH_.symbolValue();
154: final int limit;
155: if (printLength instanceof Fixnum)
156: limit = Math
157: .min(length(), ((Fixnum) printLength).value);
158: else
159: limit = length();
160: for (int i = 0; i < limit; i++) {
161: if (i > 0)
162: sb.append(' ');
163: sb.append(getRowMajor(i).writeToString());
164: }
165: if (limit < length())
166: sb.append(limit > 0 ? " ..." : "...");
167: sb.append(')');
168: return sb.toString();
169: } else {
170: StringBuffer sb = new StringBuffer();
171: sb.append(isSimpleVector() ? "SIMPLE-VECTOR " : "VECTOR ");
172: sb.append(capacity());
173: return unreadableString(sb.toString());
174: }
175: }
176:
177: public abstract AbstractVector adjustVector(int size,
178: LispObject initialElement, LispObject initialContents)
179: throws ConditionThrowable;
180:
181: public abstract AbstractVector adjustVector(int size,
182: AbstractArray displacedTo, int displacement)
183: throws ConditionThrowable;
184:
185: public LispObject vectorPushExtend(LispObject element)
186: throws ConditionThrowable {
187: return noFillPointer();
188: }
189:
190: public LispObject vectorPushExtend(LispObject element,
191: LispObject extension) throws ConditionThrowable {
192: return noFillPointer();
193: }
194: }
|