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