rangesearch.ref
Class RangeTree2DImpl

java.lang.Object
  extended byrangesearch.ref.RangeTree2DImpl
All Implemented Interfaces:
RangeTree2D

public class RangeTree2DImpl
extends java.lang.Object
implements RangeTree2D

An implementation of a two-dimensional range tree, using a balanced binary tree as the primary structure and an array as the secondary structure.


Field Summary
protected  jdsl.core.api.Comparator m_comp1
           
protected  jdsl.core.api.Comparator m_comp2
           
protected  jdsl.core.api.BinaryTree m_tree
           
 
Constructor Summary
RangeTree2DImpl()
           
 
Method Summary
protected  int binarySearchAfter(jdsl.core.ref.ArraySequence s, java.lang.Object obj, int start, int end)
          Recursively finds the index into a sorted array of the first element greater than or equal to the given element.
protected  int binarySearchBefore(jdsl.core.ref.ArraySequence s, java.lang.Object obj, int start, int end)
          Recursively finds the index into a sorted array of the last element less than or equal to the given element.
protected  void buildRangeBounds(jdsl.core.api.Position subTreeRoot)
          Extracts the minimum and maximum elements of the subtree and stores them as decorations with the string tags "min" and "max".
protected  void buildRangeSet(jdsl.core.api.Position subTreeRoot)
          Recursively builds the secondary structure (a sorted array) at each internal node.
protected  void constructTree(jdsl.core.api.Position subTreeRoot, jdsl.core.ref.ArraySequence s, int start, int end)
          Recursively constructs a tree, expanding the given node to include the given subsequence of elements.
protected  void findAllocNode(jdsl.core.api.Position subTreeRoot, jdsl.core.api.Sequence allocNodes, java.lang.Object start, java.lang.Object end)
          Recursively searches the binary tree for allocation nodes.
 void initialize(jdsl.core.api.ObjectIterator oi, jdsl.core.api.Comparator comp1, jdsl.core.api.Comparator comp2)
          Initializes the range tree to the given set of elements, with the given comparator.
protected  jdsl.core.api.Sequence mergeSortedArrays(jdsl.core.api.Sequence s1, jdsl.core.api.Sequence s2)
          Merges two sorted arrays together into another sorted array, as sorted by the secondary comparator.
 jdsl.core.api.ObjectIterator query(java.lang.Object start1, java.lang.Object end1, java.lang.Object start2, java.lang.Object end2)
          Queries the elements in the range tree with the given ranges.
protected  void queryAllocNode(jdsl.core.ref.ArraySequence s, jdsl.core.api.Sequence retSeq, java.lang.Object start, java.lang.Object end)
          Finds the nodes between start and end in the given sequence and appends them to the return sequence.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_comp1

protected jdsl.core.api.Comparator m_comp1

m_comp2

protected jdsl.core.api.Comparator m_comp2

m_tree

protected jdsl.core.api.BinaryTree m_tree
Constructor Detail

RangeTree2DImpl

public RangeTree2DImpl()
Method Detail

initialize

public void initialize(jdsl.core.api.ObjectIterator oi,
                       jdsl.core.api.Comparator comp1,
                       jdsl.core.api.Comparator comp2)
Initializes the range tree to the given set of elements, with the given comparator.

Specified by:
initialize in interface RangeTree2D
Parameters:
oi - an iterator over the elements to search.
comp1 - the comparator for the primary structure (used for building the binary tree).
comp2 - the comparator for the secondary structure (used for sorting the array.

constructTree

protected void constructTree(jdsl.core.api.Position subTreeRoot,
                             jdsl.core.ref.ArraySequence s,
                             int start,
                             int end)
Recursively constructs a tree, expanding the given node to include the given subsequence of elements.

Parameters:
subTreeRoot - the node to expand.
s - the sequence from which to extract elements.
start - the start index of the sequence to extract.
end - the end index of the sequence to extract.

buildRangeSet

protected void buildRangeSet(jdsl.core.api.Position subTreeRoot)
Recursively builds the secondary structure (a sorted array) at each internal node.

Parameters:
subTreeRoot - the node for which to build a secondary structure.

mergeSortedArrays

protected jdsl.core.api.Sequence mergeSortedArrays(jdsl.core.api.Sequence s1,
                                                   jdsl.core.api.Sequence s2)
Merges two sorted arrays together into another sorted array, as sorted by the secondary comparator.

Parameters:
s1 - the first array to merge.
s2 - the second array to merge.
Returns:
the merged, sorted sequence.

buildRangeBounds

protected void buildRangeBounds(jdsl.core.api.Position subTreeRoot)
Extracts the minimum and maximum elements of the subtree and stores them as decorations with the string tags "min" and "max".

Parameters:
subTreeRoot - the subtree on which to operate.

query

public jdsl.core.api.ObjectIterator query(java.lang.Object start1,
                                          java.lang.Object end1,
                                          java.lang.Object start2,
                                          java.lang.Object end2)
Queries the elements in the range tree with the given ranges. Elements equal to the range bounds are considered to be within the range.

Specified by:
query in interface RangeTree2D
Parameters:
start1 - the starting element for the primary search range.
end1 - the ending element for the primary search range.
start2 - the starting element for the secondary search range.
end2 - the ending element for the secondary search range.
Returns:
an iterator over the elements in the given ranges.

findAllocNode

protected void findAllocNode(jdsl.core.api.Position subTreeRoot,
                             jdsl.core.api.Sequence allocNodes,
                             java.lang.Object start,
                             java.lang.Object end)
Recursively searches the binary tree for allocation nodes.

Parameters:
subTreeRoot - the subtree to search.
start - the start of the primary search range.
end - the end of the primary search range.

queryAllocNode

protected void queryAllocNode(jdsl.core.ref.ArraySequence s,
                              jdsl.core.api.Sequence retSeq,
                              java.lang.Object start,
                              java.lang.Object end)
Finds the nodes between start and end in the given sequence and appends them to the return sequence.

Parameters:
s - the sequence to search.
retSeq - the return sequence to which allocation nodes are append.
start - the start bounds of the secondary search range.
end - the end bounds of the secondary search range.

binarySearchAfter

protected int binarySearchAfter(jdsl.core.ref.ArraySequence s,
                                java.lang.Object obj,
                                int start,
                                int end)
Recursively finds the index into a sorted array of the first element greater than or equal to the given element. Returns the sequence size if no element is greater than or equal to the given element.

Parameters:
s - the sorted array to search.
obj - the element to search for.
start - the start index of the search.
end - the end index of the search.
Returns:
the index of the first element greater than or equal to the given element.

binarySearchBefore

protected int binarySearchBefore(jdsl.core.ref.ArraySequence s,
                                 java.lang.Object obj,
                                 int start,
                                 int end)
Recursively finds the index into a sorted array of the last element less than or equal to the given element. Returns -1 if no element is less than or equal to the given element.

Parameters:
s - the sorted array to search.
obj - the element to search for.
start - the start index of the search.
end - the end index of the search.
Returns:
the index of the last element less than or equal to the given element.