source: branches/0.15.x/abcl/src/org/armedbear/lisp/BasicVector_UnsignedByte32.java

Last change on this file was 11754, checked in by vvoutilainen, 16 years ago

Convert using ClassCastException? to checking instanceof.
Performance tests show this approach to be faster.
Patch by Douglas R. Miles. I modified the patch to
remove tabs, so indentation may be slightly off in places.
That's something that we need to handle separately, abcl
doesn't have a clear indentation policy.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1/*
2 * BasicVector_UnsignedByte32.java
3 *
4 * Copyright (C) 2002-2006 Peter Graves
5 * $Id: BasicVector_UnsignedByte32.java 11754 2009-04-12 10:53:39Z vvoutilainen $
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.
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.
32 */
33
34package org.armedbear.lisp;
35
36// A basic vector is a specialized vector that is not displaced to another
37// array, has no fill pointer, and is not expressly adjustable.
38public final class BasicVector_UnsignedByte32 extends AbstractVector
39{
40  private int capacity;
41
42  private long[] elements;
43
44  public BasicVector_UnsignedByte32(int capacity)
45  {
46    elements = new long[capacity];
47    this.capacity = capacity;
48  }
49
50  public BasicVector_UnsignedByte32(LispObject[] array)
51    throws ConditionThrowable
52  {
53    capacity = array.length;
54    elements = new long[capacity];
55    for (int i = array.length; i-- > 0;)
56      elements[i] = array[i].longValue();
57  }
58
59  @Override
60  public LispObject typeOf()
61  {
62    return list(Symbol.SIMPLE_ARRAY, UNSIGNED_BYTE_32,
63                 new Cons(Fixnum.getInstance(capacity)));
64  }
65
66  @Override
67  public LispObject classOf()
68  {
69    return BuiltInClass.VECTOR;
70  }
71
72  @Override
73  public LispObject typep(LispObject type) throws ConditionThrowable
74  {
75    if (type == Symbol.SIMPLE_ARRAY)
76      return T;
77    if (type == BuiltInClass.SIMPLE_ARRAY)
78      return T;
79    return super.typep(type);
80  }
81
82  @Override
83  public LispObject getElementType()
84  {
85    return UNSIGNED_BYTE_32;
86  }
87
88  @Override
89  public boolean isSimpleVector()
90  {
91    return false;
92  }
93
94  @Override
95  public boolean hasFillPointer()
96  {
97    return false;
98  }
99
100  @Override
101  public boolean isAdjustable()
102  {
103    return false;
104  }
105
106  @Override
107  public int capacity()
108  {
109    return capacity;
110  }
111
112  @Override
113  public int length()
114  {
115    return capacity;
116  }
117
118  @Override
119  public LispObject elt(int index) throws ConditionThrowable
120  {
121    try
122      {
123        return number(elements[index]);
124      }
125    catch (ArrayIndexOutOfBoundsException e)
126      {
127        badIndex(index, capacity);
128        return NIL; // Not reached.
129      }
130  }
131
132  @Override
133  public int aref(int index) throws ConditionThrowable
134  {
135    try
136      {
137        return (int) elements[index];
138      }
139    catch (ArrayIndexOutOfBoundsException e)
140      {
141        badIndex(index, elements.length);
142        return -1; // Not reached.
143      }
144  }
145
146  @Override
147  public long aref_long(int index) throws ConditionThrowable
148  {
149    try
150      {
151        return elements[index];
152      }
153    catch (ArrayIndexOutOfBoundsException e)
154      {
155        badIndex(index, elements.length);
156        return -1; // Not reached.
157      }
158  }
159
160  @Override
161  public LispObject AREF(int index) throws ConditionThrowable
162  {
163    try
164      {
165        return number(elements[index]);
166      }
167    catch (ArrayIndexOutOfBoundsException e)
168      {
169        badIndex(index, elements.length);
170        return NIL; // Not reached.
171      }
172  }
173
174  @Override
175  public LispObject AREF(LispObject index) throws ConditionThrowable
176  {
177        final int idx = Fixnum.getValue(index);
178    try
179      {
180        return number(elements[idx]);
181      }
182    catch (ArrayIndexOutOfBoundsException e)
183      {
184        badIndex(idx, elements.length);
185        return NIL; // Not reached.
186      }
187  }
188
189  @Override
190  public void aset(int index, LispObject newValue) throws ConditionThrowable
191  {
192    try
193      {
194        elements[index] = newValue.longValue();
195      }
196    catch (ArrayIndexOutOfBoundsException e)
197      {
198        badIndex(index, capacity);
199      }
200  }
201
202  @Override
203  public LispObject subseq(int start, int end) throws ConditionThrowable
204  {
205    BasicVector_UnsignedByte32 v = new BasicVector_UnsignedByte32(end - start);
206    int i = start, j = 0;
207    try
208      {
209        while (i < end)
210          v.elements[j++] = elements[i++];
211        return v;
212      }
213    catch (ArrayIndexOutOfBoundsException e)
214      {
215        // FIXME
216        return error(new TypeError("Array index out of bounds: " + i + "."));
217      }
218  }
219
220  @Override
221  public void fill(LispObject obj) throws ConditionThrowable
222  {
223    for (int i = capacity; i-- > 0;)
224      elements[i] = obj.longValue();
225  }
226
227  @Override
228  public void shrink(int n) throws ConditionThrowable
229  {
230    if (n < capacity)
231      {
232        long[] newArray = new long[n];
233        System.arraycopy(elements, 0, newArray, 0, n);
234        elements = newArray;
235        capacity = n;
236        return;
237      }
238    if (n == capacity)
239      return;
240    error(new LispError());
241  }
242
243  @Override
244  public LispObject reverse() throws ConditionThrowable
245  {
246    BasicVector_UnsignedByte32 result = new BasicVector_UnsignedByte32(capacity);
247    int i, j;
248    for (i = 0, j = capacity - 1; i < capacity; i++, j--)
249      result.elements[i] = elements[j];
250    return result;
251  }
252
253  @Override
254  public LispObject nreverse() throws ConditionThrowable
255  {
256    int i = 0;
257    int j = capacity - 1;
258    while (i < j)
259      {
260        long temp = elements[i];
261        elements[i] = elements[j];
262        elements[j] = temp;
263        ++i;
264        --j;
265      }
266    return this;
267  }
268
269  @Override
270  public AbstractVector adjustArray(int newCapacity,
271                                     LispObject initialElement,
272                                     LispObject initialContents)
273    throws ConditionThrowable
274  {
275    if (initialContents != null)
276      {
277        LispObject[] newElements = new LispObject[newCapacity];
278        if (initialContents.listp())
279          {
280            LispObject list = initialContents;
281            for (int i = 0; i < newCapacity; i++)
282              {
283                newElements[i] = list.car();
284                list = list.cdr();
285              }
286          }
287        else if (initialContents.vectorp())
288          {
289            for (int i = 0; i < newCapacity; i++)
290              newElements[i] = initialContents.elt(i);
291          }
292        else
293          type_error(initialContents, Symbol.SEQUENCE);
294        return new BasicVector_UnsignedByte32(newElements);
295      }
296    if (capacity != newCapacity)
297      {
298        LispObject[] newElements = new LispObject[newCapacity];
299        System.arraycopy(elements, 0, newElements, 0,
300                         Math.min(capacity, newCapacity));
301        if (initialElement != null)
302            for (int i = capacity; i < newCapacity; i++)
303                newElements[i] = initialElement;
304        return new BasicVector_UnsignedByte32(newElements);
305      }
306    // No change.
307    return this;
308  }
309
310  @Override
311  public AbstractVector adjustArray(int newCapacity,
312                                     AbstractArray displacedTo,
313                                     int displacement)
314  {
315    return new ComplexVector(newCapacity, displacedTo, displacement);
316  }
317}
Note: See TracBrowser for help on using the repository browser.