knncolle
Collection of KNN methods in C++
Loading...
Searching...
No Matches
knncolle::NeighborQueue< Index_, Distance_ > Class Template Reference

Helper class to track nearest neighbors. More...

#include <NeighborQueue.hpp>

Public Member Functions

 NeighborQueue ()=default
 
void reset (Index_ k)
 
bool is_full () const
 
Distance_ limit () const
 
auto size () const
 
void add (Index_ i, Distance_ d)
 
void report (std::vector< Index_ > *output_indices, std::vector< Distance_ > *output_distances, Index_ self)
 
void report (std::vector< Index_ > *output_indices, std::vector< Distance_ > *output_distances)
 

Detailed Description

template<typename Index_, typename Distance_>
class knncolle::NeighborQueue< Index_, Distance_ >

Helper class to track nearest neighbors.

This is a priority queue that tracks the nearest neighbors of an observation of interest. Specifically, it contains indices and distances of the k nearest neighbors, in decreasing order from the top of the queue. When the queue is at capacity and new elements are added, existing elements will be displaced by incoming elements with shorter distances. (In the presence of ties, neighbors with lower indices will prevail.)

This class is intended to be used in implementations of Searcher::search() to track the k-nearest neighbors. When searching for neighbors of an existing observation in the dataset, it is recommended to search for the k + 1 neighbors. The appropriate report() overload can then be used to remove the observation of interest from its own neighbor list.

Template Parameters
Index_Integer type for the observation indices.
Distance_Numeric type for the distances, usually floating-point.

Constructor & Destructor Documentation

◆ NeighborQueue()

template<typename Index_ , typename Distance_ >
knncolle::NeighborQueue< Index_, Distance_ >::NeighborQueue ( )
default

Default constructor. The maximum number of neighbors to be retained is 1; this can be changed by reset().

Member Function Documentation

◆ add()

template<typename Index_ , typename Distance_ >
void knncolle::NeighborQueue< Index_, Distance_ >::add ( Index_ i,
Distance_ d )
inline

Attempt to add a potential nearest neighbor to the queue. If the incoming neighbor is closer than the furthest existing neighbor, the latter will be removed if is_full() == true.

Parameters
iIndex of the neighbor.
dDistance to the neighbor.

◆ is_full()

template<typename Index_ , typename Distance_ >
bool knncolle::NeighborQueue< Index_, Distance_ >::is_full ( ) const
inline
Returns
Whether the queue is full.

◆ limit()

template<typename Index_ , typename Distance_ >
Distance_ knncolle::NeighborQueue< Index_, Distance_ >::limit ( ) const
inline
Returns
The distance of the k-th furthest neighbor, where k is the number of neighbors in reset(). This should only be called if is_full() returns true.

◆ report() [1/2]

template<typename Index_ , typename Distance_ >
void knncolle::NeighborQueue< Index_, Distance_ >::report ( std::vector< Index_ > * output_indices,
std::vector< Distance_ > * output_distances )
inline

Report the indices and distances of the nearest neighbors in the queue.

Parameters
[out]output_indicesPointer to a vector in which to store the indices of the nearest neighbors, sorted by distance. If NULL, the indices will not be reported.
[out]output_distancesPointer to a vector in which to store the (sorted) distances to the nearest neighbors. If NULL, the distances will not be reported. Otherwise, on output, this will have the same length as *output_indices and contain distances to each of those neighbors.

◆ report() [2/2]

template<typename Index_ , typename Distance_ >
void knncolle::NeighborQueue< Index_, Distance_ >::report ( std::vector< Index_ > * output_indices,
std::vector< Distance_ > * output_distances,
Index_ self )
inline

Report the indices and distances of the nearest neighbors in the queue, excluding the observation of interest self. Specifically, if the observation of interest (self) is detected as its own neighbor, it will be excluded from the output vectors. If self is not found in the set of nearest neighbors (e.g., > k tied points at zero distance), the furthest neighbor will be excluded instead.

This method will report size() - 1 neighbors in the output vectors and thus should only be called if size() > 0.

Parameters
[out]output_indicesPointer to a vector in which to store the indices of the nearest neighbors, sorted by distance. If NULL, the indices will not be reported.
[out]output_distancesPointer to a vector in which to store the (sorted) distances to the nearest neighbors. If NULL, the distances will not be reported. Otherwise, on output, this will have the same length as *output_indices and contain distances to each of those neighbors.
selfIndex of the observation of interest, i.e., for which neighbors are to be identified. If present in the queue, this will be removed from output_indices and output_distances.

◆ reset()

template<typename Index_ , typename Distance_ >
void knncolle::NeighborQueue< Index_, Distance_ >::reset ( Index_ k)
inline

Resets the queue to retain k neighbors. Any existing neighbors in the queue are removed.

Parameters
kMaximum number of neighbors to retain. This should be a positive integer.

◆ size()

template<typename Index_ , typename Distance_ >
auto knncolle::NeighborQueue< Index_, Distance_ >::size ( ) const
inline
Returns
Number of elements in the queue. This will be no greater than k, and will equal k when is_full() is true.

The documentation for this class was generated from the following file: