31 #ifndef FLANN_RESULTSET_H
32 #define FLANN_RESULTSET_H
49 template <
typename T,
typename DistanceType>
65 template <
typename DistanceType>
81 template <
typename DistanceType>
87 virtual bool full()
const = 0;
100 template <
typename DistanceType>
124 dist_index_[capacity_-1].dist_ = worst_distance_;
143 return count_==capacity_;
153 if (
dist>=worst_distance_)
return;
155 if (count_ < capacity_) ++count_;
157 for (i=count_-1; i>0; --i) {
158 #ifdef FLANN_FIRST_MATCH
159 if ( (dist_index_[i-1].dist_>
dist) || ((
dist==dist_index_[i-1].dist_)&&(dist_index_[i-1].index_>index)) )
161 if (dist_index_[i-1].dist_>
dist)
164 dist_index_[i] = dist_index_[i-1];
168 dist_index_[i].dist_ =
dist;
169 dist_index_[i].index_ = index;
170 worst_distance_ = dist_index_[capacity_-1].dist_;
180 void copy(
size_t* indices, DistanceType* dists,
size_t num_elements,
bool sorted =
true)
182 size_t n =
std::min(count_, num_elements);
183 for (
size_t i=0; i<n; ++i) {
184 *indices++ = dist_index_[i].index_;
185 *dists++ = dist_index_[i].dist_;
191 return worst_distance_;
197 DistanceType worst_distance_;
198 std::vector<DistIndex> dist_index_;
204 template <
typename DistanceType>
228 dist_index_[capacity_-1].dist_ = worst_distance_;
239 return count_ == capacity_;
245 if (
dist >= worst_distance_)
return;
247 for (i = count_; i > 0; --i) {
248 #ifdef FLANN_FIRST_MATCH
249 if ( (dist_index_[i-1].dist_<=
dist) && ((
dist!=dist_index_[i-1].dist_)||(dist_index_[i-1].index_<=index)) )
251 if (dist_index_[i-1].dist_<=
dist)
255 for (
size_t j = i - 1; dist_index_[j].dist_ ==
dist && j--;) {
256 if (dist_index_[j].index_ == index) {
263 if (count_ < capacity_) ++count_;
264 for (
size_t j = count_-1; j > i; --j) {
265 dist_index_[j] = dist_index_[j-1];
267 dist_index_[i].dist_ =
dist;
268 dist_index_[i].index_ = index;
269 worst_distance_ = dist_index_[capacity_-1].dist_;
279 void copy(
size_t* indices, DistanceType* dists,
size_t num_elements,
bool sorted =
true)
281 size_t n =
std::min(count_, num_elements);
282 for (
size_t i=0; i<n; ++i) {
283 *indices++ = dist_index_[i].index_;
284 *dists++ = dist_index_[i].dist_;
290 return worst_distance_;
296 DistanceType worst_distance_;
297 std::vector<DistIndex> dist_index_;
303 template <
typename DistanceType>
313 dist_index_.reserve(capacity_);
337 return dist_index_.size();
357 if (
dist>=worst_dist_)
return;
359 if (dist_index_.size()==capacity_) {
361 std::pop_heap(dist_index_.begin(), dist_index_.end());
362 dist_index_.pop_back();
368 std::push_heap(dist_index_.begin(), dist_index_.end());
371 if (dist_index_.size()==capacity_) {
377 worst_dist_ = dist_index_[0].dist_;
388 void copy(
size_t* indices, DistanceType* dists,
size_t num_elements,
bool sorted =
true)
393 std::sort(dist_index_.begin(), dist_index_.end());
396 if (num_elements<
size()) {
397 std::nth_element(dist_index_.begin(), dist_index_.begin()+num_elements, dist_index_.end());
401 size_t n =
std::min(dist_index_.size(), num_elements);
402 for (
size_t i=0; i<n; ++i) {
403 *indices++ = dist_index_[i].index_;
404 *dists++ = dist_index_[i].dist_;
415 DistanceType worst_dist_;
416 std::vector<DistIndex> dist_index_;
425 template <
typename DistanceType>
435 dist_index_.reserve(1024);
457 return dist_index_.size();
490 void copy(
size_t* indices, DistanceType* dists,
size_t num_elements,
bool sorted =
true)
495 std::sort(dist_index_.begin(), dist_index_.end());
498 if (num_elements<
size()) {
499 std::nth_element(dist_index_.begin(), dist_index_.begin()+num_elements, dist_index_.end());
503 size_t n =
std::min(dist_index_.size(), num_elements);
504 for (
size_t i=0; i<n; ++i) {
505 *indices++ = dist_index_[i].index_;
506 *dists++ = dist_index_[i].dist_;
516 DistanceType radius_;
517 std::vector<DistIndex> dist_index_;
526 template <
typename DistanceType>
533 radius_(radius_), capacity_(capacity_)
536 dist_index_.reserve(capacity_);
550 worst_dist_ = radius_;
560 return dist_index_.size();
580 if (
dist>=worst_dist_)
return;
582 if (dist_index_.size()==capacity_) {
584 std::pop_heap(dist_index_.begin(), dist_index_.end());
585 dist_index_.pop_back();
591 std::push_heap(dist_index_.begin(), dist_index_.end());
594 if (dist_index_.size()==capacity_) {
601 worst_dist_ = dist_index_[0].dist_;
612 void copy(
size_t* indices, DistanceType* dists,
size_t num_elements,
bool sorted =
true)
617 std::sort(dist_index_.begin(), dist_index_.end());
620 if (num_elements<
size()) {
621 std::nth_element(dist_index_.begin(), dist_index_.begin()+num_elements, dist_index_.end());
625 size_t n =
std::min(dist_index_.size(), num_elements);
626 for (
size_t i=0; i<n; ++i) {
627 *indices++ = dist_index_[i].index_;
628 *dists++ = dist_index_[i].dist_;
639 DistanceType radius_;
641 DistanceType worst_dist_;
642 std::vector<DistIndex> dist_index_;
651 template <
typename DistanceType>
703 template<
typename DistanceType>
740 void copy(
size_t* indices, DistanceType*
dist,
int n_neighbors,
bool sorted =
true)
744 typedef typename std::set<DistIndex>::const_iterator Iterator;
745 for (Iterator dist_index =
dist_indices_.begin(), dist_index_end =
746 dist_indices_.end(); (dist_index != dist_index_end) && (i < n_neighbors); ++dist_index, ++indices, ++
dist, ++i) {
747 *indices = dist_index->index_;
748 *
dist = dist_index->dist_;
784 template<
typename DistanceType>
843 template<
typename DistanceType>
895 DistanceType radius_;
902 template<
typename DistanceType>
911 this->radius_ = radius;
929 DistanceType radius_;
CountRadiusResultSet(DistanceType radius_)
DistanceType worstDist() const
void addPoint(DistanceType dist, size_t index)
DistanceIndex< DistanceType > DistIndex
void copy(size_t *indices, DistanceType *dists, size_t num_elements, bool sorted=true)
DistanceType worstDist() const
void addPoint(DistanceType dist, size_t index)
KNNRadiusResultSet(DistanceType radius_, size_t capacity_)
KNNRadiusUniqueResultSet(DistanceType radius, size_t capacity)
void copy(size_t *indices, DistanceType *dists, size_t num_elements, bool sorted=true)
DistanceType worstDist() const
KNNResultSet2(size_t capacity_)
DistanceIndex< DistanceType > DistIndex
void addPoint(DistanceType dist, size_t index)
DistanceType worstDist() const
KNNResultSet(int capacity)
void addPoint(DistanceType dist, size_t index)
DistanceIndex< DistanceType > DistIndex
void copy(size_t *indices, DistanceType *dists, size_t num_elements, bool sorted=true)
DistanceIndex< DistanceType > DistIndex
KNNSimpleResultSet(size_t capacity_)
void copy(size_t *indices, DistanceType *dists, size_t num_elements, bool sorted=true)
void addPoint(DistanceType dist, size_t index)
DistanceType worstDist() const
UniqueResultSet< DistanceType >::DistIndex DistIndex
KNNUniqueResultSet(unsigned int capacity)
void addPoint(DistanceType dist, size_t index)
DistanceIndex< DistanceType > DistIndex
RadiusResultSet(DistanceType radius_)
DistanceType worstDist() const
void addPoint(DistanceType dist, size_t index)
void copy(size_t *indices, DistanceType *dists, size_t num_elements, bool sorted=true)
DistanceType worstDist() const
RadiusUniqueResultSet(DistanceType radius)
void addPoint(DistanceType dist, size_t index)
virtual void addPoint(DistanceType dist, size_t index)=0
virtual bool full() const =0
virtual DistanceType worstDist() const =0
std::set< DistIndex > dist_indices_
void copy(size_t *indices, DistanceType *dist, int n_neighbors, bool sorted=true)
DistanceType worst_distance_
DistanceType worstDist() const
static double dist(double x1, double y1, double x2, double y2)
__host__ __device__ void make_heap(RandomAccessIterator begin, size_t length, GreaterThan c=GreaterThan())
bool operator<(const BranchStruct< T, DistanceType > &rhs) const
BranchStruct(const T &aNode, DistanceType dist)
bool operator<(const DistanceIndex &dist_index) const
DistanceIndex(DistanceType dist, size_t index)
bool operator<(const DistIndex dist_index) const
DistIndex(DistanceType dist, unsigned int index)