KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > AbstractVector


1 /*
2  * AbstractVector.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */

20
21 package org.armedbear.lisp;
22
23 public abstract class AbstractVector extends AbstractArray
24 {
25     public LispObject typep(LispObject type) throws ConditionThrowable
26     {
27         if (type == Symbol.VECTOR)
28             return T;
29         if (type == BuiltInClass.VECTOR)
30             return T;
31         if (type == Symbol.SEQUENCE)
32             return T;
33         if (type == BuiltInClass.SEQUENCE)
34             return T;
35         return super.typep(type);
36     }
37
38     public final LispObject VECTORP()
39     {
40         return T;
41     }
42
43     public final boolean vectorp()
44     {
45         return true;
46     }
47
48     public boolean equalp(LispObject obj) throws ConditionThrowable
49     {
50         if (obj instanceof AbstractVector) {
51             if (length() != obj.length())
52                 return false;
53             AbstractVector v = (AbstractVector) obj;
54             for (int i = length(); i-- > 0;)
55                 if (!getRowMajor(i).equalp(v.getRowMajor(i)))
56                     return false;
57             return true;
58         }
59         return false;
60     }
61
62     public int getRank()
63     {
64         return 1;
65     }
66
67     public final LispObject getDimensions()
68     {
69         return new Cons(new Fixnum(capacity()));
70     }
71
72     public final int getDimension(int n) throws ConditionThrowable
73     {
74         if (n != 0) {
75             signal(new TypeError("bad dimension for vector"));
76             // Not reached.
77
return 0;
78         }
79         return capacity();
80     }
81
82     public final int getTotalSize()
83     {
84         return capacity();
85     }
86
87     public abstract int capacity();
88
89     public abstract LispObject subseq(int start, int end) throws ConditionThrowable;
90
91     public abstract void shrink(int n) throws ConditionThrowable;
92
93     public int checkIndex(int index) throws ConditionThrowable
94     {
95         if (index < 0 || index >= capacity())
96             badIndex(index, capacity());
97         return index;
98     }
99
100     public int checkIndex(LispObject index) throws ConditionThrowable
101     {
102         int i = Fixnum.getValue(index);
103         if (i < 0 || i >= capacity())
104             badIndex(i, capacity());
105         return i;
106     }
107
108     protected void badIndex(int index, int limit) throws ConditionThrowable
109     {
110         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Invalid array index ");
111         sb.append(index);
112         sb.append(" for ");
113         sb.append(writeToString());
114         if (limit > 0) {
115             sb.append(" (should be >= 0 and < ");
116             sb.append(limit);
117             sb.append(").");
118         }
119         signal(new TypeError(sb.toString()));
120     }
121
122     public void setFillPointer(int n) throws ConditionThrowable
123     {
124         noFillPointer();
125     }
126
127     public void setFillPointer(LispObject obj) throws ConditionThrowable
128     {
129         noFillPointer();
130     }
131
132     public boolean isSimpleVector()
133     {
134         return false;
135     }
136
137     public abstract LispObject reverse() throws ConditionThrowable;
138
139     public LispObject nreverse() throws ConditionThrowable
140     {
141         int i = 0;
142         int j = length() - 1;
143         while (i < j) {
144             LispObject temp = getRowMajor(i);
145             setRowMajor(i, getRowMajor(j));
146             setRowMajor(j, temp);
147             ++i;
148             --j;
149         }
150         return this;
151     }
152
153     public String JavaDoc writeToString() throws ConditionThrowable
154     {
155         if (_PRINT_READABLY_.symbolValue() != NIL) {
156             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("#(");
157             final int limit = length();
158             for (int i = 0; i < limit; i++) {
159                 if (i > 0)
160                     sb.append(' ');
161                 sb.append(getRowMajor(i).writeToString());
162             }
163             sb.append(')');
164             return sb.toString();
165         } else if (_PRINT_ARRAY_.symbolValue() != NIL) {
166             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("#(");
167             final LispObject printLength = _PRINT_LENGTH_.symbolValue();
168             final int limit;
169             if (printLength instanceof Fixnum)
170                 limit = Math.min(length(), ((Fixnum)printLength).value);
171             else
172                 limit = length();
173             for (int i = 0; i < limit; i++) {
174                 if (i > 0)
175                     sb.append(' ');
176                 sb.append(getRowMajor(i).writeToString());
177             }
178             if (limit < length())
179                 sb.append(limit > 0 ? " ..." : "...");
180             sb.append(')');
181             return sb.toString();
182         } else {
183             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
184             sb.append(isSimpleVector() ? "SIMPLE-VECTOR " : "VECTOR ");
185             sb.append(capacity());
186             return unreadableString(sb.toString());
187         }
188     }
189
190     public abstract AbstractVector adjustVector(int size,
191                                                 LispObject initialElement,
192                                                 LispObject initialContents)
193         throws ConditionThrowable;
194
195     public abstract AbstractVector adjustVector(int size,
196                                                 AbstractArray displacedTo,
197                                                 int displacement)
198         throws ConditionThrowable;
199
200     public LispObject vectorPushExtend(LispObject element)
201         throws ConditionThrowable
202     {
203         return noFillPointer();
204     }
205
206     public LispObject vectorPushExtend(LispObject element, LispObject extension)
207         throws ConditionThrowable
208     {
209         return noFillPointer();
210     }
211 }
212
Popular Tags