8 |
|
import java.io.Serializable; |
9 |
|
import java.util.function.Consumer; |
10 |
|
import java.util.function.Predicate; |
11 |
< |
import java.util.function.UnaryOperator; |
11 |
> |
// OPENJDK import jdk.internal.access.SharedSecrets; |
12 |
|
|
13 |
|
/** |
14 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
50 |
|
* Iterator} interfaces. |
51 |
|
* |
52 |
|
* <p>This class is a member of the |
53 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
53 |
> |
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> |
54 |
|
* Java Collections Framework</a>. |
55 |
|
* |
56 |
|
* @author Josh Bloch and Doug Lea |
117 |
|
if (jump < needed |
118 |
|
|| (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0) |
119 |
|
newCapacity = newCapacity(needed, jump); |
120 |
< |
elements = Arrays.copyOf(elements, newCapacity); |
120 |
> |
final Object[] es = elements = Arrays.copyOf(elements, newCapacity); |
121 |
|
// Exceptionally, here tail == head needs to be disambiguated |
122 |
< |
if (tail < head || (tail == head && elements[head] != null)) { |
122 |
> |
if (tail < head || (tail == head && es[head] != null)) { |
123 |
|
// wrap around; slide first leg forward to end of array |
124 |
|
int newSpace = newCapacity - oldCapacity; |
125 |
< |
System.arraycopy(elements, head, |
126 |
< |
elements, head + newSpace, |
125 |
> |
System.arraycopy(es, head, |
126 |
> |
es, head + newSpace, |
127 |
|
oldCapacity - head); |
128 |
< |
Arrays.fill(elements, head, head + newSpace, null); |
129 |
< |
head += newSpace; |
128 |
> |
for (int i = head, to = (head += newSpace); i < to; i++) |
129 |
> |
es[i] = null; |
130 |
|
} |
131 |
|
// checkInvariants(); |
132 |
|
} |
180 |
|
* sufficient to hold 16 elements. |
181 |
|
*/ |
182 |
|
public ArrayDeque() { |
183 |
< |
elements = new Object[16]; |
183 |
> |
elements = new Object[16 + 1]; |
184 |
|
} |
185 |
|
|
186 |
|
/** |
208 |
|
*/ |
209 |
|
public ArrayDeque(Collection<? extends E> c) { |
210 |
|
this(c.size()); |
211 |
< |
addAll(c); |
211 |
> |
copyElements(c); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
< |
* Increments i, mod modulus. |
215 |
> |
* Circularly increments i, mod modulus. |
216 |
|
* Precondition and postcondition: 0 <= i < modulus. |
217 |
|
*/ |
218 |
|
static final int inc(int i, int modulus) { |
221 |
|
} |
222 |
|
|
223 |
|
/** |
224 |
< |
* Decrements i, mod modulus. |
224 |
> |
* Circularly decrements i, mod modulus. |
225 |
|
* Precondition and postcondition: 0 <= i < modulus. |
226 |
|
*/ |
227 |
|
static final int dec(int i, int modulus) { |
234 |
|
* Precondition: 0 <= i < modulus, 0 <= distance <= modulus. |
235 |
|
* @return index 0 <= i < modulus |
236 |
|
*/ |
237 |
< |
static final int add(int i, int distance, int modulus) { |
238 |
< |
if ((i += distance) - modulus >= 0) distance -= modulus; |
237 |
> |
static final int inc(int i, int distance, int modulus) { |
238 |
> |
if ((i += distance) - modulus >= 0) i -= modulus; |
239 |
|
return i; |
240 |
|
} |
241 |
|
|
244 |
|
* Index i must be logically ahead of index j. |
245 |
|
* Precondition: 0 <= i < modulus, 0 <= j < modulus. |
246 |
|
* @return the "circular distance" from j to i; corner case i == j |
247 |
< |
* is diambiguated to "empty", returning 0. |
247 |
> |
* is disambiguated to "empty", returning 0. |
248 |
|
*/ |
249 |
|
static final int sub(int i, int j, int modulus) { |
250 |
|
if ((i -= j) < 0) i += modulus; |
313 |
|
/** |
314 |
|
* Adds all of the elements in the specified collection at the end |
315 |
|
* of this deque, as if by calling {@link #addLast} on each one, |
316 |
< |
* in the order that they are returned by the collection's |
317 |
< |
* iterator. |
316 |
> |
* in the order that they are returned by the collection's iterator. |
317 |
|
* |
318 |
|
* @param c the elements to be inserted into this deque |
319 |
|
* @return {@code true} if this deque changed as a result of the call |
324 |
|
final int s, needed; |
325 |
|
if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0) |
326 |
|
grow(needed); |
327 |
< |
c.forEach(this::addLast); |
327 |
> |
copyElements(c); |
328 |
|
// checkInvariants(); |
329 |
|
return size() > s; |
330 |
|
} |
331 |
|
|
332 |
+ |
private void copyElements(Collection<? extends E> c) { |
333 |
+ |
c.forEach(this::addLast); |
334 |
+ |
} |
335 |
+ |
|
336 |
|
/** |
337 |
|
* Inserts the specified element at the front of this deque. |
338 |
|
* |
523 |
|
/** |
524 |
|
* Retrieves and removes the head of the queue represented by this deque. |
525 |
|
* |
526 |
< |
* This method differs from {@link #poll poll} only in that it throws an |
527 |
< |
* exception if this deque is empty. |
526 |
> |
* This method differs from {@link #poll() poll()} only in that it |
527 |
> |
* throws an exception if this deque is empty. |
528 |
|
* |
529 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
530 |
|
* |
713 |
|
throw new NoSuchElementException(); |
714 |
|
final Object[] es = elements; |
715 |
|
E e = nonNullElementAt(es, cursor); |
716 |
< |
lastRet = cursor; |
714 |
< |
cursor = inc(cursor, es.length); |
716 |
> |
cursor = inc(lastRet = cursor, es.length); |
717 |
|
remaining--; |
718 |
|
return e; |
719 |
|
} |
761 |
|
throw new NoSuchElementException(); |
762 |
|
final Object[] es = elements; |
763 |
|
E e = nonNullElementAt(es, cursor); |
764 |
< |
lastRet = cursor; |
763 |
< |
cursor = dec(cursor, es.length); |
764 |
> |
cursor = dec(lastRet = cursor, es.length); |
765 |
|
remaining--; |
766 |
|
return e; |
767 |
|
} |
823 |
|
|
824 |
|
/** Constructs spliterator over the given range. */ |
825 |
|
DeqSpliterator(int origin, int fence) { |
826 |
+ |
// assert 0 <= origin && origin < elements.length; |
827 |
+ |
// assert 0 <= fence && fence < elements.length; |
828 |
|
this.cursor = origin; |
829 |
|
this.fence = fence; |
830 |
|
} |
844 |
|
final int i, n; |
845 |
|
return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0) |
846 |
|
? null |
847 |
< |
: new DeqSpliterator(i, cursor = add(i, n, es.length)); |
847 |
> |
: new DeqSpliterator(i, cursor = inc(i, n, es.length)); |
848 |
|
} |
849 |
|
|
850 |
|
public void forEachRemaining(Consumer<? super E> action) { |
867 |
|
} |
868 |
|
|
869 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
870 |
< |
if (action == null) |
868 |
< |
throw new NullPointerException(); |
869 |
< |
final int t, i; |
870 |
< |
if ((t = getFence()) == (i = cursor)) |
871 |
< |
return false; |
870 |
> |
Objects.requireNonNull(action); |
871 |
|
final Object[] es = elements; |
872 |
+ |
if (fence < 0) { fence = tail; cursor = head; } // late-binding |
873 |
+ |
final int i; |
874 |
+ |
if ((i = cursor) == fence) |
875 |
+ |
return false; |
876 |
+ |
E e = nonNullElementAt(es, i); |
877 |
|
cursor = inc(i, es.length); |
878 |
< |
action.accept(nonNullElementAt(es, i)); |
878 |
> |
action.accept(e); |
879 |
|
return true; |
880 |
|
} |
881 |
|
|
891 |
|
} |
892 |
|
} |
893 |
|
|
894 |
+ |
/** |
895 |
+ |
* @throws NullPointerException {@inheritDoc} |
896 |
+ |
*/ |
897 |
|
public void forEach(Consumer<? super E> action) { |
898 |
|
Objects.requireNonNull(action); |
899 |
|
final Object[] es = elements; |
916 |
|
* @param operator the operator to apply to each element |
917 |
|
* @since TBD |
918 |
|
*/ |
919 |
< |
/* public */ void replaceAll(UnaryOperator<E> operator) { |
919 |
> |
/* public */ void replaceAll(java.util.function.UnaryOperator<E> operator) { |
920 |
|
Objects.requireNonNull(operator); |
921 |
|
final Object[] es = elements; |
922 |
|
for (int i = head, end = tail, to = (i <= end) ? end : es.length; |
1082 |
|
|
1083 |
|
/** |
1084 |
|
* Nulls out slots starting at array index i, upto index end. |
1085 |
+ |
* Condition i == end means "empty" - nothing to do. |
1086 |
|
*/ |
1087 |
|
private static void circularClear(Object[] es, int i, int end) { |
1088 |
+ |
// assert 0 <= i && i < es.length; |
1089 |
+ |
// assert 0 <= end && end < es.length; |
1090 |
|
for (int to = (i <= end) ? end : es.length; |
1091 |
|
; i = 0, to = end) { |
1092 |
< |
Arrays.fill(es, i, to, null); |
1092 |
> |
for (; i < to; i++) es[i] = null; |
1093 |
|
if (to == end) break; |
1094 |
|
} |
1095 |
|
} |
1114 |
|
private <T> T[] toArray(Class<T[]> klazz) { |
1115 |
|
final Object[] es = elements; |
1116 |
|
final T[] a; |
1117 |
< |
final int size = size(), head = this.head, end; |
1118 |
< |
final int len = Math.min(size, es.length - head); |
1119 |
< |
if ((end = head + size) >= 0) { |
1117 |
> |
final int head = this.head, tail = this.tail, end; |
1118 |
> |
if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) { |
1119 |
> |
// Uses null extension feature of copyOfRange |
1120 |
|
a = Arrays.copyOfRange(es, head, end, klazz); |
1121 |
|
} else { |
1122 |
|
// integer overflow! |
1123 |
< |
a = Arrays.copyOfRange(es, 0, size, klazz); |
1124 |
< |
System.arraycopy(es, head, a, 0, len); |
1123 |
> |
a = Arrays.copyOfRange(es, 0, end - head, klazz); |
1124 |
> |
System.arraycopy(es, head, a, 0, es.length - head); |
1125 |
|
} |
1126 |
< |
if (tail < head) |
1127 |
< |
System.arraycopy(es, 0, a, len, tail); |
1126 |
> |
if (end != tail) |
1127 |
> |
System.arraycopy(es, 0, a, es.length - head, tail); |
1128 |
|
return a; |
1129 |
|
} |
1130 |
|
|
1198 |
|
} |
1199 |
|
} |
1200 |
|
|
1201 |
+ |
// OPENJDK @java.io.Serial |
1202 |
|
private static final long serialVersionUID = 2340985798034038923L; |
1203 |
|
|
1204 |
|
/** |
1210 |
|
* followed by all of its elements (each an object reference) in |
1211 |
|
* first-to-last order. |
1212 |
|
*/ |
1213 |
+ |
// OPENJDK @java.io.Serial |
1214 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1215 |
|
throws java.io.IOException { |
1216 |
|
s.defaultWriteObject(); |
1235 |
|
* could not be found |
1236 |
|
* @throws java.io.IOException if an I/O error occurs |
1237 |
|
*/ |
1238 |
+ |
// OPENJDK @java.io.Serial |
1239 |
|
private void readObject(java.io.ObjectInputStream s) |
1240 |
|
throws java.io.IOException, ClassNotFoundException { |
1241 |
|
s.defaultReadObject(); |
1242 |
|
|
1243 |
|
// Read in size and allocate array |
1244 |
|
int size = s.readInt(); |
1245 |
+ |
jsr166.Platform.checkArray(s, Object[].class, size + 1); |
1246 |
|
elements = new Object[size + 1]; |
1247 |
|
this.tail = size; |
1248 |
|
|
1257 |
|
// head == tail disambiguates to "empty". |
1258 |
|
try { |
1259 |
|
int capacity = elements.length; |
1260 |
< |
// assert head >= 0 && head < capacity; |
1261 |
< |
// assert tail >= 0 && tail < capacity; |
1260 |
> |
// assert 0 <= head && head < capacity; |
1261 |
> |
// assert 0 <= tail && tail < capacity; |
1262 |
|
// assert capacity > 0; |
1263 |
|
// assert size() < capacity; |
1264 |
|
// assert head == tail || elements[head] != null; |