source: trunk/abcl/src/org/armedbear/lisp/BasicVector_UnsignedByte8.java @ 12450

Last change on this file since 12450 was 12414, checked in by ehuelsmann, 15 years ago

Add function to retrieve a lisp byte array from a byte array output stream.

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