1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Josh Bloch of Google Inc. and released to the public domain, |
3 |
|
|
* as explained at http://creativecommons.org/licenses/publicdomain. |
4 |
|
|
*/ |
5 |
|
|
|
6 |
|
|
package java.util; |
7 |
|
|
import java.io.*; |
8 |
|
|
|
9 |
|
|
/** |
10 |
|
|
* Resizable-array implementation of the {@link Deque} interface. Array |
11 |
|
|
* deques have no capacity restrictions; they grow as necessary to support |
12 |
|
|
* usage. They are not thread-safe; in the absence of external |
13 |
|
|
* synchronization, they do not support concurrent access by multiple threads. |
14 |
|
|
* Null elements are prohibited. This class is likely to be faster than |
15 |
dl |
1.2 |
* {@link Stack} when used as a stack, and faster than {@link LinkedList} |
16 |
dl |
1.1 |
* when used as a queue. |
17 |
|
|
* |
18 |
|
|
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
19 |
|
|
* Exceptions include {@link #remove(Object) remove}, {@link |
20 |
|
|
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
21 |
jsr166 |
1.9 |
* removeLastOccurrence}, {@link #contains contains}, {@link #iterator |
22 |
dl |
1.1 |
* iterator.remove()}, and the bulk operations, all of which run in linear |
23 |
|
|
* time. |
24 |
|
|
* |
25 |
|
|
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
26 |
|
|
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
27 |
jsr166 |
1.7 |
* is created, in any way except through the iterator's own <tt>remove</tt> |
28 |
|
|
* method, the iterator will generally throw a {@link |
29 |
|
|
* ConcurrentModificationException}. Thus, in the face of concurrent |
30 |
|
|
* modification, the iterator fails quickly and cleanly, rather than risking |
31 |
|
|
* arbitrary, non-deterministic behavior at an undetermined time in the |
32 |
|
|
* future. |
33 |
dl |
1.1 |
* |
34 |
|
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
35 |
|
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
36 |
|
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
37 |
dl |
1.5 |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
38 |
dl |
1.1 |
* Therefore, it would be wrong to write a program that depended on this |
39 |
|
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
40 |
|
|
* should be used only to detect bugs.</i> |
41 |
|
|
* |
42 |
|
|
* <p>This class and its iterator implement all of the |
43 |
jsr166 |
1.9 |
* <em>optional</em> methods of the {@link Collection} and {@link |
44 |
|
|
* Iterator} interfaces. |
45 |
|
|
* |
46 |
|
|
* <p>This class is a member of the |
47 |
|
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
48 |
|
|
* Java Collections Framework</a>. |
49 |
dl |
1.1 |
* |
50 |
|
|
* @author Josh Bloch and Doug Lea |
51 |
|
|
* @since 1.6 |
52 |
|
|
* @param <E> the type of elements held in this collection |
53 |
|
|
*/ |
54 |
|
|
public class ArrayDeque<E> extends AbstractCollection<E> |
55 |
|
|
implements Deque<E>, Cloneable, Serializable |
56 |
|
|
{ |
57 |
|
|
/** |
58 |
dl |
1.4 |
* The array in which the elements of the deque are stored. |
59 |
dl |
1.1 |
* The capacity of the deque is the length of this array, which is |
60 |
|
|
* always a power of two. The array is never allowed to become |
61 |
|
|
* full, except transiently within an addX method where it is |
62 |
|
|
* resized (see doubleCapacity) immediately upon becoming full, |
63 |
|
|
* thus avoiding head and tail wrapping around to equal each |
64 |
|
|
* other. We also guarantee that all array cells not holding |
65 |
|
|
* deque elements are always null. |
66 |
|
|
*/ |
67 |
|
|
private transient E[] elements; |
68 |
|
|
|
69 |
|
|
/** |
70 |
|
|
* The index of the element at the head of the deque (which is the |
71 |
|
|
* element that would be removed by remove() or pop()); or an |
72 |
|
|
* arbitrary number equal to tail if the deque is empty. |
73 |
|
|
*/ |
74 |
|
|
private transient int head; |
75 |
|
|
|
76 |
|
|
/** |
77 |
|
|
* The index at which the next element would be added to the tail |
78 |
|
|
* of the deque (via addLast(E), add(E), or push(E)). |
79 |
|
|
*/ |
80 |
|
|
private transient int tail; |
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* The minimum capacity that we'll use for a newly created deque. |
84 |
|
|
* Must be a power of 2. |
85 |
|
|
*/ |
86 |
|
|
private static final int MIN_INITIAL_CAPACITY = 8; |
87 |
|
|
|
88 |
|
|
// ****** Array allocation and resizing utilities ****** |
89 |
|
|
|
90 |
|
|
/** |
91 |
|
|
* Allocate empty array to hold the given number of elements. |
92 |
|
|
* |
93 |
jsr166 |
1.9 |
* @param numElements the number of elements to hold |
94 |
dl |
1.1 |
*/ |
95 |
dl |
1.5 |
private void allocateElements(int numElements) { |
96 |
dl |
1.1 |
int initialCapacity = MIN_INITIAL_CAPACITY; |
97 |
|
|
// Find the best power of two to hold elements. |
98 |
|
|
// Tests "<=" because arrays aren't kept full. |
99 |
|
|
if (numElements >= initialCapacity) { |
100 |
|
|
initialCapacity = numElements; |
101 |
|
|
initialCapacity |= (initialCapacity >>> 1); |
102 |
|
|
initialCapacity |= (initialCapacity >>> 2); |
103 |
|
|
initialCapacity |= (initialCapacity >>> 4); |
104 |
|
|
initialCapacity |= (initialCapacity >>> 8); |
105 |
|
|
initialCapacity |= (initialCapacity >>> 16); |
106 |
|
|
initialCapacity++; |
107 |
|
|
|
108 |
|
|
if (initialCapacity < 0) // Too many elements, must back off |
109 |
|
|
initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements |
110 |
|
|
} |
111 |
|
|
elements = (E[]) new Object[initialCapacity]; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* Double the capacity of this deque. Call only when full, i.e., |
116 |
|
|
* when head and tail have wrapped around to become equal. |
117 |
|
|
*/ |
118 |
|
|
private void doubleCapacity() { |
119 |
dl |
1.5 |
assert head == tail; |
120 |
dl |
1.1 |
int p = head; |
121 |
|
|
int n = elements.length; |
122 |
|
|
int r = n - p; // number of elements to the right of p |
123 |
|
|
int newCapacity = n << 1; |
124 |
|
|
if (newCapacity < 0) |
125 |
|
|
throw new IllegalStateException("Sorry, deque too big"); |
126 |
|
|
Object[] a = new Object[newCapacity]; |
127 |
|
|
System.arraycopy(elements, p, a, 0, r); |
128 |
|
|
System.arraycopy(elements, 0, a, r, p); |
129 |
|
|
elements = (E[])a; |
130 |
|
|
head = 0; |
131 |
|
|
tail = n; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
/** |
135 |
jsr166 |
1.7 |
* Copies the elements from our element array into the specified array, |
136 |
dl |
1.1 |
* in order (from first to last element in the deque). It is assumed |
137 |
|
|
* that the array is large enough to hold all elements in the deque. |
138 |
|
|
* |
139 |
|
|
* @return its argument |
140 |
|
|
*/ |
141 |
|
|
private <T> T[] copyElements(T[] a) { |
142 |
|
|
if (head < tail) { |
143 |
|
|
System.arraycopy(elements, head, a, 0, size()); |
144 |
|
|
} else if (head > tail) { |
145 |
|
|
int headPortionLen = elements.length - head; |
146 |
|
|
System.arraycopy(elements, head, a, 0, headPortionLen); |
147 |
|
|
System.arraycopy(elements, 0, a, headPortionLen, tail); |
148 |
|
|
} |
149 |
|
|
return a; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
/** |
153 |
dl |
1.4 |
* Constructs an empty array deque with an initial capacity |
154 |
dl |
1.1 |
* sufficient to hold 16 elements. |
155 |
|
|
*/ |
156 |
|
|
public ArrayDeque() { |
157 |
|
|
elements = (E[]) new Object[16]; |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
/** |
161 |
|
|
* Constructs an empty array deque with an initial capacity |
162 |
|
|
* sufficient to hold the specified number of elements. |
163 |
|
|
* |
164 |
|
|
* @param numElements lower bound on initial capacity of the deque |
165 |
|
|
*/ |
166 |
|
|
public ArrayDeque(int numElements) { |
167 |
|
|
allocateElements(numElements); |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
/** |
171 |
|
|
* Constructs a deque containing the elements of the specified |
172 |
|
|
* collection, in the order they are returned by the collection's |
173 |
|
|
* iterator. (The first element returned by the collection's |
174 |
|
|
* iterator becomes the first element, or <i>front</i> of the |
175 |
|
|
* deque.) |
176 |
|
|
* |
177 |
|
|
* @param c the collection whose elements are to be placed into the deque |
178 |
|
|
* @throws NullPointerException if the specified collection is null |
179 |
|
|
*/ |
180 |
|
|
public ArrayDeque(Collection<? extends E> c) { |
181 |
|
|
allocateElements(c.size()); |
182 |
|
|
addAll(c); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
// The main insertion and extraction methods are addFirst, |
186 |
|
|
// addLast, pollFirst, pollLast. The other methods are defined in |
187 |
|
|
// terms of these. |
188 |
|
|
|
189 |
|
|
/** |
190 |
dl |
1.5 |
* Inserts the specified element at the front of this deque. |
191 |
dl |
1.1 |
* |
192 |
jsr166 |
1.9 |
* @param e the element to add |
193 |
|
|
* @throws NullPointerException if the specified element is null |
194 |
dl |
1.1 |
*/ |
195 |
|
|
public void addFirst(E e) { |
196 |
|
|
if (e == null) |
197 |
|
|
throw new NullPointerException(); |
198 |
|
|
elements[head = (head - 1) & (elements.length - 1)] = e; |
199 |
dl |
1.5 |
if (head == tail) |
200 |
dl |
1.1 |
doubleCapacity(); |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
/** |
204 |
dl |
1.6 |
* Inserts the specified element at the end of this deque. |
205 |
jsr166 |
1.9 |
* This method is equivalent to {@link #add} and {@link #push}. |
206 |
dl |
1.1 |
* |
207 |
jsr166 |
1.9 |
* @param e the element to add |
208 |
|
|
* @throws NullPointerException if the specified element is null |
209 |
dl |
1.1 |
*/ |
210 |
|
|
public void addLast(E e) { |
211 |
|
|
if (e == null) |
212 |
|
|
throw new NullPointerException(); |
213 |
|
|
elements[tail] = e; |
214 |
|
|
if ( (tail = (tail + 1) & (elements.length - 1)) == head) |
215 |
|
|
doubleCapacity(); |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
/** |
219 |
dl |
1.5 |
* Inserts the specified element at the front of this deque. |
220 |
dl |
1.1 |
* |
221 |
jsr166 |
1.9 |
* @param e the element to add |
222 |
dl |
1.1 |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
223 |
jsr166 |
1.9 |
* @throws NullPointerException if the specified element is null |
224 |
dl |
1.1 |
*/ |
225 |
|
|
public boolean offerFirst(E e) { |
226 |
|
|
addFirst(e); |
227 |
|
|
return true; |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
/** |
231 |
dl |
1.6 |
* Inserts the specified element at the end of this deque. |
232 |
dl |
1.1 |
* |
233 |
jsr166 |
1.9 |
* @param e the element to add |
234 |
dl |
1.1 |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
235 |
jsr166 |
1.9 |
* @throws NullPointerException if the specified element is null |
236 |
dl |
1.1 |
*/ |
237 |
|
|
public boolean offerLast(E e) { |
238 |
|
|
addLast(e); |
239 |
|
|
return true; |
240 |
|
|
} |
241 |
|
|
|
242 |
|
|
/** |
243 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
244 |
dl |
1.1 |
*/ |
245 |
|
|
public E removeFirst() { |
246 |
|
|
E x = pollFirst(); |
247 |
|
|
if (x == null) |
248 |
|
|
throw new NoSuchElementException(); |
249 |
|
|
return x; |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
/** |
253 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
254 |
dl |
1.1 |
*/ |
255 |
|
|
public E removeLast() { |
256 |
|
|
E x = pollLast(); |
257 |
|
|
if (x == null) |
258 |
|
|
throw new NoSuchElementException(); |
259 |
|
|
return x; |
260 |
|
|
} |
261 |
|
|
|
262 |
jsr166 |
1.9 |
public E pollFirst() { |
263 |
|
|
int h = head; |
264 |
|
|
E result = elements[h]; // Element is null if deque empty |
265 |
|
|
if (result == null) |
266 |
|
|
return null; |
267 |
|
|
elements[h] = null; // Must null out slot |
268 |
|
|
head = (h + 1) & (elements.length - 1); |
269 |
|
|
return result; |
270 |
dl |
1.1 |
} |
271 |
|
|
|
272 |
jsr166 |
1.9 |
public E pollLast() { |
273 |
|
|
int t = (tail - 1) & (elements.length - 1); |
274 |
|
|
E result = elements[t]; |
275 |
|
|
if (result == null) |
276 |
|
|
return null; |
277 |
|
|
elements[t] = null; |
278 |
|
|
tail = t; |
279 |
|
|
return result; |
280 |
dl |
1.1 |
} |
281 |
|
|
|
282 |
|
|
/** |
283 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
284 |
dl |
1.1 |
*/ |
285 |
|
|
public E getFirst() { |
286 |
|
|
E x = elements[head]; |
287 |
|
|
if (x == null) |
288 |
|
|
throw new NoSuchElementException(); |
289 |
|
|
return x; |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
/** |
293 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
294 |
dl |
1.1 |
*/ |
295 |
|
|
public E getLast() { |
296 |
|
|
E x = elements[(tail - 1) & (elements.length - 1)]; |
297 |
|
|
if (x == null) |
298 |
|
|
throw new NoSuchElementException(); |
299 |
|
|
return x; |
300 |
|
|
} |
301 |
|
|
|
302 |
jsr166 |
1.9 |
public E peekFirst() { |
303 |
|
|
return elements[head]; // elements[head] is null if deque empty |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
public E peekLast() { |
307 |
|
|
return elements[(tail - 1) & (elements.length - 1)]; |
308 |
|
|
} |
309 |
|
|
|
310 |
dl |
1.1 |
/** |
311 |
|
|
* Removes the first occurrence of the specified element in this |
312 |
jsr166 |
1.9 |
* deque (when traversing the deque from head to tail). |
313 |
|
|
* If the deque does not contain the element, it is unchanged. |
314 |
|
|
* More formally, removes the first element <tt>e</tt> such that |
315 |
|
|
* <tt>o.equals(e)</tt> (if such an element exists). |
316 |
|
|
* Returns true if this deque contained the specified element (or |
317 |
|
|
* equivalently, if this deque changed as a result of the call). |
318 |
dl |
1.1 |
* |
319 |
dl |
1.5 |
* @param o element to be removed from this deque, if present |
320 |
dl |
1.1 |
* @return <tt>true</tt> if the deque contained the specified element |
321 |
|
|
*/ |
322 |
dl |
1.5 |
public boolean removeFirstOccurrence(Object o) { |
323 |
|
|
if (o == null) |
324 |
dl |
1.1 |
return false; |
325 |
|
|
int mask = elements.length - 1; |
326 |
|
|
int i = head; |
327 |
|
|
E x; |
328 |
|
|
while ( (x = elements[i]) != null) { |
329 |
dl |
1.5 |
if (o.equals(x)) { |
330 |
dl |
1.1 |
delete(i); |
331 |
|
|
return true; |
332 |
|
|
} |
333 |
|
|
i = (i + 1) & mask; |
334 |
|
|
} |
335 |
|
|
return false; |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
/** |
339 |
|
|
* Removes the last occurrence of the specified element in this |
340 |
jsr166 |
1.9 |
* deque (when traversing the deque from head to tail). |
341 |
|
|
* If the deque does not contain the element, it is unchanged. |
342 |
|
|
* More formally, removes the last element <tt>e</tt> such that |
343 |
|
|
* <tt>o.equals(e)</tt> (if such an element exists). |
344 |
|
|
* Returns true if this deque contained the specified element (or |
345 |
|
|
* equivalently, if this deque changed as a result of the call). |
346 |
dl |
1.1 |
* |
347 |
dl |
1.5 |
* @param o element to be removed from this deque, if present |
348 |
dl |
1.1 |
* @return <tt>true</tt> if the deque contained the specified element |
349 |
|
|
*/ |
350 |
dl |
1.5 |
public boolean removeLastOccurrence(Object o) { |
351 |
|
|
if (o == null) |
352 |
dl |
1.1 |
return false; |
353 |
|
|
int mask = elements.length - 1; |
354 |
|
|
int i = (tail - 1) & mask; |
355 |
|
|
E x; |
356 |
|
|
while ( (x = elements[i]) != null) { |
357 |
dl |
1.5 |
if (o.equals(x)) { |
358 |
dl |
1.1 |
delete(i); |
359 |
|
|
return true; |
360 |
|
|
} |
361 |
|
|
i = (i - 1) & mask; |
362 |
|
|
} |
363 |
|
|
return false; |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
// *** Queue methods *** |
367 |
|
|
|
368 |
|
|
/** |
369 |
dl |
1.6 |
* Inserts the specified element at the end of this deque. |
370 |
dl |
1.1 |
* |
371 |
|
|
* <p>This method is equivalent to {@link #addLast}. |
372 |
|
|
* |
373 |
jsr166 |
1.9 |
* @param e the element to add |
374 |
dl |
1.1 |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
375 |
jsr166 |
1.9 |
* @throws NullPointerException if the specified element is null |
376 |
dl |
1.1 |
*/ |
377 |
|
|
public boolean add(E e) { |
378 |
|
|
addLast(e); |
379 |
|
|
return true; |
380 |
|
|
} |
381 |
|
|
|
382 |
|
|
/** |
383 |
jsr166 |
1.9 |
* Inserts the specified element at the end of this deque. |
384 |
dl |
1.1 |
* |
385 |
jsr166 |
1.9 |
* <p>This method is equivalent to {@link #offerLast}. |
386 |
dl |
1.1 |
* |
387 |
jsr166 |
1.9 |
* @param e the element to add |
388 |
|
|
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
389 |
|
|
* @throws NullPointerException if the specified element is null |
390 |
dl |
1.1 |
*/ |
391 |
jsr166 |
1.9 |
public boolean offer(E e) { |
392 |
|
|
return offerLast(e); |
393 |
dl |
1.1 |
} |
394 |
|
|
|
395 |
|
|
/** |
396 |
|
|
* Retrieves and removes the head of the queue represented by this deque. |
397 |
jsr166 |
1.9 |
* This method differs from {@link #poll} only in that it throws an |
398 |
|
|
* exception if this deque is empty. |
399 |
dl |
1.1 |
* |
400 |
|
|
* <p>This method is equivalent to {@link #removeFirst}. |
401 |
|
|
* |
402 |
|
|
* @return the head of the queue represented by this deque |
403 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
404 |
dl |
1.1 |
*/ |
405 |
|
|
public E remove() { |
406 |
|
|
return removeFirst(); |
407 |
|
|
} |
408 |
|
|
|
409 |
|
|
/** |
410 |
jsr166 |
1.9 |
* Retrieves and removes the head of the queue represented by this deque |
411 |
|
|
* (in other words, the first element of this deque), or returns |
412 |
|
|
* <tt>null</tt> if this deque is empty. |
413 |
dl |
1.1 |
* |
414 |
jsr166 |
1.9 |
* <p>This method is equivalent to {@link #pollFirst}. |
415 |
dl |
1.1 |
* |
416 |
|
|
* @return the head of the queue represented by this deque, or |
417 |
jsr166 |
1.9 |
* <tt>null</tt> if this deque is empty |
418 |
dl |
1.1 |
*/ |
419 |
jsr166 |
1.9 |
public E poll() { |
420 |
|
|
return pollFirst(); |
421 |
dl |
1.1 |
} |
422 |
|
|
|
423 |
|
|
/** |
424 |
|
|
* Retrieves, but does not remove, the head of the queue represented by |
425 |
jsr166 |
1.9 |
* this deque. This method differs from {@link #peek} only in that it |
426 |
|
|
* throws an exception if this deque is empty. |
427 |
dl |
1.1 |
* |
428 |
jsr166 |
1.8 |
* <p>This method is equivalent to {@link #getFirst}. |
429 |
dl |
1.1 |
* |
430 |
|
|
* @return the head of the queue represented by this deque |
431 |
jsr166 |
1.9 |
* @throws NoSuchElementException {@inheritDoc} |
432 |
dl |
1.1 |
*/ |
433 |
|
|
public E element() { |
434 |
|
|
return getFirst(); |
435 |
|
|
} |
436 |
|
|
|
437 |
jsr166 |
1.9 |
/** |
438 |
|
|
* Retrieves, but does not remove, the head of the queue represented by |
439 |
|
|
* this deque, or returns <tt>null</tt> if this deque is empty. |
440 |
|
|
* |
441 |
|
|
* <p>This method is equivalent to {@link #peekFirst}. |
442 |
|
|
* |
443 |
|
|
* @return the head of the queue represented by this deque, or |
444 |
|
|
* <tt>null</tt> if this deque is empty |
445 |
|
|
*/ |
446 |
|
|
public E peek() { |
447 |
|
|
return peekFirst(); |
448 |
|
|
} |
449 |
|
|
|
450 |
dl |
1.1 |
// *** Stack methods *** |
451 |
|
|
|
452 |
|
|
/** |
453 |
|
|
* Pushes an element onto the stack represented by this deque. In other |
454 |
dl |
1.5 |
* words, inserts the element at the front of this deque. |
455 |
dl |
1.1 |
* |
456 |
|
|
* <p>This method is equivalent to {@link #addFirst}. |
457 |
|
|
* |
458 |
|
|
* @param e the element to push |
459 |
jsr166 |
1.9 |
* @throws NullPointerException if the specified element is null |
460 |
dl |
1.1 |
*/ |
461 |
|
|
public void push(E e) { |
462 |
|
|
addFirst(e); |
463 |
|
|
} |
464 |
|
|
|
465 |
|
|
/** |
466 |
|
|
* Pops an element from the stack represented by this deque. In other |
467 |
dl |
1.2 |
* words, removes and returns the first element of this deque. |
468 |
dl |
1.1 |
* |
469 |
|
|
* <p>This method is equivalent to {@link #removeFirst()}. |
470 |
|
|
* |
471 |
|
|
* @return the element at the front of this deque (which is the top |
472 |
jsr166 |
1.9 |
* of the stack represented by this deque) |
473 |
|
|
* @throws NoSuchElementException {@inheritDoc} |
474 |
dl |
1.1 |
*/ |
475 |
|
|
public E pop() { |
476 |
|
|
return removeFirst(); |
477 |
|
|
} |
478 |
|
|
|
479 |
|
|
/** |
480 |
jsr166 |
1.7 |
* Removes the element at the specified position in the elements array, |
481 |
jsr166 |
1.9 |
* adjusting head and tail as necessary. This can result in motion of |
482 |
|
|
* elements backwards or forwards in the array. |
483 |
dl |
1.1 |
* |
484 |
dl |
1.5 |
* <p>This method is called delete rather than remove to emphasize |
485 |
jsr166 |
1.9 |
* that its semantics differ from those of {@link List#remove(int)}. |
486 |
dl |
1.5 |
* |
487 |
dl |
1.1 |
* @return true if elements moved backwards |
488 |
|
|
*/ |
489 |
|
|
private boolean delete(int i) { |
490 |
jsr166 |
1.9 |
int mask = elements.length - 1; |
491 |
|
|
|
492 |
|
|
// Invariant: head <= i < tail mod circularity |
493 |
|
|
if (((i - head) & mask) >= ((tail - head) & mask)) |
494 |
|
|
throw new ConcurrentModificationException(); |
495 |
|
|
|
496 |
dl |
1.1 |
// Case 1: Deque doesn't wrap |
497 |
|
|
// Case 2: Deque does wrap and removed element is in the head portion |
498 |
jsr166 |
1.9 |
if (i >= head) { |
499 |
dl |
1.1 |
System.arraycopy(elements, head, elements, head + 1, i - head); |
500 |
|
|
elements[head] = null; |
501 |
jsr166 |
1.9 |
head = (head + 1) & mask; |
502 |
dl |
1.1 |
return false; |
503 |
|
|
} |
504 |
|
|
|
505 |
|
|
// Case 3: Deque wraps and removed element is in the tail portion |
506 |
|
|
tail--; |
507 |
|
|
System.arraycopy(elements, i + 1, elements, i, tail - i); |
508 |
|
|
elements[tail] = null; |
509 |
|
|
return true; |
510 |
|
|
} |
511 |
|
|
|
512 |
|
|
// *** Collection Methods *** |
513 |
|
|
|
514 |
|
|
/** |
515 |
|
|
* Returns the number of elements in this deque. |
516 |
|
|
* |
517 |
|
|
* @return the number of elements in this deque |
518 |
|
|
*/ |
519 |
|
|
public int size() { |
520 |
|
|
return (tail - head) & (elements.length - 1); |
521 |
|
|
} |
522 |
|
|
|
523 |
|
|
/** |
524 |
jsr166 |
1.9 |
* Returns <tt>true</tt> if this deque contains no elements. |
525 |
dl |
1.1 |
* |
526 |
jsr166 |
1.9 |
* @return <tt>true</tt> if this deque contains no elements |
527 |
dl |
1.1 |
*/ |
528 |
|
|
public boolean isEmpty() { |
529 |
|
|
return head == tail; |
530 |
|
|
} |
531 |
|
|
|
532 |
|
|
/** |
533 |
|
|
* Returns an iterator over the elements in this deque. The elements |
534 |
|
|
* will be ordered from first (head) to last (tail). This is the same |
535 |
|
|
* order that elements would be dequeued (via successive calls to |
536 |
|
|
* {@link #remove} or popped (via successive calls to {@link #pop}). |
537 |
dl |
1.5 |
* |
538 |
dl |
1.1 |
* @return an <tt>Iterator</tt> over the elements in this deque |
539 |
|
|
*/ |
540 |
|
|
public Iterator<E> iterator() { |
541 |
|
|
return new DeqIterator(); |
542 |
|
|
} |
543 |
|
|
|
544 |
|
|
private class DeqIterator implements Iterator<E> { |
545 |
|
|
/** |
546 |
|
|
* Index of element to be returned by subsequent call to next. |
547 |
|
|
*/ |
548 |
|
|
private int cursor = head; |
549 |
|
|
|
550 |
|
|
/** |
551 |
|
|
* Tail recorded at construction (also in remove), to stop |
552 |
|
|
* iterator and also to check for comodification. |
553 |
|
|
*/ |
554 |
|
|
private int fence = tail; |
555 |
|
|
|
556 |
|
|
/** |
557 |
|
|
* Index of element returned by most recent call to next. |
558 |
|
|
* Reset to -1 if element is deleted by a call to remove. |
559 |
|
|
*/ |
560 |
|
|
private int lastRet = -1; |
561 |
|
|
|
562 |
|
|
public boolean hasNext() { |
563 |
|
|
return cursor != fence; |
564 |
|
|
} |
565 |
|
|
|
566 |
|
|
public E next() { |
567 |
|
|
E result; |
568 |
|
|
if (cursor == fence) |
569 |
|
|
throw new NoSuchElementException(); |
570 |
|
|
// This check doesn't catch all possible comodifications, |
571 |
|
|
// but does catch the ones that corrupt traversal |
572 |
|
|
if (tail != fence || (result = elements[cursor]) == null) |
573 |
|
|
throw new ConcurrentModificationException(); |
574 |
|
|
lastRet = cursor; |
575 |
|
|
cursor = (cursor + 1) & (elements.length - 1); |
576 |
|
|
return result; |
577 |
|
|
} |
578 |
|
|
|
579 |
|
|
public void remove() { |
580 |
|
|
if (lastRet < 0) |
581 |
|
|
throw new IllegalStateException(); |
582 |
|
|
if (delete(lastRet)) |
583 |
|
|
cursor--; |
584 |
|
|
lastRet = -1; |
585 |
|
|
fence = tail; |
586 |
|
|
} |
587 |
|
|
} |
588 |
|
|
|
589 |
|
|
/** |
590 |
jsr166 |
1.9 |
* Returns <tt>true</tt> if this deque contains the specified element. |
591 |
|
|
* More formally, returns <tt>true</tt> if and only if this deque contains |
592 |
|
|
* at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. |
593 |
dl |
1.1 |
* |
594 |
|
|
* @param o object to be checked for containment in this deque |
595 |
|
|
* @return <tt>true</tt> if this deque contains the specified element |
596 |
|
|
*/ |
597 |
|
|
public boolean contains(Object o) { |
598 |
|
|
if (o == null) |
599 |
|
|
return false; |
600 |
|
|
int mask = elements.length - 1; |
601 |
|
|
int i = head; |
602 |
|
|
E x; |
603 |
|
|
while ( (x = elements[i]) != null) { |
604 |
|
|
if (o.equals(x)) |
605 |
|
|
return true; |
606 |
|
|
i = (i + 1) & mask; |
607 |
|
|
} |
608 |
|
|
return false; |
609 |
|
|
} |
610 |
|
|
|
611 |
|
|
/** |
612 |
|
|
* Removes a single instance of the specified element from this deque. |
613 |
jsr166 |
1.9 |
* If the deque does not contain the element, it is unchanged. |
614 |
|
|
* More formally, removes the first element <tt>e</tt> such that |
615 |
|
|
* <tt>o.equals(e)</tt> (if such an element exists). |
616 |
|
|
* Returns true if this deque contained the specified element (or |
617 |
|
|
* equivalently, if this deque changed as a result of the call). |
618 |
|
|
* |
619 |
|
|
* <p>This method is equivalent to {@link #removeFirstOccurrence}. |
620 |
dl |
1.1 |
* |
621 |
jsr166 |
1.9 |
* @param o element to be removed from this deque, if present |
622 |
dl |
1.1 |
* @return <tt>true</tt> if this deque contained the specified element |
623 |
|
|
*/ |
624 |
jsr166 |
1.9 |
public boolean remove(Object o) { |
625 |
|
|
return removeFirstOccurrence(o); |
626 |
dl |
1.1 |
} |
627 |
|
|
|
628 |
|
|
/** |
629 |
|
|
* Removes all of the elements from this deque. |
630 |
jsr166 |
1.7 |
* The deque will be empty after this call returns. |
631 |
dl |
1.1 |
*/ |
632 |
|
|
public void clear() { |
633 |
|
|
int h = head; |
634 |
|
|
int t = tail; |
635 |
|
|
if (h != t) { // clear all cells |
636 |
|
|
head = tail = 0; |
637 |
|
|
int i = h; |
638 |
|
|
int mask = elements.length - 1; |
639 |
|
|
do { |
640 |
|
|
elements[i] = null; |
641 |
|
|
i = (i + 1) & mask; |
642 |
jsr166 |
1.9 |
} while (i != t); |
643 |
dl |
1.1 |
} |
644 |
|
|
} |
645 |
|
|
|
646 |
|
|
/** |
647 |
dl |
1.5 |
* Returns an array containing all of the elements in this deque |
648 |
dl |
1.1 |
* in the correct order. |
649 |
|
|
* |
650 |
dl |
1.5 |
* @return an array containing all of the elements in this deque |
651 |
jsr166 |
1.9 |
* in the correct order |
652 |
dl |
1.1 |
*/ |
653 |
|
|
public Object[] toArray() { |
654 |
|
|
return copyElements(new Object[size()]); |
655 |
|
|
} |
656 |
|
|
|
657 |
|
|
/** |
658 |
|
|
* Returns an array containing all of the elements in this deque in the |
659 |
|
|
* correct order; the runtime type of the returned array is that of the |
660 |
|
|
* specified array. If the deque fits in the specified array, it is |
661 |
|
|
* returned therein. Otherwise, a new array is allocated with the runtime |
662 |
|
|
* type of the specified array and the size of this deque. |
663 |
|
|
* |
664 |
|
|
* <p>If the deque fits in the specified array with room to spare (i.e., |
665 |
|
|
* the array has more elements than the deque), the element in the array |
666 |
|
|
* immediately following the end of the collection is set to <tt>null</tt>. |
667 |
|
|
* |
668 |
|
|
* @param a the array into which the elements of the deque are to |
669 |
jsr166 |
1.9 |
* be stored, if it is big enough; otherwise, a new array of the |
670 |
|
|
* same runtime type is allocated for this purpose |
671 |
dl |
1.1 |
* @return an array containing the elements of the deque |
672 |
|
|
* @throws ArrayStoreException if the runtime type of a is not a supertype |
673 |
|
|
* of the runtime type of every element in this deque |
674 |
|
|
*/ |
675 |
|
|
public <T> T[] toArray(T[] a) { |
676 |
|
|
int size = size(); |
677 |
|
|
if (a.length < size) |
678 |
|
|
a = (T[])java.lang.reflect.Array.newInstance( |
679 |
|
|
a.getClass().getComponentType(), size); |
680 |
|
|
copyElements(a); |
681 |
|
|
if (a.length > size) |
682 |
|
|
a[size] = null; |
683 |
|
|
return a; |
684 |
|
|
} |
685 |
|
|
|
686 |
|
|
// *** Object methods *** |
687 |
|
|
|
688 |
|
|
/** |
689 |
|
|
* Returns a copy of this deque. |
690 |
|
|
* |
691 |
|
|
* @return a copy of this deque |
692 |
|
|
*/ |
693 |
|
|
public ArrayDeque<E> clone() { |
694 |
dl |
1.5 |
try { |
695 |
dl |
1.1 |
ArrayDeque<E> result = (ArrayDeque<E>) super.clone(); |
696 |
|
|
// These two lines are currently faster than cloning the array: |
697 |
|
|
result.elements = (E[]) new Object[elements.length]; |
698 |
|
|
System.arraycopy(elements, 0, result.elements, 0, elements.length); |
699 |
|
|
return result; |
700 |
|
|
|
701 |
dl |
1.5 |
} catch (CloneNotSupportedException e) { |
702 |
dl |
1.1 |
throw new AssertionError(); |
703 |
|
|
} |
704 |
|
|
} |
705 |
|
|
|
706 |
|
|
/** |
707 |
|
|
* Appease the serialization gods. |
708 |
|
|
*/ |
709 |
|
|
private static final long serialVersionUID = 2340985798034038923L; |
710 |
|
|
|
711 |
|
|
/** |
712 |
|
|
* Serialize this deque. |
713 |
|
|
* |
714 |
|
|
* @serialData The current size (<tt>int</tt>) of the deque, |
715 |
|
|
* followed by all of its elements (each an object reference) in |
716 |
|
|
* first-to-last order. |
717 |
|
|
*/ |
718 |
|
|
private void writeObject(ObjectOutputStream s) throws IOException { |
719 |
|
|
s.defaultWriteObject(); |
720 |
|
|
|
721 |
|
|
// Write out size |
722 |
|
|
int size = size(); |
723 |
|
|
s.writeInt(size); |
724 |
|
|
|
725 |
|
|
// Write out elements in order. |
726 |
|
|
int i = head; |
727 |
|
|
int mask = elements.length - 1; |
728 |
|
|
for (int j = 0; j < size; j++) { |
729 |
|
|
s.writeObject(elements[i]); |
730 |
|
|
i = (i + 1) & mask; |
731 |
|
|
} |
732 |
|
|
} |
733 |
|
|
|
734 |
|
|
/** |
735 |
|
|
* Deserialize this deque. |
736 |
|
|
*/ |
737 |
|
|
private void readObject(ObjectInputStream s) |
738 |
|
|
throws IOException, ClassNotFoundException { |
739 |
|
|
s.defaultReadObject(); |
740 |
|
|
|
741 |
|
|
// Read in size and allocate array |
742 |
|
|
int size = s.readInt(); |
743 |
|
|
allocateElements(size); |
744 |
|
|
head = 0; |
745 |
|
|
tail = size; |
746 |
|
|
|
747 |
|
|
// Read in all elements in the proper order. |
748 |
|
|
for (int i = 0; i < size; i++) |
749 |
|
|
elements[i] = (E)s.readObject(); |
750 |
|
|
|
751 |
|
|
} |
752 |
|
|
} |