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. |
223 |
|
* Inserts the specified element at the front of this deque. |
224 |
|
* |
225 |
|
* @param e the element to add |
226 |
< |
* @return <tt>true</tt> (as specified by {@link Deque#offerFirst}) |
226 |
> |
* @return {@code true} (as specified by {@link Deque#offerFirst}) |
227 |
|
* @throws NullPointerException if the specified element is null |
228 |
|
*/ |
229 |
|
public boolean offerFirst(E e) { |
235 |
|
* Inserts the specified element at the end of this deque. |
236 |
|
* |
237 |
|
* @param e the element to add |
238 |
< |
* @return <tt>true</tt> (as specified by {@link Deque#offerLast}) |
238 |
> |
* @return {@code true} (as specified by {@link Deque#offerLast}) |
239 |
|
* @throws NullPointerException if the specified element is null |
240 |
|
*/ |
241 |
|
public boolean offerLast(E e) { |
323 |
|
* Removes the first occurrence of the specified element in this |
324 |
|
* deque (when traversing the deque from head to tail). |
325 |
|
* If the deque does not contain the element, it is unchanged. |
326 |
< |
* More formally, removes the first element <tt>e</tt> such that |
327 |
< |
* <tt>o.equals(e)</tt> (if such an element exists). |
328 |
< |
* Returns <tt>true</tt> if this deque contained the specified element |
326 |
> |
* More formally, removes the first element {@code e} such that |
327 |
> |
* {@code o.equals(e)} (if such an element exists). |
328 |
> |
* Returns {@code true} if this deque contained the specified element |
329 |
|
* (or equivalently, if this deque changed as a result of the call). |
330 |
|
* |
331 |
|
* @param o element to be removed from this deque, if present |
332 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
332 |
> |
* @return {@code true} if the deque contained the specified element |
333 |
|
*/ |
334 |
|
public boolean removeFirstOccurrence(Object o) { |
335 |
|
if (o == null) |
351 |
|
* Removes the last occurrence of the specified element in this |
352 |
|
* deque (when traversing the deque from head to tail). |
353 |
|
* If the deque does not contain the element, it is unchanged. |
354 |
< |
* More formally, removes the last element <tt>e</tt> such that |
355 |
< |
* <tt>o.equals(e)</tt> (if such an element exists). |
356 |
< |
* Returns <tt>true</tt> if this deque contained the specified element |
354 |
> |
* More formally, removes the last element {@code e} such that |
355 |
> |
* {@code o.equals(e)} (if such an element exists). |
356 |
> |
* Returns {@code true} if this deque contained the specified element |
357 |
|
* (or equivalently, if this deque changed as a result of the call). |
358 |
|
* |
359 |
|
* @param o element to be removed from this deque, if present |
360 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
360 |
> |
* @return {@code true} if the deque contained the specified element |
361 |
|
*/ |
362 |
|
public boolean removeLastOccurrence(Object o) { |
363 |
|
if (o == null) |
383 |
|
* <p>This method is equivalent to {@link #addLast}. |
384 |
|
* |
385 |
|
* @param e the element to add |
386 |
< |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
386 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
387 |
|
* @throws NullPointerException if the specified element is null |
388 |
|
*/ |
389 |
|
public boolean add(E e) { |
397 |
|
* <p>This method is equivalent to {@link #offerLast}. |
398 |
|
* |
399 |
|
* @param e the element to add |
400 |
< |
* @return <tt>true</tt> (as specified by {@link Queue#offer}) |
400 |
> |
* @return {@code true} (as specified by {@link Queue#offer}) |
401 |
|
* @throws NullPointerException if the specified element is null |
402 |
|
*/ |
403 |
|
public boolean offer(E e) { |
422 |
|
/** |
423 |
|
* Retrieves and removes the head of the queue represented by this deque |
424 |
|
* (in other words, the first element of this deque), or returns |
425 |
< |
* <tt>null</tt> if this deque is empty. |
425 |
> |
* {@code null} if this deque is empty. |
426 |
|
* |
427 |
|
* <p>This method is equivalent to {@link #pollFirst}. |
428 |
|
* |
429 |
|
* @return the head of the queue represented by this deque, or |
430 |
< |
* <tt>null</tt> if this deque is empty |
430 |
> |
* {@code null} if this deque is empty |
431 |
|
*/ |
432 |
|
public E poll() { |
433 |
|
return pollFirst(); |
449 |
|
|
450 |
|
/** |
451 |
|
* Retrieves, but does not remove, the head of the queue represented by |
452 |
< |
* this deque, or returns <tt>null</tt> if this deque is empty. |
452 |
> |
* this deque, or returns {@code null} if this deque is empty. |
453 |
|
* |
454 |
|
* <p>This method is equivalent to {@link #peekFirst}. |
455 |
|
* |
456 |
|
* @return the head of the queue represented by this deque, or |
457 |
< |
* <tt>null</tt> if this deque is empty |
457 |
> |
* {@code null} if this deque is empty |
458 |
|
*/ |
459 |
|
public E peek() { |
460 |
|
return peekFirst(); |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
< |
* Returns <tt>true</tt> if this deque contains no elements. |
561 |
> |
* Returns {@code true} if this deque contains no elements. |
562 |
|
* |
563 |
< |
* @return <tt>true</tt> if this deque contains no elements |
563 |
> |
* @return {@code true} if this deque contains no elements |
564 |
|
*/ |
565 |
|
public boolean isEmpty() { |
566 |
|
return head == tail; |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
< |
* Returns <tt>true</tt> if this deque contains the specified element. |
671 |
< |
* More formally, returns <tt>true</tt> if and only if this deque contains |
672 |
< |
* at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. |
670 |
> |
* Returns {@code true} if this deque contains the specified element. |
671 |
> |
* More formally, returns {@code true} if and only if this deque contains |
672 |
> |
* at least one element {@code e} such that {@code o.equals(e)}. |
673 |
|
* |
674 |
|
* @param o object to be checked for containment in this deque |
675 |
< |
* @return <tt>true</tt> if this deque contains the specified element |
675 |
> |
* @return {@code true} if this deque contains the specified element |
676 |
|
*/ |
677 |
|
public boolean contains(Object o) { |
678 |
|
if (o == null) |
691 |
|
/** |
692 |
|
* Removes a single instance of the specified element from this deque. |
693 |
|
* If the deque does not contain the element, it is unchanged. |
694 |
< |
* More formally, removes the first element <tt>e</tt> such that |
695 |
< |
* <tt>o.equals(e)</tt> (if such an element exists). |
696 |
< |
* Returns <tt>true</tt> if this deque contained the specified element |
694 |
> |
* More formally, removes the first element {@code e} such that |
695 |
> |
* {@code o.equals(e)} (if such an element exists). |
696 |
> |
* Returns {@code true} if this deque contained the specified element |
697 |
|
* (or equivalently, if this deque changed as a result of the call). |
698 |
|
* |
699 |
< |
* <p>This method is equivalent to {@link #removeFirstOccurrence}. |
699 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. |
700 |
|
* |
701 |
|
* @param o element to be removed from this deque, if present |
702 |
< |
* @return <tt>true</tt> if this deque contained the specified element |
702 |
> |
* @return {@code true} if this deque contained the specified element |
703 |
|
*/ |
704 |
|
public boolean remove(Object o) { |
705 |
|
return removeFirstOccurrence(o); |
751 |
|
* <p>If this deque fits in the specified array with room to spare |
752 |
|
* (i.e., the array has more elements than this deque), the element in |
753 |
|
* the array immediately following the end of the deque is set to |
754 |
< |
* <tt>null</tt>. |
754 |
> |
* {@code null}. |
755 |
|
* |
756 |
|
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
757 |
|
* array-based and collection-based APIs. Further, this method allows |
758 |
|
* precise control over the runtime type of the output array, and may, |
759 |
|
* under certain circumstances, be used to save allocation costs. |
760 |
|
* |
761 |
< |
* <p>Suppose <tt>x</tt> is a deque known to contain only strings. |
761 |
> |
* <p>Suppose {@code x} is a deque known to contain only strings. |
762 |
|
* The following code can be used to dump the deque into a newly |
763 |
< |
* allocated array of <tt>String</tt>: |
763 |
> |
* allocated array of {@code String}: |
764 |
|
* |
765 |
|
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
766 |
|
* |
767 |
< |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
768 |
< |
* <tt>toArray()</tt>. |
767 |
> |
* Note that {@code toArray(new Object[0])} is identical in function to |
768 |
> |
* {@code toArray()}. |
769 |
|
* |
770 |
|
* @param a the array into which the elements of the deque are to |
771 |
|
* be stored, if it is big enough; otherwise, a new array of the |
811 |
|
/** |
812 |
|
* Saves this deque to a stream (that is, serializes it). |
813 |
|
* |
814 |
< |
* @serialData The current size (<tt>int</tt>) of the deque, |
814 |
> |
* @serialData The current size ({@code int}) of the deque, |
815 |
|
* followed by all of its elements (each an object reference) in |
816 |
|
* first-to-last order. |
817 |
|
*/ |
845 |
|
for (int i = 0; i < size; i++) |
846 |
|
elements[i] = s.readObject(); |
847 |
|
} |
848 |
+ |
|
849 |
+ |
Spliterator<E> spliterator() { |
850 |
+ |
return new DeqSpliterator<E>(this, -1, -1); |
851 |
+ |
} |
852 |
+ |
|
853 |
+ |
public Stream<E> stream() { |
854 |
+ |
return Streams.stream(spliterator()); |
855 |
+ |
} |
856 |
+ |
|
857 |
+ |
public Stream<E> parallelStream() { |
858 |
+ |
return Streams.parallelStream(spliterator()); |
859 |
+ |
} |
860 |
+ |
|
861 |
+ |
static final class DeqSpliterator<E> implements Spliterator<E> { |
862 |
+ |
private final ArrayDeque<E> deq; |
863 |
+ |
private int fence; // -1 until first use |
864 |
+ |
private int index; // current index, modified on traverse/split |
865 |
+ |
|
866 |
+ |
/** Create new spliterator covering the given array and range */ |
867 |
+ |
DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) { |
868 |
+ |
this.deq = deq; |
869 |
+ |
this.index = origin; |
870 |
+ |
this.fence = fence; |
871 |
+ |
} |
872 |
+ |
|
873 |
+ |
private int getFence() { // force initialization |
874 |
+ |
int t; |
875 |
+ |
if ((t = fence) < 0) { |
876 |
+ |
t = fence = deq.tail; |
877 |
+ |
index = deq.head; |
878 |
+ |
} |
879 |
+ |
return t; |
880 |
+ |
} |
881 |
+ |
|
882 |
+ |
public DeqSpliterator<E> trySplit() { |
883 |
+ |
int t = getFence(), h = index, n = deq.elements.length; |
884 |
+ |
if (h != t && ((h + 1) & (n - 1)) != t) { |
885 |
+ |
if (h > t) |
886 |
+ |
t += n; |
887 |
+ |
int m = ((h + t) >>> 1) & (n - 1); |
888 |
+ |
return new DeqSpliterator<>(deq, h, index = m); |
889 |
+ |
} |
890 |
+ |
return null; |
891 |
+ |
} |
892 |
+ |
|
893 |
+ |
public void forEach(Consumer<? super E> consumer) { |
894 |
+ |
if (consumer == null) |
895 |
+ |
throw new NullPointerException(); |
896 |
+ |
Object[] a = deq.elements; |
897 |
+ |
int m = a.length - 1, f = getFence(), i = index; |
898 |
+ |
index = f; |
899 |
+ |
while (i != f) { |
900 |
+ |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
901 |
+ |
i = (i + 1) & m; |
902 |
+ |
if (e == null) |
903 |
+ |
throw new ConcurrentModificationException(); |
904 |
+ |
consumer.accept(e); |
905 |
+ |
} |
906 |
+ |
} |
907 |
+ |
|
908 |
+ |
public boolean tryAdvance(Consumer<? super E> consumer) { |
909 |
+ |
if (consumer == null) |
910 |
+ |
throw new NullPointerException(); |
911 |
+ |
Object[] a = deq.elements; |
912 |
+ |
int m = a.length - 1, f = getFence(), i = index; |
913 |
+ |
if (i != fence) { |
914 |
+ |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
915 |
+ |
index = (i + 1) & m; |
916 |
+ |
if (e == null) |
917 |
+ |
throw new ConcurrentModificationException(); |
918 |
+ |
consumer.accept(e); |
919 |
+ |
return true; |
920 |
+ |
} |
921 |
+ |
return false; |
922 |
+ |
} |
923 |
+ |
|
924 |
+ |
public long estimateSize() { |
925 |
+ |
int n = getFence() - index; |
926 |
+ |
if (n < 0) |
927 |
+ |
n += deq.elements.length; |
928 |
+ |
return (long) n; |
929 |
+ |
} |
930 |
+ |
|
931 |
+ |
@Override |
932 |
+ |
public int characteristics() { |
933 |
+ |
return Spliterator.ORDERED | Spliterator.SIZED | |
934 |
+ |
Spliterator.NONNULL | Spliterator.SUBSIZED; |
935 |
+ |
} |
936 |
+ |
} |
937 |
+ |
|
938 |
|
} |