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