235 |
|
* @return index 0 <= i < modulus |
236 |
|
*/ |
237 |
|
static final int add(int i, int distance, int modulus) { |
238 |
< |
if ((i += distance) - modulus >= 0) distance -= modulus; |
238 |
> |
if ((i += distance) - modulus >= 0) i -= modulus; |
239 |
|
return i; |
240 |
|
} |
241 |
|
|
322 |
|
* of its elements are null |
323 |
|
*/ |
324 |
|
public boolean addAll(Collection<? extends E> c) { |
325 |
< |
final int s = size(), needed; |
326 |
< |
if ((needed = s + c.size() - elements.length + 1) > 0) |
325 |
> |
final int s, needed; |
326 |
> |
if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0) |
327 |
|
grow(needed); |
328 |
|
c.forEach(this::addLast); |
329 |
|
// checkInvariants(); |
616 |
|
// checkInvariants(); |
617 |
|
final Object[] es = elements; |
618 |
|
final int capacity = es.length; |
619 |
< |
final int h = head; |
619 |
> |
final int h, t; |
620 |
|
// number of elements before to-be-deleted elt |
621 |
< |
final int front = sub(i, h, capacity); |
622 |
< |
final int back = size() - front - 1; // number of elements after |
621 |
> |
final int front = sub(i, h = head, capacity); |
622 |
> |
// number of elements after to-be-deleted elt |
623 |
> |
final int back = sub(t = tail, i, capacity) - 1; |
624 |
|
if (front < back) { |
625 |
|
// move front elements forwards |
626 |
|
if (h <= i) { |
636 |
|
return false; |
637 |
|
} else { |
638 |
|
// move back elements backwards |
639 |
< |
tail = dec(tail, capacity); |
639 |
> |
tail = dec(t, capacity); |
640 |
|
if (i <= tail) { |
641 |
|
System.arraycopy(es, i + 1, es, i, back); |
642 |
|
} else { // Wrap around |
643 |
< |
int firstLeg = capacity - (i + 1); |
643 |
< |
System.arraycopy(es, i + 1, es, i, firstLeg); |
643 |
> |
System.arraycopy(es, i + 1, es, i, capacity - (i + 1)); |
644 |
|
es[capacity - 1] = es[0]; |
645 |
< |
System.arraycopy(es, 1, es, 0, back - firstLeg - 1); |
645 |
> |
System.arraycopy(es, 1, es, 0, t - 1); |
646 |
|
} |
647 |
|
es[tail] = null; |
648 |
|
// checkInvariants(); |
710 |
|
throw new NoSuchElementException(); |
711 |
|
final Object[] es = elements; |
712 |
|
E e = nonNullElementAt(es, cursor); |
713 |
< |
lastRet = cursor; |
714 |
< |
cursor = inc(cursor, es.length); |
713 |
> |
cursor = inc(lastRet = cursor, es.length); |
714 |
|
remaining--; |
715 |
|
return e; |
716 |
|
} |
758 |
|
throw new NoSuchElementException(); |
759 |
|
final Object[] es = elements; |
760 |
|
E e = nonNullElementAt(es, cursor); |
761 |
< |
lastRet = cursor; |
763 |
< |
cursor = dec(cursor, es.length); |
761 |
> |
cursor = dec(lastRet = cursor, es.length); |
762 |
|
remaining--; |
763 |
|
return e; |
764 |
|
} |
820 |
|
|
821 |
|
/** Constructs spliterator over the given range. */ |
822 |
|
DeqSpliterator(int origin, int fence) { |
823 |
+ |
// assert 0 <= origin && origin < elements.length; |
824 |
+ |
// assert 0 <= fence && fence < elements.length; |
825 |
|
this.cursor = origin; |
826 |
|
this.fence = fence; |
827 |
|
} |
864 |
|
} |
865 |
|
|
866 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
867 |
< |
if (action == null) |
868 |
< |
throw new NullPointerException(); |
869 |
< |
final int t, i; |
870 |
< |
if ((t = getFence()) == (i = cursor)) |
871 |
< |
return false; |
867 |
> |
Objects.requireNonNull(action); |
868 |
|
final Object[] es = elements; |
869 |
+ |
if (fence < 0) { fence = tail; cursor = head; } // late-binding |
870 |
+ |
final int i; |
871 |
+ |
if ((i = cursor) == fence) |
872 |
+ |
return false; |
873 |
+ |
E e = nonNullElementAt(es, i); |
874 |
|
cursor = inc(i, es.length); |
875 |
< |
action.accept(nonNullElementAt(es, i)); |
875 |
> |
action.accept(e); |
876 |
|
return true; |
877 |
|
} |
878 |
|
|
1105 |
|
private <T> T[] toArray(Class<T[]> klazz) { |
1106 |
|
final Object[] es = elements; |
1107 |
|
final T[] a; |
1108 |
< |
final int size = size(), head = this.head, end; |
1109 |
< |
final int len = Math.min(size, es.length - head); |
1110 |
< |
if ((end = head + size) >= 0) { |
1108 |
> |
final int head = this.head, tail = this.tail, end; |
1109 |
> |
if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) { |
1110 |
> |
// Uses null extension feature of copyOfRange |
1111 |
|
a = Arrays.copyOfRange(es, head, end, klazz); |
1112 |
|
} else { |
1113 |
|
// integer overflow! |
1114 |
< |
a = Arrays.copyOfRange(es, 0, size, klazz); |
1115 |
< |
System.arraycopy(es, head, a, 0, len); |
1114 |
> |
a = Arrays.copyOfRange(es, 0, end - head, klazz); |
1115 |
> |
System.arraycopy(es, head, a, 0, es.length - head); |
1116 |
|
} |
1117 |
< |
if (tail < head) |
1118 |
< |
System.arraycopy(es, 0, a, len, tail); |
1117 |
> |
if (end != tail) |
1118 |
> |
System.arraycopy(es, 0, a, es.length - head, tail); |
1119 |
|
return a; |
1120 |
|
} |
1121 |
|
|
1244 |
|
// head == tail disambiguates to "empty". |
1245 |
|
try { |
1246 |
|
int capacity = elements.length; |
1247 |
< |
// assert head >= 0 && head < capacity; |
1248 |
< |
// assert tail >= 0 && tail < capacity; |
1247 |
> |
// assert 0 <= head && head < capacity; |
1248 |
> |
// assert 0 <= tail && tail < capacity; |
1249 |
|
// assert capacity > 0; |
1250 |
|
// assert size() < capacity; |
1251 |
|
// assert head == tail || elements[head] != null; |