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

Last change on this file was 15350, checked in by Mark Evenson, 4 years ago

Refactor naming for coerce methods

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.1 KB
Line 
1/*
2 * ComplexVector_UnsignedByte8.java
3 *
4 * Copyright (C) 2002-2005 Peter Graves
5 * $Id: ComplexVector_UnsignedByte8.java 15350 2020-07-23 05:12:08Z 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 specialized vector of element type (UNSIGNED-BYTE 8) that is displaced to
39// another array, has a fill pointer, and/or is expressly adjustable.
40public final class ComplexVector_UnsignedByte8 extends AbstractVector
41{
42    private int capacity;
43    private int fillPointer = -1; // -1 indicates no fill pointer.
44    private boolean isDisplaced;
45
46    // For non-displaced arrays.
47    private byte[] elements;
48
49    // For displaced arrays.
50    private AbstractArray array;
51    private int displacement;
52
53    public ComplexVector_UnsignedByte8(int capacity)
54    {
55        elements = new byte[capacity];
56        this.capacity = capacity;
57    }
58
59    public ComplexVector_UnsignedByte8(int capacity, AbstractArray array,
60                                       int displacement)
61    {
62        this.capacity = capacity;
63        this.array = array;
64        this.displacement = displacement;
65        isDisplaced = true;
66    }
67
68    @Override
69    public LispObject typeOf()
70    {
71        return list(Symbol.VECTOR, UNSIGNED_BYTE_8, Fixnum.getInstance(capacity));
72    }
73
74    @Override
75    public LispObject classOf()
76    {
77        return BuiltInClass.VECTOR;
78    }
79
80    @Override
81    public boolean hasFillPointer()
82    {
83        return fillPointer >= 0;
84    }
85
86    @Override
87    public int getFillPointer()
88    {
89        return fillPointer;
90    }
91
92    @Override
93    public void setFillPointer(int n)
94    {
95        fillPointer = n;
96    }
97
98    @Override
99    public void setFillPointer(LispObject obj)
100    {
101        if (obj == T)
102            fillPointer = capacity();
103        else {
104            int n = Fixnum.getValue(obj);
105            if (n > capacity()) {
106                StringBuffer sb = new StringBuffer("The new fill pointer (");
107                sb.append(n);
108                sb.append(") exceeds the capacity of the vector (");
109                sb.append(capacity());
110                sb.append(").");
111                error(new LispError(sb.toString()));
112            } else if (n < 0) {
113                StringBuffer sb = new StringBuffer("The new fill pointer (");
114                sb.append(n);
115                sb.append(") is negative.");
116                error(new LispError(sb.toString()));
117            } else
118                fillPointer = n;
119        }
120    }
121
122    @Override
123    public boolean isDisplaced()
124    {
125        return isDisplaced;
126    }
127
128    @Override
129    public LispObject arrayDisplacement()
130    {
131        LispObject value1, value2;
132        if (array != null) {
133            value1 = array;
134            value2 = Fixnum.getInstance(displacement);
135        } else {
136            value1 = NIL;
137            value2 = Fixnum.ZERO;
138        }
139        return LispThread.currentThread().setValues(value1, value2);
140    }
141
142    @Override
143    public LispObject getElementType()
144    {
145        return UNSIGNED_BYTE_8;
146    }
147
148    @Override
149    public boolean isSimpleVector()
150    {
151        return false;
152    }
153
154    @Override
155    public int capacity()
156    {
157        return capacity;
158    }
159
160    @Override
161    public int length()
162    {
163        return fillPointer >= 0 ? fillPointer : capacity;
164    }
165
166    @Override
167    public LispObject elt(int index)
168    {
169        final int limit = length();
170        if (index < 0 || index >= limit)
171            badIndex(index, limit);
172        return AREF(index);
173    }
174
175    // Ignores fill pointer.
176    @Override
177    public LispObject AREF(int index)
178    {
179        if (elements != null) {
180            try {
181                return coerceFromJavaByte(elements[index]);
182            }
183            catch (ArrayIndexOutOfBoundsException e) {
184                badIndex(index, elements.length);
185                return NIL; // Not reached.
186            }
187        } else {
188            // Displaced array.
189            if (index < 0 || index >= capacity)
190                badIndex(index, capacity);
191            return array.AREF(index + displacement);
192        }
193    }
194
195    @Override
196    public void aset(int index, int n)
197    {
198        if (elements != null) {
199            try {
200                elements[index] = (byte) n;
201            }
202            catch (ArrayIndexOutOfBoundsException e) {
203                badIndex(index, elements.length);
204            }
205        } else {
206            // Displaced array.
207            if (index < 0 || index >= capacity)
208                badIndex(index, capacity);
209            else
210                array.aset(index + displacement, n);
211        }
212    }
213
214    @Override
215    public void aset(int index, LispObject newValue)
216    {
217        if (elements != null) {
218            try {
219                elements[index] = coerceToJavaByte(newValue);
220            }
221            catch (ArrayIndexOutOfBoundsException e) {
222                badIndex(index, elements.length);
223            }
224        } else
225            array.aset(index + displacement, newValue);
226    }
227
228    @Override
229    public LispObject subseq(int start, int end)
230    {
231        SimpleVector v = new SimpleVector(end - start);
232        int i = start, j = 0;
233        try {
234            while (i < end)
235                v.aset(j++, AREF(i++));
236            return v;
237        }
238        catch (ArrayIndexOutOfBoundsException e) {
239            return error(new TypeError("Array index out of bounds: " + i + "."));
240        }
241    }
242
243    @Override
244    public void fill(LispObject obj)
245    {
246        if (!(obj instanceof Fixnum)) {
247            type_error(obj, Symbol.FIXNUM);
248            // Not reached.
249            return;
250        }
251        int n = ((Fixnum) obj).value;
252        if (n < 0 || n > 255) {
253            type_error(obj, UNSIGNED_BYTE_8);
254            // Not reached.
255            return;
256        }
257        for (int i = capacity; i-- > 0;)
258            elements[i] = (byte) n;
259    }
260
261    @Override
262    public void shrink(int n)
263    {
264        if (elements != null) {
265            if (n < elements.length) {
266                byte[] newArray = new byte[n];
267                System.arraycopy(elements, 0, newArray, 0, n);
268                elements = newArray;
269                capacity = n;
270                return;
271            }
272            if (n == elements.length)
273                return;
274        }
275        error(new LispError());
276    }
277
278    @Override
279    public LispObject reverse()
280    {
281        int length = length();
282        BasicVector_UnsignedByte8 result = new BasicVector_UnsignedByte8(length);
283        int i, j;
284        for (i = 0, j = length - 1; i < length; i++, j--)
285            result.aset(i, AREF(j));
286        return result;
287    }
288
289    @Override
290    public LispObject nreverse()
291    {
292        if (elements != null) {
293            int i = 0;
294            int j = length() - 1;
295            while (i < j) {
296                byte temp = elements[i];
297                elements[i] = elements[j];
298                elements[j] = temp;
299                ++i;
300                --j;
301            }
302        } else {
303            // Displaced array.
304            int length = length();
305            byte[] data = new byte[length];
306            int i, j;
307            for (i = 0, j = length - 1; i < length; i++, j--)
308                data[i] = coerceToJavaByte(AREF(j));
309            elements = data;
310            capacity = length;
311            array = null;
312            displacement = 0;
313            isDisplaced = false;
314            fillPointer = -1;
315        }
316        return this;
317    }
318
319    @Override
320    public void vectorPushExtend(LispObject element)
321    {
322        if (fillPointer < 0)
323            noFillPointer();
324        if (fillPointer >= capacity) {
325            // Need to extend vector.
326            ensureCapacity(capacity * 2 + 1);
327        }
328        aset(fillPointer, element);
329        ++fillPointer;
330    }
331
332    @Override
333    public LispObject VECTOR_PUSH_EXTEND(LispObject element)
334
335    {
336        vectorPushExtend(element);
337        return Fixnum.getInstance(fillPointer - 1);
338    }
339
340    @Override
341    public LispObject VECTOR_PUSH_EXTEND(LispObject element, LispObject extension)
342
343    {
344        int ext = Fixnum.getValue(extension);
345        if (fillPointer < 0)
346            noFillPointer();
347        if (fillPointer >= capacity) {
348            // Need to extend vector.
349            ext = Math.max(ext, capacity + 1);
350            ensureCapacity(capacity + ext);
351        }
352        aset(fillPointer, element);
353        return Fixnum.getInstance(fillPointer++);
354    }
355
356    private final void ensureCapacity(int minCapacity)
357    {
358        if (elements != null) {
359            if (capacity < minCapacity) {
360                byte[] newArray = new byte[minCapacity];
361                System.arraycopy(elements, 0, newArray, 0, capacity);
362                elements = newArray;
363                capacity = minCapacity;
364            }
365        } else {
366            // Displaced array.
367            Debug.assertTrue(array != null);
368            if (capacity < minCapacity ||
369                array.getTotalSize() - displacement < minCapacity)
370            {
371                // Copy array.
372                elements = new byte[minCapacity];
373                final int limit =
374                    Math.min(capacity, array.getTotalSize() - displacement);
375                for (int i = 0; i < limit; i++)
376                    elements[i] = coerceToJavaByte(array.AREF(displacement + i));
377                capacity = minCapacity;
378                array = null;
379                displacement = 0;
380                isDisplaced = false;
381            }
382        }
383    }
384
385    @Override
386    public AbstractVector adjustArray(int newCapacity,
387                                       LispObject initialElement,
388                                       LispObject initialContents)
389
390    {
391        if (initialContents != null) {
392            // "If INITIAL-CONTENTS is supplied, it is treated as for MAKE-
393            // ARRAY. In this case none of the original contents of array
394            // appears in the resulting array."
395            byte[] newElements = new byte[newCapacity];
396            if (initialContents.listp()) {
397                LispObject list = initialContents;
398                for (int i = 0; i < newCapacity; i++) {
399                    newElements[i] = coerceToJavaByte(list.car());
400                    list = list.cdr();
401                }
402            } else if (initialContents.vectorp()) {
403                for (int i = 0; i < newCapacity; i++)
404                    newElements[i] = coerceToJavaByte(initialContents.elt(i));
405            } else
406                type_error(initialContents, Symbol.SEQUENCE);
407            elements = newElements;
408        } else {
409            if (elements == null) {
410                // Displaced array. Copy existing elements.
411                elements = new byte[newCapacity];
412                final int limit = Math.min(capacity, newCapacity);
413                for (int i = 0; i < limit; i++)
414                    elements[i] = coerceToJavaByte(array.AREF(displacement + i));
415            } else if (capacity != newCapacity) {
416                byte[] newElements = new byte[newCapacity];
417                System.arraycopy(elements, 0, newElements, 0,
418                                 Math.min(capacity, newCapacity));
419                elements = newElements;
420            }
421            // Initialize new elements (if aapplicable).
422            if (initialElement != null) {
423                byte b = coerceToJavaByte(initialElement);
424                for (int i = capacity; i < newCapacity; i++)
425                    elements[i] = b;
426            }
427        }
428        capacity = newCapacity;
429        array = null;
430        displacement = 0;
431        isDisplaced = false;
432        return this;
433    }
434
435    @Override
436    public AbstractVector adjustArray(int newCapacity,
437                                       AbstractArray displacedTo,
438                                       int displacement)
439
440    {
441        capacity = newCapacity;
442        array = displacedTo;
443        this.displacement = displacement;
444        elements = null;
445        isDisplaced = true;
446        return this;
447    }
448}
Note: See TracBrowser for help on using the repository browser.