1#ifndef KNNCOLLE_L2_NORMALIZED_HP
2#define KNNCOLLE_L2_NORMALIZED_HP
26template<
typename Data_,
typename Normalized_>
27void l2norm(
const Data_* ptr,
size_t ndim, Normalized_* buffer) {
29 for (
size_t d = 0; d < ndim; ++d) {
30 Normalized_ val = ptr[d];
37 for (
size_t d = 0; d < ndim; ++d) {
59template<
typename Index_,
typename Data_,
typename Distance_,
typename Normalized_>
67 my_searcher(std::move(searcher)),
68 buffer(num_dimensions)
74 static_assert(std::is_floating_point<Normalized_>::value);
76 std::unique_ptr<Searcher<Index_, Normalized_, Distance_> > my_searcher;
77 std::vector<Normalized_> buffer;
83 void search(Index_ i, Index_ k, std::vector<Index_>* output_indices, std::vector<Distance_>* output_distances) {
84 my_searcher->search(i, k, output_indices, output_distances);
87 void search(
const Data_* ptr, Index_ k, std::vector<Index_>* output_indices, std::vector<Distance_>* output_distances) {
88 auto normalized = buffer.data();
89 internal::l2norm(ptr, buffer.size(), normalized);
90 my_searcher->search(normalized, k, output_indices, output_distances);
95 return my_searcher->can_search_all();
98 Index_
search_all(Index_ i, Distance_ threshold, std::vector<Index_>* output_indices, std::vector<Distance_>* output_distances) {
99 return my_searcher->search_all(i, threshold, output_indices, output_distances);
102 Index_
search_all(
const Data_* ptr, Distance_ threshold, std::vector<Index_>* output_indices, std::vector<Distance_>* output_distances) {
103 auto normalized = buffer.data();
104 internal::l2norm(ptr, buffer.size(), normalized);
105 return my_searcher->search_all(normalized, threshold, output_indices, output_distances);
123template<
typename Index_,
typename Data_,
typename Distance_,
typename Normalized_>
132 std::unique_ptr<Prebuilt<Index_, Normalized_, Distance_> > my_prebuilt;
139 return my_prebuilt->num_observations();
143 return my_prebuilt->num_dimensions();
152 std::unique_ptr<Searcher<Index_, Data_, Distance_> >
initialize()
const {
153 return std::make_unique<L2NormalizedSearcher<Index_, Data_, Distance_, Normalized_> >(my_prebuilt->initialize(), my_prebuilt->num_dimensions());
160template<
typename Index_,
typename Data_,
typename Normalized_,
typename Matrix_>
161class L2NormalizedMatrix;
173template<
typename Index_,
typename Data_,
typename Normalized_>
180 my_extractor(std::move(extractor)), buffer(dim) {}
183 std::unique_ptr<MatrixExtractor<Data_> > my_extractor;
184 std::vector<Normalized_> buffer;
187 const Normalized_*
next() {
188 auto raw = my_extractor->next();
189 auto normalized = buffer.data();
190 internal::l2norm(raw, buffer.size(), normalized);
211template<
typename Index_,
typename Data_,
typename Normalized_,
typename Matrix_ = Matrix<Index_, Data_> >
220 static_assert(std::is_same<decltype(std::declval<Matrix_>().num_observations()), Index_>::value);
221 static_assert(std::is_same<typename std::remove_pointer<decltype(std::declval<Matrix_>().new_extractor()->next())>::type,
const Data_>::value);
223 const Matrix_& my_matrix;
227 return my_matrix.num_dimensions();
231 return my_matrix.num_observations();
234 std::unique_ptr<MatrixExtractor<Normalized_> >
new_extractor()
const {
235 return std::make_unique<L2NormalizedMatrixExtractor<Index_, Data_, Normalized_> >(my_matrix.new_extractor(),
num_dimensions());
257template<
typename Index_,
typename Data_,
typename Distance_,
typename Normalized_,
class Matrix_ = Matrix<Index_, Data_> >
276 typedef typename std::conditional<
277 std::is_base_of<Matrix_, NormalizedMatrix>::value,
289 std::shared_ptr<const Builder<Index_, Normalized_, Distance_, BuilderMatrix> > my_builder;
Interface to build nearest-neighbor indices.
Interface for the input matrix.
Interface for prebuilt nearest-neighbor indices.
Interface for searching nearest-neighbor indices.
Interface to build nearest-neighbor search indices.
Definition Builder.hpp:28
Wrapper around a builder with L2 normalization.
Definition L2Normalized.hpp:258
L2NormalizedMatrix< Index_, Data_, Normalized_, Matrix_ > NormalizedMatrix
Definition L2Normalized.hpp:263
L2NormalizedBuilder(std::shared_ptr< const Builder< Index_, Normalized_, Distance_, BuilderMatrix > > builder)
Definition L2Normalized.hpp:286
Prebuilt< Index_, Data_, Distance_ > * build_raw(const Matrix_ &data) const
Definition L2Normalized.hpp:295
std::conditional< std::is_base_of< Matrix_, NormalizedMatrix >::value, Matrix_, NormalizedMatrix >::type BuilderMatrix
Definition L2Normalized.hpp:280
Wrapper around a matrix with L2 normalization.
Definition L2Normalized.hpp:212
Wrapper around a prebuilt index with L2 normalization.
Definition L2Normalized.hpp:124
L2NormalizedPrebuilt(std::unique_ptr< Prebuilt< Index_, Normalized_, Distance_ > > prebuilt)
Definition L2Normalized.hpp:129
std::unique_ptr< Searcher< Index_, Data_, Distance_ > > initialize() const
Definition L2Normalized.hpp:152
Wrapper around a search interface with L2 normalization.
Definition L2Normalized.hpp:60
L2NormalizedSearcher(std::unique_ptr< Searcher< Index_, Normalized_, Distance_ > > searcher, size_t num_dimensions)
Definition L2Normalized.hpp:66
Interface for matrix data.
Definition Matrix.hpp:53
virtual std::unique_ptr< MatrixExtractor< Normalized_ > > new_extractor() const=0
virtual Index_ num_observations() const=0
virtual size_t num_dimensions() const=0
Interface for prebuilt nearest-neighbor search indices.
Definition Prebuilt.hpp:26
virtual Index_ num_observations() const =0
virtual size_t num_dimensions() const =0
Interface for searching nearest-neighbor search indices.
Definition Searcher.hpp:28
virtual bool can_search_all() const
Definition Searcher.hpp:85
virtual Index_ search_all(Index_ i, Distance_ distance, std::vector< Index_ > *output_indices, std::vector< Distance_ > *output_distances)
Definition Searcher.hpp:106
virtual void search(Index_ i, Index_ k, std::vector< Index_ > *output_indices, std::vector< Distance_ > *output_distances)=0
Collection of KNN algorithms.
Definition Bruteforce.hpp:23