1#ifndef KNNCOLLE_FIND_NEAREST_NEIGHBORS_HPP
2#define KNNCOLLE_FIND_NEAREST_NEIGHBORS_HPP
10#include "sanisizer/sanisizer.hpp"
11#ifndef KNNCOLLE_CUSTOM_PARALLEL
37template<
typename Task_,
class Run_>
38void parallelize(
int num_workers, Task_ num_tasks, Run_ run_task_range) {
39#ifndef KNNCOLLE_CUSTOM_PARALLEL
41 subpar::parallelize(num_workers, num_tasks, std::move(run_task_range));
43 KNNCOLLE_CUSTOM_PARALLEL(num_workers, num_tasks, run_task_range);
56template<
typename Index_,
typename Distance_>
57using NeighborList = std::vector<std::vector<std::pair<Index_, Distance_> > >;
78template<
typename Index_,
typename Data_,
typename Distance_>
82 auto output = sanisizer::create<NeighborList<Index_, Distance_> >(nobs);
84 parallelize(num_threads, nobs, [&](
int, Index_ start, Index_ length) ->
void {
86 std::vector<Index_> indices;
87 std::vector<Distance_> distances;
88 for (Index_ i = start, end = start + length; i < end; ++i) {
89 sptr->search(i, k, &indices, &distances);
90 const auto actual_k = indices.size();
91 output[i].reserve(actual_k);
92 for (I<
decltype(actual_k)> j = 0; j < actual_k; ++j) {
93 output[i].emplace_back(indices[j], distances[j]);
120template<
typename Index_,
typename Data_,
typename Distance_>
124 auto output = sanisizer::create<std::vector<std::vector<Index_> > >(nobs);
126 parallelize(num_threads, nobs, [&](
int, Index_ start, Index_ length) ->
void {
128 for (Index_ i = start, end = start + length; i < end; ++i) {
129 sptr->search(i, k, &(output[i]), NULL);
Interface for prebuilt nearest-neighbor indices.
Cap the number of requested neighbors.
Interface for prebuilt nearest-neighbor search indices.
Definition Prebuilt.hpp:29
auto initialize_known() const
Definition Prebuilt.hpp:98
virtual Index_ num_observations() const =0
Collection of KNN algorithms.
Definition Bruteforce.hpp:31
NeighborList< Index_, Distance_ > find_nearest_neighbors(const Prebuilt< Index_, Data_, Distance_ > &index, int k, int num_threads=1)
Definition find_nearest_neighbors.hpp:79
void parallelize(int num_workers, Task_ num_tasks, Run_ run_task_range)
Definition find_nearest_neighbors.hpp:38
std::vector< std::vector< Index_ > > find_nearest_neighbors_index_only(const Prebuilt< Index_, Data_, Distance_ > &index, int k, int num_threads=1)
Definition find_nearest_neighbors.hpp:121
int cap_k(int k, Index_ num_observations)
Definition cap_k.hpp:27
std::vector< std::vector< std::pair< Index_, Distance_ > > > NeighborList
Definition find_nearest_neighbors.hpp:57