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