source: branches/0.17.x/abcl/src/org/armedbear/lisp/AbstractVector.java

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.5 KB
Line 
1/*
2 * AbstractVector.java
3 *
4 * Copyright (C) 2003-2006 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 * As a special exception, the copyright holders of this library give you
21 * permission to link this library with independent modules to produce an
22 * executable, regardless of the license terms of these independent
23 * modules, and to copy and distribute the resulting executable under
24 * terms of your choice, provided that you also meet, for each linked
25 * independent module, the terms and conditions of the license of that
26 * module.  An independent module is a module which is not derived from
27 * or based on this library.  If you modify this library, you may extend
28 * this exception to your version of the library, but you are not
29 * obligated to do so.  If you do not wish to do so, delete this
30 * exception statement from your version.
31 */
32
33package org.armedbear.lisp;
34
35public abstract class AbstractVector extends AbstractArray
36{
37  @Override
38  public LispObject typep(LispObject type)
39  {
40    if (type == Symbol.VECTOR)
41      return T;
42    if (type == BuiltInClass.VECTOR)
43      return T;
44    if (type == Symbol.SEQUENCE)
45      return T;
46    if (type == BuiltInClass.SEQUENCE)
47      return T;
48    return super.typep(type);
49  }
50
51  @Override
52  public final LispObject VECTORP()
53  {
54    return T;
55  }
56
57  @Override
58  public final boolean vectorp()
59  {
60    return true;
61  }
62
63  @Override
64  public boolean equalp(LispObject obj)
65  {
66    if (obj instanceof AbstractVector)
67      {
68        if (length() != obj.length())
69          return false;
70        AbstractVector v = (AbstractVector) obj;
71        for (int i = length(); i-- > 0;)
72          if (!AREF(i).equalp(v.AREF(i)))
73            return false;
74        return true;
75      }
76    return false;
77  }
78
79  @Override
80  public final int getRank()
81  {
82    return 1;
83  }
84
85  @Override
86  public final LispObject getDimensions()
87  {
88    return new Cons(Fixnum.getInstance(capacity()));
89  }
90
91  @Override
92  public final int getDimension(int n)
93  {
94    if (n != 0)
95      {
96        error(new TypeError("bad dimension for vector"));
97        // Not reached.
98        return 0;
99      }
100    return capacity();
101  }
102
103  @Override
104  public final int getTotalSize()
105  {
106    return capacity();
107  }
108
109  public abstract int capacity();
110
111  public abstract LispObject subseq(int start, int end);
112
113  public LispObject deleteEq(LispObject item)
114  {
115    final int limit = length();
116    int i = 0;
117    int j = 0;
118    while (i < limit)
119      {
120        LispObject obj = AREF(i++);
121        if (obj != item)
122          aset(j++, obj);
123      }
124    final int newLength = j;
125    if (newLength < capacity())
126      shrink(newLength);
127    return this;
128  }
129
130  public LispObject deleteEql(LispObject item)
131  {
132    final int limit = length();
133    int i = 0;
134    int j = 0;
135    while (i < limit)
136      {
137        LispObject obj = AREF(i++);
138        if (!obj.eql(item))
139          aset(j++, obj);
140      }
141    final int newLength = j;
142    if (newLength < capacity())
143      shrink(newLength);
144    return this;
145  }
146
147  public abstract void shrink(int n);
148
149  public int checkIndex(int index)
150  {
151    if (index < 0 || index >= capacity())
152      badIndex(index, capacity());
153    return index;
154  }
155
156  protected void badIndex(int index, int limit)
157  {
158    FastStringBuffer sb = new FastStringBuffer("Invalid array index ");
159    sb.append(index);
160    sb.append(" for ");
161    sb.append(writeToString());
162    if (limit > 0)
163      {
164        sb.append(" (should be >= 0 and < ");
165        sb.append(limit);
166        sb.append(").");
167      }
168    error(new TypeError(sb.toString(),
169                         Fixnum.getInstance(index),
170                         list(Symbol.INTEGER,
171                               Fixnum.ZERO,
172                               Fixnum.getInstance(limit - 1))));
173
174  }
175
176  public void setFillPointer(int n)
177  {
178    noFillPointer();
179  }
180
181  public void setFillPointer(LispObject obj)
182  {
183    noFillPointer();
184  }
185
186  public boolean isSimpleVector()
187  {
188    return false;
189  }
190
191  @Override
192  public abstract LispObject reverse();
193
194  @Override
195  public LispObject nreverse()
196  {
197    int i = 0;
198    int j = length() - 1;
199    while (i < j)
200      {
201        LispObject temp = AREF(i);
202        aset(i, AREF(j));
203        aset(j, temp);
204        ++i;
205        --j;
206      }
207    return this;
208  }
209
210  @Override
211  public String writeToString()
212  {
213    final LispThread thread = LispThread.currentThread();
214    if (Symbol.PRINT_READABLY.symbolValue(thread) != NIL)
215      {
216        FastStringBuffer sb = new FastStringBuffer("#(");
217        final int limit = length();
218        for (int i = 0; i < limit; i++)
219          {
220            if (i > 0)
221              sb.append(' ');
222            sb.append(AREF(i).writeToString());
223          }
224        sb.append(')');
225        return sb.toString();
226      }
227    else if (Symbol.PRINT_ARRAY.symbolValue(thread) != NIL)
228      {
229        int maxLevel = Integer.MAX_VALUE;
230        final LispObject printLevel =
231          Symbol.PRINT_LEVEL.symbolValue(thread);
232        if (printLevel instanceof Fixnum)
233          maxLevel = ((Fixnum)printLevel).value;
234        LispObject currentPrintLevel =
235          _CURRENT_PRINT_LEVEL_.symbolValue(thread);
236        int currentLevel = Fixnum.getValue(currentPrintLevel);
237        if (currentLevel < maxLevel)
238          {
239            StringBuffer sb = new StringBuffer("#(");
240            int maxLength = Integer.MAX_VALUE;
241            final LispObject printLength =
242              Symbol.PRINT_LENGTH.symbolValue(thread);
243            if (printLength instanceof Fixnum)
244              maxLength = ((Fixnum)printLength).value;
245            final int length = length();
246            final int limit = Math.min(length, maxLength);
247            SpecialBinding lastSpecialBinding = thread.lastSpecialBinding;
248            thread.bindSpecial(_CURRENT_PRINT_LEVEL_, currentPrintLevel.incr());
249            try
250              {
251                for (int i = 0; i < limit; i++)
252                  {
253                    if (i > 0)
254                      sb.append(' ');
255                    sb.append(AREF(i).writeToString());
256                  }
257              }
258            finally
259              {
260                thread.lastSpecialBinding = lastSpecialBinding;
261              }
262            if (limit < length)
263              sb.append(limit > 0 ? " ..." : "...");
264            sb.append(')');
265            return sb.toString();
266          }
267        else
268          return "#";
269      }
270    else
271      {
272        StringBuffer sb = new StringBuffer();
273        sb.append(isSimpleVector() ? "SIMPLE-VECTOR " : "VECTOR ");
274        sb.append(capacity());
275        return unreadableString(sb.toString());
276      }
277  }
278
279  // For EQUALP hash tables.
280  @Override
281  public int psxhash()
282  {
283    try
284      {
285        final int length = length();
286        final int limit = length < 4 ? length : 4;
287        long result = 48920713; // Chosen at random.
288        for (int i = 0; i < limit; i++)
289          result = mix(result, AREF(i).psxhash());
290        return (int) (result & 0x7fffffff);
291      }
292    catch (Throwable t)
293      {
294        // Shouldn't happen.
295        Debug.trace(t);
296        return 0;
297      }
298  }
299
300  public abstract AbstractArray adjustArray(int size,
301                                              LispObject initialElement,
302                                              LispObject initialContents)
303   ;
304  public abstract AbstractArray adjustArray(int size,
305                                              AbstractArray displacedTo,
306                                              int displacement)
307   ;
308
309
310  public AbstractArray adjustArray(int[] dims,
311                                              LispObject initialElement,
312                                              LispObject initialContents)
313    {
314      return adjustArray(dims[0], initialElement, initialContents);
315  }
316
317  public AbstractArray adjustArray(int[] dims,
318                                              AbstractArray displacedTo,
319                                              int displacement)
320    {
321      return adjustArray(dims[0], displacedTo, displacement);
322  }
323}
Note: See TracBrowser for help on using the repository browser.