9 |
|
import java.util.function.Consumer; |
10 |
|
import java.util.function.Predicate; |
11 |
|
import java.util.function.UnaryOperator; |
12 |
+ |
import jdk.internal.misc.SharedSecrets; |
13 |
|
|
14 |
|
/** |
15 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
51 |
|
* Iterator} interfaces. |
52 |
|
* |
53 |
|
* <p>This class is a member of the |
54 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
54 |
> |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
55 |
|
* Java Collections Framework</a>. |
56 |
|
* |
57 |
|
* @author Josh Bloch and Doug Lea |
69 |
|
* |
70 |
|
* Because in a circular array, elements are in general stored in |
71 |
|
* two disjoint such slices, we help the VM by writing unusual |
72 |
< |
* nested loops for all traversals over the elements. |
72 |
> |
* nested loops for all traversals over the elements. Having only |
73 |
> |
* one hot inner loop body instead of two or three eases human |
74 |
> |
* maintenance and encourages VM loop inlining into the caller. |
75 |
|
*/ |
76 |
|
|
77 |
|
/** |
78 |
|
* The array in which the elements of the deque are stored. |
79 |
< |
* We guarantee that all array cells not holding deque elements |
80 |
< |
* are always null. |
79 |
> |
* All array cells not holding deque elements are always null. |
80 |
> |
* The array always has at least one null slot (at tail). |
81 |
|
*/ |
82 |
|
transient Object[] elements; |
83 |
|
|
91 |
|
|
92 |
|
/** |
93 |
|
* The index at which the next element would be added to the tail |
94 |
< |
* of the deque (via addLast(E), add(E), or push(E)). |
94 |
> |
* of the deque (via addLast(E), add(E), or push(E)); |
95 |
> |
* elements[tail] is always null. |
96 |
|
*/ |
97 |
|
transient int tail; |
98 |
|
|
118 |
|
if (jump < needed |
119 |
|
|| (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0) |
120 |
|
newCapacity = newCapacity(needed, jump); |
121 |
< |
elements = Arrays.copyOf(elements, newCapacity); |
121 |
> |
final Object[] es = elements = Arrays.copyOf(elements, newCapacity); |
122 |
|
// Exceptionally, here tail == head needs to be disambiguated |
123 |
< |
if (tail < head || (tail == head && elements[head] != null)) { |
123 |
> |
if (tail < head || (tail == head && es[head] != null)) { |
124 |
|
// wrap around; slide first leg forward to end of array |
125 |
|
int newSpace = newCapacity - oldCapacity; |
126 |
< |
System.arraycopy(elements, head, |
127 |
< |
elements, head + newSpace, |
126 |
> |
System.arraycopy(es, head, |
127 |
> |
es, head + newSpace, |
128 |
|
oldCapacity - head); |
129 |
< |
Arrays.fill(elements, head, head + newSpace, null); |
130 |
< |
head += newSpace; |
129 |
> |
for (int i = head, to = (head += newSpace); i < to; i++) |
130 |
> |
es[i] = null; |
131 |
|
} |
132 |
|
// checkInvariants(); |
133 |
|
} |
191 |
|
* @param numElements lower bound on initial capacity of the deque |
192 |
|
*/ |
193 |
|
public ArrayDeque(int numElements) { |
194 |
< |
elements = new Object[Math.max(1, numElements + 1)]; |
194 |
> |
elements = |
195 |
> |
new Object[(numElements < 1) ? 1 : |
196 |
> |
(numElements == Integer.MAX_VALUE) ? Integer.MAX_VALUE : |
197 |
> |
numElements + 1]; |
198 |
|
} |
199 |
|
|
200 |
|
/** |
208 |
|
* @throws NullPointerException if the specified collection is null |
209 |
|
*/ |
210 |
|
public ArrayDeque(Collection<? extends E> c) { |
211 |
< |
elements = new Object[c.size() + 1]; |
211 |
> |
this(c.size()); |
212 |
|
addAll(c); |
213 |
|
} |
214 |
|
|
215 |
|
/** |
216 |
< |
* Increments i, mod modulus. |
216 |
> |
* Circularly increments i, mod modulus. |
217 |
|
* Precondition and postcondition: 0 <= i < modulus. |
218 |
|
*/ |
219 |
|
static final int inc(int i, int modulus) { |
222 |
|
} |
223 |
|
|
224 |
|
/** |
225 |
< |
* Decrements i, mod modulus. |
225 |
> |
* Circularly decrements i, mod modulus. |
226 |
|
* Precondition and postcondition: 0 <= i < modulus. |
227 |
|
*/ |
228 |
|
static final int dec(int i, int modulus) { |
231 |
|
} |
232 |
|
|
233 |
|
/** |
234 |
< |
* Adds i and j, mod modulus. |
235 |
< |
* Precondition and postcondition: 0 <= i < modulus, 0 <= j <= modulus. |
234 |
> |
* Circularly adds the given distance to index i, mod modulus. |
235 |
> |
* Precondition: 0 <= i < modulus, 0 <= distance <= modulus. |
236 |
> |
* @return index 0 <= i < modulus |
237 |
|
*/ |
238 |
< |
static final int add(int i, int j, int modulus) { |
239 |
< |
if ((i += j) - modulus >= 0) i -= modulus; |
238 |
> |
static final int inc(int i, int distance, int modulus) { |
239 |
> |
if ((i += distance) - modulus >= 0) i -= modulus; |
240 |
|
return i; |
241 |
|
} |
242 |
|
|
243 |
|
/** |
244 |
|
* Subtracts j from i, mod modulus. |
245 |
< |
* Index i must be logically ahead of j. |
246 |
< |
* Returns the "circular distance" from j to i. |
247 |
< |
* Precondition and postcondition: 0 <= i < modulus, 0 <= j < modulus. |
245 |
> |
* Index i must be logically ahead of index j. |
246 |
> |
* Precondition: 0 <= i < modulus, 0 <= j < modulus. |
247 |
> |
* @return the "circular distance" from j to i; corner case i == j |
248 |
> |
* is disambiguated to "empty", returning 0. |
249 |
|
*/ |
250 |
|
static final int sub(int i, int j, int modulus) { |
251 |
|
if ((i -= j) < 0) i += modulus; |
314 |
|
/** |
315 |
|
* Adds all of the elements in the specified collection at the end |
316 |
|
* of this deque, as if by calling {@link #addLast} on each one, |
317 |
< |
* in the order that they are returned by the collection's |
309 |
< |
* iterator. |
317 |
> |
* in the order that they are returned by the collection's iterator. |
318 |
|
* |
319 |
|
* @param c the elements to be inserted into this deque |
320 |
|
* @return {@code true} if this deque changed as a result of the call |
322 |
|
* of its elements are null |
323 |
|
*/ |
324 |
|
public boolean addAll(Collection<? extends E> c) { |
325 |
< |
final int s = size(), needed; |
326 |
< |
if ((needed = s + c.size() - elements.length + 1) > 0) |
325 |
> |
final int s, needed; |
326 |
> |
if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0) |
327 |
|
grow(needed); |
328 |
< |
c.forEach((e) -> addLast(e)); |
328 |
> |
c.forEach(this::addLast); |
329 |
|
// checkInvariants(); |
330 |
|
return size() > s; |
331 |
|
} |
520 |
|
/** |
521 |
|
* Retrieves and removes the head of the queue represented by this deque. |
522 |
|
* |
523 |
< |
* This method differs from {@link #poll poll} only in that it throws an |
524 |
< |
* exception if this deque is empty. |
523 |
> |
* This method differs from {@link #poll() poll()} only in that it |
524 |
> |
* throws an exception if this deque is empty. |
525 |
|
* |
526 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
527 |
|
* |
616 |
|
// checkInvariants(); |
617 |
|
final Object[] es = elements; |
618 |
|
final int capacity = es.length; |
619 |
< |
final int h = head; |
619 |
> |
final int h, t; |
620 |
|
// number of elements before to-be-deleted elt |
621 |
< |
final int front = sub(i, h, capacity); |
622 |
< |
final int back = size() - front - 1; // number of elements after |
621 |
> |
final int front = sub(i, h = head, capacity); |
622 |
> |
// number of elements after to-be-deleted elt |
623 |
> |
final int back = sub(t = tail, i, capacity) - 1; |
624 |
|
if (front < back) { |
625 |
|
// move front elements forwards |
626 |
|
if (h <= i) { |
636 |
|
return false; |
637 |
|
} else { |
638 |
|
// move back elements backwards |
639 |
< |
tail = dec(tail, capacity); |
639 |
> |
tail = dec(t, capacity); |
640 |
|
if (i <= tail) { |
641 |
|
System.arraycopy(es, i + 1, es, i, back); |
642 |
|
} else { // Wrap around |
643 |
< |
int firstLeg = capacity - (i + 1); |
635 |
< |
System.arraycopy(es, i + 1, es, i, firstLeg); |
643 |
> |
System.arraycopy(es, i + 1, es, i, capacity - (i + 1)); |
644 |
|
es[capacity - 1] = es[0]; |
645 |
< |
System.arraycopy(es, 1, es, 0, back - firstLeg - 1); |
645 |
> |
System.arraycopy(es, 1, es, 0, t - 1); |
646 |
|
} |
647 |
|
es[tail] = null; |
648 |
|
// checkInvariants(); |
710 |
|
throw new NoSuchElementException(); |
711 |
|
final Object[] es = elements; |
712 |
|
E e = nonNullElementAt(es, cursor); |
713 |
< |
lastRet = cursor; |
706 |
< |
cursor = inc(cursor, es.length); |
713 |
> |
cursor = inc(lastRet = cursor, es.length); |
714 |
|
remaining--; |
715 |
|
return e; |
716 |
|
} |
758 |
|
throw new NoSuchElementException(); |
759 |
|
final Object[] es = elements; |
760 |
|
E e = nonNullElementAt(es, cursor); |
761 |
< |
lastRet = cursor; |
755 |
< |
cursor = dec(cursor, es.length); |
761 |
> |
cursor = dec(lastRet = cursor, es.length); |
762 |
|
remaining--; |
763 |
|
return e; |
764 |
|
} |
785 |
|
if (to == end) { |
786 |
|
if (end != head) |
787 |
|
throw new ConcurrentModificationException(); |
788 |
< |
lastRet = head; |
788 |
> |
lastRet = end; |
789 |
|
break; |
790 |
|
} |
791 |
|
} |
820 |
|
|
821 |
|
/** Constructs spliterator over the given range. */ |
822 |
|
DeqSpliterator(int origin, int fence) { |
823 |
+ |
// assert 0 <= origin && origin < elements.length; |
824 |
+ |
// assert 0 <= fence && fence < elements.length; |
825 |
|
this.cursor = origin; |
826 |
|
this.fence = fence; |
827 |
|
} |
841 |
|
final int i, n; |
842 |
|
return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0) |
843 |
|
? null |
844 |
< |
: new DeqSpliterator(i, cursor = add(i, n, es.length)); |
844 |
> |
: new DeqSpliterator(i, cursor = inc(i, n, es.length)); |
845 |
|
} |
846 |
|
|
847 |
|
public void forEachRemaining(Consumer<? super E> action) { |
864 |
|
} |
865 |
|
|
866 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
867 |
< |
if (action == null) |
868 |
< |
throw new NullPointerException(); |
869 |
< |
int t, i; |
870 |
< |
if ((t = fence) < 0) t = getFence(); |
871 |
< |
if (t == (i = cursor)) |
867 |
> |
Objects.requireNonNull(action); |
868 |
> |
final Object[] es = elements; |
869 |
> |
if (fence < 0) { fence = tail; cursor = head; } // late-binding |
870 |
> |
final int i; |
871 |
> |
if ((i = cursor) == fence) |
872 |
|
return false; |
873 |
< |
final Object[] es; |
866 |
< |
action.accept(nonNullElementAt(es = elements, i)); |
873 |
> |
E e = nonNullElementAt(es, i); |
874 |
|
cursor = inc(i, es.length); |
875 |
+ |
action.accept(e); |
876 |
|
return true; |
877 |
|
} |
878 |
|
|
888 |
|
} |
889 |
|
} |
890 |
|
|
891 |
+ |
/** |
892 |
+ |
* @throws NullPointerException {@inheritDoc} |
893 |
+ |
*/ |
894 |
|
public void forEach(Consumer<? super E> action) { |
895 |
|
Objects.requireNonNull(action); |
896 |
|
final Object[] es = elements; |
961 |
|
; i = 0, to = end) { |
962 |
|
for (; i < to; i++) |
963 |
|
if (filter.test(elementAt(es, i))) |
964 |
< |
return bulkRemoveModified(filter, i, to); |
964 |
> |
return bulkRemoveModified(filter, i); |
965 |
|
if (to == end) { |
966 |
|
if (end != tail) throw new ConcurrentModificationException(); |
967 |
|
break; |
970 |
|
return false; |
971 |
|
} |
972 |
|
|
973 |
+ |
// A tiny bit set implementation |
974 |
+ |
|
975 |
+ |
private static long[] nBits(int n) { |
976 |
+ |
return new long[((n - 1) >> 6) + 1]; |
977 |
+ |
} |
978 |
+ |
private static void setBit(long[] bits, int i) { |
979 |
+ |
bits[i >> 6] |= 1L << i; |
980 |
+ |
} |
981 |
+ |
private static boolean isClear(long[] bits, int i) { |
982 |
+ |
return (bits[i >> 6] & (1L << i)) == 0; |
983 |
+ |
} |
984 |
+ |
|
985 |
|
/** |
986 |
|
* Helper for bulkRemove, in case of at least one deletion. |
987 |
< |
* @param i valid index of first element to be deleted |
987 |
> |
* Tolerate predicates that reentrantly access the collection for |
988 |
> |
* read (but writers still get CME), so traverse once to find |
989 |
> |
* elements to delete, a second pass to physically expunge. |
990 |
> |
* |
991 |
> |
* @param beg valid index of first element to be deleted |
992 |
|
*/ |
993 |
|
private boolean bulkRemoveModified( |
994 |
< |
Predicate<? super E> filter, int i, int to) { |
994 |
> |
Predicate<? super E> filter, final int beg) { |
995 |
|
final Object[] es = elements; |
996 |
|
final int capacity = es.length; |
970 |
– |
// a two-finger algorithm, with hare i reading, tortoise j writing |
971 |
– |
int j = i++; |
997 |
|
final int end = tail; |
998 |
< |
try { |
999 |
< |
for (;; j = 0) { // j rejoins i on second leg |
1000 |
< |
E e; |
1001 |
< |
// In this loop, i and j are on the same leg, with i > j |
1002 |
< |
for (; i < to; i++) |
1003 |
< |
if (!filter.test(e = elementAt(es, i))) |
1004 |
< |
es[j++] = e; |
1005 |
< |
if (to == end) break; |
1006 |
< |
// In this loop, j is on the first leg, i on the second |
1007 |
< |
for (i = 0, to = end; i < to && j < capacity; i++) |
1008 |
< |
if (!filter.test(e = elementAt(es, i))) |
1009 |
< |
es[j++] = e; |
1010 |
< |
if (i >= to) { |
1011 |
< |
if (j == capacity) j = 0; // "corner" case |
1012 |
< |
break; |
1013 |
< |
} |
998 |
> |
final long[] deathRow = nBits(sub(end, beg, capacity)); |
999 |
> |
deathRow[0] = 1L; // set bit 0 |
1000 |
> |
for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg; |
1001 |
> |
; i = 0, to = end, k -= capacity) { |
1002 |
> |
for (; i < to; i++) |
1003 |
> |
if (filter.test(elementAt(es, i))) |
1004 |
> |
setBit(deathRow, i - k); |
1005 |
> |
if (to == end) break; |
1006 |
> |
} |
1007 |
> |
// a two-finger traversal, with hare i reading, tortoise w writing |
1008 |
> |
int w = beg; |
1009 |
> |
for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg; |
1010 |
> |
; w = 0) { // w rejoins i on second leg |
1011 |
> |
// In this loop, i and w are on the same leg, with i > w |
1012 |
> |
for (; i < to; i++) |
1013 |
> |
if (isClear(deathRow, i - k)) |
1014 |
> |
es[w++] = es[i]; |
1015 |
> |
if (to == end) break; |
1016 |
> |
// In this loop, w is on the first leg, i on the second |
1017 |
> |
for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++) |
1018 |
> |
if (isClear(deathRow, i - k)) |
1019 |
> |
es[w++] = es[i]; |
1020 |
> |
if (i >= to) { |
1021 |
> |
if (w == capacity) w = 0; // "corner" case |
1022 |
> |
break; |
1023 |
|
} |
990 |
– |
return true; |
991 |
– |
} catch (Throwable ex) { |
992 |
– |
// copy remaining elements |
993 |
– |
for (; i != end; i = inc(i, capacity), j = inc(j, capacity)) |
994 |
– |
es[j] = es[i]; |
995 |
– |
throw ex; |
996 |
– |
} finally { |
997 |
– |
if (end != tail) throw new ConcurrentModificationException(); |
998 |
– |
circularClear(es, tail = j, end); |
999 |
– |
// checkInvariants(); |
1024 |
|
} |
1025 |
+ |
if (end != tail) throw new ConcurrentModificationException(); |
1026 |
+ |
circularClear(es, tail = w, end); |
1027 |
+ |
// checkInvariants(); |
1028 |
+ |
return true; |
1029 |
|
} |
1030 |
|
|
1031 |
|
/** |
1079 |
|
|
1080 |
|
/** |
1081 |
|
* Nulls out slots starting at array index i, upto index end. |
1082 |
+ |
* Condition i == end means "empty" - nothing to do. |
1083 |
|
*/ |
1084 |
|
private static void circularClear(Object[] es, int i, int end) { |
1085 |
+ |
// assert 0 <= i && i < es.length; |
1086 |
+ |
// assert 0 <= end && end < es.length; |
1087 |
|
for (int to = (i <= end) ? end : es.length; |
1088 |
|
; i = 0, to = end) { |
1089 |
< |
Arrays.fill(es, i, to, null); |
1089 |
> |
for (; i < to; i++) es[i] = null; |
1090 |
|
if (to == end) break; |
1091 |
|
} |
1092 |
|
} |
1111 |
|
private <T> T[] toArray(Class<T[]> klazz) { |
1112 |
|
final Object[] es = elements; |
1113 |
|
final T[] a; |
1114 |
< |
final int size = size(), head = this.head, end; |
1115 |
< |
final int len = Math.min(size, es.length - head); |
1116 |
< |
if ((end = head + size) >= 0) { |
1114 |
> |
final int head = this.head, tail = this.tail, end; |
1115 |
> |
if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) { |
1116 |
> |
// Uses null extension feature of copyOfRange |
1117 |
|
a = Arrays.copyOfRange(es, head, end, klazz); |
1118 |
|
} else { |
1119 |
|
// integer overflow! |
1120 |
< |
a = Arrays.copyOfRange(es, 0, size, klazz); |
1121 |
< |
System.arraycopy(es, head, a, 0, len); |
1120 |
> |
a = Arrays.copyOfRange(es, 0, end - head, klazz); |
1121 |
> |
System.arraycopy(es, head, a, 0, es.length - head); |
1122 |
|
} |
1123 |
< |
if (tail < head) |
1124 |
< |
System.arraycopy(es, 0, a, len, tail); |
1123 |
> |
if (end != tail) |
1124 |
> |
System.arraycopy(es, 0, a, es.length - head, tail); |
1125 |
|
return a; |
1126 |
|
} |
1127 |
|
|
1236 |
|
|
1237 |
|
// Read in size and allocate array |
1238 |
|
int size = s.readInt(); |
1239 |
+ |
SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size + 1); |
1240 |
|
elements = new Object[size + 1]; |
1241 |
|
this.tail = size; |
1242 |
|
|
1247 |
|
|
1248 |
|
/** debugging */ |
1249 |
|
void checkInvariants() { |
1250 |
+ |
// Use head and tail fields with empty slot at tail strategy. |
1251 |
+ |
// head == tail disambiguates to "empty". |
1252 |
|
try { |
1253 |
|
int capacity = elements.length; |
1254 |
< |
// assert head >= 0 && head < capacity; |
1255 |
< |
// assert tail >= 0 && tail < capacity; |
1254 |
> |
// assert 0 <= head && head < capacity; |
1255 |
> |
// assert 0 <= tail && tail < capacity; |
1256 |
|
// assert capacity > 0; |
1257 |
|
// assert size() < capacity; |
1258 |
|
// assert head == tail || elements[head] != null; |