1 |
dl |
1.1 |
/* |
2 |
dl |
1.47 |
* 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 |
dl |
1.1 |
*/ |
5 |
|
|
|
6 |
|
|
package java.util; |
7 |
jsr166 |
1.61 |
|
8 |
dl |
1.47 |
import java.io.Serializable; |
9 |
|
|
import java.util.function.Consumer; |
10 |
jsr166 |
1.75 |
import java.util.function.Predicate; |
11 |
|
|
import java.util.function.UnaryOperator; |
12 |
jsr166 |
1.132 |
import jdk.internal.misc.SharedSecrets; |
13 |
dl |
1.1 |
|
14 |
|
|
/** |
15 |
|
|
* Resizable-array implementation of the {@link Deque} interface. Array |
16 |
|
|
* deques have no capacity restrictions; they grow as necessary to support |
17 |
|
|
* usage. They are not thread-safe; in the absence of external |
18 |
|
|
* synchronization, they do not support concurrent access by multiple threads. |
19 |
|
|
* Null elements are prohibited. This class is likely to be faster than |
20 |
dl |
1.2 |
* {@link Stack} when used as a stack, and faster than {@link LinkedList} |
21 |
dl |
1.1 |
* when used as a queue. |
22 |
|
|
* |
23 |
jsr166 |
1.43 |
* <p>Most {@code ArrayDeque} operations run in amortized constant time. |
24 |
jsr166 |
1.51 |
* Exceptions include |
25 |
|
|
* {@link #remove(Object) remove}, |
26 |
|
|
* {@link #removeFirstOccurrence removeFirstOccurrence}, |
27 |
|
|
* {@link #removeLastOccurrence removeLastOccurrence}, |
28 |
|
|
* {@link #contains contains}, |
29 |
|
|
* {@link #iterator iterator.remove()}, |
30 |
|
|
* and the bulk operations, all of which run in linear time. |
31 |
dl |
1.41 |
* |
32 |
jsr166 |
1.51 |
* <p>The iterators returned by this class's {@link #iterator() iterator} |
33 |
|
|
* method are <em>fail-fast</em>: If the deque is modified at any time after |
34 |
|
|
* the iterator is created, in any way except through the iterator's own |
35 |
|
|
* {@code remove} method, the iterator will generally throw a {@link |
36 |
jsr166 |
1.7 |
* ConcurrentModificationException}. Thus, in the face of concurrent |
37 |
|
|
* modification, the iterator fails quickly and cleanly, rather than risking |
38 |
|
|
* arbitrary, non-deterministic behavior at an undetermined time in the |
39 |
|
|
* future. |
40 |
dl |
1.1 |
* |
41 |
|
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
42 |
|
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
43 |
|
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
44 |
jsr166 |
1.43 |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
45 |
dl |
1.1 |
* Therefore, it would be wrong to write a program that depended on this |
46 |
|
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
47 |
|
|
* should be used only to detect bugs.</i> |
48 |
|
|
* |
49 |
|
|
* <p>This class and its iterator implement all of the |
50 |
jsr166 |
1.9 |
* <em>optional</em> methods of the {@link Collection} and {@link |
51 |
|
|
* Iterator} interfaces. |
52 |
|
|
* |
53 |
|
|
* <p>This class is a member of the |
54 |
jsr166 |
1.128 |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
55 |
jsr166 |
1.9 |
* Java Collections Framework</a>. |
56 |
dl |
1.1 |
* |
57 |
|
|
* @author Josh Bloch and Doug Lea |
58 |
jsr166 |
1.74 |
* @param <E> the type of elements held in this deque |
59 |
dl |
1.1 |
* @since 1.6 |
60 |
|
|
*/ |
61 |
|
|
public class ArrayDeque<E> extends AbstractCollection<E> |
62 |
dl |
1.47 |
implements Deque<E>, Cloneable, Serializable |
63 |
dl |
1.1 |
{ |
64 |
jsr166 |
1.106 |
/* |
65 |
|
|
* VMs excel at optimizing simple array loops where indices are |
66 |
|
|
* incrementing or decrementing over a valid slice, e.g. |
67 |
|
|
* |
68 |
|
|
* for (int i = start; i < end; i++) ... elements[i] |
69 |
|
|
* |
70 |
|
|
* Because in a circular array, elements are in general stored in |
71 |
|
|
* two disjoint such slices, we help the VM by writing unusual |
72 |
jsr166 |
1.114 |
* nested loops for all traversals over the elements. Having only |
73 |
|
|
* one hot inner loop body instead of two or three eases human |
74 |
|
|
* maintenance and encourages VM loop inlining into the caller. |
75 |
jsr166 |
1.106 |
*/ |
76 |
|
|
|
77 |
dl |
1.1 |
/** |
78 |
dl |
1.4 |
* The array in which the elements of the deque are stored. |
79 |
jsr166 |
1.114 |
* All array cells not holding deque elements are always null. |
80 |
|
|
* The array always has at least one null slot (at tail). |
81 |
dl |
1.1 |
*/ |
82 |
jsr166 |
1.75 |
transient Object[] elements; |
83 |
dl |
1.1 |
|
84 |
|
|
/** |
85 |
|
|
* The index of the element at the head of the deque (which is the |
86 |
|
|
* element that would be removed by remove() or pop()); or an |
87 |
jsr166 |
1.106 |
* arbitrary number 0 <= head < elements.length equal to tail if |
88 |
|
|
* the deque is empty. |
89 |
dl |
1.1 |
*/ |
90 |
dl |
1.41 |
transient int head; |
91 |
dl |
1.1 |
|
92 |
jsr166 |
1.106 |
/** |
93 |
|
|
* The index at which the next element would be added to the tail |
94 |
jsr166 |
1.114 |
* of the deque (via addLast(E), add(E), or push(E)); |
95 |
|
|
* elements[tail] is always null. |
96 |
jsr166 |
1.106 |
*/ |
97 |
|
|
transient int tail; |
98 |
jsr166 |
1.75 |
|
99 |
dl |
1.1 |
/** |
100 |
jsr166 |
1.75 |
* The maximum size of array to allocate. |
101 |
|
|
* Some VMs reserve some header words in an array. |
102 |
|
|
* Attempts to allocate larger arrays may result in |
103 |
|
|
* OutOfMemoryError: Requested array size exceeds VM limit |
104 |
dl |
1.1 |
*/ |
105 |
jsr166 |
1.75 |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
106 |
dl |
1.1 |
|
107 |
|
|
/** |
108 |
jsr166 |
1.75 |
* Increases the capacity of this deque by at least the given amount. |
109 |
|
|
* |
110 |
|
|
* @param needed the required minimum extra capacity; must be positive |
111 |
dl |
1.1 |
*/ |
112 |
jsr166 |
1.106 |
private void grow(int needed) { |
113 |
jsr166 |
1.75 |
// overflow-conscious code |
114 |
jsr166 |
1.91 |
final int oldCapacity = elements.length; |
115 |
jsr166 |
1.75 |
int newCapacity; |
116 |
jsr166 |
1.106 |
// Double capacity if small; else grow by 50% |
117 |
jsr166 |
1.75 |
int jump = (oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1); |
118 |
|
|
if (jump < needed |
119 |
|
|
|| (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0) |
120 |
|
|
newCapacity = newCapacity(needed, jump); |
121 |
jsr166 |
1.125 |
final Object[] es = elements = Arrays.copyOf(elements, newCapacity); |
122 |
jsr166 |
1.106 |
// Exceptionally, here tail == head needs to be disambiguated |
123 |
jsr166 |
1.125 |
if (tail < head || (tail == head && es[head] != null)) { |
124 |
jsr166 |
1.75 |
// wrap around; slide first leg forward to end of array |
125 |
|
|
int newSpace = newCapacity - oldCapacity; |
126 |
jsr166 |
1.125 |
System.arraycopy(es, head, |
127 |
|
|
es, head + newSpace, |
128 |
jsr166 |
1.75 |
oldCapacity - head); |
129 |
jsr166 |
1.125 |
for (int i = head, to = (head += newSpace); i < to; i++) |
130 |
|
|
es[i] = null; |
131 |
jsr166 |
1.75 |
} |
132 |
|
|
// checkInvariants(); |
133 |
|
|
} |
134 |
dl |
1.1 |
|
135 |
jsr166 |
1.75 |
/** Capacity calculation for edge conditions, especially overflow. */ |
136 |
|
|
private int newCapacity(int needed, int jump) { |
137 |
jsr166 |
1.91 |
final int oldCapacity = elements.length, minCapacity; |
138 |
jsr166 |
1.75 |
if ((minCapacity = oldCapacity + needed) - MAX_ARRAY_SIZE > 0) { |
139 |
|
|
if (minCapacity < 0) |
140 |
|
|
throw new IllegalStateException("Sorry, deque too big"); |
141 |
|
|
return Integer.MAX_VALUE; |
142 |
|
|
} |
143 |
|
|
if (needed > jump) |
144 |
|
|
return minCapacity; |
145 |
|
|
return (oldCapacity + jump - MAX_ARRAY_SIZE < 0) |
146 |
|
|
? oldCapacity + jump |
147 |
|
|
: MAX_ARRAY_SIZE; |
148 |
|
|
} |
149 |
dl |
1.1 |
|
150 |
|
|
/** |
151 |
jsr166 |
1.75 |
* Increases the internal storage of this collection, if necessary, |
152 |
|
|
* to ensure that it can hold at least the given number of elements. |
153 |
dl |
1.1 |
* |
154 |
jsr166 |
1.75 |
* @param minCapacity the desired minimum capacity |
155 |
jsr166 |
1.80 |
* @since TBD |
156 |
dl |
1.1 |
*/ |
157 |
jsr166 |
1.80 |
/* public */ void ensureCapacity(int minCapacity) { |
158 |
jsr166 |
1.106 |
int needed; |
159 |
|
|
if ((needed = (minCapacity + 1 - elements.length)) > 0) |
160 |
|
|
grow(needed); |
161 |
jsr166 |
1.75 |
// checkInvariants(); |
162 |
dl |
1.1 |
} |
163 |
|
|
|
164 |
|
|
/** |
165 |
jsr166 |
1.75 |
* Minimizes the internal storage of this collection. |
166 |
jsr166 |
1.80 |
* |
167 |
|
|
* @since TBD |
168 |
dl |
1.1 |
*/ |
169 |
jsr166 |
1.80 |
/* public */ void trimToSize() { |
170 |
jsr166 |
1.106 |
int size; |
171 |
|
|
if ((size = size()) + 1 < elements.length) { |
172 |
|
|
elements = toArray(new Object[size + 1]); |
173 |
jsr166 |
1.75 |
head = 0; |
174 |
jsr166 |
1.106 |
tail = size; |
175 |
jsr166 |
1.75 |
} |
176 |
|
|
// checkInvariants(); |
177 |
dl |
1.1 |
} |
178 |
|
|
|
179 |
|
|
/** |
180 |
dl |
1.4 |
* Constructs an empty array deque with an initial capacity |
181 |
dl |
1.1 |
* sufficient to hold 16 elements. |
182 |
|
|
*/ |
183 |
|
|
public ArrayDeque() { |
184 |
jsr166 |
1.134 |
elements = new Object[16 + 1]; |
185 |
dl |
1.1 |
} |
186 |
|
|
|
187 |
|
|
/** |
188 |
|
|
* Constructs an empty array deque with an initial capacity |
189 |
|
|
* sufficient to hold the specified number of elements. |
190 |
|
|
* |
191 |
jsr166 |
1.75 |
* @param numElements lower bound on initial capacity of the deque |
192 |
dl |
1.1 |
*/ |
193 |
|
|
public ArrayDeque(int numElements) { |
194 |
jsr166 |
1.114 |
elements = |
195 |
|
|
new Object[(numElements < 1) ? 1 : |
196 |
|
|
(numElements == Integer.MAX_VALUE) ? Integer.MAX_VALUE : |
197 |
|
|
numElements + 1]; |
198 |
dl |
1.1 |
} |
199 |
|
|
|
200 |
|
|
/** |
201 |
|
|
* Constructs a deque containing the elements of the specified |
202 |
|
|
* collection, in the order they are returned by the collection's |
203 |
|
|
* iterator. (The first element returned by the collection's |
204 |
|
|
* iterator becomes the first element, or <i>front</i> of the |
205 |
|
|
* deque.) |
206 |
|
|
* |
207 |
|
|
* @param c the collection whose elements are to be placed into the deque |
208 |
|
|
* @throws NullPointerException if the specified collection is null |
209 |
|
|
*/ |
210 |
|
|
public ArrayDeque(Collection<? extends E> c) { |
211 |
jsr166 |
1.114 |
this(c.size()); |
212 |
jsr166 |
1.133 |
copyElements(c); |
213 |
jsr166 |
1.75 |
} |
214 |
|
|
|
215 |
|
|
/** |
216 |
jsr166 |
1.131 |
* Circularly increments i, mod modulus. |
217 |
jsr166 |
1.79 |
* Precondition and postcondition: 0 <= i < modulus. |
218 |
jsr166 |
1.75 |
*/ |
219 |
jsr166 |
1.79 |
static final int inc(int i, int modulus) { |
220 |
jsr166 |
1.89 |
if (++i >= modulus) i = 0; |
221 |
jsr166 |
1.79 |
return i; |
222 |
jsr166 |
1.75 |
} |
223 |
|
|
|
224 |
|
|
/** |
225 |
jsr166 |
1.131 |
* Circularly decrements i, mod modulus. |
226 |
jsr166 |
1.79 |
* Precondition and postcondition: 0 <= i < modulus. |
227 |
jsr166 |
1.75 |
*/ |
228 |
jsr166 |
1.79 |
static final int dec(int i, int modulus) { |
229 |
jsr166 |
1.89 |
if (--i < 0) i = modulus - 1; |
230 |
jsr166 |
1.75 |
return i; |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
/** |
234 |
jsr166 |
1.114 |
* Circularly adds the given distance to index i, mod modulus. |
235 |
|
|
* Precondition: 0 <= i < modulus, 0 <= distance <= modulus. |
236 |
|
|
* @return index 0 <= i < modulus |
237 |
jsr166 |
1.75 |
*/ |
238 |
jsr166 |
1.131 |
static final int inc(int i, int distance, int modulus) { |
239 |
jsr166 |
1.124 |
if ((i += distance) - modulus >= 0) i -= modulus; |
240 |
jsr166 |
1.75 |
return i; |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
/** |
244 |
jsr166 |
1.106 |
* Subtracts j from i, mod modulus. |
245 |
jsr166 |
1.114 |
* Index i must be logically ahead of index j. |
246 |
|
|
* Precondition: 0 <= i < modulus, 0 <= j < modulus. |
247 |
|
|
* @return the "circular distance" from j to i; corner case i == j |
248 |
jsr166 |
1.130 |
* is disambiguated to "empty", returning 0. |
249 |
jsr166 |
1.106 |
*/ |
250 |
|
|
static final int sub(int i, int j, int modulus) { |
251 |
|
|
if ((i -= j) < 0) i += modulus; |
252 |
|
|
return i; |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
/** |
256 |
jsr166 |
1.75 |
* Returns element at array index i. |
257 |
jsr166 |
1.106 |
* This is a slight abuse of generics, accepted by javac. |
258 |
jsr166 |
1.75 |
*/ |
259 |
|
|
@SuppressWarnings("unchecked") |
260 |
jsr166 |
1.106 |
static final <E> E elementAt(Object[] es, int i) { |
261 |
|
|
return (E) es[i]; |
262 |
jsr166 |
1.75 |
} |
263 |
|
|
|
264 |
|
|
/** |
265 |
|
|
* A version of elementAt that checks for null elements. |
266 |
|
|
* This check doesn't catch all possible comodifications, |
267 |
jsr166 |
1.106 |
* but does catch ones that corrupt traversal. |
268 |
jsr166 |
1.75 |
*/ |
269 |
jsr166 |
1.96 |
static final <E> E nonNullElementAt(Object[] es, int i) { |
270 |
|
|
@SuppressWarnings("unchecked") E e = (E) es[i]; |
271 |
jsr166 |
1.75 |
if (e == null) |
272 |
|
|
throw new ConcurrentModificationException(); |
273 |
|
|
return e; |
274 |
dl |
1.1 |
} |
275 |
|
|
|
276 |
|
|
// The main insertion and extraction methods are addFirst, |
277 |
|
|
// addLast, pollFirst, pollLast. The other methods are defined in |
278 |
|
|
// terms of these. |
279 |
|
|
|
280 |
|
|
/** |
281 |
dl |
1.5 |
* Inserts the specified element at the front of this deque. |
282 |
dl |
1.1 |
* |
283 |
jsr166 |
1.9 |
* @param e the element to add |
284 |
|
|
* @throws NullPointerException if the specified element is null |
285 |
dl |
1.1 |
*/ |
286 |
|
|
public void addFirst(E e) { |
287 |
jsr166 |
1.106 |
if (e == null) |
288 |
|
|
throw new NullPointerException(); |
289 |
|
|
final Object[] es = elements; |
290 |
|
|
es[head = dec(head, es.length)] = e; |
291 |
|
|
if (head == tail) |
292 |
|
|
grow(1); |
293 |
jsr166 |
1.83 |
// checkInvariants(); |
294 |
dl |
1.1 |
} |
295 |
|
|
|
296 |
|
|
/** |
297 |
dl |
1.6 |
* Inserts the specified element at the end of this deque. |
298 |
jsr166 |
1.14 |
* |
299 |
|
|
* <p>This method is equivalent to {@link #add}. |
300 |
dl |
1.1 |
* |
301 |
jsr166 |
1.9 |
* @param e the element to add |
302 |
|
|
* @throws NullPointerException if the specified element is null |
303 |
dl |
1.1 |
*/ |
304 |
|
|
public void addLast(E e) { |
305 |
jsr166 |
1.106 |
if (e == null) |
306 |
|
|
throw new NullPointerException(); |
307 |
|
|
final Object[] es = elements; |
308 |
|
|
es[tail] = e; |
309 |
|
|
if (head == (tail = inc(tail, es.length))) |
310 |
|
|
grow(1); |
311 |
jsr166 |
1.83 |
// checkInvariants(); |
312 |
jsr166 |
1.75 |
} |
313 |
|
|
|
314 |
|
|
/** |
315 |
|
|
* Adds all of the elements in the specified collection at the end |
316 |
|
|
* of this deque, as if by calling {@link #addLast} on each one, |
317 |
jsr166 |
1.129 |
* in the order that they are returned by the collection's iterator. |
318 |
jsr166 |
1.75 |
* |
319 |
|
|
* @param c the elements to be inserted into this deque |
320 |
|
|
* @return {@code true} if this deque changed as a result of the call |
321 |
|
|
* @throws NullPointerException if the specified collection or any |
322 |
|
|
* of its elements are null |
323 |
|
|
*/ |
324 |
|
|
public boolean addAll(Collection<? extends E> c) { |
325 |
jsr166 |
1.117 |
final int s, needed; |
326 |
|
|
if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0) |
327 |
jsr166 |
1.87 |
grow(needed); |
328 |
jsr166 |
1.133 |
copyElements(c); |
329 |
jsr166 |
1.75 |
// checkInvariants(); |
330 |
jsr166 |
1.106 |
return size() > s; |
331 |
dl |
1.1 |
} |
332 |
|
|
|
333 |
jsr166 |
1.133 |
private void copyElements(Collection<? extends E> c) { |
334 |
|
|
c.forEach(this::addLast); |
335 |
|
|
} |
336 |
|
|
|
337 |
dl |
1.1 |
/** |
338 |
dl |
1.5 |
* Inserts the specified element at the front of this deque. |
339 |
dl |
1.1 |
* |
340 |
jsr166 |
1.9 |
* @param e the element to add |
341 |
jsr166 |
1.40 |
* @return {@code true} (as specified by {@link Deque#offerFirst}) |
342 |
jsr166 |
1.9 |
* @throws NullPointerException if the specified element is null |
343 |
dl |
1.1 |
*/ |
344 |
|
|
public boolean offerFirst(E e) { |
345 |
|
|
addFirst(e); |
346 |
|
|
return true; |
347 |
|
|
} |
348 |
|
|
|
349 |
|
|
/** |
350 |
dl |
1.6 |
* Inserts the specified element at the end of this deque. |
351 |
dl |
1.1 |
* |
352 |
jsr166 |
1.9 |
* @param e the element to add |
353 |
jsr166 |
1.40 |
* @return {@code true} (as specified by {@link Deque#offerLast}) |
354 |
jsr166 |
1.9 |
* @throws NullPointerException if the specified element is null |
355 |
dl |
1.1 |
*/ |
356 |
|
|
public boolean offerLast(E e) { |
357 |
|
|
addLast(e); |
358 |
|
|
return true; |
359 |
|
|
} |
360 |
|
|
|
361 |
|
|
/** |
362 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
363 |
dl |
1.1 |
*/ |
364 |
|
|
public E removeFirst() { |
365 |
jsr166 |
1.84 |
E e = pollFirst(); |
366 |
|
|
if (e == null) |
367 |
dl |
1.1 |
throw new NoSuchElementException(); |
368 |
jsr166 |
1.106 |
// checkInvariants(); |
369 |
jsr166 |
1.84 |
return e; |
370 |
dl |
1.1 |
} |
371 |
|
|
|
372 |
|
|
/** |
373 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
374 |
dl |
1.1 |
*/ |
375 |
|
|
public E removeLast() { |
376 |
jsr166 |
1.84 |
E e = pollLast(); |
377 |
|
|
if (e == null) |
378 |
dl |
1.1 |
throw new NoSuchElementException(); |
379 |
jsr166 |
1.106 |
// checkInvariants(); |
380 |
jsr166 |
1.84 |
return e; |
381 |
dl |
1.1 |
} |
382 |
|
|
|
383 |
jsr166 |
1.9 |
public E pollFirst() { |
384 |
jsr166 |
1.106 |
final Object[] es; |
385 |
|
|
final int h; |
386 |
|
|
E e = elementAt(es = elements, h = head); |
387 |
|
|
if (e != null) { |
388 |
|
|
es[h] = null; |
389 |
|
|
head = inc(h, es.length); |
390 |
|
|
} |
391 |
jsr166 |
1.75 |
// checkInvariants(); |
392 |
|
|
return e; |
393 |
dl |
1.1 |
} |
394 |
|
|
|
395 |
jsr166 |
1.9 |
public E pollLast() { |
396 |
jsr166 |
1.106 |
final Object[] es; |
397 |
|
|
final int t; |
398 |
|
|
E e = elementAt(es = elements, t = dec(tail, es.length)); |
399 |
|
|
if (e != null) |
400 |
|
|
es[tail = t] = null; |
401 |
jsr166 |
1.75 |
// checkInvariants(); |
402 |
|
|
return e; |
403 |
dl |
1.1 |
} |
404 |
|
|
|
405 |
|
|
/** |
406 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
407 |
dl |
1.1 |
*/ |
408 |
|
|
public E getFirst() { |
409 |
jsr166 |
1.106 |
E e = elementAt(elements, head); |
410 |
|
|
if (e == null) |
411 |
|
|
throw new NoSuchElementException(); |
412 |
jsr166 |
1.75 |
// checkInvariants(); |
413 |
jsr166 |
1.106 |
return e; |
414 |
dl |
1.1 |
} |
415 |
|
|
|
416 |
|
|
/** |
417 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
418 |
dl |
1.1 |
*/ |
419 |
|
|
public E getLast() { |
420 |
jsr166 |
1.106 |
final Object[] es = elements; |
421 |
|
|
E e = elementAt(es, dec(tail, es.length)); |
422 |
|
|
if (e == null) |
423 |
|
|
throw new NoSuchElementException(); |
424 |
jsr166 |
1.75 |
// checkInvariants(); |
425 |
jsr166 |
1.106 |
return e; |
426 |
dl |
1.1 |
} |
427 |
|
|
|
428 |
jsr166 |
1.9 |
public E peekFirst() { |
429 |
jsr166 |
1.75 |
// checkInvariants(); |
430 |
jsr166 |
1.106 |
return elementAt(elements, head); |
431 |
jsr166 |
1.9 |
} |
432 |
|
|
|
433 |
|
|
public E peekLast() { |
434 |
jsr166 |
1.75 |
// checkInvariants(); |
435 |
jsr166 |
1.106 |
final Object[] es; |
436 |
|
|
return elementAt(es = elements, dec(tail, es.length)); |
437 |
jsr166 |
1.9 |
} |
438 |
|
|
|
439 |
dl |
1.1 |
/** |
440 |
|
|
* Removes the first occurrence of the specified element in this |
441 |
jsr166 |
1.9 |
* deque (when traversing the deque from head to tail). |
442 |
|
|
* If the deque does not contain the element, it is unchanged. |
443 |
jsr166 |
1.40 |
* More formally, removes the first element {@code e} such that |
444 |
|
|
* {@code o.equals(e)} (if such an element exists). |
445 |
|
|
* Returns {@code true} if this deque contained the specified element |
446 |
jsr166 |
1.12 |
* (or equivalently, if this deque changed as a result of the call). |
447 |
dl |
1.1 |
* |
448 |
dl |
1.5 |
* @param o element to be removed from this deque, if present |
449 |
jsr166 |
1.40 |
* @return {@code true} if the deque contained the specified element |
450 |
dl |
1.1 |
*/ |
451 |
dl |
1.5 |
public boolean removeFirstOccurrence(Object o) { |
452 |
jsr166 |
1.58 |
if (o != null) { |
453 |
jsr166 |
1.99 |
final Object[] es = elements; |
454 |
jsr166 |
1.106 |
for (int i = head, end = tail, to = (i <= end) ? end : es.length; |
455 |
|
|
; i = 0, to = end) { |
456 |
jsr166 |
1.92 |
for (; i < to; i++) |
457 |
jsr166 |
1.99 |
if (o.equals(es[i])) { |
458 |
jsr166 |
1.89 |
delete(i); |
459 |
|
|
return true; |
460 |
|
|
} |
461 |
jsr166 |
1.106 |
if (to == end) break; |
462 |
dl |
1.1 |
} |
463 |
|
|
} |
464 |
|
|
return false; |
465 |
|
|
} |
466 |
|
|
|
467 |
|
|
/** |
468 |
|
|
* Removes the last occurrence of the specified element in this |
469 |
jsr166 |
1.9 |
* deque (when traversing the deque from head to tail). |
470 |
|
|
* If the deque does not contain the element, it is unchanged. |
471 |
jsr166 |
1.40 |
* More formally, removes the last element {@code e} such that |
472 |
|
|
* {@code o.equals(e)} (if such an element exists). |
473 |
|
|
* Returns {@code true} if this deque contained the specified element |
474 |
jsr166 |
1.12 |
* (or equivalently, if this deque changed as a result of the call). |
475 |
dl |
1.1 |
* |
476 |
dl |
1.5 |
* @param o element to be removed from this deque, if present |
477 |
jsr166 |
1.40 |
* @return {@code true} if the deque contained the specified element |
478 |
dl |
1.1 |
*/ |
479 |
dl |
1.5 |
public boolean removeLastOccurrence(Object o) { |
480 |
jsr166 |
1.59 |
if (o != null) { |
481 |
jsr166 |
1.99 |
final Object[] es = elements; |
482 |
jsr166 |
1.107 |
for (int i = tail, end = head, to = (i >= end) ? end : 0; |
483 |
|
|
; i = es.length, to = end) { |
484 |
jsr166 |
1.110 |
for (i--; i > to - 1; i--) |
485 |
jsr166 |
1.99 |
if (o.equals(es[i])) { |
486 |
jsr166 |
1.89 |
delete(i); |
487 |
|
|
return true; |
488 |
|
|
} |
489 |
jsr166 |
1.106 |
if (to == end) break; |
490 |
dl |
1.1 |
} |
491 |
|
|
} |
492 |
|
|
return false; |
493 |
|
|
} |
494 |
|
|
|
495 |
|
|
// *** Queue methods *** |
496 |
|
|
|
497 |
|
|
/** |
498 |
dl |
1.6 |
* Inserts the specified element at the end of this deque. |
499 |
dl |
1.1 |
* |
500 |
|
|
* <p>This method is equivalent to {@link #addLast}. |
501 |
|
|
* |
502 |
jsr166 |
1.9 |
* @param e the element to add |
503 |
jsr166 |
1.40 |
* @return {@code true} (as specified by {@link Collection#add}) |
504 |
jsr166 |
1.9 |
* @throws NullPointerException if the specified element is null |
505 |
dl |
1.1 |
*/ |
506 |
|
|
public boolean add(E e) { |
507 |
|
|
addLast(e); |
508 |
|
|
return true; |
509 |
|
|
} |
510 |
|
|
|
511 |
|
|
/** |
512 |
jsr166 |
1.9 |
* Inserts the specified element at the end of this deque. |
513 |
dl |
1.1 |
* |
514 |
jsr166 |
1.9 |
* <p>This method is equivalent to {@link #offerLast}. |
515 |
dl |
1.1 |
* |
516 |
jsr166 |
1.9 |
* @param e the element to add |
517 |
jsr166 |
1.40 |
* @return {@code true} (as specified by {@link Queue#offer}) |
518 |
jsr166 |
1.9 |
* @throws NullPointerException if the specified element is null |
519 |
dl |
1.1 |
*/ |
520 |
jsr166 |
1.9 |
public boolean offer(E e) { |
521 |
|
|
return offerLast(e); |
522 |
dl |
1.1 |
} |
523 |
|
|
|
524 |
|
|
/** |
525 |
|
|
* Retrieves and removes the head of the queue represented by this deque. |
526 |
jsr166 |
1.15 |
* |
527 |
jsr166 |
1.127 |
* This method differs from {@link #poll() poll()} only in that it |
528 |
|
|
* throws an exception if this deque is empty. |
529 |
dl |
1.1 |
* |
530 |
|
|
* <p>This method is equivalent to {@link #removeFirst}. |
531 |
|
|
* |
532 |
|
|
* @return the head of the queue represented by this deque |
533 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
534 |
dl |
1.1 |
*/ |
535 |
|
|
public E remove() { |
536 |
|
|
return removeFirst(); |
537 |
|
|
} |
538 |
|
|
|
539 |
|
|
/** |
540 |
jsr166 |
1.9 |
* Retrieves and removes the head of the queue represented by this deque |
541 |
|
|
* (in other words, the first element of this deque), or returns |
542 |
jsr166 |
1.40 |
* {@code null} if this deque is empty. |
543 |
dl |
1.1 |
* |
544 |
jsr166 |
1.9 |
* <p>This method is equivalent to {@link #pollFirst}. |
545 |
dl |
1.1 |
* |
546 |
|
|
* @return the head of the queue represented by this deque, or |
547 |
jsr166 |
1.40 |
* {@code null} if this deque is empty |
548 |
dl |
1.1 |
*/ |
549 |
jsr166 |
1.9 |
public E poll() { |
550 |
|
|
return pollFirst(); |
551 |
dl |
1.1 |
} |
552 |
|
|
|
553 |
|
|
/** |
554 |
|
|
* Retrieves, but does not remove, the head of the queue represented by |
555 |
jsr166 |
1.15 |
* this deque. This method differs from {@link #peek peek} only in |
556 |
|
|
* that it throws an exception if this deque is empty. |
557 |
dl |
1.1 |
* |
558 |
jsr166 |
1.8 |
* <p>This method is equivalent to {@link #getFirst}. |
559 |
dl |
1.1 |
* |
560 |
|
|
* @return the head of the queue represented by this deque |
561 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
562 |
dl |
1.1 |
*/ |
563 |
|
|
public E element() { |
564 |
|
|
return getFirst(); |
565 |
|
|
} |
566 |
|
|
|
567 |
jsr166 |
1.9 |
/** |
568 |
|
|
* Retrieves, but does not remove, the head of the queue represented by |
569 |
jsr166 |
1.40 |
* this deque, or returns {@code null} if this deque is empty. |
570 |
jsr166 |
1.9 |
* |
571 |
|
|
* <p>This method is equivalent to {@link #peekFirst}. |
572 |
|
|
* |
573 |
|
|
* @return the head of the queue represented by this deque, or |
574 |
jsr166 |
1.40 |
* {@code null} if this deque is empty |
575 |
jsr166 |
1.9 |
*/ |
576 |
|
|
public E peek() { |
577 |
|
|
return peekFirst(); |
578 |
|
|
} |
579 |
|
|
|
580 |
dl |
1.1 |
// *** Stack methods *** |
581 |
|
|
|
582 |
|
|
/** |
583 |
|
|
* Pushes an element onto the stack represented by this deque. In other |
584 |
dl |
1.5 |
* words, inserts the element at the front of this deque. |
585 |
dl |
1.1 |
* |
586 |
|
|
* <p>This method is equivalent to {@link #addFirst}. |
587 |
|
|
* |
588 |
|
|
* @param e the element to push |
589 |
jsr166 |
1.9 |
* @throws NullPointerException if the specified element is null |
590 |
dl |
1.1 |
*/ |
591 |
|
|
public void push(E e) { |
592 |
|
|
addFirst(e); |
593 |
|
|
} |
594 |
|
|
|
595 |
|
|
/** |
596 |
|
|
* Pops an element from the stack represented by this deque. In other |
597 |
dl |
1.2 |
* words, removes and returns the first element of this deque. |
598 |
dl |
1.1 |
* |
599 |
|
|
* <p>This method is equivalent to {@link #removeFirst()}. |
600 |
|
|
* |
601 |
|
|
* @return the element at the front of this deque (which is the top |
602 |
jsr166 |
1.9 |
* of the stack represented by this deque) |
603 |
|
|
* @throws NoSuchElementException {@inheritDoc} |
604 |
dl |
1.1 |
*/ |
605 |
|
|
public E pop() { |
606 |
|
|
return removeFirst(); |
607 |
|
|
} |
608 |
|
|
|
609 |
|
|
/** |
610 |
jsr166 |
1.75 |
* Removes the element at the specified position in the elements array. |
611 |
|
|
* This can result in forward or backwards motion of array elements. |
612 |
|
|
* We optimize for least element motion. |
613 |
dl |
1.1 |
* |
614 |
dl |
1.5 |
* <p>This method is called delete rather than remove to emphasize |
615 |
jsr166 |
1.9 |
* that its semantics differ from those of {@link List#remove(int)}. |
616 |
dl |
1.5 |
* |
617 |
jsr166 |
1.106 |
* @return true if elements near tail moved backwards |
618 |
dl |
1.1 |
*/ |
619 |
jsr166 |
1.71 |
boolean delete(int i) { |
620 |
jsr166 |
1.75 |
// checkInvariants(); |
621 |
jsr166 |
1.99 |
final Object[] es = elements; |
622 |
|
|
final int capacity = es.length; |
623 |
jsr166 |
1.118 |
final int h, t; |
624 |
jsr166 |
1.106 |
// number of elements before to-be-deleted elt |
625 |
jsr166 |
1.118 |
final int front = sub(i, h = head, capacity); |
626 |
|
|
// number of elements after to-be-deleted elt |
627 |
|
|
final int back = sub(t = tail, i, capacity) - 1; |
628 |
jsr166 |
1.30 |
if (front < back) { |
629 |
jsr166 |
1.75 |
// move front elements forwards |
630 |
jsr166 |
1.30 |
if (h <= i) { |
631 |
jsr166 |
1.99 |
System.arraycopy(es, h, es, h + 1, front); |
632 |
jsr166 |
1.30 |
} else { // Wrap around |
633 |
jsr166 |
1.99 |
System.arraycopy(es, 0, es, 1, i); |
634 |
|
|
es[0] = es[capacity - 1]; |
635 |
|
|
System.arraycopy(es, h, es, h + 1, front - (i + 1)); |
636 |
jsr166 |
1.30 |
} |
637 |
jsr166 |
1.99 |
es[h] = null; |
638 |
jsr166 |
1.106 |
head = inc(h, capacity); |
639 |
jsr166 |
1.75 |
// checkInvariants(); |
640 |
jsr166 |
1.30 |
return false; |
641 |
|
|
} else { |
642 |
jsr166 |
1.75 |
// move back elements backwards |
643 |
jsr166 |
1.118 |
tail = dec(t, capacity); |
644 |
jsr166 |
1.75 |
if (i <= tail) { |
645 |
jsr166 |
1.99 |
System.arraycopy(es, i + 1, es, i, back); |
646 |
jsr166 |
1.30 |
} else { // Wrap around |
647 |
jsr166 |
1.118 |
System.arraycopy(es, i + 1, es, i, capacity - (i + 1)); |
648 |
jsr166 |
1.99 |
es[capacity - 1] = es[0]; |
649 |
jsr166 |
1.118 |
System.arraycopy(es, 1, es, 0, t - 1); |
650 |
jsr166 |
1.30 |
} |
651 |
jsr166 |
1.99 |
es[tail] = null; |
652 |
jsr166 |
1.75 |
// checkInvariants(); |
653 |
jsr166 |
1.30 |
return true; |
654 |
|
|
} |
655 |
dl |
1.23 |
} |
656 |
|
|
|
657 |
dl |
1.1 |
// *** Collection Methods *** |
658 |
|
|
|
659 |
|
|
/** |
660 |
|
|
* Returns the number of elements in this deque. |
661 |
|
|
* |
662 |
|
|
* @return the number of elements in this deque |
663 |
|
|
*/ |
664 |
|
|
public int size() { |
665 |
jsr166 |
1.106 |
return sub(tail, head, elements.length); |
666 |
dl |
1.1 |
} |
667 |
|
|
|
668 |
|
|
/** |
669 |
jsr166 |
1.40 |
* Returns {@code true} if this deque contains no elements. |
670 |
dl |
1.1 |
* |
671 |
jsr166 |
1.40 |
* @return {@code true} if this deque contains no elements |
672 |
dl |
1.1 |
*/ |
673 |
|
|
public boolean isEmpty() { |
674 |
jsr166 |
1.106 |
return head == tail; |
675 |
dl |
1.1 |
} |
676 |
|
|
|
677 |
|
|
/** |
678 |
|
|
* Returns an iterator over the elements in this deque. The elements |
679 |
|
|
* will be ordered from first (head) to last (tail). This is the same |
680 |
|
|
* order that elements would be dequeued (via successive calls to |
681 |
|
|
* {@link #remove} or popped (via successive calls to {@link #pop}). |
682 |
dl |
1.5 |
* |
683 |
jsr166 |
1.18 |
* @return an iterator over the elements in this deque |
684 |
dl |
1.1 |
*/ |
685 |
|
|
public Iterator<E> iterator() { |
686 |
|
|
return new DeqIterator(); |
687 |
|
|
} |
688 |
|
|
|
689 |
dl |
1.16 |
public Iterator<E> descendingIterator() { |
690 |
|
|
return new DescendingIterator(); |
691 |
|
|
} |
692 |
|
|
|
693 |
dl |
1.1 |
private class DeqIterator implements Iterator<E> { |
694 |
jsr166 |
1.75 |
/** Index of element to be returned by subsequent call to next. */ |
695 |
|
|
int cursor; |
696 |
dl |
1.1 |
|
697 |
jsr166 |
1.75 |
/** Number of elements yet to be returned. */ |
698 |
jsr166 |
1.106 |
int remaining = size(); |
699 |
dl |
1.1 |
|
700 |
|
|
/** |
701 |
|
|
* Index of element returned by most recent call to next. |
702 |
|
|
* Reset to -1 if element is deleted by a call to remove. |
703 |
|
|
*/ |
704 |
jsr166 |
1.75 |
int lastRet = -1; |
705 |
dl |
1.1 |
|
706 |
jsr166 |
1.75 |
DeqIterator() { cursor = head; } |
707 |
|
|
|
708 |
|
|
public final boolean hasNext() { |
709 |
|
|
return remaining > 0; |
710 |
|
|
} |
711 |
|
|
|
712 |
jsr166 |
1.81 |
public E next() { |
713 |
jsr166 |
1.91 |
if (remaining <= 0) |
714 |
dl |
1.1 |
throw new NoSuchElementException(); |
715 |
jsr166 |
1.99 |
final Object[] es = elements; |
716 |
|
|
E e = nonNullElementAt(es, cursor); |
717 |
jsr166 |
1.122 |
cursor = inc(lastRet = cursor, es.length); |
718 |
jsr166 |
1.75 |
remaining--; |
719 |
|
|
return e; |
720 |
dl |
1.1 |
} |
721 |
|
|
|
722 |
jsr166 |
1.81 |
void postDelete(boolean leftShifted) { |
723 |
|
|
if (leftShifted) |
724 |
jsr166 |
1.106 |
cursor = dec(cursor, elements.length); |
725 |
jsr166 |
1.81 |
} |
726 |
|
|
|
727 |
jsr166 |
1.75 |
public final void remove() { |
728 |
dl |
1.1 |
if (lastRet < 0) |
729 |
|
|
throw new IllegalStateException(); |
730 |
jsr166 |
1.81 |
postDelete(delete(lastRet)); |
731 |
dl |
1.1 |
lastRet = -1; |
732 |
|
|
} |
733 |
jsr166 |
1.68 |
|
734 |
jsr166 |
1.81 |
public void forEachRemaining(Consumer<? super E> action) { |
735 |
jsr166 |
1.98 |
Objects.requireNonNull(action); |
736 |
jsr166 |
1.106 |
int r; |
737 |
|
|
if ((r = remaining) <= 0) |
738 |
|
|
return; |
739 |
|
|
remaining = 0; |
740 |
|
|
final Object[] es = elements; |
741 |
|
|
if (es[cursor] == null || sub(tail, cursor, es.length) != r) |
742 |
|
|
throw new ConcurrentModificationException(); |
743 |
|
|
for (int i = cursor, end = tail, to = (i <= end) ? end : es.length; |
744 |
|
|
; i = 0, to = end) { |
745 |
|
|
for (; i < to; i++) |
746 |
|
|
action.accept(elementAt(es, i)); |
747 |
|
|
if (to == end) { |
748 |
|
|
if (end != tail) |
749 |
|
|
throw new ConcurrentModificationException(); |
750 |
|
|
lastRet = dec(end, es.length); |
751 |
|
|
break; |
752 |
|
|
} |
753 |
jsr166 |
1.89 |
} |
754 |
jsr166 |
1.68 |
} |
755 |
dl |
1.1 |
} |
756 |
|
|
|
757 |
jsr166 |
1.75 |
private class DescendingIterator extends DeqIterator { |
758 |
jsr166 |
1.109 |
DescendingIterator() { cursor = dec(tail, elements.length); } |
759 |
jsr166 |
1.75 |
|
760 |
jsr166 |
1.81 |
public final E next() { |
761 |
jsr166 |
1.91 |
if (remaining <= 0) |
762 |
jsr166 |
1.81 |
throw new NoSuchElementException(); |
763 |
jsr166 |
1.99 |
final Object[] es = elements; |
764 |
|
|
E e = nonNullElementAt(es, cursor); |
765 |
jsr166 |
1.123 |
cursor = dec(lastRet = cursor, es.length); |
766 |
jsr166 |
1.81 |
remaining--; |
767 |
|
|
return e; |
768 |
jsr166 |
1.75 |
} |
769 |
|
|
|
770 |
jsr166 |
1.81 |
void postDelete(boolean leftShifted) { |
771 |
|
|
if (!leftShifted) |
772 |
jsr166 |
1.106 |
cursor = inc(cursor, elements.length); |
773 |
jsr166 |
1.81 |
} |
774 |
|
|
|
775 |
|
|
public final void forEachRemaining(Consumer<? super E> action) { |
776 |
jsr166 |
1.98 |
Objects.requireNonNull(action); |
777 |
jsr166 |
1.106 |
int r; |
778 |
|
|
if ((r = remaining) <= 0) |
779 |
|
|
return; |
780 |
|
|
remaining = 0; |
781 |
|
|
final Object[] es = elements; |
782 |
|
|
if (es[cursor] == null || sub(cursor, head, es.length) + 1 != r) |
783 |
|
|
throw new ConcurrentModificationException(); |
784 |
jsr166 |
1.108 |
for (int i = cursor, end = head, to = (i >= end) ? end : 0; |
785 |
jsr166 |
1.106 |
; i = es.length - 1, to = end) { |
786 |
jsr166 |
1.110 |
// hotspot generates faster code than for: i >= to ! |
787 |
|
|
for (; i > to - 1; i--) |
788 |
jsr166 |
1.106 |
action.accept(elementAt(es, i)); |
789 |
|
|
if (to == end) { |
790 |
jsr166 |
1.108 |
if (end != head) |
791 |
jsr166 |
1.106 |
throw new ConcurrentModificationException(); |
792 |
jsr166 |
1.112 |
lastRet = end; |
793 |
jsr166 |
1.106 |
break; |
794 |
jsr166 |
1.98 |
} |
795 |
jsr166 |
1.89 |
} |
796 |
jsr166 |
1.75 |
} |
797 |
|
|
} |
798 |
|
|
|
799 |
jsr166 |
1.52 |
/** |
800 |
jsr166 |
1.75 |
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
801 |
|
|
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
802 |
|
|
* deque. |
803 |
|
|
* |
804 |
|
|
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
805 |
|
|
* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and |
806 |
|
|
* {@link Spliterator#NONNULL}. Overriding implementations should document |
807 |
|
|
* the reporting of additional characteristic values. |
808 |
|
|
* |
809 |
|
|
* @return a {@code Spliterator} over the elements in this deque |
810 |
|
|
* @since 1.8 |
811 |
jsr166 |
1.52 |
*/ |
812 |
jsr166 |
1.75 |
public Spliterator<E> spliterator() { |
813 |
jsr166 |
1.106 |
return new DeqSpliterator(); |
814 |
jsr166 |
1.75 |
} |
815 |
|
|
|
816 |
jsr166 |
1.106 |
final class DeqSpliterator implements Spliterator<E> { |
817 |
|
|
private int fence; // -1 until first use |
818 |
|
|
private int cursor; // current index, modified on traverse/split |
819 |
jsr166 |
1.75 |
|
820 |
jsr166 |
1.76 |
/** Constructs late-binding spliterator over all elements. */ |
821 |
jsr166 |
1.106 |
DeqSpliterator() { |
822 |
|
|
this.fence = -1; |
823 |
jsr166 |
1.76 |
} |
824 |
|
|
|
825 |
jsr166 |
1.106 |
/** Constructs spliterator over the given range. */ |
826 |
|
|
DeqSpliterator(int origin, int fence) { |
827 |
jsr166 |
1.124 |
// assert 0 <= origin && origin < elements.length; |
828 |
|
|
// assert 0 <= fence && fence < elements.length; |
829 |
jsr166 |
1.106 |
this.cursor = origin; |
830 |
|
|
this.fence = fence; |
831 |
jsr166 |
1.75 |
} |
832 |
|
|
|
833 |
jsr166 |
1.106 |
/** Ensures late-binding initialization; then returns fence. */ |
834 |
|
|
private int getFence() { // force initialization |
835 |
|
|
int t; |
836 |
|
|
if ((t = fence) < 0) { |
837 |
|
|
t = fence = tail; |
838 |
jsr166 |
1.76 |
cursor = head; |
839 |
|
|
} |
840 |
jsr166 |
1.106 |
return t; |
841 |
jsr166 |
1.76 |
} |
842 |
|
|
|
843 |
jsr166 |
1.106 |
public DeqSpliterator trySplit() { |
844 |
|
|
final Object[] es = elements; |
845 |
|
|
final int i, n; |
846 |
|
|
return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0) |
847 |
|
|
? null |
848 |
jsr166 |
1.131 |
: new DeqSpliterator(i, cursor = inc(i, n, es.length)); |
849 |
jsr166 |
1.75 |
} |
850 |
dl |
1.16 |
|
851 |
jsr166 |
1.75 |
public void forEachRemaining(Consumer<? super E> action) { |
852 |
jsr166 |
1.106 |
if (action == null) |
853 |
|
|
throw new NullPointerException(); |
854 |
|
|
final int end = getFence(), cursor = this.cursor; |
855 |
|
|
final Object[] es = elements; |
856 |
|
|
if (cursor != end) { |
857 |
|
|
this.cursor = end; |
858 |
|
|
// null check at both ends of range is sufficient |
859 |
|
|
if (es[cursor] == null || es[dec(end, es.length)] == null) |
860 |
|
|
throw new ConcurrentModificationException(); |
861 |
|
|
for (int i = cursor, to = (i <= end) ? end : es.length; |
862 |
|
|
; i = 0, to = end) { |
863 |
|
|
for (; i < to; i++) |
864 |
|
|
action.accept(elementAt(es, i)); |
865 |
|
|
if (to == end) break; |
866 |
|
|
} |
867 |
|
|
} |
868 |
dl |
1.16 |
} |
869 |
|
|
|
870 |
jsr166 |
1.75 |
public boolean tryAdvance(Consumer<? super E> action) { |
871 |
jsr166 |
1.121 |
Objects.requireNonNull(action); |
872 |
|
|
final Object[] es = elements; |
873 |
|
|
if (fence < 0) { fence = tail; cursor = head; } // late-binding |
874 |
|
|
final int i; |
875 |
|
|
if ((i = cursor) == fence) |
876 |
jsr166 |
1.75 |
return false; |
877 |
jsr166 |
1.121 |
E e = nonNullElementAt(es, i); |
878 |
jsr166 |
1.106 |
cursor = inc(i, es.length); |
879 |
jsr166 |
1.121 |
action.accept(e); |
880 |
jsr166 |
1.75 |
return true; |
881 |
|
|
} |
882 |
|
|
|
883 |
|
|
public long estimateSize() { |
884 |
jsr166 |
1.106 |
return sub(getFence(), cursor, elements.length); |
885 |
jsr166 |
1.75 |
} |
886 |
|
|
|
887 |
|
|
public int characteristics() { |
888 |
|
|
return Spliterator.NONNULL |
889 |
|
|
| Spliterator.ORDERED |
890 |
|
|
| Spliterator.SIZED |
891 |
|
|
| Spliterator.SUBSIZED; |
892 |
dl |
1.16 |
} |
893 |
jsr166 |
1.75 |
} |
894 |
dl |
1.16 |
|
895 |
jsr166 |
1.126 |
/** |
896 |
|
|
* @throws NullPointerException {@inheritDoc} |
897 |
|
|
*/ |
898 |
jsr166 |
1.75 |
public void forEach(Consumer<? super E> action) { |
899 |
|
|
Objects.requireNonNull(action); |
900 |
jsr166 |
1.99 |
final Object[] es = elements; |
901 |
jsr166 |
1.106 |
for (int i = head, end = tail, to = (i <= end) ? end : es.length; |
902 |
|
|
; i = 0, to = end) { |
903 |
jsr166 |
1.92 |
for (; i < to; i++) |
904 |
jsr166 |
1.106 |
action.accept(elementAt(es, i)); |
905 |
|
|
if (to == end) { |
906 |
|
|
if (end != tail) throw new ConcurrentModificationException(); |
907 |
|
|
break; |
908 |
|
|
} |
909 |
jsr166 |
1.89 |
} |
910 |
jsr166 |
1.75 |
// checkInvariants(); |
911 |
|
|
} |
912 |
|
|
|
913 |
|
|
/** |
914 |
|
|
* Replaces each element of this deque with the result of applying the |
915 |
|
|
* operator to that element, as specified by {@link List#replaceAll}. |
916 |
|
|
* |
917 |
|
|
* @param operator the operator to apply to each element |
918 |
jsr166 |
1.80 |
* @since TBD |
919 |
jsr166 |
1.75 |
*/ |
920 |
jsr166 |
1.80 |
/* public */ void replaceAll(UnaryOperator<E> operator) { |
921 |
jsr166 |
1.75 |
Objects.requireNonNull(operator); |
922 |
jsr166 |
1.99 |
final Object[] es = elements; |
923 |
jsr166 |
1.106 |
for (int i = head, end = tail, to = (i <= end) ? end : es.length; |
924 |
|
|
; i = 0, to = end) { |
925 |
jsr166 |
1.92 |
for (; i < to; i++) |
926 |
jsr166 |
1.106 |
es[i] = operator.apply(elementAt(es, i)); |
927 |
|
|
if (to == end) { |
928 |
|
|
if (end != tail) throw new ConcurrentModificationException(); |
929 |
|
|
break; |
930 |
|
|
} |
931 |
jsr166 |
1.89 |
} |
932 |
jsr166 |
1.75 |
// checkInvariants(); |
933 |
|
|
} |
934 |
|
|
|
935 |
|
|
/** |
936 |
|
|
* @throws NullPointerException {@inheritDoc} |
937 |
|
|
*/ |
938 |
|
|
public boolean removeIf(Predicate<? super E> filter) { |
939 |
|
|
Objects.requireNonNull(filter); |
940 |
|
|
return bulkRemove(filter); |
941 |
|
|
} |
942 |
|
|
|
943 |
|
|
/** |
944 |
|
|
* @throws NullPointerException {@inheritDoc} |
945 |
|
|
*/ |
946 |
|
|
public boolean removeAll(Collection<?> c) { |
947 |
|
|
Objects.requireNonNull(c); |
948 |
|
|
return bulkRemove(e -> c.contains(e)); |
949 |
|
|
} |
950 |
|
|
|
951 |
|
|
/** |
952 |
|
|
* @throws NullPointerException {@inheritDoc} |
953 |
|
|
*/ |
954 |
|
|
public boolean retainAll(Collection<?> c) { |
955 |
|
|
Objects.requireNonNull(c); |
956 |
|
|
return bulkRemove(e -> !c.contains(e)); |
957 |
|
|
} |
958 |
|
|
|
959 |
|
|
/** Implementation of bulk remove methods. */ |
960 |
|
|
private boolean bulkRemove(Predicate<? super E> filter) { |
961 |
|
|
// checkInvariants(); |
962 |
jsr166 |
1.99 |
final Object[] es = elements; |
963 |
jsr166 |
1.106 |
// Optimize for initial run of survivors |
964 |
|
|
for (int i = head, end = tail, to = (i <= end) ? end : es.length; |
965 |
|
|
; i = 0, to = end) { |
966 |
jsr166 |
1.104 |
for (; i < to; i++) |
967 |
jsr166 |
1.106 |
if (filter.test(elementAt(es, i))) |
968 |
jsr166 |
1.113 |
return bulkRemoveModified(filter, i); |
969 |
jsr166 |
1.106 |
if (to == end) { |
970 |
|
|
if (end != tail) throw new ConcurrentModificationException(); |
971 |
|
|
break; |
972 |
|
|
} |
973 |
jsr166 |
1.104 |
} |
974 |
jsr166 |
1.106 |
return false; |
975 |
jsr166 |
1.104 |
} |
976 |
|
|
|
977 |
jsr166 |
1.113 |
// A tiny bit set implementation |
978 |
|
|
|
979 |
|
|
private static long[] nBits(int n) { |
980 |
|
|
return new long[((n - 1) >> 6) + 1]; |
981 |
|
|
} |
982 |
|
|
private static void setBit(long[] bits, int i) { |
983 |
|
|
bits[i >> 6] |= 1L << i; |
984 |
|
|
} |
985 |
|
|
private static boolean isClear(long[] bits, int i) { |
986 |
|
|
return (bits[i >> 6] & (1L << i)) == 0; |
987 |
|
|
} |
988 |
|
|
|
989 |
jsr166 |
1.104 |
/** |
990 |
|
|
* Helper for bulkRemove, in case of at least one deletion. |
991 |
jsr166 |
1.113 |
* Tolerate predicates that reentrantly access the collection for |
992 |
|
|
* read (but writers still get CME), so traverse once to find |
993 |
|
|
* elements to delete, a second pass to physically expunge. |
994 |
|
|
* |
995 |
|
|
* @param beg valid index of first element to be deleted |
996 |
jsr166 |
1.104 |
*/ |
997 |
jsr166 |
1.106 |
private boolean bulkRemoveModified( |
998 |
jsr166 |
1.113 |
Predicate<? super E> filter, final int beg) { |
999 |
jsr166 |
1.104 |
final Object[] es = elements; |
1000 |
jsr166 |
1.99 |
final int capacity = es.length; |
1001 |
jsr166 |
1.106 |
final int end = tail; |
1002 |
jsr166 |
1.113 |
final long[] deathRow = nBits(sub(end, beg, capacity)); |
1003 |
|
|
deathRow[0] = 1L; // set bit 0 |
1004 |
|
|
for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg; |
1005 |
|
|
; i = 0, to = end, k -= capacity) { |
1006 |
|
|
for (; i < to; i++) |
1007 |
|
|
if (filter.test(elementAt(es, i))) |
1008 |
|
|
setBit(deathRow, i - k); |
1009 |
|
|
if (to == end) break; |
1010 |
|
|
} |
1011 |
|
|
// a two-finger traversal, with hare i reading, tortoise w writing |
1012 |
|
|
int w = beg; |
1013 |
|
|
for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg; |
1014 |
|
|
; w = 0) { // w rejoins i on second leg |
1015 |
|
|
// In this loop, i and w are on the same leg, with i > w |
1016 |
|
|
for (; i < to; i++) |
1017 |
|
|
if (isClear(deathRow, i - k)) |
1018 |
|
|
es[w++] = es[i]; |
1019 |
|
|
if (to == end) break; |
1020 |
|
|
// In this loop, w is on the first leg, i on the second |
1021 |
|
|
for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++) |
1022 |
|
|
if (isClear(deathRow, i - k)) |
1023 |
|
|
es[w++] = es[i]; |
1024 |
|
|
if (i >= to) { |
1025 |
|
|
if (w == capacity) w = 0; // "corner" case |
1026 |
|
|
break; |
1027 |
jsr166 |
1.104 |
} |
1028 |
dl |
1.16 |
} |
1029 |
jsr166 |
1.113 |
if (end != tail) throw new ConcurrentModificationException(); |
1030 |
|
|
circularClear(es, tail = w, end); |
1031 |
|
|
// checkInvariants(); |
1032 |
|
|
return true; |
1033 |
dl |
1.16 |
} |
1034 |
|
|
|
1035 |
dl |
1.1 |
/** |
1036 |
jsr166 |
1.40 |
* Returns {@code true} if this deque contains the specified element. |
1037 |
|
|
* More formally, returns {@code true} if and only if this deque contains |
1038 |
|
|
* at least one element {@code e} such that {@code o.equals(e)}. |
1039 |
dl |
1.1 |
* |
1040 |
|
|
* @param o object to be checked for containment in this deque |
1041 |
jsr166 |
1.40 |
* @return {@code true} if this deque contains the specified element |
1042 |
dl |
1.1 |
*/ |
1043 |
|
|
public boolean contains(Object o) { |
1044 |
jsr166 |
1.58 |
if (o != null) { |
1045 |
jsr166 |
1.99 |
final Object[] es = elements; |
1046 |
jsr166 |
1.106 |
for (int i = head, end = tail, to = (i <= end) ? end : es.length; |
1047 |
|
|
; i = 0, to = end) { |
1048 |
jsr166 |
1.92 |
for (; i < to; i++) |
1049 |
jsr166 |
1.99 |
if (o.equals(es[i])) |
1050 |
jsr166 |
1.89 |
return true; |
1051 |
jsr166 |
1.106 |
if (to == end) break; |
1052 |
jsr166 |
1.89 |
} |
1053 |
dl |
1.1 |
} |
1054 |
|
|
return false; |
1055 |
|
|
} |
1056 |
|
|
|
1057 |
|
|
/** |
1058 |
|
|
* Removes a single instance of the specified element from this deque. |
1059 |
jsr166 |
1.9 |
* If the deque does not contain the element, it is unchanged. |
1060 |
jsr166 |
1.40 |
* More formally, removes the first element {@code e} such that |
1061 |
|
|
* {@code o.equals(e)} (if such an element exists). |
1062 |
|
|
* Returns {@code true} if this deque contained the specified element |
1063 |
jsr166 |
1.12 |
* (or equivalently, if this deque changed as a result of the call). |
1064 |
jsr166 |
1.9 |
* |
1065 |
jsr166 |
1.46 |
* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. |
1066 |
dl |
1.1 |
* |
1067 |
jsr166 |
1.9 |
* @param o element to be removed from this deque, if present |
1068 |
jsr166 |
1.40 |
* @return {@code true} if this deque contained the specified element |
1069 |
dl |
1.1 |
*/ |
1070 |
jsr166 |
1.9 |
public boolean remove(Object o) { |
1071 |
|
|
return removeFirstOccurrence(o); |
1072 |
dl |
1.1 |
} |
1073 |
|
|
|
1074 |
|
|
/** |
1075 |
|
|
* Removes all of the elements from this deque. |
1076 |
jsr166 |
1.7 |
* The deque will be empty after this call returns. |
1077 |
dl |
1.1 |
*/ |
1078 |
|
|
public void clear() { |
1079 |
jsr166 |
1.106 |
circularClear(elements, head, tail); |
1080 |
|
|
head = tail = 0; |
1081 |
jsr166 |
1.75 |
// checkInvariants(); |
1082 |
dl |
1.1 |
} |
1083 |
|
|
|
1084 |
|
|
/** |
1085 |
jsr166 |
1.106 |
* Nulls out slots starting at array index i, upto index end. |
1086 |
jsr166 |
1.125 |
* Condition i == end means "empty" - nothing to do. |
1087 |
jsr166 |
1.89 |
*/ |
1088 |
jsr166 |
1.106 |
private static void circularClear(Object[] es, int i, int end) { |
1089 |
jsr166 |
1.125 |
// assert 0 <= i && i < es.length; |
1090 |
|
|
// assert 0 <= end && end < es.length; |
1091 |
jsr166 |
1.106 |
for (int to = (i <= end) ? end : es.length; |
1092 |
|
|
; i = 0, to = end) { |
1093 |
jsr166 |
1.125 |
for (; i < to; i++) es[i] = null; |
1094 |
jsr166 |
1.106 |
if (to == end) break; |
1095 |
jsr166 |
1.99 |
} |
1096 |
jsr166 |
1.89 |
} |
1097 |
|
|
|
1098 |
|
|
/** |
1099 |
dl |
1.5 |
* Returns an array containing all of the elements in this deque |
1100 |
jsr166 |
1.10 |
* in proper sequence (from first to last element). |
1101 |
dl |
1.1 |
* |
1102 |
jsr166 |
1.10 |
* <p>The returned array will be "safe" in that no references to it are |
1103 |
|
|
* maintained by this deque. (In other words, this method must allocate |
1104 |
|
|
* a new array). The caller is thus free to modify the returned array. |
1105 |
jsr166 |
1.13 |
* |
1106 |
jsr166 |
1.11 |
* <p>This method acts as bridge between array-based and collection-based |
1107 |
|
|
* APIs. |
1108 |
|
|
* |
1109 |
dl |
1.5 |
* @return an array containing all of the elements in this deque |
1110 |
dl |
1.1 |
*/ |
1111 |
|
|
public Object[] toArray() { |
1112 |
jsr166 |
1.86 |
return toArray(Object[].class); |
1113 |
|
|
} |
1114 |
|
|
|
1115 |
|
|
private <T> T[] toArray(Class<T[]> klazz) { |
1116 |
jsr166 |
1.99 |
final Object[] es = elements; |
1117 |
jsr166 |
1.86 |
final T[] a; |
1118 |
jsr166 |
1.119 |
final int head = this.head, tail = this.tail, end; |
1119 |
|
|
if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) { |
1120 |
jsr166 |
1.120 |
// Uses null extension feature of copyOfRange |
1121 |
jsr166 |
1.99 |
a = Arrays.copyOfRange(es, head, end, klazz); |
1122 |
jsr166 |
1.86 |
} else { |
1123 |
|
|
// integer overflow! |
1124 |
jsr166 |
1.119 |
a = Arrays.copyOfRange(es, 0, end - head, klazz); |
1125 |
|
|
System.arraycopy(es, head, a, 0, es.length - head); |
1126 |
jsr166 |
1.86 |
} |
1127 |
jsr166 |
1.119 |
if (end != tail) |
1128 |
|
|
System.arraycopy(es, 0, a, es.length - head, tail); |
1129 |
jsr166 |
1.50 |
return a; |
1130 |
dl |
1.1 |
} |
1131 |
|
|
|
1132 |
|
|
/** |
1133 |
jsr166 |
1.10 |
* Returns an array containing all of the elements in this deque in |
1134 |
|
|
* proper sequence (from first to last element); the runtime type of the |
1135 |
|
|
* returned array is that of the specified array. If the deque fits in |
1136 |
|
|
* the specified array, it is returned therein. Otherwise, a new array |
1137 |
|
|
* is allocated with the runtime type of the specified array and the |
1138 |
|
|
* size of this deque. |
1139 |
|
|
* |
1140 |
|
|
* <p>If this deque fits in the specified array with room to spare |
1141 |
|
|
* (i.e., the array has more elements than this deque), the element in |
1142 |
|
|
* the array immediately following the end of the deque is set to |
1143 |
jsr166 |
1.40 |
* {@code null}. |
1144 |
jsr166 |
1.10 |
* |
1145 |
|
|
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
1146 |
|
|
* array-based and collection-based APIs. Further, this method allows |
1147 |
|
|
* precise control over the runtime type of the output array, and may, |
1148 |
|
|
* under certain circumstances, be used to save allocation costs. |
1149 |
|
|
* |
1150 |
jsr166 |
1.40 |
* <p>Suppose {@code x} is a deque known to contain only strings. |
1151 |
jsr166 |
1.10 |
* The following code can be used to dump the deque into a newly |
1152 |
jsr166 |
1.40 |
* allocated array of {@code String}: |
1153 |
jsr166 |
1.10 |
* |
1154 |
jsr166 |
1.63 |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
1155 |
jsr166 |
1.10 |
* |
1156 |
jsr166 |
1.40 |
* Note that {@code toArray(new Object[0])} is identical in function to |
1157 |
|
|
* {@code toArray()}. |
1158 |
dl |
1.1 |
* |
1159 |
|
|
* @param a the array into which the elements of the deque are to |
1160 |
jsr166 |
1.9 |
* be stored, if it is big enough; otherwise, a new array of the |
1161 |
|
|
* same runtime type is allocated for this purpose |
1162 |
jsr166 |
1.10 |
* @return an array containing all of the elements in this deque |
1163 |
|
|
* @throws ArrayStoreException if the runtime type of the specified array |
1164 |
|
|
* is not a supertype of the runtime type of every element in |
1165 |
|
|
* this deque |
1166 |
|
|
* @throws NullPointerException if the specified array is null |
1167 |
dl |
1.1 |
*/ |
1168 |
jsr166 |
1.34 |
@SuppressWarnings("unchecked") |
1169 |
dl |
1.1 |
public <T> T[] toArray(T[] a) { |
1170 |
jsr166 |
1.99 |
final int size; |
1171 |
jsr166 |
1.106 |
if ((size = size()) > a.length) |
1172 |
jsr166 |
1.86 |
return toArray((Class<T[]>) a.getClass()); |
1173 |
jsr166 |
1.99 |
final Object[] es = elements; |
1174 |
jsr166 |
1.106 |
for (int i = head, j = 0, len = Math.min(size, es.length - i); |
1175 |
|
|
; i = 0, len = tail) { |
1176 |
jsr166 |
1.101 |
System.arraycopy(es, i, a, j, len); |
1177 |
jsr166 |
1.106 |
if ((j += len) == size) break; |
1178 |
jsr166 |
1.101 |
} |
1179 |
jsr166 |
1.86 |
if (size < a.length) |
1180 |
|
|
a[size] = null; |
1181 |
dl |
1.1 |
return a; |
1182 |
|
|
} |
1183 |
|
|
|
1184 |
|
|
// *** Object methods *** |
1185 |
|
|
|
1186 |
|
|
/** |
1187 |
|
|
* Returns a copy of this deque. |
1188 |
|
|
* |
1189 |
|
|
* @return a copy of this deque |
1190 |
|
|
*/ |
1191 |
|
|
public ArrayDeque<E> clone() { |
1192 |
dl |
1.5 |
try { |
1193 |
jsr166 |
1.34 |
@SuppressWarnings("unchecked") |
1194 |
dl |
1.1 |
ArrayDeque<E> result = (ArrayDeque<E>) super.clone(); |
1195 |
jsr166 |
1.28 |
result.elements = Arrays.copyOf(elements, elements.length); |
1196 |
dl |
1.1 |
return result; |
1197 |
dl |
1.5 |
} catch (CloneNotSupportedException e) { |
1198 |
dl |
1.1 |
throw new AssertionError(); |
1199 |
|
|
} |
1200 |
|
|
} |
1201 |
|
|
|
1202 |
|
|
private static final long serialVersionUID = 2340985798034038923L; |
1203 |
|
|
|
1204 |
|
|
/** |
1205 |
jsr166 |
1.38 |
* Saves this deque to a stream (that is, serializes it). |
1206 |
dl |
1.1 |
* |
1207 |
jsr166 |
1.56 |
* @param s the stream |
1208 |
jsr166 |
1.57 |
* @throws java.io.IOException if an I/O error occurs |
1209 |
jsr166 |
1.40 |
* @serialData The current size ({@code int}) of the deque, |
1210 |
dl |
1.1 |
* followed by all of its elements (each an object reference) in |
1211 |
|
|
* first-to-last order. |
1212 |
|
|
*/ |
1213 |
jsr166 |
1.32 |
private void writeObject(java.io.ObjectOutputStream s) |
1214 |
|
|
throws java.io.IOException { |
1215 |
dl |
1.1 |
s.defaultWriteObject(); |
1216 |
|
|
|
1217 |
|
|
// Write out size |
1218 |
jsr166 |
1.106 |
s.writeInt(size()); |
1219 |
dl |
1.1 |
|
1220 |
|
|
// Write out elements in order. |
1221 |
jsr166 |
1.99 |
final Object[] es = elements; |
1222 |
jsr166 |
1.106 |
for (int i = head, end = tail, to = (i <= end) ? end : es.length; |
1223 |
|
|
; i = 0, to = end) { |
1224 |
jsr166 |
1.92 |
for (; i < to; i++) |
1225 |
jsr166 |
1.99 |
s.writeObject(es[i]); |
1226 |
jsr166 |
1.106 |
if (to == end) break; |
1227 |
jsr166 |
1.89 |
} |
1228 |
dl |
1.1 |
} |
1229 |
|
|
|
1230 |
|
|
/** |
1231 |
jsr166 |
1.38 |
* Reconstitutes this deque from a stream (that is, deserializes it). |
1232 |
jsr166 |
1.56 |
* @param s the stream |
1233 |
jsr166 |
1.57 |
* @throws ClassNotFoundException if the class of a serialized object |
1234 |
|
|
* could not be found |
1235 |
|
|
* @throws java.io.IOException if an I/O error occurs |
1236 |
dl |
1.1 |
*/ |
1237 |
jsr166 |
1.32 |
private void readObject(java.io.ObjectInputStream s) |
1238 |
|
|
throws java.io.IOException, ClassNotFoundException { |
1239 |
dl |
1.1 |
s.defaultReadObject(); |
1240 |
|
|
|
1241 |
|
|
// Read in size and allocate array |
1242 |
jsr166 |
1.106 |
int size = s.readInt(); |
1243 |
jsr166 |
1.132 |
SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size + 1); |
1244 |
jsr166 |
1.106 |
elements = new Object[size + 1]; |
1245 |
|
|
this.tail = size; |
1246 |
dl |
1.1 |
|
1247 |
|
|
// Read in all elements in the proper order. |
1248 |
|
|
for (int i = 0; i < size; i++) |
1249 |
jsr166 |
1.34 |
elements[i] = s.readObject(); |
1250 |
dl |
1.1 |
} |
1251 |
dl |
1.41 |
|
1252 |
jsr166 |
1.75 |
/** debugging */ |
1253 |
jsr166 |
1.89 |
void checkInvariants() { |
1254 |
jsr166 |
1.114 |
// Use head and tail fields with empty slot at tail strategy. |
1255 |
|
|
// head == tail disambiguates to "empty". |
1256 |
jsr166 |
1.75 |
try { |
1257 |
|
|
int capacity = elements.length; |
1258 |
jsr166 |
1.124 |
// assert 0 <= head && head < capacity; |
1259 |
|
|
// assert 0 <= tail && tail < capacity; |
1260 |
jsr166 |
1.106 |
// assert capacity > 0; |
1261 |
|
|
// assert size() < capacity; |
1262 |
|
|
// assert head == tail || elements[head] != null; |
1263 |
|
|
// assert elements[tail] == null; |
1264 |
|
|
// assert head == tail || elements[dec(tail, capacity)] != null; |
1265 |
jsr166 |
1.75 |
} catch (Throwable t) { |
1266 |
jsr166 |
1.106 |
System.err.printf("head=%d tail=%d capacity=%d%n", |
1267 |
|
|
head, tail, elements.length); |
1268 |
jsr166 |
1.75 |
System.err.printf("elements=%s%n", |
1269 |
|
|
Arrays.toString(elements)); |
1270 |
|
|
throw t; |
1271 |
dl |
1.41 |
} |
1272 |
|
|
} |
1273 |
|
|
|
1274 |
dl |
1.1 |
} |