source: trunk/abcl/src/org/armedbear/lisp/SimpleArray_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: 10.6 KB
Line 
1/*
2 * SimpleArray_UnsignedByte8.java
3 *
4 * Copyright (C) 2003-2005 Peter Graves
5 * $Id: SimpleArray_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
38public final class SimpleArray_UnsignedByte8 extends AbstractArray
39{
40    private final int[] dimv;
41    private final int totalSize;
42    final byte[] data;
43
44    public SimpleArray_UnsignedByte8(int[] dimv)
45    {
46        this.dimv = dimv;
47        totalSize = computeTotalSize(dimv);
48        data = new byte[totalSize];
49    }
50
51    public SimpleArray_UnsignedByte8(int[] dimv, LispObject initialContents)
52
53    {
54        this.dimv = dimv;
55        final int rank = dimv.length;
56        LispObject rest = initialContents;
57        for (int i = 0; i < rank; i++) {
58            dimv[i] = rest.length();
59            rest = rest.elt(0);
60        }
61        totalSize = computeTotalSize(dimv);
62        data = new byte[totalSize];
63        setInitialContents(0, dimv, initialContents, 0);
64    }
65
66    public SimpleArray_UnsignedByte8(int rank, LispObject initialContents)
67
68    {
69        if (rank < 2)
70            Debug.assertTrue(false);
71        dimv = new int[rank];
72        LispObject rest = initialContents;
73        for (int i = 0; i < rank; i++) {
74            dimv[i] = rest.length();
75            if (rest == NIL || rest.length() == 0)
76                break;
77            rest = rest.elt(0);
78        }
79        totalSize = computeTotalSize(dimv);
80        data = new byte[totalSize];
81        setInitialContents(0, dimv, initialContents, 0);
82    }
83
84    private int setInitialContents(int axis, int[] dims, LispObject contents,
85                                   int index)
86
87    {
88        if (dims.length == 0) {
89            try {
90                data[index] = coerceToJavaByte(contents);
91            }
92            catch (ArrayIndexOutOfBoundsException e) {
93                error(new LispError("Bad initial contents for array."));
94                return -1;
95            }
96            ++index;
97        } else {
98            int dim = dims[0];
99            if (dim != contents.length()) {
100                error(new LispError("Bad initial contents for array."));
101                return -1;
102            }
103            int[] newDims = new int[dims.length-1];
104            for (int i = 1; i < dims.length; i++)
105                newDims[i-1] = dims[i];
106            if (contents.listp()) {
107                for (int i = contents.length();i-- > 0;) {
108                    LispObject content = contents.car();
109                    index =
110                        setInitialContents(axis + 1, newDims, content, index);
111                    contents = contents.cdr();
112                }
113            } else {
114                AbstractVector v = checkVector(contents);
115                final int length = v.length();
116                for (int i = 0; i < length; i++) {
117                    LispObject content = v.AREF(i);
118                    index =
119                        setInitialContents(axis + 1, newDims, content, index);
120                }
121            }
122        }
123        return index;
124    }
125
126    @Override
127    public LispObject typeOf()
128    {
129        return list(Symbol.SIMPLE_ARRAY, UNSIGNED_BYTE_8, getDimensions());
130    }
131
132    @Override
133    public LispObject classOf()
134    {
135        return BuiltInClass.SIMPLE_ARRAY;
136    }
137
138    @Override
139    public LispObject typep(LispObject typeSpecifier)
140    {
141        if (typeSpecifier == Symbol.SIMPLE_ARRAY)
142            return T;
143        if (typeSpecifier == BuiltInClass.SIMPLE_ARRAY)
144            return T;
145        return super.typep(typeSpecifier);
146    }
147
148    @Override
149    public int getRank()
150    {
151        return dimv.length;
152    }
153
154    @Override
155    public LispObject getDimensions()
156    {
157        LispObject result = NIL;
158        for (int i = dimv.length; i-- > 0;)
159            result = new Cons(Fixnum.getInstance(dimv[i]), result);
160        return result;
161    }
162
163    @Override
164    public int getDimension(int n)
165    {
166        try {
167            return dimv[n];
168        }
169        catch (ArrayIndexOutOfBoundsException e) {
170            error(new TypeError("Bad array dimension " + n + "."));
171            return -1;
172        }
173    }
174
175    @Override
176    public LispObject getElementType()
177    {
178        return UNSIGNED_BYTE_8;
179    }
180
181    @Override
182    public int getTotalSize()
183    {
184        return totalSize;
185    }
186
187    @Override
188    public boolean isAdjustable()
189    {
190        return false;
191    }
192
193    @Override
194    public LispObject AREF(int index)
195    {
196        try {
197            return coerceFromJavaByte(data[index]);
198        }
199        catch (ArrayIndexOutOfBoundsException e) {
200            return error(new TypeError("Bad row major index " + index + "."));
201        }
202    }
203
204    @Override
205    public void aset(int index, LispObject newValue)
206    {
207        try {
208            data[index] = coerceToJavaByte(newValue);
209        }
210        catch (ArrayIndexOutOfBoundsException e) {
211            error(new TypeError("Bad row major index " + index + "."));
212        }
213    }
214
215    @Override
216    public int getRowMajorIndex(int[] subscripts)
217    {
218        final int rank = dimv.length;
219        if (rank != subscripts.length) {
220            StringBuffer sb = new StringBuffer("Wrong number of subscripts (");
221            sb.append(subscripts.length);
222            sb.append(") for array of rank ");
223            sb.append(rank);
224            sb.append('.');
225            program_error(sb.toString());
226        }
227        int sum = 0;
228        int size = 1;
229        for (int i = rank; i-- > 0;) {
230            final int dim = dimv[i];
231            final int lastSize = size;
232            size *= dim;
233            int n = subscripts[i];
234            if (n < 0 || n >= dim) {
235                StringBuffer sb = new StringBuffer("Invalid index ");
236                sb.append(n);
237                sb.append(" for array ");
238                sb.append(this);
239                sb.append('.');
240                program_error(sb.toString());
241            }
242            sum += n * lastSize;
243        }
244        return sum;
245    }
246
247    @Override
248    public LispObject get(int[] subscripts)
249    {
250        try {
251            return coerceFromJavaByte(data[getRowMajorIndex(subscripts)]);
252        }
253        catch (ArrayIndexOutOfBoundsException e) {
254            return error(new TypeError("Bad row major index " +
255                                        getRowMajorIndex(subscripts) + "."));
256        }
257    }
258
259    @Override
260    public void set(int[] subscripts, LispObject newValue)
261
262    {
263        try {
264            data[getRowMajorIndex(subscripts)] = coerceToJavaByte(newValue);
265        }
266        catch (ArrayIndexOutOfBoundsException e) {
267            error(new TypeError("Bad row major index " +
268                                 getRowMajorIndex(subscripts) + "."));
269        }
270    }
271
272    @Override
273    public void fill(LispObject obj)
274    {
275        if (!(obj instanceof Fixnum)) {
276            type_error(obj, Symbol.FIXNUM);
277            // Not reached.
278            return;
279        }
280        int n = ((Fixnum) obj).value;
281        if (n < 0 || n > 255) {
282            type_error(obj, UNSIGNED_BYTE_8);
283            // Not reached.
284            return;
285        }
286        for (int i = totalSize; i-- > 0;)
287            data[i] = (byte) n;
288    }
289
290    @Override
291    public String printObject()
292    {
293        if (Symbol.PRINT_READABLY.symbolValue() != NIL) {
294            error(new PrintNotReadable(list(Keyword.OBJECT, this)));
295            // Not reached.
296            return null;
297        }
298        return printObject(dimv);
299    }
300
301    public AbstractArray adjustArray(int[] dimv, LispObject initialElement,
302                                     LispObject initialContents)
303
304    {
305        if (initialContents != null)
306            return new SimpleArray_UnsignedByte8(dimv, initialContents);
307        for (int i = 0; i < dimv.length; i++) {
308            if (dimv[i] != this.dimv[i]) {
309                SimpleArray_UnsignedByte8 newArray =
310                    new SimpleArray_UnsignedByte8(dimv);
311                if (initialElement != null)
312                    newArray.fill(initialElement);
313                copyArray(this, newArray);
314                return newArray;
315            }
316        }
317        // New dimensions are identical to old dimensions.
318        return this;
319    }
320
321    // Copy a1 to a2 for index tuples that are valid for both arrays.
322    static void copyArray(AbstractArray a1, AbstractArray a2)
323
324    {
325        Debug.assertTrue(a1.getRank() == a2.getRank());
326        int[] subscripts = new int[a1.getRank()];
327        int axis = 0;
328        copySubArray(a1, a2, subscripts, axis);
329    }
330
331    private static void copySubArray(AbstractArray a1, AbstractArray a2,
332                                     int[] subscripts, int axis)
333
334    {
335        if (axis < subscripts.length) {
336            final int limit =
337                Math.min(a1.getDimension(axis), a2.getDimension(axis));
338            for (int i = 0; i < limit; i++) {
339                subscripts[axis] = i;
340                copySubArray(a1, a2, subscripts, axis + 1);
341            }
342        } else {
343            int i1 = a1.getRowMajorIndex(subscripts);
344            int i2 = a2.getRowMajorIndex(subscripts);
345            a2.aset(i2, a1.AREF(i1));
346        }
347    }
348
349    public AbstractArray adjustArray(int[] dimv, AbstractArray displacedTo,
350                                     int displacement)
351    {
352        return new ComplexArray(dimv, displacedTo, displacement);
353    }
354}
Note: See TracBrowser for help on using the repository browser.