1 |
|
/* |
2 |
– |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 |
– |
* |
4 |
– |
* This code is free software; you can redistribute it and/or modify it |
5 |
– |
* under the terms of the GNU General Public License version 2 only, as |
6 |
– |
* published by the Free Software Foundation. Oracle designates this |
7 |
– |
* particular file as subject to the "Classpath" exception as provided |
8 |
– |
* by Oracle in the LICENSE file that accompanied this code. |
9 |
– |
* |
10 |
– |
* This code is distributed in the hope that it will be useful, but WITHOUT |
11 |
– |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
12 |
– |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
13 |
– |
* version 2 for more details (a copy is included in the LICENSE file that |
14 |
– |
* accompanied this code). |
15 |
– |
* |
16 |
– |
* You should have received a copy of the GNU General Public License version |
17 |
– |
* 2 along with this work; if not, write to the Free Software Foundation, |
18 |
– |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
– |
* |
20 |
– |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
– |
* or visit www.oracle.com if you need additional information or have any |
22 |
– |
* questions. |
23 |
– |
*/ |
24 |
– |
|
25 |
– |
/* |
26 |
– |
* This file is available under and governed by the GNU General Public |
27 |
– |
* License version 2 only, as published by the Free Software Foundation. |
28 |
– |
* However, the following notice accompanied the original version of this |
29 |
– |
* file: |
30 |
– |
* |
2 |
|
* Written by Josh Bloch of Google Inc. and released to the public domain, |
3 |
|
* as explained at http://creativecommons.org/publicdomain/zero/1.0/. |
4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
< |
import java.util.Spliterator; |
7 |
> |
import java.io.Serializable; |
8 |
> |
import java.util.function.Consumer; |
9 |
|
import java.util.stream.Stream; |
38 |
– |
import java.util.stream.Streams; |
39 |
– |
import java.util.function.Block; |
10 |
|
|
11 |
|
/** |
12 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
17 |
|
* {@link Stack} when used as a stack, and faster than {@link LinkedList} |
18 |
|
* when used as a queue. |
19 |
|
* |
20 |
< |
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
21 |
< |
* Exceptions include {@link #remove(Object) remove}, {@link |
22 |
< |
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
23 |
< |
* removeLastOccurrence}, {@link #contains contains}, {@link #iterator |
24 |
< |
* iterator.remove()}, and the bulk operations, all of which run in linear |
25 |
< |
* time. |
26 |
< |
* |
27 |
< |
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
28 |
< |
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
29 |
< |
* is created, in any way except through the iterator's own <tt>remove</tt> |
30 |
< |
* method, the iterator will generally throw a {@link |
20 |
> |
* <p>Most {@code ArrayDeque} operations run in amortized constant time. |
21 |
> |
* Exceptions include |
22 |
> |
* {@link #remove(Object) remove}, |
23 |
> |
* {@link #removeFirstOccurrence removeFirstOccurrence}, |
24 |
> |
* {@link #removeLastOccurrence removeLastOccurrence}, |
25 |
> |
* {@link #contains contains}, |
26 |
> |
* {@link #iterator iterator.remove()}, |
27 |
> |
* and the bulk operations, all of which run in linear time. |
28 |
> |
* |
29 |
> |
* <p>The iterators returned by this class's {@link #iterator() iterator} |
30 |
> |
* method are <em>fail-fast</em>: If the deque is modified at any time after |
31 |
> |
* the iterator is created, in any way except through the iterator's own |
32 |
> |
* {@code remove} method, the iterator will generally throw a {@link |
33 |
|
* ConcurrentModificationException}. Thus, in the face of concurrent |
34 |
|
* modification, the iterator fails quickly and cleanly, rather than risking |
35 |
|
* arbitrary, non-deterministic behavior at an undetermined time in the |
38 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
39 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
40 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
41 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
41 |
> |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
42 |
|
* Therefore, it would be wrong to write a program that depended on this |
43 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
44 |
|
* should be used only to detect bugs.</i> |
56 |
|
* @param <E> the type of elements held in this collection |
57 |
|
*/ |
58 |
|
public class ArrayDeque<E> extends AbstractCollection<E> |
59 |
< |
implements Deque<E>, Cloneable, java.io.Serializable |
59 |
> |
implements Deque<E>, Cloneable, Serializable |
60 |
|
{ |
61 |
|
/** |
62 |
|
* The array in which the elements of the deque are stored. |
136 |
|
} |
137 |
|
|
138 |
|
/** |
167 |
– |
* Copies the elements from our element array into the specified array, |
168 |
– |
* in order (from first to last element in the deque). It is assumed |
169 |
– |
* that the array is large enough to hold all elements in the deque. |
170 |
– |
* |
171 |
– |
* @return its argument |
172 |
– |
*/ |
173 |
– |
private <T> T[] copyElements(T[] a) { |
174 |
– |
if (head < tail) { |
175 |
– |
System.arraycopy(elements, head, a, 0, size()); |
176 |
– |
} else if (head > tail) { |
177 |
– |
int headPortionLen = elements.length - head; |
178 |
– |
System.arraycopy(elements, head, a, 0, headPortionLen); |
179 |
– |
System.arraycopy(elements, 0, a, headPortionLen, tail); |
180 |
– |
} |
181 |
– |
return a; |
182 |
– |
} |
183 |
– |
|
184 |
– |
/** |
139 |
|
* Constructs an empty array deque with an initial capacity |
140 |
|
* sufficient to hold 16 elements. |
141 |
|
*/ |
612 |
|
} |
613 |
|
} |
614 |
|
|
615 |
+ |
/** |
616 |
+ |
* This class is nearly a mirror-image of DeqIterator, using tail |
617 |
+ |
* instead of head for initial cursor, and head instead of tail |
618 |
+ |
* for fence. |
619 |
+ |
*/ |
620 |
|
private class DescendingIterator implements Iterator<E> { |
662 |
– |
/* |
663 |
– |
* This class is nearly a mirror-image of DeqIterator, using |
664 |
– |
* tail instead of head for initial cursor, and head instead of |
665 |
– |
* tail for fence. |
666 |
– |
*/ |
621 |
|
private int cursor = tail; |
622 |
|
private int fence = head; |
623 |
|
private int lastRet = -1; |
679 |
|
* Returns {@code true} if this deque contained the specified element |
680 |
|
* (or equivalently, if this deque changed as a result of the call). |
681 |
|
* |
682 |
< |
* <p>This method is equivalent to {@link #removeFirstOccurrence}. |
682 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. |
683 |
|
* |
684 |
|
* @param o element to be removed from this deque, if present |
685 |
|
* @return {@code true} if this deque contained the specified element |
720 |
|
* @return an array containing all of the elements in this deque |
721 |
|
*/ |
722 |
|
public Object[] toArray() { |
723 |
< |
return copyElements(new Object[size()]); |
723 |
> |
final int head = this.head; |
724 |
> |
final int tail = this.tail; |
725 |
> |
boolean wrap = (tail < head); |
726 |
> |
int end = wrap ? tail + elements.length : tail; |
727 |
> |
Object[] a = Arrays.copyOfRange(elements, head, end); |
728 |
> |
if (wrap) |
729 |
> |
System.arraycopy(elements, 0, a, elements.length - head, tail); |
730 |
> |
return a; |
731 |
|
} |
732 |
|
|
733 |
|
/** |
768 |
|
*/ |
769 |
|
@SuppressWarnings("unchecked") |
770 |
|
public <T> T[] toArray(T[] a) { |
771 |
< |
int size = size(); |
772 |
< |
if (a.length < size) |
773 |
< |
a = (T[])java.lang.reflect.Array.newInstance( |
774 |
< |
a.getClass().getComponentType(), size); |
775 |
< |
copyElements(a); |
776 |
< |
if (a.length > size) |
777 |
< |
a[size] = null; |
771 |
> |
final int head = this.head; |
772 |
> |
final int tail = this.tail; |
773 |
> |
boolean wrap = (tail < head); |
774 |
> |
int size = (tail - head) + (wrap ? elements.length : 0); |
775 |
> |
int firstLeg = size - (wrap ? tail : 0); |
776 |
> |
int len = a.length; |
777 |
> |
if (size > len) { |
778 |
> |
a = (T[]) Arrays.copyOfRange(elements, head, head + size, |
779 |
> |
a.getClass()); |
780 |
> |
} else { |
781 |
> |
System.arraycopy(elements, head, a, 0, firstLeg); |
782 |
> |
if (size < len) |
783 |
> |
a[size] = null; |
784 |
> |
} |
785 |
> |
if (wrap) |
786 |
> |
System.arraycopy(elements, 0, a, firstLeg, tail); |
787 |
|
return a; |
788 |
|
} |
789 |
|
|
845 |
|
elements[i] = s.readObject(); |
846 |
|
} |
847 |
|
|
848 |
< |
public Stream<E> stream() { |
849 |
< |
int flags = Streams.STREAM_IS_ORDERED | Streams.STREAM_IS_SIZED; |
880 |
< |
return Streams.stream |
881 |
< |
(() -> new DeqSpliterator<E>(this, head, tail), flags); |
882 |
< |
} |
883 |
< |
public Stream<E> parallelStream() { |
884 |
< |
int flags = Streams.STREAM_IS_ORDERED | Streams.STREAM_IS_SIZED; |
885 |
< |
return Streams.parallelStream |
886 |
< |
(() -> new DeqSpliterator<E>(this, head, tail), flags); |
848 |
> |
public Spliterator<E> spliterator() { |
849 |
> |
return new DeqSpliterator<E>(this, -1, -1); |
850 |
|
} |
851 |
|
|
852 |
< |
|
890 |
< |
static final class DeqSpliterator<E> implements Spliterator<E>, Iterator<E> { |
852 |
> |
static final class DeqSpliterator<E> implements Spliterator<E> { |
853 |
|
private final ArrayDeque<E> deq; |
854 |
< |
private final Object[] array; |
855 |
< |
private final int fence; // initially tail |
894 |
< |
private int index; // current index, modified on traverse/split |
854 |
> |
private int fence; // -1 until first use |
855 |
> |
private int index; // current index, modified on traverse/split |
856 |
|
|
857 |
< |
/** Create new spliterator covering the given array and range */ |
857 |
> |
/** Creates new spliterator covering the given array and range */ |
858 |
|
DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) { |
859 |
< |
this.deq = deq; this.array = deq.elements; |
860 |
< |
this.index = origin; this.fence = fence; |
859 |
> |
this.deq = deq; |
860 |
> |
this.index = origin; |
861 |
> |
this.fence = fence; |
862 |
|
} |
863 |
|
|
864 |
< |
public DeqSpliterator<E> trySplit() { |
865 |
< |
int n = array.length; |
866 |
< |
int h = index, t = fence; |
864 |
> |
private int getFence() { // force initialization |
865 |
> |
int t; |
866 |
> |
if ((t = fence) < 0) { |
867 |
> |
t = fence = deq.tail; |
868 |
> |
index = deq.head; |
869 |
> |
} |
870 |
> |
return t; |
871 |
> |
} |
872 |
> |
|
873 |
> |
public Spliterator<E> trySplit() { |
874 |
> |
int t = getFence(), h = index, n = deq.elements.length; |
875 |
|
if (h != t && ((h + 1) & (n - 1)) != t) { |
876 |
|
if (h > t) |
877 |
|
t += n; |
878 |
|
int m = ((h + t) >>> 1) & (n - 1); |
879 |
< |
return new DeqSpliterator<E>(deq, h, index = m); |
879 |
> |
return new DeqSpliterator<>(deq, h, index = m); |
880 |
|
} |
881 |
|
return null; |
882 |
|
} |
883 |
|
|
884 |
< |
@SuppressWarnings("unchecked") |
885 |
< |
public void forEach(Block<? super E> block) { |
916 |
< |
if (block == null) |
884 |
> |
public void forEachRemaining(Consumer<? super E> consumer) { |
885 |
> |
if (consumer == null) |
886 |
|
throw new NullPointerException(); |
887 |
< |
Object[] a = array; |
888 |
< |
if (a != deq.elements) |
920 |
< |
throw new ConcurrentModificationException(); |
921 |
< |
int m = a.length - 1, f = fence, i = index; |
887 |
> |
Object[] a = deq.elements; |
888 |
> |
int m = a.length - 1, f = getFence(), i = index; |
889 |
|
index = f; |
890 |
|
while (i != f) { |
891 |
< |
Object e = a[i]; |
891 |
> |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
892 |
> |
i = (i + 1) & m; |
893 |
|
if (e == null) |
894 |
|
throw new ConcurrentModificationException(); |
895 |
< |
block.accept((E)e); |
928 |
< |
i = (i + 1) & m; |
895 |
> |
consumer.accept(e); |
896 |
|
} |
897 |
|
} |
898 |
|
|
899 |
< |
@SuppressWarnings("unchecked") |
900 |
< |
public boolean tryAdvance(Block<? super E> block) { |
934 |
< |
if (block == null) |
899 |
> |
public boolean tryAdvance(Consumer<? super E> consumer) { |
900 |
> |
if (consumer == null) |
901 |
|
throw new NullPointerException(); |
902 |
< |
Object[] a = array; |
903 |
< |
if (a != deq.elements) |
938 |
< |
throw new ConcurrentModificationException(); |
939 |
< |
int m = a.length - 1, i = index; |
902 |
> |
Object[] a = deq.elements; |
903 |
> |
int m = a.length - 1, f = getFence(), i = index; |
904 |
|
if (i != fence) { |
905 |
< |
Object e = a[i]; |
905 |
> |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
906 |
> |
index = (i + 1) & m; |
907 |
|
if (e == null) |
908 |
|
throw new ConcurrentModificationException(); |
909 |
< |
block.accept((E)e); |
945 |
< |
index = (i + 1) & m; |
909 |
> |
consumer.accept(e); |
910 |
|
return true; |
911 |
|
} |
912 |
|
return false; |
913 |
|
} |
914 |
|
|
951 |
– |
// Iterator support |
952 |
– |
public Iterator<E> iterator() { |
953 |
– |
return this; |
954 |
– |
} |
955 |
– |
|
956 |
– |
public boolean hasNext() { |
957 |
– |
return index >= 0 && index != fence; |
958 |
– |
} |
959 |
– |
|
960 |
– |
@SuppressWarnings("unchecked") |
961 |
– |
public E next() { |
962 |
– |
if (index < 0 || index == fence) |
963 |
– |
throw new NoSuchElementException(); |
964 |
– |
Object[] a = array; |
965 |
– |
if (a != deq.elements) |
966 |
– |
throw new ConcurrentModificationException(); |
967 |
– |
Object e = a[index]; |
968 |
– |
if (e == null) |
969 |
– |
throw new ConcurrentModificationException(); |
970 |
– |
index = (index + 1) & (a.length - 1); |
971 |
– |
return (E) e; |
972 |
– |
} |
973 |
– |
|
974 |
– |
public void remove() { throw new UnsupportedOperationException(); } |
975 |
– |
|
976 |
– |
// Other spliterator methods |
915 |
|
public long estimateSize() { |
916 |
< |
int n = fence - index; |
916 |
> |
int n = getFence() - index; |
917 |
|
if (n < 0) |
918 |
< |
n += array.length; |
919 |
< |
return (long)n; |
918 |
> |
n += deq.elements.length; |
919 |
> |
return (long) n; |
920 |
> |
} |
921 |
> |
|
922 |
> |
@Override |
923 |
> |
public int characteristics() { |
924 |
> |
return Spliterator.ORDERED | Spliterator.SIZED | |
925 |
> |
Spliterator.NONNULL | Spliterator.SUBSIZED; |
926 |
|
} |
983 |
– |
public boolean hasExactSize() { return true; } |
984 |
– |
public boolean hasExactSplits() { return true; } |
927 |
|
} |
928 |
|
|
929 |
|
} |