Class PrimitiveSparseArray

All Implemented Interfaces:
LongArray<Long>, Serializable, Cloneable, Iterable<Long>

public class PrimitiveSparseArray extends AbstractSparseArray<Long> implements LongArray<Long>
A data structure resembling a long array indexed by long values.

While the PrimitiveSparseArray implements the LongArray interface, it stores all values as primitive longs, and as such any Object supplied to its LongArray interface must be provided as a java.lang.Long. The PrimitiveSparseArray provides additional methods whose signatures utilize primitive longs to avoid the creation of temporary Long objects.

Author:
mf 10.08.2007
See Also:
  • Constructor Details

    • PrimitiveSparseArray

      public PrimitiveSparseArray()
      Default constructor.
    • PrimitiveSparseArray

      public PrimitiveSparseArray(PrimitiveSparseArray array)
      Copy constructor.
      Parameters:
      array - a PrimitiveSparseArray to copy from.
  • Method Details

    • iterator

      public PrimitiveSparseArray.Iterator iterator()
      Obtain a LongArray.Iterator of the contents of the LongArray.
      Specified by:
      iterator in interface Iterable<Long>
      Specified by:
      iterator in interface LongArray<Long>
      Overrides:
      iterator in class AbstractSparseArray<Long>
      Returns:
      an instance of LongArray.Iterator
    • iterator

      public PrimitiveSparseArray.Iterator iterator(long lIndex)
      Obtain a LongArray.Iterator of the contents of the LongArray, starting at a particular index such that the first call to next will set the location of the iterator at the first existent index that is greater than or equal to the specified index, or will throw a NoSuchElementException if there is no such existent index.
      Specified by:
      iterator in interface LongArray<Long>
      Overrides:
      iterator in class AbstractSparseArray<Long>
      Parameters:
      lIndex - the LongArray index to iterate from
      Returns:
      an instance of LongArray.Iterator
    • reverseIterator

      public PrimitiveSparseArray.Iterator reverseIterator()
      Obtain a LongArray.Iterator of the contents of the LongArray in reverse order (decreasing indices).
      Specified by:
      reverseIterator in interface LongArray<Long>
      Overrides:
      reverseIterator in class AbstractSparseArray<Long>
      Returns:
      an instance of LongArray.Iterator
    • reverseIterator

      public PrimitiveSparseArray.Iterator reverseIterator(long lIndex)
      Obtain a LongArray.Iterator of the contents of the LongArray in reverse order (decreasing indices), starting at a particular index such that the first call to next will set the location of the iterator at the first existent index that is less than or equal to the specified index, or will throw a NoSuchElementException if there is no such existent index.
      Specified by:
      reverseIterator in interface LongArray<Long>
      Overrides:
      reverseIterator in class AbstractSparseArray<Long>
      Parameters:
      lIndex - the LongArray index to iterate from
      Returns:
      an instance of LongArray.Iterator
    • getPrimitive

      public long getPrimitive(long lIndex)
      Return the value stored at the specified index.
      Parameters:
      lIndex - a long index value
      Returns:
      the value stored at the specified index, or NOT_FOUND if none exists
    • removePrimitive

      public long removePrimitive(long lIndex)
      Remove the specified index from the PrimitiveSparseArray, returning its associated value.
      Parameters:
      lIndex - the index into the LongArray
      Returns:
      the associated value or NOT_FOUND if the specified index does not exist
    • setPrimitive

      public long setPrimitive(long lIndex, long lValue)
      Add the passed item to the PrimitiveSparseArray at the specified index.

      If the index is already used, the passed value will replace the current value stored with the key, and the replaced value will be returned.

      It is expected that LongArray implementations will "grow" as necessary to support the specified index.

      Parameters:
      lIndex - a long index value
      lValue - the long value to store at the specified index
      Returns:
      the value that was stored at the specified index, or NOT_FOUND if the specified index does not exist
    • instantiateNode

      protected AbstractSparseArray.Node instantiateNode(long lKey, Long value)
      Factory pattern: create a new Node with the specified key and value.
      Specified by:
      instantiateNode in class AbstractSparseArray<Long>
      Parameters:
      lKey - the long key
      value - the object value
      Returns:
      the new node
    • instantiateNode

      protected AbstractSparseArray.Node instantiateNode(long lKey, long lValue)
      Create a new Node with the specified key and value.
      Parameters:
      lKey - the long key
      lValue - the long value
      Returns:
      the new node
    • instantiateCrawler

      protected AbstractSparseArray<Long>.Crawler instantiateCrawler(AbstractSparseArray.Node head, int fromdir, boolean fForward)
      Instantiate a new Crawler at the specified location and direction.
      Overrides:
      instantiateCrawler in class AbstractSparseArray<Long>
      Parameters:
      head - the node at which to start crawling
      fromdir - the direction in which to start crawling
      fForward - true iff crawler should advance forward towards the next element
      Returns:
      the new crawler