@ -143,76 +143,82 @@ public class ImplicitLinkedHashCollection<E extends ImplicitLinkedHashCollection
@@ -143,76 +143,82 @@ public class ImplicitLinkedHashCollection<E extends ImplicitLinkedHashCollection
}
private class ImplicitLinkedHashCollectionIterator implements ListIterator < E > {
private int cursor = 0 ;
private Element cur = head ;
private int lastReturnedSlot = INVALID_INDEX ;
private int index = 0 ;
private Element cur ;
private Element lastReturned ;
ImplicitLinkedHashCollectionIterator ( int index ) {
this . cur = indexToElement ( head , elements , head . next ( ) ) ;
for ( int i = 0 ; i < index ; + + i ) {
cur = indexToElement ( head , elements , cur . next ( ) ) ;
cursor + + ;
next ( ) ;
}
this . lastReturned = null ;
}
@Override
public boolean hasNext ( ) {
return cursor ! = size ;
return cur ! = head ;
}
@Override
public boolean hasPrevious ( ) {
return curso r ! = 0 ;
return indexToElement ( head , elements , cur . p rev ( ) ) ! = head ;
}
@Override
public E next ( ) {
if ( cursor = = size ) {
if ( ! hasNext ( ) ) {
throw new NoSuchElementException ( ) ;
}
lastReturnedSlot = cur . next ( ) ;
cur = indexToElement ( head , elements , cur . next ( ) ) ;
+ + cursor ;
@SuppressWarnings ( "unchecked" )
E returnValue = ( E ) cur ;
lastReturned = cur ;
cur = indexToElement ( head , elements , cur . next ( ) ) ;
+ + index ;
return returnValue ;
}
@Override
public E previous ( ) {
if ( cursor = = 0 ) {
Element prev = indexToElement ( head , elements , cur . prev ( ) ) ;
if ( prev = = head ) {
throw new NoSuchElementException ( ) ;
}
cur = prev ;
- - index ;
lastReturned = cur ;
@SuppressWarnings ( "unchecked" )
E returnValue = ( E ) cur ;
cur = indexToElement ( head , elements , cur . prev ( ) ) ;
lastReturnedSlot = cur . next ( ) ;
- - cursor ;
return returnValue ;
}
@Override
public int nextIndex ( ) {
return cursor ;
return index ;
}
@Override
public int previousIndex ( ) {
return cursor - 1 ;
return index - 1 ;
}
@Override
public void remove ( ) {
if ( lastReturnedSlot = = INVALID_INDEX ) {
if ( lastReturned = = null ) {
throw new IllegalStateException ( ) ;
}
if ( cur = = indexToElement ( head , elements , lastReturnedSlot ) ) {
cursor - - ;
cur = indexToElement ( head , elements , cur . prev ( ) ) ;
Element nextElement = indexToElement ( head , elements , lastReturned . next ( ) ) ;
removeFromList ( head , elements , nextElement . prev ( ) ) ;
size - - ;
if ( lastReturned = = cur ) {
// If the element we are removing was cur, set cur to cur->next.
cur = nextElement ;
} else {
// If the element we are removing comes before cur, decrement the index,
// since there are now fewer entries before cur.
- - index ;
}
ImplicitLinkedHashCollection . this . removeElementAtSlot ( lastReturnedSlot ) ;
lastReturnedSlot = INVALID_INDEX ;
lastReturned = null ;
}
@Override
@ -561,6 +567,22 @@ public class ImplicitLinkedHashCollection<E extends ImplicitLinkedHashCollection
@@ -561,6 +567,22 @@ public class ImplicitLinkedHashCollection<E extends ImplicitLinkedHashCollection
clear ( elements . length ) ;
}
/ * *
* Moves an element which is already in the collection so that it comes last
* in iteration order .
* /
final public void moveToEnd ( E element ) {
if ( element . prev ( ) = = INVALID_INDEX | | element . next ( ) = = INVALID_INDEX ) {
throw new RuntimeException ( "Element " + element + " is not in the collection." ) ;
}
Element prevElement = indexToElement ( head , elements , element . prev ( ) ) ;
Element nextElement = indexToElement ( head , elements , element . next ( ) ) ;
int slot = prevElement . next ( ) ;
prevElement . setNext ( element . next ( ) ) ;
nextElement . setPrev ( element . prev ( ) ) ;
addToListTail ( head , elements , slot ) ;
}
/ * *
* Removes all of the elements from this set , and resets the set capacity
* based on the provided expected number of elements .