Neighbors find_neighbors_bruteforce_impl(const RandomAccessIterator& begin, const RandomAccessIterator& end, Callback callback, IndexType k) { timed_context context("Distance sorting based neighbors search"); typedef std::pair<RandomAccessIterator, ScalarType> DistanceRecord; typedef std::vector<DistanceRecord> Distances; Neighbors neighbors; neighbors.reserve(end-begin); for (RandomAccessIterator iter=begin; iter!=end; ++iter) { Distances distances; for (RandomAccessIterator around_iter=begin; around_iter!=end; ++around_iter) distances.push_back(std::make_pair(around_iter, callback.distance(iter,around_iter))); std::nth_element(distances.begin(),distances.begin()+k+1,distances.end(), distances_comparator<DistanceRecord>()); LocalNeighbors local_neighbors; local_neighbors.reserve(k); for (typename Distances::const_iterator neighbors_iter=distances.begin(); neighbors_iter!=distances.begin()+k+1; ++neighbors_iter) { if (neighbors_iter->first != iter) local_neighbors.push_back(neighbors_iter->first - begin); } neighbors.push_back(local_neighbors); } return neighbors; }
Neighbors find_neighbors_vptree_impl(const RandomAccessIterator& begin, const RandomAccessIterator& end, Callback callback, IndexType k) { timed_context context("VP-Tree based neighbors search"); Neighbors neighbors; neighbors.reserve(end-begin); VantagePointTree<RandomAccessIterator,Callback> tree(begin,end,callback); for (RandomAccessIterator i=begin; i!=end; ++i) { LocalNeighbors local_neighbors = tree.search(i,k+1); std::remove(local_neighbors.begin(),local_neighbors.end(),i-begin); neighbors.push_back(local_neighbors); } return neighbors; }