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