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