source: branches/0.17.x/abcl/src/org/armedbear/lisp/SimpleArray_UnsignedByte16.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.6 KB
Line 
1/*
2 * SimpleArray_UnsignedByte16.java
3 *
4 * Copyright (C) 2003-2005 Peter Graves
5 * $Id: SimpleArray_UnsignedByte16.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_UnsignedByte16 extends AbstractArray
37{
38    private final int[] dimv;
39    private final int totalSize;
40    private final int[] data;
41
42    public SimpleArray_UnsignedByte16(int[] dimv)
43    {
44        this.dimv = dimv;
45        totalSize = computeTotalSize(dimv);
46        data = new int[totalSize];
47    }
48
49    public SimpleArray_UnsignedByte16(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 int[totalSize];
61        setInitialContents(0, dimv, initialContents, 0);
62    }
63
64    public SimpleArray_UnsignedByte16(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 int[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_16, 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_16;
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 int aref(int index)
193    {
194        try {
195            return data[index];
196        }
197        catch (ArrayIndexOutOfBoundsException e) {
198            error(new TypeError("Bad row major index " + index + "."));
199            // Not reached.
200            return 0;
201        }
202    }
203
204    @Override
205    public LispObject AREF(int index)
206    {
207        try {
208            return Fixnum.getInstance(data[index]);
209        }
210        catch (ArrayIndexOutOfBoundsException e) {
211            return error(new TypeError("Bad row major index " + index + "."));
212        }
213    }
214
215    @Override
216    public void aset(int index, LispObject obj)
217    {
218        try {
219            data[index] = Fixnum.getValue(obj);
220        }
221        catch (ArrayIndexOutOfBoundsException e) {
222            error(new TypeError("Bad row major index " + index + "."));
223        }
224    }
225
226    @Override
227    public int getRowMajorIndex(int[] subscripts)
228    {
229        final int rank = dimv.length;
230        if (rank != subscripts.length) {
231            StringBuffer sb = new StringBuffer("Wrong number of subscripts (");
232            sb.append(subscripts.length);
233            sb.append(") for array of rank ");
234            sb.append(rank);
235            sb.append('.');
236            error(new ProgramError(sb.toString()));
237        }
238        int sum = 0;
239        int size = 1;
240        for (int i = rank; i-- > 0;) {
241            final int dim = dimv[i];
242            final int lastSize = size;
243            size *= dim;
244            int n = subscripts[i];
245            if (n < 0 || n >= dim) {
246                StringBuffer sb = new StringBuffer("Invalid index ");
247                sb.append(n);
248                sb.append(" for array ");
249                sb.append(this);
250                sb.append('.');
251                error(new ProgramError(sb.toString()));
252            }
253            sum += n * lastSize;
254        }
255        return sum;
256    }
257
258    @Override
259    public LispObject get(int[] subscripts)
260    {
261        try {
262            return Fixnum.getInstance(data[getRowMajorIndex(subscripts)]);
263        }
264        catch (ArrayIndexOutOfBoundsException e) {
265            return error(new TypeError("Bad row major index " +
266                                        getRowMajorIndex(subscripts) + "."));
267        }
268    }
269
270    @Override
271    public void set(int[] subscripts, LispObject obj)
272
273    {
274        try {
275            data[getRowMajorIndex(subscripts)] = Fixnum.getValue(obj);
276        }
277        catch (ArrayIndexOutOfBoundsException e) {
278            error(new TypeError("Bad row major index " +
279                                 getRowMajorIndex(subscripts) + "."));
280        }
281    }
282
283    @Override
284    public void fill(LispObject obj)
285    {
286        int n = Fixnum.getValue(obj);
287        for (int i = totalSize; i-- > 0;)
288            data[i] = n;
289    }
290
291    @Override
292    public String writeToString()
293    {
294        if (Symbol.PRINT_READABLY.symbolValue() != NIL) {
295            error(new PrintNotReadable(list(Keyword.OBJECT, this)));
296            // Not reached.
297            return null;
298        }
299        return writeToString(dimv);
300    }
301
302    public AbstractArray adjustArray(int[] dimv, LispObject initialElement,
303                                     LispObject initialContents)
304
305    {
306        if (initialContents != null)
307            return new SimpleArray_UnsignedByte16(dimv, initialContents);
308        for (int i = 0; i < dimv.length; i++) {
309            if (dimv[i] != this.dimv[i]) {
310                SimpleArray_UnsignedByte16 newArray =
311                    new SimpleArray_UnsignedByte16(dimv);
312                if (initialElement != null)
313                    newArray.fill(initialElement);
314                copyArray(this, newArray);
315                return newArray;
316            }
317        }
318        // New dimensions are identical to old dimensions.
319        return this;
320    }
321
322    // Copy a1 to a2 for index tuples that are valid for both arrays.
323    private static void copyArray(AbstractArray a1, AbstractArray a2)
324
325    {
326        Debug.assertTrue(a1.getRank() == a2.getRank());
327        int[] subscripts = new int[a1.getRank()];
328        int axis = 0;
329        copySubArray(a1, a2, subscripts, axis);
330    }
331
332    private static void copySubArray(AbstractArray a1, AbstractArray a2,
333                                     int[] subscripts, int axis)
334
335    {
336        if (axis < subscripts.length) {
337            final int limit =
338                Math.min(a1.getDimension(axis), a2.getDimension(axis));
339            for (int i = 0; i < limit; i++) {
340                subscripts[axis] = i;
341                copySubArray(a1, a2, subscripts, axis + 1);
342            }
343        } else {
344            int i1 = a1.getRowMajorIndex(subscripts);
345            int i2 = a2.getRowMajorIndex(subscripts);
346            a2.aset(i2, a1.AREF(i1));
347        }
348    }
349
350    public AbstractArray adjustArray(int[] dimv, AbstractArray displacedTo,
351                                     int displacement)
352    {
353        return new ComplexArray(dimv, displacedTo, displacement);
354    }
355}
Note: See TracBrowser for help on using the repository browser.