source: branches/0.17.x/abcl/src/org/armedbear/lisp/SimpleArray_UnsignedByte8.java

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

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