19 |
|
* when used as a queue. |
20 |
|
* |
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. |
22 |
> |
* Exceptions include |
23 |
> |
* {@link #remove(Object) remove}, |
24 |
> |
* {@link #removeFirstOccurrence removeFirstOccurrence}, |
25 |
> |
* {@link #removeLastOccurrence removeLastOccurrence}, |
26 |
> |
* {@link #contains contains}, |
27 |
> |
* {@link #iterator iterator.remove()}, |
28 |
> |
* and the bulk operations, all of which run in linear time. |
29 |
|
* |
30 |
< |
* <p>The iterators returned by this class's {@code iterator} method are |
31 |
< |
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
32 |
< |
* is created, in any way except through the iterator's own {@code remove} |
33 |
< |
* method, the iterator will generally throw a {@link |
30 |
> |
* <p>The iterators returned by this class's {@link #iterator() iterator} |
31 |
> |
* method are <em>fail-fast</em>: If the deque is modified at any time after |
32 |
> |
* the iterator is created, in any way except through the iterator's own |
33 |
> |
* {@code remove} method, the iterator will generally throw a {@link |
34 |
|
* ConcurrentModificationException}. Thus, in the face of concurrent |
35 |
|
* modification, the iterator fails quickly and cleanly, rather than risking |
36 |
|
* arbitrary, non-deterministic behavior at an undetermined time in the |
137 |
|
} |
138 |
|
|
139 |
|
/** |
138 |
– |
* Copies the elements from our element array into the specified array, |
139 |
– |
* in order (from first to last element in the deque). It is assumed |
140 |
– |
* that the array is large enough to hold all elements in the deque. |
141 |
– |
* |
142 |
– |
* @return its argument |
143 |
– |
*/ |
144 |
– |
private <T> T[] copyElements(T[] a) { |
145 |
– |
if (head < tail) { |
146 |
– |
System.arraycopy(elements, head, a, 0, size()); |
147 |
– |
} else if (head > tail) { |
148 |
– |
int headPortionLen = elements.length - head; |
149 |
– |
System.arraycopy(elements, head, a, 0, headPortionLen); |
150 |
– |
System.arraycopy(elements, 0, a, headPortionLen, tail); |
151 |
– |
} |
152 |
– |
return a; |
153 |
– |
} |
154 |
– |
|
155 |
– |
/** |
140 |
|
* Constructs an empty array deque with an initial capacity |
141 |
|
* sufficient to hold 16 elements. |
142 |
|
*/ |
721 |
|
* @return an array containing all of the elements in this deque |
722 |
|
*/ |
723 |
|
public Object[] toArray() { |
724 |
< |
return copyElements(new Object[size()]); |
724 |
> |
final int head = this.head; |
725 |
> |
final int tail = this.tail; |
726 |
> |
boolean wrap = (tail < head); |
727 |
> |
int end = wrap ? tail + elements.length : tail; |
728 |
> |
Object[] a = Arrays.copyOfRange(elements, head, end); |
729 |
> |
if (wrap) |
730 |
> |
System.arraycopy(elements, 0, a, elements.length - head, tail); |
731 |
> |
return a; |
732 |
|
} |
733 |
|
|
734 |
|
/** |
769 |
|
*/ |
770 |
|
@SuppressWarnings("unchecked") |
771 |
|
public <T> T[] toArray(T[] a) { |
772 |
< |
int size = size(); |
773 |
< |
if (a.length < size) |
774 |
< |
a = (T[])java.lang.reflect.Array.newInstance( |
775 |
< |
a.getClass().getComponentType(), size); |
776 |
< |
copyElements(a); |
777 |
< |
if (a.length > size) |
778 |
< |
a[size] = null; |
772 |
> |
final int head = this.head; |
773 |
> |
final int tail = this.tail; |
774 |
> |
boolean wrap = (tail < head); |
775 |
> |
int size = (tail - head) + (wrap ? elements.length : 0); |
776 |
> |
int firstLeg = size - (wrap ? tail : 0); |
777 |
> |
int len = a.length; |
778 |
> |
if (size > len) { |
779 |
> |
a = (T[]) Arrays.copyOfRange(elements, head, head + size, |
780 |
> |
a.getClass()); |
781 |
> |
} else { |
782 |
> |
System.arraycopy(elements, head, a, 0, firstLeg); |
783 |
> |
if (size < len) |
784 |
> |
a[size] = null; |
785 |
> |
} |
786 |
> |
if (wrap) |
787 |
> |
System.arraycopy(elements, 0, a, firstLeg, tail); |
788 |
|
return a; |
789 |
|
} |
790 |
|
|
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 */ |
865 |
> |
|
866 |
> |
/** Creates 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; |
930 |
|
|
931 |
|
@Override |
932 |
|
public int characteristics() { |
933 |
< |
return Spliterator.ORDERED | Spliterator.SIZED | |
933 |
> |
return Spliterator.ORDERED | Spliterator.SIZED | |
934 |
|
Spliterator.NONNULL | Spliterator.SUBSIZED; |
935 |
|
} |
936 |
|
} |