source: branches/0.17.x/abcl/src/org/armedbear/lisp/ComplexArray_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: 8.9 KB
Line 
1/*
2 * ComplexArray_UnsignedByte8.java
3 *
4 * Copyright (C) 2003-2005 Peter Graves
5 * $Id: ComplexArray_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 ComplexArray_UnsignedByte8 extends AbstractArray
37{
38    private final int[] dimv;
39    private int totalSize;
40
41    // For non-displaced arrays.
42    private byte[] data;
43
44    // For displaced arrays.
45    private AbstractArray array;
46    private int displacement;
47
48    public ComplexArray_UnsignedByte8(int[] dimv)
49    {
50        this.dimv = dimv;
51        totalSize = computeTotalSize(dimv);
52        data = new byte[totalSize];
53    }
54
55    public ComplexArray_UnsignedByte8(int[] dimv, LispObject initialContents)
56
57    {
58        this.dimv = dimv;
59        final int rank = dimv.length;
60        LispObject rest = initialContents;
61        for (int i = 0; i < rank; i++) {
62            dimv[i] = rest.length();
63            rest = rest.elt(0);
64        }
65        totalSize = computeTotalSize(dimv);
66        data = new byte[totalSize];
67        setInitialContents(0, dimv, initialContents, 0);
68    }
69
70    public ComplexArray_UnsignedByte8(int[] dimv, AbstractArray array, int displacement)
71    {
72        this.dimv = dimv;
73        this.array = array;
74        this.displacement = displacement;
75        totalSize = computeTotalSize(dimv);
76    }
77
78    private int setInitialContents(int axis, int[] dims, LispObject contents,
79                                   int index)
80
81    {
82        if (dims.length == 0) {
83            try {
84                data[index] = coerceLispObjectToJavaByte(contents);
85            }
86            catch (ArrayIndexOutOfBoundsException e) {
87                error(new LispError("Bad initial contents for array."));
88                return -1;
89            }
90            ++index;
91        } else {
92            int dim = dims[0];
93            if (dim != contents.length()) {
94                error(new LispError("Bad initial contents for array."));
95                return -1;
96            }
97            int[] newDims = new int[dims.length-1];
98            for (int i = 1; i < dims.length; i++)
99                newDims[i-1] = dims[i];
100            if (contents.listp()) {
101                for (int i = contents.length();i-- > 0;) {
102                    LispObject content = contents.car();
103                    index =
104                        setInitialContents(axis + 1, newDims, content, index);
105                    contents = contents.cdr();
106                }
107            } else {
108                AbstractVector v = checkVector(contents);
109                final int length = v.length();
110                for (int i = 0; i < length; i++) {
111                    LispObject content = v.AREF(i);
112                    index =
113                        setInitialContents(axis + 1, newDims, content, index);
114                }
115            }
116        }
117        return index;
118    }
119
120    @Override
121    public LispObject typeOf()
122    {
123        return list(Symbol.ARRAY, UNSIGNED_BYTE_8, getDimensions());
124    }
125
126    @Override
127    public LispObject classOf()
128    {
129        return BuiltInClass.ARRAY;
130    }
131
132    @Override
133    public int getRank()
134    {
135        return dimv.length;
136    }
137
138    @Override
139    public LispObject getDimensions()
140    {
141        LispObject result = NIL;
142        for (int i = dimv.length; i-- > 0;)
143            result = new Cons(Fixnum.getInstance(dimv[i]), result);
144        return result;
145    }
146
147    @Override
148    public int getDimension(int n)
149    {
150        try {
151            return dimv[n];
152        }
153        catch (ArrayIndexOutOfBoundsException e) {
154            error(new TypeError("Bad array dimension " + n + "."));
155            return -1;
156        }
157    }
158
159    @Override
160    public LispObject getElementType()
161    {
162        return UNSIGNED_BYTE_8;
163    }
164
165    @Override
166    public int getTotalSize()
167    {
168        return totalSize;
169    }
170
171    @Override
172    public LispObject arrayDisplacement()
173    {
174        LispObject value1, value2;
175        if (array != null) {
176            value1 = array;
177            value2 = Fixnum.getInstance(displacement);
178        } else {
179            value1 = NIL;
180            value2 = Fixnum.ZERO;
181        }
182        return LispThread.currentThread().setValues(value1, value2);
183    }
184
185    @Override
186    public LispObject AREF(int index)
187    {
188        if (data != null) {
189            try {
190                return coerceJavaByteToLispObject(data[index]);
191            }
192            catch (ArrayIndexOutOfBoundsException e) {
193                return error(new TypeError("Bad row major index " + index + "."));
194            }
195        } else
196            return array.AREF(index + displacement);
197    }
198
199    @Override
200    public void aset(int index, LispObject newValue)
201    {
202        if (data != null) {
203            try {
204                data[index] = coerceLispObjectToJavaByte(newValue);
205            }
206            catch (ArrayIndexOutOfBoundsException e) {
207                error(new TypeError("Bad row major index " + index + "."));
208            }
209        } else
210            array.aset(index + displacement, newValue);
211    }
212
213    @Override
214    public void fill(LispObject obj)
215    {
216        if (data != null) {
217            byte b = coerceLispObjectToJavaByte(obj);
218            for (int i = data.length; i-- > 0;)
219                data[i] = b;
220        } else {
221            for (int i = totalSize; i-- > 0;)
222                aset(i, obj);
223        }
224    }
225
226    @Override
227    public String writeToString()
228    {
229        if (Symbol.PRINT_READABLY.symbolValue() != NIL) {
230            error(new PrintNotReadable(list(Keyword.OBJECT, this)));
231            // Not reached.
232            return null;
233        }
234        return writeToString(dimv);
235    }
236
237
238    @Override
239    public AbstractArray adjustArray(int[] dims,
240                                              LispObject initialElement,
241                                              LispObject initialContents)
242            {
243        if (isAdjustable()) {
244            if (initialContents != null)
245                setInitialContents(0, dims, initialContents, 0);
246            else {
247                //### FIXME Take the easy way out: we don't want to reorganize
248                // all of the array code yet
249                SimpleArray_UnsignedByte8 tempArray = new SimpleArray_UnsignedByte8(dims);
250                if (initialElement != null)
251                    tempArray.fill(initialElement);
252                SimpleArray_UnsignedByte8.copyArray(this, tempArray);
253                this.data = tempArray.data;
254
255                for (int i = 0; i < dims.length; i++)
256                    dimv[i] = dims[i];
257            }
258            return this;
259        } else {
260            if (initialContents != null)
261                return new ComplexArray_UnsignedByte8(dims, initialContents);
262            else {
263                ComplexArray_UnsignedByte8 newArray = new ComplexArray_UnsignedByte8(dims);
264                if (initialElement != null)
265                    newArray.fill(initialElement);
266                return newArray;
267            }
268        }
269    }
270
271    @Override
272    public AbstractArray adjustArray(int[] dims,
273                                              AbstractArray displacedTo,
274                                              int displacement)
275            {
276        if (isAdjustable()) {
277            for (int i = 0; i < dims.length; i++)
278                dimv[i] = dims[i];
279
280            this.data = null;
281            this.array = displacedTo;
282            this.displacement = displacement;
283            this.totalSize = computeTotalSize(dims);
284
285            return this;
286        } else {
287            ComplexArray_UnsignedByte8 a = new ComplexArray_UnsignedByte8(dims, displacedTo, displacement);
288
289            return a;
290        }
291    }
292}
Note: See TracBrowser for help on using the repository browser.