source: branches/0.17.x/abcl/src/org/armedbear/lisp/adjust_array.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: 5.5 KB
Line 
1/*
2 * adjust_array.java
3 *
4 * Copyright (C) 2004-2007 Peter Graves
5 * $Id: adjust_array.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
36// ### %adjust-array array new-dimensions element-type initial-element
37// initial-element-p initial-contents initial-contents-p fill-pointer
38// displaced-to displaced-index-offset => new-array
39public final class adjust_array extends Primitive
40{
41    public adjust_array()
42    {
43        super("%adjust-array", PACKAGE_SYS, false);
44    }
45
46    @Override
47    public LispObject execute(LispObject[] args)
48    {
49        if (args.length != 10)
50            return error(new WrongNumberOfArgumentsException(this));
51        AbstractArray array = checkArray(args[0]);
52        LispObject dimensions = args[1];
53        LispObject elementType = args[2];
54        boolean initialElementProvided = args[4] != NIL;
55        boolean initialContentsProvided = args[6] != NIL;
56        LispObject initialElement = initialElementProvided ? args[3] : null;
57        LispObject initialContents = initialContentsProvided ? args[5] : null;
58        LispObject fillPointer = args[7];
59        LispObject displacedTo = args[8];
60        LispObject displacedIndexOffset = args[9];
61        if (initialElementProvided && initialContentsProvided) {
62            return error(new LispError("ADJUST-ARRAY: cannot specify both initial element and initial contents."));
63        }
64        if (elementType != array.getElementType() &&
65            getUpgradedArrayElementType(elementType) != array.getElementType())
66        {
67            return error(new LispError("ADJUST-ARRAY: incompatible element type."));
68        }
69        if (array.getRank() == 0) {
70            return array.adjustArray(new int[0], initialElement, initialContents);
71        }
72        if (!initialElementProvided && array.getElementType() == T)
73            initialElement = Fixnum.ZERO;
74        if (array.getRank() == 1) {
75            final int newSize;
76            if (dimensions instanceof Cons && dimensions.length() == 1)
77                newSize = Fixnum.getValue(dimensions.car());
78            else
79                newSize = Fixnum.getValue(dimensions);
80            if (array instanceof AbstractVector) {
81                AbstractVector v = (AbstractVector) array;
82                AbstractArray v2;
83                if (displacedTo != NIL) {
84                    final int displacement;
85                    if (displacedIndexOffset == NIL)
86                        displacement = 0;
87                    else
88                        displacement = Fixnum.getValue(displacedIndexOffset);
89                    v2 = v.adjustArray(newSize,
90                                        checkArray(displacedTo),
91                                        displacement);
92                } else {
93                    v2 = v.adjustArray(newSize,
94                                        initialElement,
95                                        initialContents);
96                }
97                if (fillPointer != NIL)
98                    v2.setFillPointer(fillPointer);
99                return v2;
100            }
101        }
102        // rank > 1
103        final int rank = dimensions.listp() ? dimensions.length() : 1;
104        int[] dimv = new int[rank];
105        if (dimensions.listp()) {
106            for (int i = 0; i < rank; i++) {
107                LispObject dim = dimensions.car();
108                dimv[i] = Fixnum.getValue(dim);
109                dimensions = dimensions.cdr();
110            }
111        } else
112            dimv[0] = Fixnum.getValue(dimensions);
113
114        if (displacedTo != NIL) {
115            final int displacement;
116            if (displacedIndexOffset == NIL)
117                displacement = 0;
118            else
119                displacement = Fixnum.getValue(displacedIndexOffset);
120            return array.adjustArray(dimv,
121                                     checkArray(displacedTo),
122                                     displacement);
123        } else {
124            return array.adjustArray(dimv,
125                                     initialElement,
126                                     initialContents);
127        }
128    }
129
130    private static final Primitive _ADJUST_ARRAY = new adjust_array();
131}
Note: See TracBrowser for help on using the repository browser.