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