KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > adjust_array


1 /*
2  * adjust_array.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: adjust_array.java,v 1.12 2004/02/26 19:51:04 piso Exp $
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
22 package org.armedbear.lisp;
23
24 // ### %adjust-array array new-dimensions element-type initial-element
25
// initial-element-p initial-contents initial-contents-p fill-pointer
26
// displaced-to displaced-index-offset => new-array
27
public final class adjust_array extends Primitive
28 {
29     public adjust_array()
30     {
31         super("%adjust-array", PACKAGE_SYS, false);
32     }
33
34     public LispObject execute(LispObject[] args) throws ConditionThrowable
35     {
36         if (args.length != 10)
37             return signal(new WrongNumberOfArgumentsException(this));
38         AbstractArray array = checkArray(args[0]);
39         LispObject dimensions = args[1];
40         LispObject elementType = args[2];
41         LispObject initialElement = args[3];
42         LispObject initialElementProvided = args[4];
43         LispObject initialContents = args[5];
44         LispObject initialContentsProvided = args[6];
45         LispObject fillPointer = args[7];
46         LispObject displacedTo = args[8];
47         LispObject displacedIndexOffset = args[9];
48         if (initialElementProvided != NIL && initialContents != NIL) {
49             return signal(new LispError("ADJUST-ARRAY: cannot specify both initial element and initial contents."));
50         }
51         if (elementType != array.getElementType() &&
52             getUpgradedArrayElementType(elementType) != array.getElementType())
53         {
54             return signal(new LispError("ADJUST-ARRAY: incompatible element type."));
55         }
56         if (array.getRank() == 0) {
57             if (initialContentsProvided != NIL)
58                 array.setRowMajor(0, initialContents);
59             return array;
60         }
61         if (array.getRank() == 1) {
62             final int newSize;
63             if (dimensions instanceof Cons && dimensions.length() == 1)
64                 newSize = Fixnum.getValue(dimensions.car());
65             else
66                 newSize = Fixnum.getValue(dimensions);
67             if (array instanceof AbstractVector) {
68                 AbstractVector v = (AbstractVector) array;
69                 AbstractVector v2;
70                 if (displacedTo != NIL) {
71                     final int displacement;
72                     if (displacedIndexOffset == NIL)
73                         displacement = 0;
74                     else
75                         displacement = Fixnum.getValue(displacedIndexOffset);
76                     v2 = v.adjustVector(newSize,
77                                         checkArray(displacedTo),
78                                         displacement);
79                 } else {
80                     v2 = v.adjustVector(newSize,
81                                         initialElement,
82                                         initialContents);
83                 }
84                 if (fillPointer != NIL)
85                     v2.setFillPointer(fillPointer);
86                 return v2;
87             }
88         }
89         // rank > 1
90
final int rank = dimensions.listp() ? dimensions.length() : 1;
91         int[] dimv = new int[rank];
92         if (dimensions.listp()) {
93             for (int i = 0; i < rank; i++) {
94                 LispObject dim = dimensions.car();
95                 dimv[i] = Fixnum.getValue(dim);
96                 dimensions = dimensions.cdr();
97             }
98         } else
99             dimv[0] = Fixnum.getValue(dimensions);
100         if (array instanceof SimpleArray) {
101             SimpleArray a = (SimpleArray) array;
102             if (displacedTo != NIL) {
103                 final int displacement;
104                 if (displacedIndexOffset == NIL)
105                     displacement = 0;
106                 else
107                     displacement = Fixnum.getValue(displacedIndexOffset);
108                 return a.adjustArray(dimv,
109                                      checkArray(displacedTo),
110                                      displacement);
111             } else {
112                 return a.adjustArray(dimv,
113                                      initialElement,
114                                      initialContents);
115             }
116         }
117         return signal(new LispError("ADJUST-ARRAY: unsupported case."));
118     }
119
120     private static final Primitive _ADJUST_ARRAY = new adjust_array();
121 }
122
Popular Tags