001: /*
002: * AbstractArray.java
003: *
004: * Copyright (C) 2003 Peter Graves
005: * $Id: AbstractArray.java,v 1.6 2003/11/15 11:03:33 beedlem Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: public abstract class AbstractArray extends LispObject {
025: public LispObject typep(LispObject type) throws ConditionThrowable {
026: if (type == Symbol.ARRAY)
027: return T;
028: if (type == BuiltInClass.ARRAY)
029: return T;
030: return super .typep(type);
031: }
032:
033: public boolean equalp(LispObject obj) throws ConditionThrowable {
034: if (obj instanceof AbstractArray) {
035: AbstractArray a = (AbstractArray) obj;
036: if (getRank() != a.getRank())
037: return false;
038: for (int i = getRank(); i-- > 0;) {
039: if (getDimension(i) != a.getDimension(i))
040: return false;
041: }
042: for (int i = getTotalSize(); i-- > 0;) {
043: if (!getRowMajor(i).equalp(a.getRowMajor(i)))
044: return false;
045: }
046: return true;
047: }
048: return false;
049: }
050:
051: public LispObject AREF(LispObject index) throws ConditionThrowable {
052: StringBuffer sb = new StringBuffer("AREF: ");
053: sb.append("wrong number of subscripts (1) for array of rank ");
054: sb.append(getRank());
055: throw new ConditionThrowable(new ProgramError(sb.toString()));
056: }
057:
058: public abstract int getRank();
059:
060: public abstract LispObject getDimensions();
061:
062: public abstract int getDimension(int n) throws ConditionThrowable;
063:
064: public abstract LispObject getElementType();
065:
066: public abstract int getTotalSize();
067:
068: public abstract LispObject getRowMajor(int index)
069: throws ConditionThrowable;
070:
071: public abstract void setRowMajor(int index, LispObject newValue)
072: throws ConditionThrowable;
073:
074: // Helper for toString().
075: protected void appendContents(int[] dimensions, int index,
076: StringBuffer sb) {
077: try {
078: if (dimensions.length == 0) {
079: sb.append(getRowMajor(index));
080: } else {
081: sb.append('(');
082: int[] dims = new int[dimensions.length - 1];
083: for (int i = 1; i < dimensions.length; i++)
084: dims[i - 1] = dimensions[i];
085: int count = 1;
086: for (int i = 0; i < dims.length; i++)
087: count *= dims[i];
088: int length = dimensions[0];
089: for (int i = 0; i < length; i++) {
090: if (i != 0)
091: sb.append(' ');
092: appendContents(dims, index, sb);
093: index += count;
094: }
095: sb.append(')');
096: }
097: } catch (ConditionThrowable t) {
098: Debug.trace(t);
099: }
100: }
101: }
|