DeletedRangeArrayFilter.java revision 953:221a84ef44c0
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.internal.runtime.arrays;
27
28import java.lang.reflect.Array;
29import jdk.nashorn.internal.runtime.ScriptRuntime;
30
31/**
32 * This filter handles the deletion of array elements.
33 */
34final class DeletedRangeArrayFilter extends ArrayFilter {
35    /** Range (inclusive) tracking deletions */
36    private long lo, hi;
37
38    DeletedRangeArrayFilter(final ArrayData underlying, final long lo, final long hi) {
39        super(maybeSparse(underlying, hi));
40        this.lo = lo;
41        this.hi = hi;
42    }
43
44    private static ArrayData maybeSparse(final ArrayData underlying, final long hi) {
45        if(hi < SparseArrayData.MAX_DENSE_LENGTH || underlying instanceof SparseArrayData) {
46            return underlying;
47        }
48        return new SparseArrayData(underlying, underlying.length());
49    }
50
51    private boolean isEmpty() {
52        return lo > hi;
53    }
54
55    private boolean isDeleted(final int index) {
56        final long longIndex = ArrayIndex.toLongIndex(index);
57        return lo <= longIndex && longIndex <= hi;
58    }
59
60    @Override
61    public ArrayData copy() {
62        return new DeletedRangeArrayFilter(underlying.copy(), lo, hi);
63    }
64
65    @Override
66    public Object[] asObjectArray() {
67        final Object[] value = super.asObjectArray();
68
69        if (lo <= Integer.MAX_VALUE) {
70            final int intHi = (int)Math.min(hi, Integer.MAX_VALUE);
71            for (int i = (int)lo; i <= intHi; i++) {
72                value[i] = ScriptRuntime.UNDEFINED;
73            }
74        }
75
76        return value;
77    }
78
79    @Override
80    public Object asArrayOfType(final Class<?> componentType) {
81        final Object value = super.asArrayOfType(componentType);
82        final Object undefValue = convertUndefinedValue(componentType);
83
84        if (lo <= Integer.MAX_VALUE) {
85            final int intHi = (int)Math.min(hi, Integer.MAX_VALUE);
86            for (int i = (int)lo; i <= intHi; i++) {
87                Array.set(value, i, undefValue);
88            }
89        }
90
91        return value;
92    }
93
94    @Override
95    public ArrayData ensure(final long safeIndex) {
96        if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH && safeIndex >= length()) {
97            return new SparseArrayData(this, safeIndex + 1);
98        }
99
100        return super.ensure(safeIndex);
101    }
102
103    @Override
104    public void shiftLeft(final int by) {
105        super.shiftLeft(by);
106        lo = Math.max(0, lo - by);
107        hi = Math.max(-1, hi - by);
108    }
109
110    @Override
111    public ArrayData shiftRight(final int by) {
112        super.shiftRight(by);
113        lo = Math.min(length(), lo + by);
114        hi = Math.min(length() - 1, hi + by);
115
116        return isEmpty() ? getUnderlying() : this;
117    }
118
119    @Override
120    public ArrayData shrink(final long newLength) {
121        super.shrink(newLength);
122        lo = Math.min(newLength, lo);
123        hi = Math.min(newLength - 1, hi);
124
125        return isEmpty() ? getUnderlying() : this;
126    }
127
128    @Override
129    public ArrayData set(final int index, final Object value, final boolean strict) {
130        final long longIndex = ArrayIndex.toLongIndex(index);
131        if (longIndex < lo || longIndex > hi) {
132            return super.set(index, value, strict);
133        } else if (longIndex > lo && longIndex < hi) {
134            return getDeletedArrayFilter().set(index, value, strict);
135        }
136        if (longIndex == lo) {
137            lo++;
138        } else {
139            assert longIndex == hi;
140            hi--;
141        }
142
143        return isEmpty() ? getUnderlying().set(index, value, strict) : super.set(index, value, strict);
144    }
145
146    @Override
147    public ArrayData set(final int index, final int value, final boolean strict) {
148        final long longIndex = ArrayIndex.toLongIndex(index);
149        if (longIndex < lo || longIndex > hi) {
150            return super.set(index, value, strict);
151        } else if (longIndex > lo && longIndex < hi) {
152            return getDeletedArrayFilter().set(index, value, strict);
153        }
154        if (longIndex == lo) {
155            lo++;
156        } else {
157            assert longIndex == hi;
158            hi--;
159        }
160
161        return isEmpty() ? getUnderlying().set(index, value, strict) : super.set(index, value, strict);
162    }
163
164    @Override
165    public ArrayData set(final int index, final long value, final boolean strict) {
166        final long longIndex = ArrayIndex.toLongIndex(index);
167        if (longIndex < lo || longIndex > hi) {
168            return super.set(index, value, strict);
169        } else if (longIndex > lo && longIndex < hi) {
170            return getDeletedArrayFilter().set(index, value, strict);
171        }
172        if (longIndex == lo) {
173            lo++;
174        } else {
175            assert longIndex == hi;
176            hi--;
177        }
178
179        return isEmpty() ? getUnderlying().set(index, value, strict) : super.set(index, value, strict);
180    }
181
182    @Override
183    public ArrayData set(final int index, final double value, final boolean strict) {
184        final long longIndex = ArrayIndex.toLongIndex(index);
185        if (longIndex < lo || longIndex > hi) {
186            return super.set(index, value, strict);
187        } else if (longIndex > lo && longIndex < hi) {
188            return getDeletedArrayFilter().set(index, value, strict);
189        }
190        if (longIndex == lo) {
191            lo++;
192        } else {
193            assert longIndex == hi;
194            hi--;
195        }
196
197        return isEmpty() ? getUnderlying().set(index, value, strict) : super.set(index, value, strict);
198    }
199
200    @Override
201    public boolean has(final int index) {
202        return super.has(index) && !isDeleted(index);
203    }
204
205    private ArrayData getDeletedArrayFilter() {
206        final ArrayData deleteFilter = new DeletedArrayFilter(getUnderlying());
207        deleteFilter.delete(lo, hi);
208        return deleteFilter;
209    }
210
211    @Override
212    public ArrayData delete(final int index) {
213        final long longIndex = ArrayIndex.toLongIndex(index);
214        underlying.setEmpty(index);
215
216        if (longIndex + 1 == lo) {
217            lo = longIndex;
218        } else if (longIndex - 1 == hi) {
219            hi = longIndex;
220        } else if (longIndex < lo || hi < longIndex) {
221           return getDeletedArrayFilter().delete(index);
222        }
223
224        return this;
225    }
226
227    @Override
228    public ArrayData delete(final long fromIndex, final long toIndex) {
229        if (fromIndex > hi + 1  || toIndex < lo - 1) {
230            return getDeletedArrayFilter().delete(fromIndex, toIndex);
231        }
232        lo = Math.min(fromIndex, lo);
233        hi = Math.max(toIndex, hi);
234        underlying.setEmpty(lo, hi);
235        return this;
236    }
237
238    @Override
239    public Object pop() {
240        final int index = (int)(length() - 1);
241        if (super.has(index)) {
242            final boolean isDeleted = isDeleted(index);
243            final Object value      = super.pop();
244
245            lo = Math.min(index + 1, lo);
246            hi = Math.min(index, hi);
247            return isDeleted ? ScriptRuntime.UNDEFINED : value;
248        }
249
250        return super.pop();
251    }
252
253    @Override
254    public ArrayData slice(final long from, final long to) {
255        return new DeletedRangeArrayFilter(underlying.slice(from, to), Math.max(0, lo - from), Math.max(0, hi - from));
256    }
257}
258