source: branches/0.19.x/abcl/src/org/armedbear/lisp/BasicVector_UnsignedByte16.java

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

Don't extend Lisp in LispObject, static import Lisp wherever
necessary. Patch by Douglas R. Miles.

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