@Override public void remove() { if (lastEntry == null) { throw new IllegalStateException("No current element"); } checkForModification(); if (lastNode.size == 1) { // Safe to remove lastNode, the iterator is on the next node tree.deleteNode(lastNode); } else if (lastNode.leftIndex == lastIndex) { // Safe to remove leftmost entry, the iterator is on the next index lastNode.removeEntryLeft(); } else if (lastNode.rightIndex == lastIndex) { // Safe to remove the rightmost entry, the iterator is on the next node lastNode.removeEntryRight(); } else { // Remove entry in the middle of the array assert node == lastNode; int oldRIndex = lastNode.rightIndex; lastNode.removeEntryAt(lastIndex); if (oldRIndex > lastNode.rightIndex) { // Entries moved to the left in the array so index must be reset index = lastIndex; } } lastEntry = null; iteratorModCount++; tree.modCount++; tree.size--; }
private E removeElementLeft(Node<E> node) { modCount++; size--; E entry = node.removeEntryLeft(); if (node.isEmpty()) { deleteNode(node); } else if (node.prev != null && (Node.NODE_SIZE - 1 - node.prev.rightIndex) >= node.size) { // Remaining entries fit in the prev node, move them and delete this node node.prev.addEntriesRight(node); deleteNode(node); } else if (node.next != null && node.next.leftIndex >= node.size) { // Remaining entries fit in the next node, move them and delete this node node.next.addEntriesLeft(node); deleteNode(node); } else if (node.prev != null && node.prev.size < node.leftIndex) { // Entries in prev node will fit in this node, move them and delete prev node.addEntriesLeft(node.prev); deleteNode(node.prev); } return entry; }