jdsl
Class ArrayListSequence

java.lang.Object
  |
  +--jdsl.ArrayListSequence

public class ArrayListSequence
extends java.lang.Object
implements Sequence

A Sequence implemented with a java.util.ArrayList. You will probably need to read it to understand the operation and the time complexities of this class. In particular, since ArrayList allocates a new array and copies the old array into the new one whenever it runs out of space, calls like vec_.add(index,object) are NOT constant-time operations.


Constructor Summary
ArrayListSequence()
          Constructs a new Sequence with an initial capacity of 4.
ArrayListSequence(int initialCapacity)
          Constructs a new sequence with an underlying ArrayList of size initialCapacity.
 
Method Summary
 Position after(Position successor)
          Gets the position after a position.
 Position atRank(int rank)
           
 Position before(Position successor)
          Gets the position before a position.
protected  jdsl.IndexedPosition castToIndexedPosition(Position p)
          For the following routine, we pass back an InvalidPositionException, which will be more informational and useful to the programmer than a CCE.
protected  void checkEmpty()
           
protected  void checkRank(int rank)
           
protected  void decSize()
          Provided for remove, which needs to decrease the size by one
 java.util.Iterator elements()
          Gets all the elements in this container, in order.
 Position first()
          Gets the first position of this sequence.
protected  void incSize()
          Provided for insertAtRank, which needs to up the size by one
 Position insertAfter(Position predecessor, java.lang.Object o)
          Inserts the given element before the given Position, creating and returning a new Position (before the given one) at which to store the element.
 Position insertAtRank(int rank, java.lang.Object o)
          Inserts the given element at the given rank, creating and returning a new Position at which to store the element.
 Position insertBefore(Position successor, java.lang.Object o)
          Inserts the given element before the given Position, creating and returning a new Position (before the given one) at which to store the element.
 Position insertFirst(java.lang.Object o)
          Inserts the given element first in the sequence, creating and returning a new Position at which to store the element.
 Position insertLast(java.lang.Object o)
          Inserts the given element last in the sequence, creating and returning a new Position at which to store the element.
 boolean isEmpty()
          Tests if the container is empty.
 Position last()
          Gets the last position of this sequence.
 Container newContainer()
          Returns a new, empty ArrayListSequence.
 java.util.Iterator positions()
          Gets all the Positions in this container, in order.
 int rankOf(Position p)
          Gets the rank of a position.
 java.lang.Object remove(Position p)
          Removes and invalidates the given Position, returning the element stored at it.
 java.lang.Object removeAfter(Position p)
           
 java.lang.Object removeAtRank(int i)
          Removes an element at a particular rank.
 java.lang.Object removeBefore(Position p)
           
 java.lang.Object removeFirst()
          Removes the first element of the sequence.
 java.lang.Object removeLast()
          Removes the last element of the sequence.
 java.lang.Object replace(Position p, java.lang.Object newElement)
          Replaces the element at a position with a new element.
 int size()
          Gets the size of this container.
 void swap(Position a, Position b)
          Swaps the elements associated with the two Positions, leaving the Positions themselves "where" they were.
protected  void updateIndicesStartingAt(int index)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayListSequence

public ArrayListSequence()
Constructs a new Sequence with an initial capacity of 4.

ArrayListSequence

public ArrayListSequence(int initialCapacity)
Constructs a new sequence with an underlying ArrayList of size initialCapacity.
Method Detail

newContainer

public Container newContainer()
Returns a new, empty ArrayListSequence.

atRank

public Position atRank(int rank)
                throws BoundaryViolationException
Specified by:
atRank in interface Sequence
Parameters:
rank - An integer between 0 and N-1, where N is the size() of the sequence
Returns:
The Position at that rank


insertAtRank

public Position insertAtRank(int rank,
                             java.lang.Object o)
                      throws BoundaryViolationException
Inserts the given element at the given rank, creating and returning a new Position at which to store the element.

Specified by:
insertAtRank in interface Sequence
Parameters:
rank - Integer representing the rank of the newly inserted element after insertion is completely (i.e., the ranks of all following positions will increase by 1)
o - Any java.lang.Object
Returns:
Position at which the inserted element is located


remove

public java.lang.Object remove(Position p)
                        throws InvalidPositionException
Removes and invalidates the given Position, returning the element stored at it. The ranks of all following Positions decrease by 1.

Parameters:
p - A Position in this sequence
Returns:
Object formerly stored at Position p


removeAfter

public java.lang.Object removeAfter(Position p)

removeBefore

public java.lang.Object removeBefore(Position p)

incSize

protected void incSize()
Provided for insertAtRank, which needs to up the size by one

decSize

protected void decSize()
Provided for remove, which needs to decrease the size by one

first

public Position first()
               throws EmptyContainerException
Gets the first position of this sequence.
Returns:
Position for first element in the sequence, if any
Throws:
EmptyContainerException - if the container is empty

last

