493 |
|
* Unlinks non-null first node. |
494 |
|
*/ |
495 |
|
private void unlinkFirst(Node<E> first, Node<E> next) { |
496 |
< |
// assert first != null && next != null && first.item == null; |
496 |
> |
// assert first != null; |
497 |
> |
// assert next != null; |
498 |
> |
// assert first.item == null; |
499 |
|
Node<E> o = null, p = next; |
500 |
|
for (int hops = 0; ; ++hops) { |
501 |
|
Node<E> q; |
529 |
|
* Unlinks non-null last node. |
530 |
|
*/ |
531 |
|
private void unlinkLast(Node<E> last, Node<E> prev) { |
532 |
< |
// assert last != null && prev != null && last.item == null; |
532 |
> |
// assert last != null; |
533 |
> |
// assert prev != null; |
534 |
> |
// assert last.item == null; |
535 |
|
Node<E> o = null, p = prev; |
536 |
|
for (int hops = 0; ; ++hops) { |
537 |
|
Node<E> q; |
789 |
|
t = newNode; |
790 |
|
} |
791 |
|
} |
792 |
< |
if (h == null) |
793 |
< |
h = t = new Node<E>(null); |
792 |
> |
initHeadTail(h, t); |
793 |
> |
} |
794 |
> |
|
795 |
> |
/** |
796 |
> |
* Initializes head and tail, ensuring invariants hold. |
797 |
> |
*/ |
798 |
> |
private void initHeadTail(Node<E> h, Node<E> t) { |
799 |
> |
if (h == t) { |
800 |
> |
if (h == null) |
801 |
> |
h = t = new Node<E>(null); |
802 |
> |
else { |
803 |
> |
// Avoid edge case of a single Node with non-null item. |
804 |
> |
Node<E> newNode = new Node<E>(null); |
805 |
> |
t.lazySetNext(newNode); |
806 |
> |
newNode.lazySetPrev(t); |
807 |
> |
t = newNode; |
808 |
> |
} |
809 |
> |
} |
810 |
|
head = h; |
811 |
|
tail = t; |
812 |
|
} |
1352 |
|
t = newNode; |
1353 |
|
} |
1354 |
|
} |
1355 |
< |
if (h == null) |
1336 |
< |
h = t = new Node<E>(null); |
1337 |
< |
head = h; |
1338 |
< |
tail = t; |
1355 |
> |
initHeadTail(h, t); |
1356 |
|
} |
1357 |
|
|
1358 |
|
// Unsafe mechanics |