4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
+ |
import java.io.Serializable; |
8 |
+ |
import java.util.function.Consumer; |
9 |
+ |
import java.util.stream.Stream; |
10 |
+ |
import java.util.stream.Streams; |
11 |
|
|
12 |
|
/** |
13 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
18 |
|
* {@link Stack} when used as a stack, and faster than {@link LinkedList} |
19 |
|
* when used as a queue. |
20 |
|
* |
21 |
< |
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
21 |
> |
* <p>Most {@code ArrayDeque} operations run in amortized constant time. |
22 |
|
* Exceptions include {@link #remove(Object) remove}, {@link |
23 |
|
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
24 |
|
* removeLastOccurrence}, {@link #contains contains}, {@link #iterator |
25 |
|
* iterator.remove()}, and the bulk operations, all of which run in linear |
26 |
|
* time. |
27 |
|
* |
28 |
< |
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
28 |
> |
* <p>The iterators returned by this class's {@code iterator} method are |
29 |
|
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
30 |
< |
* is created, in any way except through the iterator's own <tt>remove</tt> |
30 |
> |
* is created, in any way except through the iterator's own {@code remove} |
31 |
|
* method, the iterator will generally throw a {@link |
32 |
|
* ConcurrentModificationException}. Thus, in the face of concurrent |
33 |
|
* modification, the iterator fails quickly and cleanly, rather than risking |
37 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
38 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
39 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
40 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
40 |
> |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
41 |
|
* Therefore, it would be wrong to write a program that depended on this |
42 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
43 |
|
* should be used only to detect bugs.</i> |
55 |
|
* @param <E> the type of elements held in this collection |
56 |
|
*/ |
57 |
|
public class ArrayDeque<E> extends AbstractCollection<E> |
58 |
< |
implements Deque<E>, Cloneable, java.io.Serializable |
58 |
> |
implements Deque<E>, Cloneable, Serializable |
59 |
|
{ |
60 |
|
/** |
61 |
|
* The array in which the elements of the deque are stored. |
67 |
|
* other. We also guarantee that all array cells not holding |
68 |
|
* deque elements are always null. |
69 |
|
*/ |
70 |
< |
private transient Object[] elements; |
70 |
> |
transient Object[] elements; // non-private to simplify nested class access |
71 |
|
|
72 |
|
/** |
73 |
|
* The index of the element at the head of the deque (which is the |
74 |
|
* element that would be removed by remove() or pop()); or an |
75 |
|
* arbitrary number equal to tail if the deque is empty. |
76 |
|
*/ |
77 |
< |
private transient int head; |
77 |
> |
transient int head; |
78 |
|
|
79 |
|
/** |
80 |
|
* The index at which the next element would be added to the tail |
81 |
|
* of the deque (via addLast(E), add(E), or push(E)). |
82 |
|
*/ |
83 |
< |
private transient int tail; |
83 |
> |
transient int tail; |
84 |
|
|
85 |
|
/** |
86 |
|
* The minimum capacity that we'll use for a newly created deque. |
91 |
|
// ****** Array allocation and resizing utilities ****** |
92 |
|
|
93 |
|
/** |
94 |
< |
* Allocate empty array to hold the given number of elements. |
94 |
> |
* Allocates empty array to hold the given number of elements. |
95 |
|
* |
96 |
|
* @param numElements the number of elements to hold |
97 |
|
*/ |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* Double the capacity of this deque. Call only when full, i.e., |
118 |
> |
* Doubles the capacity of this deque. Call only when full, i.e., |
119 |
|
* when head and tail have wrapped around to become equal. |
120 |
|
*/ |
121 |
|
private void doubleCapacity() { |
135 |
|
} |
136 |
|
|
137 |
|
/** |
134 |
– |
* Copies the elements from our element array into the specified array, |
135 |
– |
* in order (from first to last element in the deque). It is assumed |
136 |
– |
* that the array is large enough to hold all elements in the deque. |
137 |
– |
* |
138 |
– |
* @return its argument |
139 |
– |
*/ |
140 |
– |
private <T> T[] copyElements(T[] a) { |
141 |
– |
if (head < tail) { |
142 |
– |
System.arraycopy(elements, head, a, 0, size()); |
143 |
– |
} else if (head > tail) { |
144 |
– |
int headPortionLen = elements.length - head; |
145 |
– |
System.arraycopy(elements, head, a, 0, headPortionLen); |
146 |
– |
System.arraycopy(elements, 0, a, headPortionLen, tail); |
147 |
– |
} |
148 |
– |
return a; |
149 |
– |
} |
150 |
– |
|
151 |
– |
/** |
138 |
|
* Constructs an empty array deque with an initial capacity |
139 |
|
* sufficient to hold 16 elements. |
140 |
|
*/ |
205 |
|
* Inserts the specified element at the front of this deque. |
206 |
|
* |
207 |
|
* @param e the element to add |
208 |
< |
* @return <tt>true</tt> (as specified by {@link Deque#offerFirst}) |
208 |
> |
* @return {@code true} (as specified by {@link Deque#offerFirst}) |
209 |
|
* @throws NullPointerException if the specified element is null |
210 |
|
*/ |
211 |
|
public boolean offerFirst(E e) { |
217 |
|
* Inserts the specified element at the end of this deque. |
218 |
|
* |
219 |
|
* @param e the element to add |
220 |
< |
* @return <tt>true</tt> (as specified by {@link Deque#offerLast}) |
220 |
> |
* @return {@code true} (as specified by {@link Deque#offerLast}) |
221 |
|
* @throws NullPointerException if the specified element is null |
222 |
|
*/ |
223 |
|
public boolean offerLast(E e) { |
305 |
|
* Removes the first occurrence of the specified element in this |
306 |
|
* deque (when traversing the deque from head to tail). |
307 |
|
* If the deque does not contain the element, it is unchanged. |
308 |
< |
* More formally, removes the first element <tt>e</tt> such that |
309 |
< |
* <tt>o.equals(e)</tt> (if such an element exists). |
310 |
< |
* Returns <tt>true</tt> if this deque contained the specified element |
308 |
> |
* More formally, removes the first element {@code e} such that |
309 |
> |
* {@code o.equals(e)} (if such an element exists). |
310 |
> |
* Returns {@code true} if this deque contained the specified element |
311 |
|
* (or equivalently, if this deque changed as a result of the call). |
312 |
|
* |
313 |
|
* @param o element to be removed from this deque, if present |
314 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
314 |
> |
* @return {@code true} if the deque contained the specified element |
315 |
|
*/ |
316 |
|
public boolean removeFirstOccurrence(Object o) { |
317 |
|
if (o == null) |
333 |
|
* Removes the last occurrence of the specified element in this |
334 |
|
* deque (when traversing the deque from head to tail). |
335 |
|
* If the deque does not contain the element, it is unchanged. |
336 |
< |
* More formally, removes the last element <tt>e</tt> such that |
337 |
< |
* <tt>o.equals(e)</tt> (if such an element exists). |
338 |
< |
* Returns <tt>true</tt> if this deque contained the specified element |
336 |
> |
* More formally, removes the last element {@code e} such that |
337 |
> |
* {@code o.equals(e)} (if such an element exists). |
338 |
> |
* Returns {@code true} if this deque contained the specified element |
339 |
|
* (or equivalently, if this deque changed as a result of the call). |
340 |
|
* |
341 |
|
* @param o element to be removed from this deque, if present |
342 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
342 |
> |
* @return {@code true} if the deque contained the specified element |
343 |
|
*/ |
344 |
|
public boolean removeLastOccurrence(Object o) { |
345 |
|
if (o == null) |
365 |
|
* <p>This method is equivalent to {@link #addLast}. |
366 |
|
* |
367 |
|
* @param e the element to add |
368 |
< |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
368 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
369 |
|
* @throws NullPointerException if the specified element is null |
370 |
|
*/ |
371 |
|
public boolean add(E e) { |
379 |
|
* <p>This method is equivalent to {@link #offerLast}. |
380 |
|
* |
381 |
|
* @param e the element to add |
382 |
< |
* @return <tt>true</tt> (as specified by {@link Queue#offer}) |
382 |
> |
* @return {@code true} (as specified by {@link Queue#offer}) |
383 |
|
* @throws NullPointerException if the specified element is null |
384 |
|
*/ |
385 |
|
public boolean offer(E e) { |
404 |
|
/** |
405 |
|
* Retrieves and removes the head of the queue represented by this deque |
406 |
|
* (in other words, the first element of this deque), or returns |
407 |
< |
* <tt>null</tt> if this deque is empty. |
407 |
> |
* {@code null} if this deque is empty. |
408 |
|
* |
409 |
|
* <p>This method is equivalent to {@link #pollFirst}. |
410 |
|
* |
411 |
|
* @return the head of the queue represented by this deque, or |
412 |
< |
* <tt>null</tt> if this deque is empty |
412 |
> |
* {@code null} if this deque is empty |
413 |
|
*/ |
414 |
|
public E poll() { |
415 |
|
return pollFirst(); |
431 |
|
|
432 |
|
/** |
433 |
|
* Retrieves, but does not remove, the head of the queue represented by |
434 |
< |
* this deque, or returns <tt>null</tt> if this deque is empty. |
434 |
> |
* this deque, or returns {@code null} if this deque is empty. |
435 |
|
* |
436 |
|
* <p>This method is equivalent to {@link #peekFirst}. |
437 |
|
* |
438 |
|
* @return the head of the queue represented by this deque, or |
439 |
< |
* <tt>null</tt> if this deque is empty |
439 |
> |
* {@code null} if this deque is empty |
440 |
|
*/ |
441 |
|
public E peek() { |
442 |
|
return peekFirst(); |
540 |
|
} |
541 |
|
|
542 |
|
/** |
543 |
< |
* Returns <tt>true</tt> if this deque contains no elements. |
543 |
> |
* Returns {@code true} if this deque contains no elements. |
544 |
|
* |
545 |
< |
* @return <tt>true</tt> if this deque contains no elements |
545 |
> |
* @return {@code true} if this deque contains no elements |
546 |
|
*/ |
547 |
|
public boolean isEmpty() { |
548 |
|
return head == tail; |
649 |
|
} |
650 |
|
|
651 |
|
/** |
652 |
< |
* Returns <tt>true</tt> if this deque contains the specified element. |
653 |
< |
* More formally, returns <tt>true</tt> if and only if this deque contains |
654 |
< |
* at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. |
652 |
> |
* Returns {@code true} if this deque contains the specified element. |
653 |
> |
* More formally, returns {@code true} if and only if this deque contains |
654 |
> |
* at least one element {@code e} such that {@code o.equals(e)}. |
655 |
|
* |
656 |
|
* @param o object to be checked for containment in this deque |
657 |
< |
* @return <tt>true</tt> if this deque contains the specified element |
657 |
> |
* @return {@code true} if this deque contains the specified element |
658 |
|
*/ |
659 |
|
public boolean contains(Object o) { |
660 |
|
if (o == null) |
673 |
|
/** |
674 |
|
* Removes a single instance of the specified element from this deque. |
675 |
|
* If the deque does not contain the element, it is unchanged. |
676 |
< |
* More formally, removes the first element <tt>e</tt> such that |
677 |
< |
* <tt>o.equals(e)</tt> (if such an element exists). |
678 |
< |
* Returns <tt>true</tt> if this deque contained the specified element |
676 |
> |
* More formally, removes the first element {@code e} such that |
677 |
> |
* {@code o.equals(e)} (if such an element exists). |
678 |
> |
* Returns {@code true} if this deque contained the specified element |
679 |
|
* (or equivalently, if this deque changed as a result of the call). |
680 |
|
* |
681 |
< |
* <p>This method is equivalent to {@link #removeFirstOccurrence}. |
681 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. |
682 |
|
* |
683 |
|
* @param o element to be removed from this deque, if present |
684 |
< |
* @return <tt>true</tt> if this deque contained the specified element |
684 |
> |
* @return {@code true} if this deque contained the specified element |
685 |
|
*/ |
686 |
|
public boolean remove(Object o) { |
687 |
|
return removeFirstOccurrence(o); |
719 |
|
* @return an array containing all of the elements in this deque |
720 |
|
*/ |
721 |
|
public Object[] toArray() { |
722 |
< |
return copyElements(new Object[size()]); |
722 |
> |
final int head = this.head; |
723 |
> |
final int tail = this.tail; |
724 |
> |
boolean wrap = (tail < head); |
725 |
> |
int end = wrap ? tail + elements.length : tail; |
726 |
> |
Object[] a = Arrays.copyOfRange(elements, head, end); |
727 |
> |
if (wrap) |
728 |
> |
System.arraycopy(elements, 0, a, elements.length - head, tail); |
729 |
> |
return a; |
730 |
|
} |
731 |
|
|
732 |
|
/** |
740 |
|
* <p>If this deque fits in the specified array with room to spare |
741 |
|
* (i.e., the array has more elements than this deque), the element in |
742 |
|
* the array immediately following the end of the deque is set to |
743 |
< |
* <tt>null</tt>. |
743 |
> |
* {@code null}. |
744 |
|
* |
745 |
|
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
746 |
|
* array-based and collection-based APIs. Further, this method allows |
747 |
|
* precise control over the runtime type of the output array, and may, |
748 |
|
* under certain circumstances, be used to save allocation costs. |
749 |
|
* |
750 |
< |
* <p>Suppose <tt>x</tt> is a deque known to contain only strings. |
750 |
> |
* <p>Suppose {@code x} is a deque known to contain only strings. |
751 |
|
* The following code can be used to dump the deque into a newly |
752 |
< |
* allocated array of <tt>String</tt>: |
752 |
> |
* allocated array of {@code String}: |
753 |
|
* |
754 |
|
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
755 |
|
* |
756 |
< |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
757 |
< |
* <tt>toArray()</tt>. |
756 |
> |
* Note that {@code toArray(new Object[0])} is identical in function to |
757 |
> |
* {@code toArray()}. |
758 |
|
* |
759 |
|
* @param a the array into which the elements of the deque are to |
760 |
|
* be stored, if it is big enough; otherwise, a new array of the |
767 |
|
*/ |
768 |
|
@SuppressWarnings("unchecked") |
769 |
|
public <T> T[] toArray(T[] a) { |
770 |
< |
int size = size(); |
771 |
< |
if (a.length < size) |
772 |
< |
a = (T[])java.lang.reflect.Array.newInstance( |
773 |
< |
a.getClass().getComponentType(), size); |
774 |
< |
copyElements(a); |
775 |
< |
if (a.length > size) |
776 |
< |
a[size] = null; |
770 |
> |
final int head = this.head; |
771 |
> |
final int tail = this.tail; |
772 |
> |
boolean wrap = (tail < head); |
773 |
> |
int size = (tail - head) + (wrap ? elements.length : 0); |
774 |
> |
int firstLeg = size - (wrap ? tail : 0); |
775 |
> |
int len = a.length; |
776 |
> |
if (size > len) { |
777 |
> |
a = (T[]) Arrays.copyOfRange(elements, head, head + size, |
778 |
> |
a.getClass()); |
779 |
> |
} else { |
780 |
> |
System.arraycopy(elements, head, a, 0, firstLeg); |
781 |
> |
if (size < len) |
782 |
> |
a[size] = null; |
783 |
> |
} |
784 |
> |
if (wrap) |
785 |
> |
System.arraycopy(elements, 0, a, firstLeg, tail); |
786 |
|
return a; |
787 |
|
} |
788 |
|
|
804 |
|
} |
805 |
|
} |
806 |
|
|
805 |
– |
/** |
806 |
– |
* Appease the serialization gods. |
807 |
– |
*/ |
807 |
|
private static final long serialVersionUID = 2340985798034038923L; |
808 |
|
|
809 |
|
/** |
810 |
< |
* Serialize this deque. |
810 |
> |
* Saves this deque to a stream (that is, serializes it). |
811 |
|
* |
812 |
< |
* @serialData The current size (<tt>int</tt>) of the deque, |
812 |
> |
* @serialData The current size ({@code int}) of the deque, |
813 |
|
* followed by all of its elements (each an object reference) in |
814 |
|
* first-to-last order. |
815 |
|
*/ |
827 |
|
} |
828 |
|
|
829 |
|
/** |
830 |
< |
* Deserialize this deque. |
830 |
> |
* Reconstitutes this deque from a stream (that is, deserializes it). |
831 |
|
*/ |
832 |
|
private void readObject(java.io.ObjectInputStream s) |
833 |
|
throws java.io.IOException, ClassNotFoundException { |
843 |
|
for (int i = 0; i < size; i++) |
844 |
|
elements[i] = s.readObject(); |
845 |
|
} |
846 |
+ |
|
847 |
+ |
Spliterator<E> spliterator() { |
848 |
+ |
return new DeqSpliterator<E>(this, -1, -1); |
849 |
+ |
} |
850 |
+ |
|
851 |
+ |
public Stream<E> stream() { |
852 |
+ |
return Streams.stream(spliterator()); |
853 |
+ |
} |
854 |
+ |
|
855 |
+ |
public Stream<E> parallelStream() { |
856 |
+ |
return Streams.parallelStream(spliterator()); |
857 |
+ |
} |
858 |
+ |
|
859 |
+ |
static final class DeqSpliterator<E> implements Spliterator<E> { |
860 |
+ |
private final ArrayDeque<E> deq; |
861 |
+ |
private int fence; // -1 until first use |
862 |
+ |
private int index; // current index, modified on traverse/split |
863 |
+ |
|
864 |
+ |
/** Creates new spliterator covering the given array and range */ |
865 |
+ |
DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) { |
866 |
+ |
this.deq = deq; |
867 |
+ |
this.index = origin; |
868 |
+ |
this.fence = fence; |
869 |
+ |
} |
870 |
+ |
|
871 |
+ |
private int getFence() { // force initialization |
872 |
+ |
int t; |
873 |
+ |
if ((t = fence) < 0) { |
874 |
+ |
t = fence = deq.tail; |
875 |
+ |
index = deq.head; |
876 |
+ |
} |
877 |
+ |
return t; |
878 |
+ |
} |
879 |
+ |
|
880 |
+ |
public DeqSpliterator<E> trySplit() { |
881 |
+ |
int t = getFence(), h = index, n = deq.elements.length; |
882 |
+ |
if (h != t && ((h + 1) & (n - 1)) != t) { |
883 |
+ |
if (h > t) |
884 |
+ |
t += n; |
885 |
+ |
int m = ((h + t) >>> 1) & (n - 1); |
886 |
+ |
return new DeqSpliterator<>(deq, h, index = m); |
887 |
+ |
} |
888 |
+ |
return null; |
889 |
+ |
} |
890 |
+ |
|
891 |
+ |
public void forEach(Consumer<? super E> consumer) { |
892 |
+ |
if (consumer == null) |
893 |
+ |
throw new NullPointerException(); |
894 |
+ |
Object[] a = deq.elements; |
895 |
+ |
int m = a.length - 1, f = getFence(), i = index; |
896 |
+ |
index = f; |
897 |
+ |
while (i != f) { |
898 |
+ |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
899 |
+ |
i = (i + 1) & m; |
900 |
+ |
if (e == null) |
901 |
+ |
throw new ConcurrentModificationException(); |
902 |
+ |
consumer.accept(e); |
903 |
+ |
} |
904 |
+ |
} |
905 |
+ |
|
906 |
+ |
public boolean tryAdvance(Consumer<? super E> consumer) { |
907 |
+ |
if (consumer == null) |
908 |
+ |
throw new NullPointerException(); |
909 |
+ |
Object[] a = deq.elements; |
910 |
+ |
int m = a.length - 1, f = getFence(), i = index; |
911 |
+ |
if (i != fence) { |
912 |
+ |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
913 |
+ |
index = (i + 1) & m; |
914 |
+ |
if (e == null) |
915 |
+ |
throw new ConcurrentModificationException(); |
916 |
+ |
consumer.accept(e); |
917 |
+ |
return true; |
918 |
+ |
} |
919 |
+ |
return false; |
920 |
+ |
} |
921 |
+ |
|
922 |
+ |
public long estimateSize() { |
923 |
+ |
int n = getFence() - index; |
924 |
+ |
if (n < 0) |
925 |
+ |
n += deq.elements.length; |
926 |
+ |
return (long) n; |
927 |
+ |
} |
928 |
+ |
|
929 |
+ |
@Override |
930 |
+ |
public int characteristics() { |
931 |
+ |
return Spliterator.ORDERED | Spliterator.SIZED | |
932 |
+ |
Spliterator.NONNULL | Spliterator.SUBSIZED; |
933 |
+ |
} |
934 |
+ |
} |
935 |
+ |
|
936 |
|
} |