1#ifndef KNNCOLLE_FIND_NEAREST_NEIGHBORS_HPP
2#define KNNCOLLE_FIND_NEAREST_NEIGHBORS_HPP
16#ifndef KNNCOLLE_CUSTOM_PARALLEL
34template<
typename Task_,
class Run_>
35void parallelize(
int num_workers, Task_ num_tasks, Run_ run_task_range) {
36#ifndef KNNCOLLE_CUSTOM_PARALLEL
38 subpar::parallelize(num_workers, num_tasks, std::move(run_task_range));
40 KNNCOLLE_CUSTOM_PARALLEL(num_workers, num_tasks, run_task_range);
53template<
typename Index_ =
int,
typename Float_ =
double>
54using NeighborList = std::vector<std::vector<std::pair<Index_, Float_> > >;
68template<
typename Index_>
69int cap_k(
int k, Index_ num_observations) {
70 if constexpr(std::is_signed<Index_>::value) {
71 if (k < num_observations) {
75 if (
static_cast<typename std::make_unsigned<Index_>::type
>(k) < num_observations) {
79 if (num_observations) {
80 return num_observations - 1;
104template<
typename Dim_,
typename Index_,
typename Float_>
110 parallelize(num_threads, nobs, [&](
int, Index_ start, Index_ length) ->
void {
112 std::vector<Index_> indices;
113 std::vector<Float_> distances;
114 for (Index_ i = start, end = start + length; i < end; ++i) {
115 sptr->search(i, k, &indices, &distances);
116 int actual_k = indices.size();
117 output[i].reserve(actual_k);
118 for (
int j = 0; j < actual_k; ++j) {
119 output[i].emplace_back(indices[j], distances[j]);
146template<
typename Dim_,
typename Index_,
typename Float_>
150 std::vector<std::vector<Index_> > output(nobs);
152 parallelize(num_threads, nobs, [&](
int, Index_ start, Index_ length) ->
void {
154 for (Index_ i = start, end = start + length; i < end; ++i) {
155 sptr->search(i, k, &(output[i]), NULL);
Interface for prebuilt nearest-neighbor indices.
Interface for prebuilt nearest-neighbor search indices.
Definition Prebuilt.hpp:28
virtual Index_ num_observations() const =0
virtual std::unique_ptr< Searcher< Index_, Float_ > > initialize() const =0
Collection of KNN algorithms.
Definition Bruteforce.hpp:22
std::vector< std::vector< Index_ > > find_nearest_neighbors_index_only(const Prebuilt< Dim_, Index_, Float_ > &index, int k, int num_threads=1)
Definition find_nearest_neighbors.hpp:147
NeighborList< Index_, Float_ > find_nearest_neighbors(const Prebuilt< Dim_, Index_, Float_ > &index, int k, int num_threads=1)
Definition find_nearest_neighbors.hpp:105
std::vector< std::vector< std::pair< Index_, Float_ > > > NeighborList
Definition find_nearest_neighbors.hpp:54
void parallelize(int num_workers, Task_ num_tasks, Run_ run_task_range)
Definition find_nearest_neighbors.hpp:35
int cap_k(int k, Index_ num_observations)
Definition find_nearest_neighbors.hpp:69