NumberArrayData.java revision 1073:06c06c8443fd
1227825Stheraven/*
2227825Stheraven * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3227825Stheraven * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4227825Stheraven *
5227825Stheraven * This code is free software; you can redistribute it and/or modify it
6227825Stheraven * under the terms of the GNU General Public License version 2 only, as
7227825Stheraven * published by the Free Software Foundation.  Oracle designates this
8227825Stheraven * particular file as subject to the "Classpath" exception as provided
9227825Stheraven * by Oracle in the LICENSE file that accompanied this code.
10227825Stheraven *
11227825Stheraven * This code is distributed in the hope that it will be useful, but WITHOUT
12227825Stheraven * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13227825Stheraven * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14227825Stheraven * version 2 for more details (a copy is included in the LICENSE file that
15227825Stheraven * accompanied this code).
16227825Stheraven *
17227825Stheraven * You should have received a copy of the GNU General Public License version
18227825Stheraven * 2 along with this work; if not, write to the Free Software Foundation,
19227825Stheraven * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20227825Stheraven *
21227825Stheraven * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22227825Stheraven * or visit www.oracle.com if you need additional information or have any
23227825Stheraven * questions.
24227825Stheraven */
25227825Stheraven
26227825Stheravenpackage jdk.nashorn.internal.runtime.arrays;
27227825Stheraven
28227825Stheravenimport static jdk.nashorn.internal.codegen.CompilerConstants.specialCall;
29227825Stheravenimport static jdk.nashorn.internal.lookup.Lookup.MH;
30227825Stheravenimport static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31227825Stheraven
32227825Stheravenimport java.lang.invoke.MethodHandle;
33227825Stheravenimport java.lang.invoke.MethodHandles;
34227825Stheravenimport java.util.Arrays;
35227825Stheraven
36227825Stheraven/**
37227825Stheraven * Implementation of {@link ArrayData} as soon as a double has been
38227825Stheraven * written to the array
39227825Stheraven */
40227825Stheravenfinal class NumberArrayData extends ContinuousArrayData implements NumericElements {
41227825Stheraven    /**
42227825Stheraven     * The wrapped array
43227825Stheraven     */
44227825Stheraven    private double[] array;
45227825Stheraven
46227825Stheraven    /**
47227825Stheraven     * Constructor
48227825Stheraven     * @param array an int array
49227825Stheraven     * @param length a length, not necessarily array.length
50227825Stheraven     */
51227825Stheraven    NumberArrayData(final double[] array, final int length) {
52227825Stheraven        super(length);
53227825Stheraven        assert array.length >= length;
54227825Stheraven        this.array = array;
55227825Stheraven    }
56227825Stheraven
57227825Stheraven    @Override
58227825Stheraven    public final Class<?> getElementType() {
59227825Stheraven        return double.class;
60227825Stheraven    }
61227825Stheraven
62227825Stheraven    @Override
63227825Stheraven    public final Class<?> getBoxedElementType() {
64227825Stheraven        return Double.class;
65227825Stheraven    }
66227825Stheraven
67227825Stheraven    @Override
68227825Stheraven    public final int getElementWeight() {
69227825Stheraven        return 3;
70227825Stheraven    }
71227825Stheraven
72227825Stheraven    @Override
73227825Stheraven    public final ContinuousArrayData widest(final ContinuousArrayData otherData) {
74227825Stheraven        return otherData instanceof IntOrLongElements ? this : otherData;
75227825Stheraven    }
76227825Stheraven
77227825Stheraven    @Override
78227825Stheraven    public NumberArrayData copy() {
79227825Stheraven        return new NumberArrayData(array.clone(), (int)length);
80227825Stheraven    }
81227825Stheraven
82227825Stheraven    @Override
83227825Stheraven    public Object[] asObjectArray() {
84227825Stheraven        return toObjectArray(true);
85227825Stheraven    }
86227825Stheraven
87227825Stheraven    private Object[] toObjectArray(final boolean trim) {
88227825Stheraven        assert length <= array.length : "length exceeds internal array size";
89227825Stheraven        final Object[] oarray = new Object[trim ? (int)length : array.length];
90227825Stheraven
91227825Stheraven        for (int index = 0; index < length; index++) {
92227825Stheraven            oarray[index] = Double.valueOf(array[index]);
93227825Stheraven        }
94227825Stheraven        return oarray;
95227825Stheraven    }
96227825Stheraven
97227825Stheraven    @Override
98227825Stheraven    public Object asArrayOfType(final Class<?> componentType) {
99227825Stheraven        if(componentType == double.class) {
100227825Stheraven            return array.length == length ? array.clone() : Arrays.copyOf(array, (int)length);
101227825Stheraven        }
102227825Stheraven        return super.asArrayOfType(componentType);
103227825Stheraven    }
104227825Stheraven
105227825Stheraven    @Override
106227825Stheraven    public ContinuousArrayData convert(final Class<?> type) {
107227825Stheraven        if (type != Double.class && type != Integer.class && type != Long.class) {
108227825Stheraven            final int len = (int)length;
109227825Stheraven            return new ObjectArrayData(toObjectArray(false), len);
110227825Stheraven        }
111227825Stheraven        return this;
112227825Stheraven    }
113227825Stheraven
114227825Stheraven    @Override
115227825Stheraven    public void shiftLeft(final int by) {
116227825Stheraven        System.arraycopy(array, by, array, 0, array.length - by);
117227825Stheraven    }
118227825Stheraven
119227825Stheraven    @Override
120227825Stheraven    public ArrayData shiftRight(final int by) {
121227825Stheraven        final ArrayData newData = ensure(by + length - 1);
122227825Stheraven        if (newData != this) {
123227825Stheraven            newData.shiftRight(by);
124227825Stheraven            return newData;
125        }
126        System.arraycopy(array, 0, array, by, array.length - by);
127        return this;
128    }
129
130    @Override
131    public ArrayData ensure(final long safeIndex) {
132        if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) {
133            return new SparseArrayData(this, safeIndex + 1);
134        }
135        final int alen = array.length;
136        if (safeIndex >= alen) {
137            final int newLength = ArrayData.nextSize((int)safeIndex);
138            array = Arrays.copyOf(array, newLength); //todo fill with nan or never accessed?
139        }
140        setLength(safeIndex + 1);
141        return this;
142
143    }
144
145    @Override
146    public ArrayData shrink(final long newLength) {
147        Arrays.fill(array, (int)newLength, array.length, 0.0);
148        return this;
149    }
150
151    @Override
152    public ArrayData set(final int index, final Object value, final boolean strict) {
153        if (value instanceof Double || value instanceof Integer || value instanceof Long) {
154            return set(index, ((Number)value).doubleValue(), strict);
155        } else if (value == UNDEFINED) {
156            return new UndefinedArrayFilter(this).set(index, value, strict);
157        }
158
159        final ArrayData newData = convert(value == null ? Object.class : value.getClass());
160        return newData.set(index, value, strict);
161    }
162
163    @Override
164    public ArrayData set(final int index, final int value, final boolean strict) {
165        array[index] = value;
166        setLength(Math.max(index + 1, length));
167        return this;
168    }
169
170    @Override
171    public ArrayData set(final int index, final long value, final boolean strict) {
172        array[index] = value;
173        setLength(Math.max(index + 1, length));
174        return this;
175    }
176
177    @Override
178    public ArrayData set(final int index, final double value, final boolean strict) {
179        array[index] = value;
180        setLength(Math.max(index + 1, length));
181        return this;
182    }
183
184    private static final MethodHandle HAS_GET_ELEM = specialCall(MethodHandles.lookup(), NumberArrayData.class, "getElem", double.class, int.class).methodHandle();
185    private static final MethodHandle SET_ELEM     = specialCall(MethodHandles.lookup(), NumberArrayData.class, "setElem", void.class, int.class, double.class).methodHandle();
186
187    @SuppressWarnings("unused")
188    private double getElem(final int index) {
189        if (has(index)) {
190            return array[index];
191        }
192        throw new ClassCastException();
193    }
194
195    @SuppressWarnings("unused")
196    private void setElem(final int index, final double elem) {
197        if (hasRoomFor(index)) {
198            array[index] = elem;
199            return;
200        }
201        throw new ClassCastException();
202    }
203
204    @Override
205    public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
206        if (returnType == int.class || returnType == long.class) {
207            return null;
208        }
209        return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint);
210    }
211
212    @Override
213    public MethodHandle getElementSetter(final Class<?> elementType) {
214        return elementType.isPrimitive() ? getContinuousElementSetter(MH.asType(SET_ELEM, SET_ELEM.type().changeParameterType(2, elementType)), elementType) : null;
215    }
216
217    @Override
218    public int getInt(final int index) {
219        return (int)array[index];
220    }
221
222    @Override
223    public long getLong(final int index) {
224        return (long)array[index];
225    }
226
227    @Override
228    public double getDouble(final int index) {
229        return array[index];
230    }
231
232    @Override
233    public double getDoubleOptimistic(final int index, final int programPoint) {
234        return array[index];
235    }
236
237    @Override
238    public Object getObject(final int index) {
239        return array[index];
240    }
241
242    @Override
243    public boolean has(final int index) {
244        return 0 <= index && index < length;
245    }
246
247    @Override
248    public ArrayData delete(final int index) {
249        return new DeletedRangeArrayFilter(this, index, index);
250    }
251
252    @Override
253    public ArrayData delete(final long fromIndex, final long toIndex) {
254        return new DeletedRangeArrayFilter(this, fromIndex, toIndex);
255    }
256
257    @Override
258    public Object pop() {
259        if (length == 0) {
260            return UNDEFINED;
261        }
262
263        final int newLength = (int)length - 1;
264        final double elem = array[newLength];
265        array[newLength] = 0;
266        setLength(newLength);
267        return elem;
268    }
269
270    @Override
271    public ArrayData slice(final long from, final long to) {
272        final long start     = from < 0 ? from + length : from;
273        final long newLength = to - start;
274        return new NumberArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)newLength);
275    }
276
277    @Override
278    public final ArrayData push(final boolean strict, final double item) {
279        final long      len     = length;
280        final ArrayData newData = ensure(len);
281        if (newData == this) {
282            array[(int)len] = item;
283            return this;
284        }
285        return newData.set((int)len, item, strict);
286    }
287
288    @Override
289    public ArrayData fastSplice(final int start, final int removed, final int added) throws UnsupportedOperationException {
290        final long oldLength = length;
291        final long newLength = oldLength - removed + added;
292        if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) {
293            throw new UnsupportedOperationException();
294        }
295        final ArrayData returnValue = removed == 0 ?
296                EMPTY_ARRAY : new NumberArrayData(Arrays.copyOfRange(array, start, start + removed), removed);
297
298        if (newLength != oldLength) {
299            final double[] newArray;
300
301            if (newLength > array.length) {
302                newArray = new double[ArrayData.nextSize((int)newLength)];
303                System.arraycopy(array, 0, newArray, 0, start);
304            } else {
305                newArray = array;
306            }
307
308            System.arraycopy(array, start + removed, newArray, start + added, (int)(oldLength - start - removed));
309            array = newArray;
310            setLength(newLength);
311        }
312
313        return returnValue;
314    }
315
316    @Override
317    public long fastPush(final int arg) {
318        return fastPush((double)arg);
319    }
320
321    @Override
322    public long fastPush(final long arg) {
323        return fastPush((double)arg);
324    }
325
326    @Override
327    public long fastPush(final double arg) {
328        final int len = (int)length;
329        if (len == array.length) {
330           //note that fastpush never creates spares arrays, there is nothing to gain by that - it will just use even more memory
331           array = Arrays.copyOf(array, nextSize(len));
332        }
333        array[len] = arg;
334        return ++length;
335    }
336
337    @Override
338    public double fastPopDouble() {
339        if (length == 0) {
340            throw new ClassCastException();
341        }
342        final int newLength = (int)--length;
343        final double elem = array[newLength];
344        array[newLength] = 0;
345        return elem;
346    }
347
348    @Override
349    public Object fastPopObject() {
350        return fastPopDouble();
351    }
352
353    @Override
354    public ContinuousArrayData fastConcat(final ContinuousArrayData otherData) {
355        final int   otherLength = (int)otherData.length;
356        final int   thisLength  = (int)length;
357        assert otherLength > 0 && thisLength > 0;
358
359        final double[] otherArray = ((NumberArrayData)otherData).array;
360        final int      newLength  = otherLength + thisLength;
361        final double[] newArray   = new double[ArrayData.alignUp(newLength)];
362
363        System.arraycopy(array, 0, newArray, 0, thisLength);
364        System.arraycopy(otherArray, 0, newArray, thisLength, otherLength);
365
366        return new NumberArrayData(newArray, newLength);
367    }
368
369    @Override
370    public String toString() {
371        assert length <= array.length : length + " > " + array.length;
372        return getClass().getSimpleName() + ':' + Arrays.toString(Arrays.copyOf(array, (int)length));
373    }
374}
375