source: trunk/abcl/src/org/armedbear/lisp/BasicVector_UnsignedByte16.java

Last change on this file was 15734, checked in by Mark Evenson, 8 months ago

Implement vector-to-vector REPLACE as a primitive

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.8 KB
Line 
1/*
2 * BasicVector_UnsignedByte16.java
3 *
4 * Copyright (C) 2002-2005 Peter Graves
5 * $Id: BasicVector_UnsignedByte16.java 15734 2023-09-01 06:28:20Z mevenson $
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    @Override
159    public void aset(int index, int n)
160    {
161        try {
162            elements[index] = n;
163        }
164        catch (ArrayIndexOutOfBoundsException e) {
165            badIndex(index, capacity);
166        }
167    }
168
169    @Override
170    public void aset(int index, LispObject obj)
171    {
172        if (obj instanceof Fixnum) {
173                try {
174            elements[index] = ((Fixnum)obj).value;
175        }
176        catch (ArrayIndexOutOfBoundsException e) {
177            badIndex(index, capacity);
178        }
179        }
180        else {
181            type_error(obj, UNSIGNED_BYTE_16);
182        }
183    }
184
185    @Override
186    public LispObject subseq(int start, int end)
187    {
188        BasicVector_UnsignedByte16 v = new BasicVector_UnsignedByte16(end - start);
189        int i = start, j = 0;
190        try {
191            while (i < end)
192                v.elements[j++] = elements[i++];
193            return v;
194        }
195        catch (ArrayIndexOutOfBoundsException e) {
196            return error(new TypeError("Array index out of bounds: " + i + "."));
197        }
198    }
199
200    @Override
201    public void fill(LispObject obj)
202    {
203        if (!(obj instanceof Fixnum)) {
204            type_error(obj, Symbol.FIXNUM);
205            // Not reached.
206            return;
207        }
208        int n = ((Fixnum) obj).value;
209        if (n < 0 || n > 65535) {
210            type_error(obj, UNSIGNED_BYTE_16);
211            // Not reached.
212            return;
213        }
214        for (int i = capacity; i-- > 0;)
215            elements[i] = n;
216    }
217
218    @Override
219    public void shrink(int n)
220    {
221        if (n < capacity) {
222            int[] newArray = new int[n];
223            System.arraycopy(elements, 0, newArray, 0, n);
224            elements = newArray;
225            capacity = n;
226            return;
227        }
228        if (n == capacity)
229            return;
230        error(new LispError());
231    }
232
233    @Override
234    public LispObject reverse()
235    {
236        BasicVector_UnsignedByte16 result = new BasicVector_UnsignedByte16(capacity);
237        int i, j;
238        for (i = 0, j = capacity - 1; i < capacity; i++, j--)
239            result.elements[i] = elements[j];
240        return result;
241    }
242
243    @Override
244    public LispObject nreverse()
245    {
246        int i = 0;
247        int j = capacity - 1;
248        while (i < j) {
249            int temp = elements[i];
250            elements[i] = elements[j];
251            elements[j] = temp;
252            ++i;
253            --j;
254        }
255        return this;
256    }
257
258    @Override
259    public AbstractVector adjustArray(int newCapacity,
260                                       LispObject initialElement,
261                                       LispObject initialContents)
262
263    {
264        if (initialContents != null) {
265            LispObject[] newElements = new LispObject[newCapacity];
266            if (initialContents.listp()) {
267                LispObject list = initialContents;
268                for (int i = 0; i < newCapacity; i++) {
269                    newElements[i] = list.car();
270                    list = list.cdr();
271                }
272            } else if (initialContents.vectorp()) {
273                for (int i = 0; i < newCapacity; i++)
274                    newElements[i] = initialContents.elt(i);
275            } else
276                type_error(initialContents, Symbol.SEQUENCE);
277            return new BasicVector_UnsignedByte16(newElements);
278        }
279        if (capacity != newCapacity) {
280            LispObject[] newElements = new LispObject[newCapacity];
281            System.arraycopy(elements, 0, newElements, 0,
282                             Math.min(capacity, newCapacity));
283            if (initialElement != null)
284                for (int i = capacity; i < newCapacity; i++)
285                    newElements[i] = initialElement;
286            return new BasicVector_UnsignedByte16(newElements);
287        }
288        // No change.
289        return this;
290    }
291
292    @Override
293    public AbstractVector adjustArray(int newCapacity,
294                                       AbstractArray displacedTo,
295                                       int displacement)
296    {
297        return new ComplexVector(newCapacity, displacedTo, displacement);
298    }
299
300    @Override
301    public AbstractVector replace(AbstractVector source,
302                                  int targetStart, int targetEnd,
303                                  int sourceStart, int sourceEnd)
304    {
305        if (source instanceof BasicVector_UnsignedByte16) {
306            System.arraycopy(((BasicVector_UnsignedByte16)source).elements, sourceStart,
307                             elements, targetStart,
308                             Math.min(targetEnd - targetStart, sourceEnd - sourceStart));
309            return this;
310        } else {
311            return super.replace(source, targetStart, targetEnd, sourceStart, sourceEnd);
312        }
313    }
314}
Note: See TracBrowser for help on using the repository browser.