1#ifndef KNNCOLLE_FIND_NEAREST_NEIGHBORS_HPP
2#define KNNCOLLE_FIND_NEAREST_NEIGHBORS_HPP
10#include "sanisizer/sanisizer.hpp"
11#ifndef KNNCOLLE_CUSTOM_PARALLEL
36template<
typename Task_,
class Run_>
37void parallelize(
int num_workers, Task_ num_tasks, Run_ run_task_range) {
38#ifndef KNNCOLLE_CUSTOM_PARALLEL
40 subpar::parallelize(num_workers, num_tasks, std::move(run_task_range));
42 KNNCOLLE_CUSTOM_PARALLEL(num_workers, num_tasks, run_task_range);
55template<
typename Index_,
typename Distance_>
56using NeighborList = std::vector<std::vector<std::pair<Index_, Distance_> > >;
70template<
typename Index_>
71int cap_k(
int k, Index_ num_observations) {
72 if (sanisizer::is_less_than(sanisizer::attest_gez(k), sanisizer::attest_gez(num_observations))) {
74 }
else if (num_observations) {
75 return num_observations - 1;
91template<
typename Index_>
93 return sanisizer::min(sanisizer::attest_gez(k), sanisizer::attest_gez(num_observations));
115template<
typename Index_,
typename Data_,
typename Distance_>
119 auto output = sanisizer::create<NeighborList<Index_, Distance_> >(sanisizer::attest_gez(nobs));
121 parallelize(num_threads, nobs, [&](
int, Index_ start, Index_ length) ->
void {
123 std::vector<Index_> indices;
124 std::vector<Distance_> distances;
125 for (Index_ i = start, end = start + length; i < end; ++i) {
126 sptr->search(i, k, &indices, &distances);
127 const auto actual_k = indices.size();
128 output[i].reserve(actual_k);
129 for (I<
decltype(actual_k)> j = 0; j < actual_k; ++j) {
130 output[i].emplace_back(indices[j], distances[j]);
157template<
typename Index_,
typename Data_,
typename Distance_>
161 auto output = sanisizer::create<std::vector<std::vector<Index_> > >(sanisizer::attest_gez(nobs));
163 parallelize(num_threads, nobs, [&](
int, Index_ start, Index_ length) ->
void {
165 for (Index_ i = start, end = start + length; i < end; ++i) {
166 sptr->search(i, k, &(output[i]), NULL);
Interface for prebuilt nearest-neighbor indices.
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:29
NeighborList< Index_, Distance_ > find_nearest_neighbors(const Prebuilt< Index_, Data_, Distance_ > &index, int k, int num_threads=1)
Definition find_nearest_neighbors.hpp:116
void parallelize(int num_workers, Task_ num_tasks, Run_ run_task_range)
Definition find_nearest_neighbors.hpp:37
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:158
int cap_k_query(int k, Index_ num_observations)
Definition find_nearest_neighbors.hpp:92
int cap_k(int k, Index_ num_observations)
Definition find_nearest_neighbors.hpp:71
std::vector< std::vector< std::pair< Index_, Distance_ > > > NeighborList
Definition find_nearest_neighbors.hpp:56