source: branches/0.19.x/abcl/src/org/armedbear/lisp/BasicVector_UnsignedByte32.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: 7.5 KB
Line 
1/*
2 * BasicVector_UnsignedByte32.java
3 *
4 * Copyright (C) 2002-2006 Peter Graves
5 * $Id: BasicVector_UnsignedByte32.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_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 LispObject AREF(LispObject index)
178  {
179        final int idx = Fixnum.getValue(index);
180    try
181      {
182        return number(elements[idx]);
183      }
184    catch (ArrayIndexOutOfBoundsException e)
185      {
186        badIndex(idx, elements.length);
187        return NIL; // Not reached.
188      }
189  }
190
191  @Override
192  public void aset(int index, LispObject newValue)
193  {
194    try
195      {
196        elements[index] = newValue.longValue();
197      }
198    catch (ArrayIndexOutOfBoundsException e)
199      {
200        badIndex(index, capacity);
201      }
202  }
203
204  @Override
205  public LispObject subseq(int start, int end)
206  {
207    BasicVector_UnsignedByte32 v = new BasicVector_UnsignedByte32(end - start);
208    int i = start, j = 0;
209    try
210      {
211        while (i < end)
212          v.elements[j++] = elements[i++];
213        return v;
214      }
215    catch (ArrayIndexOutOfBoundsException e)
216      {
217        // FIXME
218        return error(new TypeError("Array index out of bounds: " + i + "."));
219      }
220  }
221
222  @Override
223  public void fill(LispObject obj)
224  {
225    for (int i = capacity; i-- > 0;)
226      elements[i] = obj.longValue();
227  }
228
229  @Override
230  public void shrink(int n)
231  {
232    if (n < capacity)
233      {
234        long[] newArray = new long[n];
235        System.arraycopy(elements, 0, newArray, 0, n);
236        elements = newArray;
237        capacity = n;
238        return;
239      }
240    if (n == capacity)
241      return;
242    error(new LispError());
243  }
244
245  @Override
246  public LispObject reverse()
247  {
248    BasicVector_UnsignedByte32 result = new BasicVector_UnsignedByte32(capacity);
249    int i, j;
250    for (i = 0, j = capacity - 1; i < capacity; i++, j--)
251      result.elements[i] = elements[j];
252    return result;
253  }
254
255  @Override
256  public LispObject nreverse()
257  {
258    int i = 0;
259    int j = capacity - 1;
260    while (i < j)
261      {
262        long temp = elements[i];
263        elements[i] = elements[j];
264        elements[j] = temp;
265        ++i;
266        --j;
267      }
268    return this;
269  }
270
271  @Override
272  public AbstractVector adjustArray(int newCapacity,
273                                     LispObject initialElement,
274                                     LispObject initialContents)
275
276  {
277    if (initialContents != null)
278      {
279        LispObject[] newElements = new LispObject[newCapacity];
280        if (initialContents.listp())
281          {
282            LispObject list = initialContents;
283            for (int i = 0; i < newCapacity; i++)
284              {
285                newElements[i] = list.car();
286                list = list.cdr();
287              }
288          }
289        else if (initialContents.vectorp())
290          {
291            for (int i = 0; i < newCapacity; i++)
292              newElements[i] = initialContents.elt(i);
293          }
294        else
295          type_error(initialContents, Symbol.SEQUENCE);
296        return new BasicVector_UnsignedByte32(newElements);
297      }
298    if (capacity != newCapacity)
299      {
300        LispObject[] newElements = new LispObject[newCapacity];
301        System.arraycopy(elements, 0, newElements, 0,
302                         Math.min(capacity, newCapacity));
303        if (initialElement != null)
304            for (int i = capacity; i < newCapacity; i++)
305                newElements[i] = initialElement;
306        return new BasicVector_UnsignedByte32(newElements);
307      }
308    // No change.
309    return this;
310  }
311
312  @Override
313  public AbstractVector adjustArray(int newCapacity,
314                                     AbstractArray displacedTo,
315                                     int displacement)
316  {
317    return new ComplexVector(newCapacity, displacedTo, displacement);
318  }
319}
Note: See TracBrowser for help on using the repository browser.