author | lagergren |
Fri, 26 Sep 2014 18:47:20 +0200 | |
changeset 26886 | 18c744ab4df2 |
parent 25865 | d38d876f1654 |
child 27525 | e7d3c00b9fb8 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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 |
||
26 |
package jdk.nashorn.internal.runtime.arrays; |
|
27 |
||
28 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
29 |
import java.lang.reflect.Array; |
|
30 |
import jdk.nashorn.internal.runtime.BitVector; |
|
31 |
||
32 |
/** |
|
33 |
* This filter handles the deletion of array elements. |
|
34 |
*/ |
|
35 |
final class DeletedArrayFilter extends ArrayFilter { |
|
36 |
/** Bit vector tracking deletions. */ |
|
37 |
private final BitVector deleted; |
|
38 |
||
39 |
DeletedArrayFilter(final ArrayData underlying) { |
|
40 |
super(underlying); |
|
41 |
||
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
42 |
this.deleted = new BitVector(underlying.length); |
16147 | 43 |
} |
44 |
||
45 |
@Override |
|
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
46 |
public ArrayData copy() { |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
19236
diff
changeset
|
47 |
final DeletedArrayFilter copy = new DeletedArrayFilter(underlying.copy()); |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
48 |
copy.getDeleted().copy(deleted); |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
49 |
return copy; |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
50 |
} |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
51 |
|
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18606
diff
changeset
|
52 |
@Override |
16147 | 53 |
public Object[] asObjectArray() { |
54 |
final Object[] value = super.asObjectArray(); |
|
55 |
||
56 |
for (int i = 0; i < value.length; i++) { |
|
57 |
if (deleted.isSet(i)) { |
|
58 |
value[i] = UNDEFINED; |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
return value; |
|
63 |
} |
|
64 |
||
65 |
@Override |
|
66 |
public Object asArrayOfType(final Class<?> componentType) { |
|
67 |
final Object value = super.asArrayOfType(componentType); |
|
68 |
final Object undefValue = convertUndefinedValue(componentType); |
|
69 |
final int l = Array.getLength(value); |
|
70 |
for (int i = 0; i < l; i++) { |
|
71 |
if (deleted.isSet(i)) { |
|
72 |
Array.set(value, i, undefValue); |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
return value; |
|
77 |
} |
|
78 |
||
79 |
@Override |
|
80 |
public void shiftLeft(final int by) { |
|
81 |
super.shiftLeft(by); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
82 |
deleted.shiftLeft(by, length); |
16147 | 83 |
} |
84 |
||
85 |
@Override |
|
86 |
public ArrayData shiftRight(final int by) { |
|
87 |
super.shiftRight(by); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
88 |
deleted.shiftRight(by, length); |
16147 | 89 |
|
90 |
return this; |
|
91 |
} |
|
92 |
||
93 |
@Override |
|
94 |
public ArrayData ensure(final long safeIndex) { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
95 |
if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH && safeIndex >= length) { |
16147 | 96 |
return new SparseArrayData(this, safeIndex + 1); |
97 |
} |
|
98 |
||
99 |
super.ensure(safeIndex); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
100 |
deleted.resize(length); |
16147 | 101 |
|
102 |
return this; |
|
103 |
} |
|
104 |
||
105 |
@Override |
|
106 |
public ArrayData shrink(final long newLength) { |
|
107 |
super.shrink(newLength); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
108 |
deleted.resize(length); |
16147 | 109 |
|
110 |
return this; |
|
111 |
} |
|
112 |
||
113 |
@Override |
|
114 |
public ArrayData set(final int index, final Object value, final boolean strict) { |
|
115 |
deleted.clear(ArrayIndex.toLongIndex(index)); |
|
116 |
||
117 |
return super.set(index, value, strict); |
|
118 |
} |
|
119 |
||
120 |
@Override |
|
121 |
public ArrayData set(final int index, final int value, final boolean strict) { |
|
122 |
deleted.clear(ArrayIndex.toLongIndex(index)); |
|
123 |
||
124 |
return super.set(index, value, strict); |
|
125 |
} |
|
126 |
||
127 |
@Override |
|
128 |
public ArrayData set(final int index, final long value, final boolean strict) { |
|
129 |
deleted.clear(ArrayIndex.toLongIndex(index)); |
|
130 |
||
131 |
return super.set(index, value, strict); |
|
132 |
} |
|
133 |
||
134 |
@Override |
|
135 |
public ArrayData set(final int index, final double value, final boolean strict) { |
|
136 |
deleted.clear(ArrayIndex.toLongIndex(index)); |
|
137 |
||
138 |
return super.set(index, value, strict); |
|
139 |
} |
|
140 |
||
141 |
@Override |
|
142 |
public boolean has(final int index) { |
|
143 |
return super.has(index) && deleted.isClear(ArrayIndex.toLongIndex(index)); |
|
144 |
} |
|
145 |
||
146 |
@Override |
|
147 |
public ArrayData delete(final int index) { |
|
148 |
final long longIndex = ArrayIndex.toLongIndex(index); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
149 |
assert longIndex >= 0 && longIndex < length; |
16147 | 150 |
deleted.set(longIndex); |
18606 | 151 |
underlying.setEmpty(index); |
16147 | 152 |
return this; |
153 |
} |
|
154 |
||
155 |
@Override |
|
156 |
public ArrayData delete(final long fromIndex, final long toIndex) { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
157 |
assert fromIndex >= 0 && fromIndex <= toIndex && toIndex < length; |
16147 | 158 |
deleted.setRange(fromIndex, toIndex + 1); |
18606 | 159 |
underlying.setEmpty(fromIndex, toIndex); |
16147 | 160 |
return this; |
161 |
} |
|
162 |
||
163 |
@Override |
|
164 |
public Object pop() { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
165 |
final long index = length - 1; |
16147 | 166 |
|
167 |
if (super.has((int)index)) { |
|
168 |
final boolean isDeleted = deleted.isSet(index); |
|
169 |
final Object value = super.pop(); |
|
170 |
||
171 |
return isDeleted ? UNDEFINED : value; |
|
172 |
} |
|
173 |
||
174 |
return super.pop(); |
|
175 |
} |
|
176 |
||
177 |
@Override |
|
178 |
public ArrayData slice(final long from, final long to) { |
|
179 |
final ArrayData newArray = underlying.slice(from, to); |
|
180 |
final DeletedArrayFilter newFilter = new DeletedArrayFilter(newArray); |
|
181 |
newFilter.getDeleted().copy(deleted); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
25865
diff
changeset
|
182 |
newFilter.getDeleted().shiftLeft(from, newFilter.length); |
16147 | 183 |
|
184 |
return newFilter; |
|
185 |
} |
|
186 |
||
187 |
private BitVector getDeleted() { |
|
188 |
return deleted; |
|
189 |
} |
|
190 |
} |