source: trunk/abcl/src/org/armedbear/lisp/ComplexArray_UnsignedByte8.java @ 12517

Last change on this file since 12517 was 12288, checked in by vvoutilainen, 15 years ago

Don't extend Lisp in LispObject, static import Lisp wherever
necessary. Patch by Douglas R. Miles.

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