Package com.tangosol.util
Class AbstractSparseArray<V>
- java.lang.Object
-
- com.tangosol.util.AbstractLongArray<V>
-
- com.tangosol.util.AbstractSparseArray<V>
-
- All Implemented Interfaces:
LongArray<V>
,Serializable
,Cloneable
,Iterable<V>
- Direct Known Subclasses:
PrimitiveSparseArray
,SparseArray
public abstract class AbstractSparseArray<V> extends AbstractLongArray<V>
A data structure resembling an array indexed by long values, stored as an AVL tree. Implementation is based on the public domain implementation by Julienne Walker. This implementation is not thread-safe.- Author:
- cp, mf 2007.10.08
- See Also:
- Implementation by Julienne Walker, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
AbstractSparseArray.Crawler
A tree node iterator.protected static class
AbstractSparseArray.Node<V>
An AVL tree node.-
Nested classes/interfaces inherited from interface com.tangosol.util.LongArray
LongArray.Iterator<V>
-
-
Constructor Summary
Constructors Constructor Description AbstractSparseArray()
Default constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
adjustDoubleBalance(AbstractSparseArray.Node<V> node, AbstractSparseArray.Node<V> child, int iBal)
Adjust the balance factor of a node and its descendants prior to a a double rotation.protected void
balancedInsertion(AbstractSparseArray.Node<V> parent, AbstractSparseArray.Node<V> child)
Insert a node into a tree and rebalance.protected void
balancePostRemove(AbstractSparseArray.Node<V> pruned, boolean fPrunedLeft)
Rebalance the tree following the removal of a node.V
ceiling(long lIndex)
Return the "first" value with an index which is greater than or equal to the specified index.long
ceilingIndex(long lIndex)
Return the "first" index which is greater than or equal to the specified index.void
clear()
Remove all nodes from the LongArray.AbstractSparseArray<V>
clone()
Make a clone of the LongArray.protected AbstractSparseArray.Node<V>
doubleRotate(AbstractSparseArray.Node<V> node, boolean fLeft)
Double rotate a node in a given direction.boolean
exists(long lIndex)
Determine if the specified index is in use.protected AbstractSparseArray.Node<V>
find(long lIndex)
Find the specified key and return its node.protected AbstractSparseArray.Node<V>
findInsertionPoint(long lIndex)
Find the point at which a Node with the specified index would be inserted.V
floor(long lIndex)
Return the "first" value with an index which is less than or equal to the specified index.long
floorIndex(long lIndex)
Return the "first" index which is less than or equal to the specified index.V
get(long lIndex)
Return the value stored at the specified index.long
getFirstIndex()
Determine the first index that exists in the LongArray.long
getLastIndex()
Determine the last index that exists in the LongArray.int
getSize()
Determine the size of the LongArray.protected AbstractSparseArray.Crawler
instantiateCrawler(AbstractSparseArray.Node<V> head, int fromdir, boolean fForward)
Instantiate a new Crawler at the specified location and direction.protected abstract AbstractSparseArray.Node<V>
instantiateNode(long lKey, V oValue)
Factory pattern: create a new Node with the specified key and value.LongArray.Iterator<V>
iterator()
Obtain a LongArray.Iterator of the contents of the LongArray.LongArray.Iterator<V>
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.void
print()
In-order printing of the contents of the SparseArray.V
remove(long lIndex)
Remove the specified index from the LongArray, returning its associated value.void
remove(long lIndexFrom, long lIndexTo)
Remove all nodes in the specified range.protected void
remove(AbstractSparseArray.Node<V> node)
Remove the specified node from the tree.protected AbstractSparseArray.Node<V>
replace(AbstractSparseArray.Node<V> nodeA, AbstractSparseArray.Node<V> nodeB)
Replace one node with another.LongArray.Iterator<V>
reverseIterator()
Obtain a LongArray.Iterator of the contents of the LongArray in reverse order (decreasing indices).LongArray.Iterator<V>
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.protected AbstractSparseArray.Node<V>
rotate(AbstractSparseArray.Node<V> node, boolean fLeft)
Rotate a node in a given direction.V
set(long lIndex, V oValue)
Add the passed item to the LongArray at the specified index.void
validate()
Validate that the tree is a proper AVL tree.* Note, Java assertions must be enabled for this to be effective.-
Methods inherited from class com.tangosol.util.AbstractLongArray
add, contains, equals, hashCode, indexOf, indexOf, isEmpty, lastIndexOf, lastIndexOf, toString
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Field Detail
-
m_head
protected AbstractSparseArray.Node<V> m_head
The first node of the tree (or null if the tree is empty). The first node is referred to as the "head" or the "root" node.
-
m_size
protected int m_size
The number of nodes in the tree. This can be determined by iterating through the tree, but by keeping the size cached, certain operations that need the size of the tree up front are much more efficient.
-
-
Method Detail
-
get
public V get(long lIndex)
Return the value stored at the specified index.
-
floorIndex
public long floorIndex(long lIndex)
Return the "first" index which is less than or equal to the specified index.- Parameters:
lIndex
- the index- Returns:
- the index or NOT_FOUND
-
floor
public V floor(long lIndex)
Return the "first" value with an index which is less than or equal to the specified index.- Parameters:
lIndex
- the index- Returns:
- the value or null
-
ceilingIndex
public long ceilingIndex(long lIndex)
Return the "first" index which is greater than or equal to the specified index.- Parameters:
lIndex
- the index- Returns:
- the index or NOT_FOUND
-
ceiling
public V ceiling(long lIndex)
Return the "first" value with an index which is greater than or equal to the specified index.- Parameters:
lIndex
- the index- Returns:
- the value or null
-
set
public V set(long lIndex, V oValue)
Add the passed item to the LongArray 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 valueoValue
- the object to store at the specified index- Returns:
- the object that was stored at the specified index, or null
-
exists
public boolean exists(long lIndex)
Determine if the specified index is in use.
-
remove
public V remove(long lIndex)
Remove the specified index from the LongArray, returning its associated value.
-
remove
public void remove(long lIndexFrom, long lIndexTo)
Remove all nodes in the specified range.
-
clear
public void clear()
Remove all nodes from the LongArray.
-
getSize
public int getSize()
Determine the size of the LongArray.
-
iterator
public LongArray.Iterator<V> iterator()
Obtain a LongArray.Iterator of the contents of the LongArray.- Returns:
- an instance of LongArray.Iterator
-
iterator
public LongArray.Iterator<V> 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.- Parameters:
lIndex
- the LongArray index to iterate from- Returns:
- an instance of LongArray.Iterator
-
reverseIterator
public LongArray.Iterator<V> reverseIterator()
Obtain a LongArray.Iterator of the contents of the LongArray in reverse order (decreasing indices).- Returns:
- an instance of LongArray.Iterator
-
reverseIterator
public LongArray.Iterator<V> 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.- Parameters:
lIndex
- the LongArray index to iterate from- Returns:
- an instance of LongArray.Iterator
-
getFirstIndex
public long getFirstIndex()
Determine the first index that exists in the LongArray.- Specified by:
getFirstIndex
in interfaceLongArray<V>
- Overrides:
getFirstIndex
in classAbstractLongArray<V>
- Returns:
- the lowest long value that exists in this LongArray, or NOT_FOUND if the LongArray is empty
-
getLastIndex
public long getLastIndex()
Determine the last index that exists in the LongArray.- Specified by:
getLastIndex
in interfaceLongArray<V>
- Overrides:
getLastIndex
in classAbstractLongArray<V>
- Returns:
- the highest long value that exists in this LongArray, or NOT_FOUND if the LongArray is empty
-
clone
public AbstractSparseArray<V> clone()
Make a clone of the LongArray. The element values are not deep-cloned.
-
print
public void print()
In-order printing of the contents of the SparseArray.
-
validate
public void validate()
Validate that the tree is a proper AVL tree.* Note, Java assertions must be enabled for this to be effective.
-
find
protected AbstractSparseArray.Node<V> find(long lIndex)
Find the specified key and return its node.- Parameters:
lIndex
- the long index to look for in the SparseArray- Returns:
- the node containing the index or null if the index is not in the SparseArray
-
remove
protected void remove(AbstractSparseArray.Node<V> node)
Remove the specified node from the tree.The supplied node must be part of the tree.
- Parameters:
node
- the node to remove
-
replace
protected AbstractSparseArray.Node<V> replace(AbstractSparseArray.Node<V> nodeA, AbstractSparseArray.Node<V> nodeB)
Replace one node with another.- Parameters:
nodeA
- the node to be unlinkednodeB
- the node to be linked in nodeA's place; may be null- Returns:
- nodeB's new parent;
-
rotate
protected AbstractSparseArray.Node<V> rotate(AbstractSparseArray.Node<V> node, boolean fLeft)
Rotate a node in a given direction.- Parameters:
node
- the node to rotatefLeft
- the rotation direction- Returns:
- the node's new parent (former child)
-
doubleRotate
protected AbstractSparseArray.Node<V> doubleRotate(AbstractSparseArray.Node<V> node, boolean fLeft)
Double rotate a node in a given direction.- Parameters:
node
- the node to rotatefLeft
- the final rotation direction- Returns:
- the node's new parent (former grandchild)
-
adjustDoubleBalance
protected void adjustDoubleBalance(AbstractSparseArray.Node<V> node, AbstractSparseArray.Node<V> child, int iBal)
Adjust the balance factor of a node and its descendants prior to a a double rotation.- Parameters:
node
- the node which was rotatedchild
- the child to adjustiBal
- the balance adjustment
-
findInsertionPoint
protected AbstractSparseArray.Node<V> findInsertionPoint(long lIndex)
Find the point at which a Node with the specified index would be inserted.If the tree is empty then null is returned. If the index already exists then the existing Node is returned, otherwise the Node which will be the parent of the new Node is returned.
- Parameters:
lIndex
- the index of the new node- Returns:
- null, node, or parent
-
balancedInsertion
protected void balancedInsertion(AbstractSparseArray.Node<V> parent, AbstractSparseArray.Node<V> child)
Insert a node into a tree and rebalance.- Parameters:
parent
- the location at which to insert the nodechild
- the node to insert
-
balancePostRemove
protected void balancePostRemove(AbstractSparseArray.Node<V> pruned, boolean fPrunedLeft)
Rebalance the tree following the removal of a node.- Parameters:
pruned
- the node whose sub-tree shrunkfPrunedLeft
- the side on which the sub-tree shrunk
-
instantiateNode
protected abstract AbstractSparseArray.Node<V> instantiateNode(long lKey, V oValue)
Factory pattern: create a new Node with the specified key and value.- Parameters:
lKey
- the long keyoValue
- the object value- Returns:
- the new node
-
instantiateCrawler
protected AbstractSparseArray.Crawler instantiateCrawler(AbstractSparseArray.Node<V> head, int fromdir, boolean fForward)
Instantiate a new Crawler at the specified location and direction.- Parameters:
head
- the node at which to start crawlingfromdir
- the direction in which to start crawlingfForward
- true iff crawler should advance forward towards the next element- Returns:
- the new crawler
-
-