479 |
|
return removeFirst(); |
480 |
|
} |
481 |
|
|
482 |
+ |
private void checkInvariants() { |
483 |
+ |
assert elements[tail] == null; |
484 |
+ |
assert head == tail ? elements[head] == null : |
485 |
+ |
(elements[head] != null && |
486 |
+ |
elements[(tail - 1) & (elements.length - 1)] != null); |
487 |
+ |
assert elements[(head - 1) & (elements.length - 1)] == null; |
488 |
+ |
} |
489 |
+ |
|
490 |
|
/** |
491 |
|
* Removes the element at the specified position in the elements array, |
492 |
|
* adjusting head and tail as necessary. This can result in motion of |
498 |
|
* @return true if elements moved backwards |
499 |
|
*/ |
500 |
|
private boolean delete(int i) { |
501 |
+ |
checkInvariants(); |
502 |
|
final E[] elements = this.elements; |
503 |
|
final int mask = elements.length - 1; |
504 |
|
final int h = head; |
536 |
|
} |
537 |
|
} |
538 |
|
|
530 |
– |
private boolean xdelete(int i) { |
531 |
– |
int mask = elements.length - 1; |
532 |
– |
int front = (i - head) & mask; |
533 |
– |
int back = (tail - i) & mask; |
534 |
– |
|
535 |
– |
// Invariant: head <= i < tail mod circularity |
536 |
– |
if (front >= ((tail - head) & mask)) |
537 |
– |
throw new ConcurrentModificationException(); |
538 |
– |
|
539 |
– |
// Optimize for least element motion |
540 |
– |
if (front < back) { |
541 |
– |
if (head <= i) { |
542 |
– |
System.arraycopy(elements, head, elements, head + 1, front); |
543 |
– |
} else { // Wrap around |
544 |
– |
System.arraycopy(elements, 0, elements, 1, i); |
545 |
– |
elements[0] = elements[mask]; |
546 |
– |
System.arraycopy(elements, head, elements, head + 1, mask - head); |
547 |
– |
} |
548 |
– |
elements[head] = null; |
549 |
– |
head = (head + 1) & mask; |
550 |
– |
return false; |
551 |
– |
} else { |
552 |
– |
int t = tail; |
553 |
– |
tail = (tail - 1) & mask; |
554 |
– |
if (i < t) { // Copy the null tail as well |
555 |
– |
System.arraycopy(elements, i + 1, elements, i, back); |
556 |
– |
} else { // Wrap around |
557 |
– |
System.arraycopy(elements, i + 1, elements, i, mask - i); |
558 |
– |
elements[mask] = elements[0]; |
559 |
– |
System.arraycopy(elements, 1, elements, 0, t); |
560 |
– |
} |
561 |
– |
return true; |
562 |
– |
} |
563 |
– |
} |
564 |
– |
|
539 |
|
// *** Collection Methods *** |
540 |
|
|
541 |
|
/** |
617 |
|
} |
618 |
|
} |
619 |
|
|
646 |
– |
|
620 |
|
private class DescendingIterator implements Iterator<E> { |
621 |
|
/* |
622 |
|
* This class is nearly a mirror-image of DeqIterator, using |
837 |
|
// Read in all elements in the proper order. |
838 |
|
for (int i = 0; i < size; i++) |
839 |
|
elements[i] = (E)s.readObject(); |
867 |
– |
|
840 |
|
} |
841 |
|
} |