Class QuadTreeRKD0<T>

  • Type Parameters:
    T - Value type.
    All Implemented Interfaces:
    BoxMap<T>, BoxMultimap<T>, Index

    public class QuadTreeRKD0<T>
    extends Object
    implements BoxMap<T>, BoxMultimap<T>
    A simple MX-quadtree implementation with configurable maximum depth, maximum nodes size, and (if desired) automatic guessing of root rectangle.
    Author:
    ztilmann
    • Method Detail

      • create

        public static <T> QuadTreeRKD0<T> create​(int dims)
      • create

        public static <T> QuadTreeRKD0<T> create​(int dims,
                                                 int maxNodeSize,
                                                 double[] min,
                                                 double[] max)
      • create

        public static <T> QuadTreeRKD0<T> create​(int dims,
                                                 int maxNodeSize,
                                                 double[] center,
                                                 double radius)
      • insert

        public void insert​(double[] keyL,
                           double[] keyU,
                           T value)
        Insert a key-value pair.
        Specified by:
        insert in interface BoxMap<T>
        Specified by:
        insert in interface BoxMultimap<T>
        Parameters:
        keyL - the lower part of the key
        keyU - the upper part of the key
        value - the value
      • containsExact

        public boolean containsExact​(double[] keyL,
                                     double[] keyU)
        Check whether a given key exists.
        Parameters:
        keyL - the lower key to check
        keyU - the upper key to check
        Returns:
        true iff the key exists
      • queryExact

        public T queryExact​(double[] min,
                            double[] max)
        Get the value associates with the key.
        Specified by:
        queryExact in interface BoxMap<T>
        Parameters:
        min - the lower key to look up
        max - the upper key to look up
        Returns:
        the value for the key or 'null' if the key was not found
      • contains

        public boolean contains​(double[] min,
                                double[] max)
        Description copied from interface: BoxMap
        Lookup an entry, using exact match.
        Specified by:
        contains in interface BoxMap<T>
        Parameters:
        min - minimum corner
        max - maximum corner
        Returns:
        `true` if an entry was found, otherwise `false`.
      • contains

        public boolean contains​(double[] lower,
                                double[] upper,
                                T value)
        Description copied from interface: BoxMultimap
        Lookup an entry, using exact match.
        Specified by:
        contains in interface BoxMultimap<T>
        Parameters:
        lower - minimum corner
        upper - maximum corner
        value - the value
        Returns:
        `true` if an entry was found, otherwise `false`.
      • queryExactBox

        public Index.BoxIterator<T> queryExactBox​(double[] lower,
                                                  double[] upper)
        Description copied from interface: BoxMultimap
        Lookup an entry, using exact match.
        Specified by:
        queryExactBox in interface BoxMultimap<T>
        Parameters:
        lower - minimum corner
        upper - maximum corner
        Returns:
        an iterator over all entries with the exact given box shape
      • remove

        public T remove​(double[] keyL,
                        double[] keyU)
        Remove a key.
        Specified by:
        remove in interface BoxMap<T>
        Parameters:
        keyL - key to remove
        keyU - key to remove
        Returns:
        the value associated with the key or 'null' if the key was not found
      • remove

        public boolean remove​(double[] lower,
                              double[] upper,
                              T value)
        Description copied from interface: BoxMultimap
        Remove *one*n entry with the given value.
        Specified by:
        remove in interface BoxMultimap<T>
        Parameters:
        lower - minimum corner
        upper - maximum corner
        value - value
        Returns:
        the value of the entry or null if the entry was not found
      • removeIf

        public boolean removeIf​(double[] lower,
                                double[] upper,
                                Predicate<Index.BoxEntry<T>> condition)
        Description copied from interface: BoxMultimap
        Remove *one* entry with the given condition.
        Specified by:
        removeIf in interface BoxMultimap<T>
        Parameters:
        lower - minimum corner
        upper - maximum corner
        condition - the condition required for removing an entry
        Returns:
        the value of the entry or null if the entry was not found
      • update

        public boolean update​(double[] oldKeyL,
                              double[] oldKeyU,
                              double[] newKeyL,
                              double[] newKeyU,
                              T value)
        Description copied from interface: BoxMultimap
        Update the position of an entry.
        Specified by:
        update in interface BoxMultimap<T>
        Parameters:
        oldKeyL - old min
        oldKeyU - old max
        newKeyL - new min
        newKeyU - new max
        value - only entries with this value are updated
        Returns:
        the value, or null if the entries was not found
      • update

        public T update​(double[] oldKeyL,
                        double[] oldKeyU,
                        double[] newKeyL,
                        double[] newKeyU)
        Reinsert the key.
        Specified by:
        update in interface BoxMap<T>
        Parameters:
        oldKeyL - old key
        oldKeyU - old key
        newKeyL - new key
        newKeyU - new key
        Returns:
        the value associated with the key or 'null' if the key was not found.
      • size

        public int size()
        Get the number of key-value pairs in the tree.
        Specified by:
        size in interface Index
        Returns:
        the size
      • clear

        public void clear()
        Removes all elements from the tree.
        Specified by:
        clear in interface Index
      • queryIntersect

        public QuadTreeRKD0.QRIterator<T> queryIntersect​(double[] min,
                                                         double[] max)
        Query the tree, returning all points in the axis-aligned rectangle between 'min' and 'max'.
        Specified by:
        queryIntersect in interface BoxMap<T>
        Specified by:
        queryIntersect in interface BoxMultimap<T>
        Parameters:
        min - lower left corner of query
        max - upper right corner of query
        Returns:
        all entries in the rectangle
      • query1nn

        public Index.BoxEntryKnn<T> query1nn​(double[] center)
        Description copied from interface: BoxMap
        Finds the nearest neighbor. This uses Euclidean 'edge distance'. Other distance types can only be specified directly on the index implementations.
        Specified by:
        query1nn in interface BoxMap<T>
        Specified by:
        query1nn in interface BoxMultimap<T>
        Parameters:
        center - center point
        Returns:
        the nearest neighbor
      • toStringTree

        public String toStringTree()
        Returns a printable list of the tree.
        Specified by:
        toStringTree in interface Index
        Returns:
        the tree as String
      • getStats

        public QuadTreeKD0.QStats getStats()
        Specified by:
        getStats in interface Index
        Returns:
        Collect and return some index statistics. Note that indexes are not required to fill all fields. Also, individual indexes may use subclasses with additional fields.
      • getDims

        public int getDims()
        Specified by:
        getDims in interface Index
        Returns:
        the number of dimensions
      • getNodeCount

        public int getNodeCount()
        Specified by:
        getNodeCount in interface Index
      • queryKnn

        public QRIteratorKnn<T> queryKnn​(double[] center,
                                         int k)
        Description copied from interface: BoxMap
        Finds the nearest neighbor. This uses Euclidean 'edge distance', i.e. the distance to the edge of a box. Distance is 0 if the box overlaps with the search point. Other distance types can only be specified directly on the index implementations.
        Specified by:
        queryKnn in interface BoxMap<T>
        Specified by:
        queryKnn in interface BoxMultimap<T>
        Parameters:
        center - center point
        k - number of neighbors
        Returns:
        list of nearest neighbors
      • queryKnn

        public Index.BoxIteratorKnn<T> queryKnn​(double[] center,
                                                int k,
                                                BoxDistance distFn)
        Description copied from interface: BoxMultimap
        Finds the nearest neighbor. This uses a custom distance function for distances to boxes.
        Specified by:
        queryKnn in interface BoxMultimap<T>
        Parameters:
        center - center point
        k - number of neighbors
        distFn - distance function
        Returns:
        list of nearest neighbors
      • getDepth

        public int getDepth()
        Specified by:
        getDepth in interface Index