93 |
|
private void grow(int needed) { |
94 |
|
// overflow-conscious code |
95 |
|
// checkInvariants(); |
96 |
< |
int oldCapacity = elements.length; |
96 |
> |
final int oldCapacity = elements.length; |
97 |
|
int newCapacity; |
98 |
|
// Double size if small; else grow by 50% |
99 |
|
int jump = (oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1); |
115 |
|
|
116 |
|
/** Capacity calculation for edge conditions, especially overflow. */ |
117 |
|
private int newCapacity(int needed, int jump) { |
118 |
< |
int oldCapacity = elements.length; |
119 |
< |
int minCapacity; |
118 |
> |
final int oldCapacity = elements.length, minCapacity; |
119 |
|
if ((minCapacity = oldCapacity + needed) - MAX_ARRAY_SIZE > 0) { |
120 |
|
if (minCapacity < 0) |
121 |
|
throw new IllegalStateException("Sorry, deque too big"); |
199 |
|
* Precondition and postcondition: 0 <= i < modulus. |
200 |
|
*/ |
201 |
|
static final int inc(int i, int modulus) { |
202 |
< |
if (++i == modulus) i = 0; |
202 |
> |
if (++i >= modulus) i = 0; |
203 |
|
return i; |
204 |
|
} |
205 |
|
|
208 |
|
* Precondition and postcondition: 0 <= i < modulus. |
209 |
|
*/ |
210 |
|
static final int dec(int i, int modulus) { |
211 |
< |
if (--i < 0) i += modulus; |
211 |
> |
if (--i < 0) i = modulus - 1; |
212 |
|
return i; |
213 |
|
} |
214 |
|
|
233 |
|
* Returns element at array index i. |
234 |
|
*/ |
235 |
|
@SuppressWarnings("unchecked") |
236 |
< |
final E elementAt(int i) { |
236 |
> |
private E elementAt(int i) { |
237 |
|
return (E) elements[i]; |
238 |
|
} |
239 |
|
|
240 |
|
/** |
241 |
|
* A version of elementAt that checks for null elements. |
242 |
|
* This check doesn't catch all possible comodifications, |
243 |
< |
* but does catch ones that corrupt traversal. |
243 |
> |
* but does catch ones that corrupt traversal. It's a little |
244 |
> |
* surprising that javac allows this abuse of generics. |
245 |
|
*/ |
246 |
< |
E checkedElementAt(Object[] elements, int i) { |
246 |
> |
static final <E> E nonNullElementAt(Object[] elements, int i) { |
247 |
|
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
248 |
|
if (e == null) |
249 |
|
throw new ConcurrentModificationException(); |
263 |
|
public void addFirst(E e) { |
264 |
|
// checkInvariants(); |
265 |
|
Objects.requireNonNull(e); |
266 |
< |
final Object[] elements; |
267 |
< |
final int capacity, s; |
268 |
< |
if ((s = size) == (capacity = (elements = this.elements).length)) |
269 |
< |
addFirstSlowPath(e); |
270 |
< |
else |
271 |
< |
elements[head = dec(head, capacity)] = e; |
266 |
> |
Object[] elements; |
267 |
> |
int capacity, h; |
268 |
> |
final int s; |
269 |
> |
if ((s = size) == (capacity = (elements = this.elements).length)) { |
270 |
> |
grow(1); |
271 |
> |
capacity = (elements = this.elements).length; |
272 |
> |
} |
273 |
> |
if ((h = head - 1) < 0) h = capacity - 1; |
274 |
> |
elements[head = h] = e; |
275 |
|
size = s + 1; |
276 |
|
// checkInvariants(); |
277 |
|
} |
278 |
|
|
276 |
– |
private void addFirstSlowPath(E e) { |
277 |
– |
grow(1); |
278 |
– |
final Object[] elements = this.elements; |
279 |
– |
elements[head = dec(head, elements.length)] = e; |
280 |
– |
} |
281 |
– |
|
279 |
|
/** |
280 |
|
* Inserts the specified element at the end of this deque. |
281 |
|
* |
287 |
|
public void addLast(E e) { |
288 |
|
// checkInvariants(); |
289 |
|
Objects.requireNonNull(e); |
290 |
< |
final Object[] elements; |
291 |
< |
final int capacity, s; |
292 |
< |
if ((s = size) == (capacity = (elements = this.elements).length)) |
293 |
< |
addLastSlowPath(e); |
294 |
< |
else |
295 |
< |
elements[add(head, s, capacity)] = e; |
290 |
> |
Object[] elements; |
291 |
> |
int capacity; |
292 |
> |
final int s; |
293 |
> |
if ((s = size) == (capacity = (elements = this.elements).length)) { |
294 |
> |
grow(1); |
295 |
> |
capacity = (elements = this.elements).length; |
296 |
> |
} |
297 |
> |
elements[add(head, s, capacity)] = e; |
298 |
|
size = s + 1; |
299 |
|
// checkInvariants(); |
300 |
|
} |
301 |
|
|
303 |
– |
private void addLastSlowPath(E e) { |
304 |
– |
grow(1); |
305 |
– |
final Object[] elements = this.elements; |
306 |
– |
elements[add(head, size, elements.length)] = e; |
307 |
– |
} |
308 |
– |
|
302 |
|
/** |
303 |
|
* Adds all of the elements in the specified collection at the end |
304 |
|
* of this deque, as if by calling {@link #addLast} on each one, |
367 |
|
|
368 |
|
public E pollFirst() { |
369 |
|
// checkInvariants(); |
370 |
< |
final int s, h; |
371 |
< |
if ((s = size) == 0) |
370 |
> |
int s, h; |
371 |
> |
if ((s = size) <= 0) |
372 |
|
return null; |
373 |
|
final Object[] elements = this.elements; |
374 |
|
@SuppressWarnings("unchecked") E e = (E) elements[h = head]; |
375 |
|
elements[h] = null; |
376 |
< |
head = inc(h, elements.length); |
376 |
> |
if (++h >= elements.length) h = 0; |
377 |
> |
head = h; |
378 |
|
size = s - 1; |
379 |
|
return e; |
380 |
|
} |
382 |
|
public E pollLast() { |
383 |
|
// checkInvariants(); |
384 |
|
final int s, tail; |
385 |
< |
if ((s = size) == 0) |
385 |
> |
if ((s = size) <= 0) |
386 |
|
return null; |
387 |
|
final Object[] elements = this.elements; |
388 |
|
@SuppressWarnings("unchecked") |
397 |
|
*/ |
398 |
|
public E getFirst() { |
399 |
|
// checkInvariants(); |
400 |
< |
if (size == 0) throw new NoSuchElementException(); |
400 |
> |
if (size <= 0) throw new NoSuchElementException(); |
401 |
|
return elementAt(head); |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
|
* @throws NoSuchElementException {@inheritDoc} |
406 |
|
*/ |
407 |
+ |
@SuppressWarnings("unchecked") |
408 |
|
public E getLast() { |
409 |
|
// checkInvariants(); |
410 |
< |
if (size == 0) throw new NoSuchElementException(); |
411 |
< |
return elementAt(tail()); |
410 |
> |
final int s; |
411 |
> |
if ((s = size) <= 0) throw new NoSuchElementException(); |
412 |
> |
final Object[] elements = this.elements; |
413 |
> |
return (E) elements[add(head, s - 1, elements.length)]; |
414 |
|
} |
415 |
|
|
416 |
|
public E peekFirst() { |
417 |
|
// checkInvariants(); |
418 |
< |
return (size == 0) ? null : elementAt(head); |
418 |
> |
return (size <= 0) ? null : elementAt(head); |
419 |
|
} |
420 |
|
|
421 |
+ |
@SuppressWarnings("unchecked") |
422 |
|
public E peekLast() { |
423 |
|
// checkInvariants(); |
424 |
< |
return (size == 0) ? null : elementAt(tail()); |
424 |
> |
final int s; |
425 |
> |
if ((s = size) <= 0) return null; |
426 |
> |
final Object[] elements = this.elements; |
427 |
> |
return (E) elements[add(head, s - 1, elements.length)]; |
428 |
|
} |
429 |
|
|
430 |
|
/** |
440 |
|
* @return {@code true} if the deque contained the specified element |
441 |
|
*/ |
442 |
|
public boolean removeFirstOccurrence(Object o) { |
442 |
– |
// checkInvariants(); |
443 |
|
if (o != null) { |
444 |
|
final Object[] elements = this.elements; |
445 |
|
final int capacity = elements.length; |
446 |
< |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) { |
447 |
< |
if (o.equals(elements[i])) { |
448 |
< |
delete(i); |
449 |
< |
return true; |
450 |
< |
} |
446 |
> |
int i, end, to, todo; |
447 |
> |
todo = (end = (i = head) + size) |
448 |
> |
- (to = (capacity - end >= 0) ? end : capacity); |
449 |
> |
for (;; i = 0, to = todo, todo = 0) { |
450 |
> |
for (; i < to; i++) |
451 |
> |
if (o.equals(elements[i])) { |
452 |
> |
delete(i); |
453 |
> |
return true; |
454 |
> |
} |
455 |
> |
if (todo == 0) break; |
456 |
|
} |
457 |
|
} |
458 |
|
return false; |
474 |
|
if (o != null) { |
475 |
|
final Object[] elements = this.elements; |
476 |
|
final int capacity = elements.length; |
477 |
< |
for (int k = size, i = add(head, k - 1, capacity); |
478 |
< |
--k >= 0; i = dec(i, capacity)) { |
479 |
< |
if (o.equals(elements[i])) { |
480 |
< |
delete(i); |
481 |
< |
return true; |
482 |
< |
} |
477 |
> |
int i, to, end, todo; |
478 |
> |
todo = (to = ((end = (i = tail()) - size) >= -1) ? end : -1) - end; |
479 |
> |
for (;; i = capacity - 1, to = capacity - 1 - todo, todo = 0) { |
480 |
> |
for (; i > to; i--) |
481 |
> |
if (o.equals(elements[i])) { |
482 |
> |
delete(i); |
483 |
> |
return true; |
484 |
> |
} |
485 |
> |
if (todo == 0) break; |
486 |
|
} |
487 |
|
} |
488 |
|
return false; |
630 |
|
System.arraycopy(elements, h, elements, h + 1, front - (i + 1)); |
631 |
|
} |
632 |
|
elements[h] = null; |
633 |
< |
head = inc(h, capacity); |
633 |
> |
if ((head = (h + 1)) >= capacity) head = 0; |
634 |
|
size--; |
635 |
|
// checkInvariants(); |
636 |
|
return false; |
708 |
|
} |
709 |
|
|
710 |
|
public E next() { |
711 |
< |
if (remaining == 0) |
711 |
> |
if (remaining <= 0) |
712 |
|
throw new NoSuchElementException(); |
713 |
< |
E e = checkedElementAt(elements, cursor); |
713 |
> |
final Object[] elements = ArrayDeque.this.elements; |
714 |
> |
E e = nonNullElementAt(elements, cursor); |
715 |
|
lastRet = cursor; |
716 |
< |
cursor = inc(cursor, elements.length); |
716 |
> |
if (++cursor >= elements.length) cursor = 0; |
717 |
|
remaining--; |
718 |
|
return e; |
719 |
|
} |
720 |
|
|
721 |
|
void postDelete(boolean leftShifted) { |
722 |
|
if (leftShifted) |
723 |
< |
cursor = dec(cursor, elements.length); // undo inc in next |
723 |
> |
if (--cursor < 0) cursor = elements.length - 1; |
724 |
|
} |
725 |
|
|
726 |
|
public final void remove() { |
731 |
|
} |
732 |
|
|
733 |
|
public void forEachRemaining(Consumer<? super E> action) { |
734 |
< |
Objects.requireNonNull(action); |
735 |
< |
final Object[] elements = ArrayDeque.this.elements; |
736 |
< |
final int capacity = elements.length; |
737 |
< |
int k = remaining; |
738 |
< |
remaining = 0; |
739 |
< |
for (int i = cursor; --k >= 0; i = inc(i, capacity)) |
740 |
< |
action.accept(checkedElementAt(elements, i)); |
734 |
> |
final int k; |
735 |
> |
if ((k = remaining) > 0) { |
736 |
> |
remaining = 0; |
737 |
> |
ArrayDeque.forEachRemaining(action, elements, cursor, k); |
738 |
> |
if ((lastRet = cursor + k - 1) >= elements.length) |
739 |
> |
lastRet -= elements.length; |
740 |
> |
} |
741 |
|
} |
742 |
|
} |
743 |
|
|
745 |
|
DescendingIterator() { cursor = tail(); } |
746 |
|
|
747 |
|
public final E next() { |
748 |
< |
if (remaining == 0) |
748 |
> |
if (remaining <= 0) |
749 |
|
throw new NoSuchElementException(); |
750 |
< |
E e = checkedElementAt(elements, cursor); |
750 |
> |
final Object[] elements = ArrayDeque.this.elements; |
751 |
> |
E e = nonNullElementAt(elements, cursor); |
752 |
|
lastRet = cursor; |
753 |
< |
cursor = dec(cursor, elements.length); |
753 |
> |
if (--cursor < 0) cursor = elements.length - 1; |
754 |
|
remaining--; |
755 |
|
return e; |
756 |
|
} |
757 |
|
|
758 |
|
void postDelete(boolean leftShifted) { |
759 |
|
if (!leftShifted) |
760 |
< |
cursor = inc(cursor, elements.length); // undo dec in next |
760 |
> |
if (++cursor >= elements.length) cursor = 0; |
761 |
|
} |
762 |
|
|
763 |
|
public final void forEachRemaining(Consumer<? super E> action) { |
764 |
< |
Objects.requireNonNull(action); |
765 |
< |
final Object[] elements = ArrayDeque.this.elements; |
766 |
< |
final int capacity = elements.length; |
767 |
< |
int k = remaining; |
768 |
< |
remaining = 0; |
769 |
< |
for (int i = cursor; --k >= 0; i = dec(i, capacity)) |
770 |
< |
action.accept(checkedElementAt(elements, i)); |
764 |
> |
final int k; |
765 |
> |
if ((k = remaining) > 0) { |
766 |
> |
remaining = 0; |
767 |
> |
forEachRemainingDescending(action, elements, cursor, k); |
768 |
> |
if ((lastRet = cursor - (k - 1)) < 0) |
769 |
> |
lastRet += elements.length; |
770 |
> |
} |
771 |
|
} |
772 |
|
} |
773 |
|
|
824 |
|
} |
825 |
|
|
826 |
|
public void forEachRemaining(Consumer<? super E> action) { |
827 |
< |
Objects.requireNonNull(action); |
818 |
< |
final Object[] elements = ArrayDeque.this.elements; |
819 |
< |
final int capacity = elements.length; |
820 |
< |
int k = remaining(); |
827 |
> |
final int k = remaining(); // side effect! |
828 |
|
remaining = 0; |
829 |
< |
for (int i = cursor; --k >= 0; i = inc(i, capacity)) |
823 |
< |
action.accept(checkedElementAt(elements, i)); |
829 |
> |
ArrayDeque.forEachRemaining(action, elements, cursor, k); |
830 |
|
} |
831 |
|
|
832 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
833 |
|
Objects.requireNonNull(action); |
834 |
< |
if (remaining() == 0) |
834 |
> |
final int k; |
835 |
> |
if ((k = remaining()) <= 0) |
836 |
|
return false; |
837 |
< |
action.accept(checkedElementAt(elements, cursor)); |
838 |
< |
cursor = inc(cursor, elements.length); |
839 |
< |
remaining--; |
837 |
> |
action.accept(nonNullElementAt(elements, cursor)); |
838 |
> |
if (++cursor >= elements.length) cursor = 0; |
839 |
> |
remaining = k - 1; |
840 |
|
return true; |
841 |
|
} |
842 |
|
|
852 |
|
} |
853 |
|
} |
854 |
|
|
855 |
+ |
@SuppressWarnings("unchecked") |
856 |
|
public void forEach(Consumer<? super E> action) { |
849 |
– |
// checkInvariants(); |
857 |
|
Objects.requireNonNull(action); |
858 |
|
final Object[] elements = this.elements; |
859 |
|
final int capacity = elements.length; |
860 |
< |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
861 |
< |
action.accept(elementAt(i)); |
860 |
> |
int i, end, to, todo; |
861 |
> |
todo = (end = (i = head) + size) |
862 |
> |
- (to = (capacity - end >= 0) ? end : capacity); |
863 |
> |
for (;; i = 0, to = todo, todo = 0) { |
864 |
> |
for (; i < to; i++) |
865 |
> |
action.accept((E) elements[i]); |
866 |
> |
if (todo == 0) break; |
867 |
> |
} |
868 |
|
// checkInvariants(); |
869 |
|
} |
870 |
|
|
871 |
|
/** |
872 |
+ |
* Calls action on remaining elements, starting at index i and |
873 |
+ |
* traversing in ascending order. A variant of forEach that also |
874 |
+ |
* checks for concurrent modification, for use in iterators. |
875 |
+ |
*/ |
876 |
+ |
static <E> void forEachRemaining( |
877 |
+ |
Consumer<? super E> action, Object[] elements, int i, int remaining) { |
878 |
+ |
Objects.requireNonNull(action); |
879 |
+ |
final int capacity = elements.length; |
880 |
+ |
int end, to, todo; |
881 |
+ |
todo = (end = i + remaining) |
882 |
+ |
- (to = (capacity - end >= 0) ? end : capacity); |
883 |
+ |
for (;; i = 0, to = todo, todo = 0) { |
884 |
+ |
for (; i < to; i++) |
885 |
+ |
action.accept(nonNullElementAt(elements, i)); |
886 |
+ |
if (todo == 0) break; |
887 |
+ |
} |
888 |
+ |
} |
889 |
+ |
|
890 |
+ |
static <E> void forEachRemainingDescending( |
891 |
+ |
Consumer<? super E> action, Object[] elements, int i, int remaining) { |
892 |
+ |
Objects.requireNonNull(action); |
893 |
+ |
final int capacity = elements.length; |
894 |
+ |
int end, to, todo; |
895 |
+ |
todo = (to = ((end = i - remaining) >= -1) ? end : -1) - end; |
896 |
+ |
for (;; i = capacity - 1, to = capacity - 1 - todo, todo = 0) { |
897 |
+ |
for (; i > to; i--) |
898 |
+ |
action.accept(nonNullElementAt(elements, i)); |
899 |
+ |
if (todo == 0) break; |
900 |
+ |
} |
901 |
+ |
} |
902 |
+ |
|
903 |
+ |
/** |
904 |
|
* Replaces each element of this deque with the result of applying the |
905 |
|
* operator to that element, as specified by {@link List#replaceAll}. |
906 |
|
* |
911 |
|
Objects.requireNonNull(operator); |
912 |
|
final Object[] elements = this.elements; |
913 |
|
final int capacity = elements.length; |
914 |
< |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
915 |
< |
elements[i] = operator.apply(elementAt(i)); |
914 |
> |
int i, end, to, todo; |
915 |
> |
todo = (end = (i = head) + size) |
916 |
> |
- (to = (capacity - end >= 0) ? end : capacity); |
917 |
> |
for (;; i = 0, to = todo, todo = 0) { |
918 |
> |
for (; i < to; i++) |
919 |
> |
elements[i] = operator.apply(elementAt(i)); |
920 |
> |
if (todo == 0) break; |
921 |
> |
} |
922 |
|
// checkInvariants(); |
923 |
|
} |
924 |
|
|
953 |
|
final int capacity = elements.length; |
954 |
|
int i = head, j = i, remaining = size, deleted = 0; |
955 |
|
try { |
956 |
< |
for (; remaining > 0; remaining--, i = inc(i, capacity)) { |
956 |
> |
for (; remaining > 0; remaining--) { |
957 |
|
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
958 |
|
if (filter.test(e)) |
959 |
|
deleted++; |
960 |
|
else { |
961 |
|
if (j != i) |
962 |
|
elements[j] = e; |
963 |
< |
j = inc(j, capacity); |
963 |
> |
if (++j >= capacity) j = 0; |
964 |
|
} |
965 |
+ |
if (++i >= capacity) i = 0; |
966 |
|
} |
967 |
|
return deleted > 0; |
968 |
|
} catch (Throwable ex) { |
969 |
|
if (deleted > 0) |
970 |
< |
for (; remaining > 0; |
919 |
< |
remaining--, i = inc(i, capacity), j = inc(j, capacity)) |
970 |
> |
for (; remaining > 0; remaining--) { |
971 |
|
elements[j] = elements[i]; |
972 |
+ |
if (++i >= capacity) i = 0; |
973 |
+ |
if (++j >= capacity) j = 0; |
974 |
+ |
} |
975 |
|
throw ex; |
976 |
|
} finally { |
977 |
|
size -= deleted; |
978 |
< |
for (; --deleted >= 0; j = inc(j, capacity)) |
925 |
< |
elements[j] = null; |
978 |
> |
clearSlice(elements, j, deleted); |
979 |
|
// checkInvariants(); |
980 |
|
} |
981 |
|
} |
992 |
|
if (o != null) { |
993 |
|
final Object[] elements = this.elements; |
994 |
|
final int capacity = elements.length; |
995 |
< |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
996 |
< |
if (o.equals(elements[i])) |
997 |
< |
return true; |
995 |
> |
int i, end, to, todo; |
996 |
> |
todo = (end = (i = head) + size) |
997 |
> |
- (to = (capacity - end >= 0) ? end : capacity); |
998 |
> |
for (;; i = 0, to = todo, todo = 0) { |
999 |
> |
for (; i < to; i++) |
1000 |
> |
if (o.equals(elements[i])) |
1001 |
> |
return true; |
1002 |
> |
if (todo == 0) break; |
1003 |
> |
} |
1004 |
|
} |
1005 |
|
return false; |
1006 |
|
} |
1027 |
|
* The deque will be empty after this call returns. |
1028 |
|
*/ |
1029 |
|
public void clear() { |
1030 |
< |
final Object[] elements = this.elements; |
972 |
< |
final int capacity = elements.length, tail = head + size; |
973 |
< |
if (capacity - tail >= 0) |
974 |
< |
Arrays.fill(elements, head, tail, null); |
975 |
< |
else { |
976 |
< |
Arrays.fill(elements, head, capacity, null); |
977 |
< |
Arrays.fill(elements, 0, tail - capacity, null); |
978 |
< |
} |
1030 |
> |
clearSlice(elements, head, size); |
1031 |
|
size = head = 0; |
1032 |
|
// checkInvariants(); |
1033 |
|
} |
1034 |
|
|
1035 |
|
/** |
1036 |
+ |
* Nulls out count elements, starting at array index from. |
1037 |
+ |
*/ |
1038 |
+ |
private static void clearSlice(Object[] elements, int from, int count) { |
1039 |
+ |
final int capacity = elements.length, end = from + count; |
1040 |
+ |
final int leg = (capacity - end >= 0) ? end : capacity; |
1041 |
+ |
Arrays.fill(elements, from, leg, null); |
1042 |
+ |
if (leg != end) |
1043 |
+ |
Arrays.fill(elements, 0, end - capacity, null); |
1044 |
+ |
} |
1045 |
+ |
|
1046 |
+ |
/** |
1047 |
|
* Returns an array containing all of the elements in this deque |
1048 |
|
* in proper sequence (from first to last element). |
1049 |
|
* |
1063 |
|
private <T> T[] toArray(Class<T[]> klazz) { |
1064 |
|
final Object[] elements = this.elements; |
1065 |
|
final int capacity = elements.length; |
1066 |
< |
final int head = this.head, tail = head + size; |
1066 |
> |
final int head = this.head, end = head + size; |
1067 |
|
final T[] a; |
1068 |
< |
if (tail >= 0) { |
1069 |
< |
a = Arrays.copyOfRange(elements, head, tail, klazz); |
1068 |
> |
if (end >= 0) { |
1069 |
> |
a = Arrays.copyOfRange(elements, head, end, klazz); |
1070 |
|
} else { |
1071 |
|
// integer overflow! |
1072 |
|
a = Arrays.copyOfRange(elements, 0, size, klazz); |
1073 |
|
System.arraycopy(elements, head, a, 0, capacity - head); |
1074 |
|
} |
1075 |
< |
if (tail - capacity > 0) |
1076 |
< |
System.arraycopy(elements, 0, a, capacity - head, tail - capacity); |
1075 |
> |
if (end - capacity > 0) |
1076 |
> |
System.arraycopy(elements, 0, a, capacity - head, end - capacity); |
1077 |
|
return a; |
1078 |
|
} |
1079 |
|
|
1120 |
|
return toArray((Class<T[]>) a.getClass()); |
1121 |
|
final Object[] elements = this.elements; |
1122 |
|
final int capacity = elements.length; |
1123 |
< |
final int head = this.head, tail = head + size; |
1124 |
< |
if (capacity - tail >= 0) |
1125 |
< |
System.arraycopy(elements, head, a, 0, size); |
1126 |
< |
else { |
1127 |
< |
System.arraycopy(elements, head, a, 0, capacity - head); |
1065 |
< |
System.arraycopy(elements, 0, a, capacity - head, tail - capacity); |
1066 |
< |
} |
1123 |
> |
final int head = this.head, end = head + size; |
1124 |
> |
final int front = (capacity - end >= 0) ? size : capacity - head; |
1125 |
> |
System.arraycopy(elements, head, a, 0, front); |
1126 |
> |
if (front != size) |
1127 |
> |
System.arraycopy(elements, 0, a, capacity - head, end - capacity); |
1128 |
|
if (size < a.length) |
1129 |
|
a[size] = null; |
1130 |
|
return a; |
1169 |
|
// Write out elements in order. |
1170 |
|
final Object[] elements = this.elements; |
1171 |
|
final int capacity = elements.length; |
1172 |
< |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
1173 |
< |
s.writeObject(elements[i]); |
1172 |
> |
int i, end, to, todo; |
1173 |
> |
todo = (end = (i = head) + size) |
1174 |
> |
- (to = (capacity - end >= 0) ? end : capacity); |
1175 |
> |
for (;; i = 0, to = todo, todo = 0) { |
1176 |
> |
for (; i < to; i++) |
1177 |
> |
s.writeObject(elements[i]); |
1178 |
> |
if (todo == 0) break; |
1179 |
> |
} |
1180 |
|
} |
1181 |
|
|
1182 |
|
/** |
1199 |
|
} |
1200 |
|
|
1201 |
|
/** debugging */ |
1202 |
< |
private void checkInvariants() { |
1202 |
> |
void checkInvariants() { |
1203 |
|
try { |
1204 |
|
int capacity = elements.length; |
1205 |
< |
assert size >= 0 && size <= capacity; |
1206 |
< |
assert head >= 0 && ((capacity == 0 && head == 0 && size == 0) |
1207 |
< |
|| head < capacity); |
1208 |
< |
assert size == 0 |
1209 |
< |
|| (elements[head] != null && elements[tail()] != null); |
1210 |
< |
assert size == capacity |
1211 |
< |
|| (elements[dec(head, capacity)] == null |
1145 |
< |
&& elements[inc(tail(), capacity)] == null); |
1205 |
> |
// assert size >= 0 && size <= capacity; |
1206 |
> |
// assert head >= 0; |
1207 |
> |
// assert capacity == 0 || head < capacity; |
1208 |
> |
// assert size == 0 || elements[head] != null; |
1209 |
> |
// assert size == 0 || elements[tail()] != null; |
1210 |
> |
// assert size == capacity || elements[dec(head, capacity)] == null; |
1211 |
> |
// assert size == capacity || elements[inc(tail(), capacity)] == null; |
1212 |
|
} catch (Throwable t) { |
1213 |
|
System.err.printf("head=%d size=%d capacity=%d%n", |
1214 |
|
head, size, elements.length); |