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(); |
1104 |
|
private <T> T[] toArray(Class<T[]> klazz) { |
1105 |
|
final Object[] es = elements; |
1106 |
|
final T[] a; |
1107 |
< |
final int size = size(), head = this.head, end; |
1108 |
< |
final int len = Math.min(size, es.length - head); |
1109 |
< |
if ((end = head + size) >= 0) { |
1107 |
> |
final int head = this.head, tail = this.tail, end; |
1108 |
> |
if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) { |
1109 |
|
a = Arrays.copyOfRange(es, head, end, klazz); |
1110 |
|
} else { |
1111 |
|
// integer overflow! |
1112 |
< |
a = Arrays.copyOfRange(es, 0, size, klazz); |
1113 |
< |
System.arraycopy(es, head, a, 0, len); |
1112 |
> |
a = Arrays.copyOfRange(es, 0, end - head, klazz); |
1113 |
> |
System.arraycopy(es, head, a, 0, es.length - head); |
1114 |
|
} |
1115 |
< |
if (tail < head) |
1116 |
< |
System.arraycopy(es, 0, a, len, tail); |
1115 |
> |
if (end != tail) |
1116 |
> |
System.arraycopy(es, 0, a, es.length - head, tail); |
1117 |
|
return a; |
1118 |
|
} |
1119 |
|
|