public Position last()
Gets the last position of this sequence.
Returns:
Position for last element in the sequence, if any
Throws:
EmptyContainerException - if the container is empty

before

public Position before(Position successor)
                throws InvalidPositionException,
                       BoundaryViolationException
Gets the position before a position.
Parameters:
successor - A Position in this sequence
Returns:
The Position before the given Position
Throws:
InvalidPositionException - if successor is not from this container.
BoundaryViolationException - if successor is the first position of this sequence.

after

public Position after(Position successor)
               throws InvalidPositionException,
                      BoundaryViolationException
Gets the position after a position.
Parameters:
predecessor - A Position in this sequence
Returns:
The Position after the given Position

rankOf

public int rankOf(Position p)
           throws InvalidPositionException
Gets the rank of a position. Note that this is zero based, rankOf ( first() ) == 0
Specified by:
rankOf in interface Sequence
Parameters:
p - A Position in this sequence
Returns:
An integer representing the rank of the given Position in the sequence
Throws:
InvalidPositionException - if p is null * or not from this container.

insertFirst

public Position insertFirst(java.lang.Object o)
Inserts the given element first in the sequence, creating and returning a new Position at which to store the element.
Parameters:
o - Any java.lang.Object
Returns:
Position at which the inserted element is located

insertLast

public Position insertLast(java.lang.Object o)
Inserts the given element last in the sequence, creating and returning a new Position at which to store the element.
Parameters:
o - Any java.lang.Object
Returns:
Position at which the inserted element is located

insertBefore

public Position insertBefore(Position successor,
                             java.lang.Object o)
                      throws InvalidPositionException,
                             BoundaryViolationException
Inserts the given element before the given Position, creating and returning a new Position (before the given one) at which to store the element.
Parameters:
successor - Position that will follow the inserted Position
o - Any java.lang.Object
Throws:
InvalidPositionException - if predecessor is null not from this container.
BoundaryViolationException - if predecessor is the last position of this sequence.

insertAfter

public Position insertAfter(Position predecessor,
                            java.lang.Object o)
                     throws InvalidPositionException,
                            BoundaryViolationException
Inserts the given element before the given Position, creating and returning a new Position (before the given one) at which to store the element.
Parameters:
successor - Position that will follow the inserted Position
o - Any java.lang.Object
Throws:
InvalidPositionException - if predecessor is null not from this container.
BoundaryViolationException - if predecessor is the last position of this sequence.

removeLast

public java.lang.Object removeLast()
                            throws EmptyContainerException
Removes the last element of the sequence.
Returns:
The last element of the sequence.
Throws:
EmptyContainerException - if the container is empty.

removeFirst

public java.lang.Object removeFirst()
                             throws EmptyContainerException
Removes the first element of the sequence.
Returns:
The first element of the sequence.
Throws:
EmptyContainerException - if the container is empty.

removeAtRank

public java.lang.Object removeAtRank(int i)
                              throws EmptyContainerException,
                                     BoundaryViolationException
Removes an element at a particular rank.
Specified by:
removeAtRank in interface Sequence
Parameters:
i - The rank of the element to remove.
Returns:
The element at rank i
Throws:
EmptyContainerException - if the container is empty.
BoundaryViolationException - if i is out of bounds.

positions

public java.util.Iterator positions()
Gets all the Positions in this container, in order.
Returns:
An Iterator of all Positions in the container

replace

public java.lang.Object replace(Position p,
                                java.lang.Object newElement)
                         throws InvalidPositionException
Replaces the element at a position with a new element.
Parameters:
p - The Position at which replacement is to occur.
newElement - The element now to be stored at Position p
Returns:
The old element, formerly stored at Position p
Throws:
InvalidPositionException - if p is null or not from this container.

swap

public void swap(Position a,
                 Position b)
          throws InvalidPositionException
Swaps the elements associated with the two Positions, leaving the Positions themselves "where" they were. One of the Positions can be from another IndexedSequence, and the swap will be across containers.
Parameters:
a -  
b -  
Throws:
InvalidPositionException - if either position is null

size

public int size()
Gets the size of this container.
Returns:
Number of elements in the container, where each occurrence of a duplicated element adds 1 to the size() of the container.

isEmpty

public boolean isEmpty()
Tests if the container is empty.
Returns:
true if the container is empty, and false otherwise.

elements

public java.util.Iterator elements()
Gets all the elements in this container, in order.
Returns:
A Iterator of all elements in the container

castToIndexedPosition

protected jdsl.IndexedPosition castToIndexedPosition(Position p)
                                              throws InvalidPositionException
For the following routine, we pass back an InvalidPositionException, which will be more informational and useful to the programmer than a CCE.

checkEmpty

protected void checkEmpty()
                   throws EmptyContainerException

checkRank

protected void checkRank(int rank)
                  throws BoundaryViolationException

updateIndicesStartingAt

protected void updateIndicesStartingAt(int index)