1#ifndef KNNCOLLE_L2_NORMALIZED_HP
2#define KNNCOLLE_L2_NORMALIZED_HP
27template<
typename Data_,
typename Normalized_>
28void l2norm(
const Data_* ptr, std::size_t ndim, Normalized_* buffer) {
30 for (std::size_t d = 0; d < ndim; ++d) {
31 Normalized_ val = ptr[d];
38 for (std::size_t d = 0; d < ndim; ++d) {
60template<
typename Index_,
typename Data_,
typename Distance_,
typename Normalized_>
68 my_searcher(std::move(searcher)),
69 buffer(num_dimensions)
75 static_assert(std::is_floating_point<Normalized_>::value);
77 std::unique_ptr<Searcher<Index_, Normalized_, Distance_> > my_searcher;
78 std::vector<Normalized_> buffer;
84 void search(Index_ i, Index_ k, std::vector<Index_>* output_indices, std::vector<Distance_>* output_distances) {
85 my_searcher->search(i, k, output_indices, output_distances);
88 void search(
const Data_* ptr, Index_ k, std::vector<Index_>* output_indices, std::vector<Distance_>* output_distances) {
89 auto normalized = buffer.data();
90 internal::l2norm(ptr, buffer.size(), normalized);
91 my_searcher->search(normalized, k, output_indices, output_distances);
96 return my_searcher->can_search_all();
99 Index_
search_all(Index_ i, Distance_ threshold, std::vector<Index_>* output_indices, std::vector<Distance_>* output_distances) {
100 return my_searcher->search_all(i, threshold, output_indices, output_distances);
103 Index_
search_all(
const Data_* ptr, Distance_ threshold, std::vector<Index_>* output_indices, std::vector<Distance_>* output_distances) {
104 auto normalized = buffer.data();
105 internal::l2norm(ptr, buffer.size(), normalized);
106 return my_searcher->search_all(normalized, threshold, output_indices, output_distances);
124template<
typename Index_,
typename Data_,
typename Distance_,
typename Normalized_>
133 std::unique_ptr<Prebuilt<Index_, Normalized_, Distance_> > my_prebuilt;
140 return my_prebuilt->num_observations();
144 return my_prebuilt->num_dimensions();
153 std::unique_ptr<Searcher<Index_, Data_, Distance_> >
initialize()
const {
154 return std::make_unique<L2NormalizedSearcher<Index_, Data_, Distance_, Normalized_> >(my_prebuilt->initialize(), my_prebuilt->num_dimensions());
161template<
typename Index_,
typename Data_,
typename Normalized_,
typename Matrix_>
162class L2NormalizedMatrix;
174template<
typename Index_,
typename Data_,
typename Normalized_>
181 my_extractor(std::move(extractor)), buffer(dim) {}
184 std::unique_ptr<MatrixExtractor<Data_> > my_extractor;
185 std::vector<Normalized_> buffer;
188 const Normalized_*
next() {
189 auto raw = my_extractor->next();
190 auto normalized = buffer.data();
191 internal::l2norm(raw, buffer.size(), normalized);
212template<
typename Index_,
typename Data_,
typename Normalized_,
typename Matrix_ = Matrix<Index_, Data_> >
221 static_assert(std::is_same<decltype(std::declval<Matrix_>().num_observations()), Index_>::value);
222 static_assert(std::is_same<typename std::remove_pointer<decltype(std::declval<Matrix_>().new_extractor()->next())>::type,
const Data_>::value);
224 const Matrix_& my_matrix;
228 return my_matrix.num_dimensions();
232 return my_matrix.num_observations();
235 std::unique_ptr<MatrixExtractor<Normalized_> >
new_extractor()
const {
236 return std::make_unique<L2NormalizedMatrixExtractor<Index_, Data_, Normalized_> >(my_matrix.new_extractor(),
num_dimensions());
258template<
typename Index_,
typename Data_,
typename Distance_,
typename Normalized_,
class Matrix_ = Matrix<Index_, Data_> >
277 typedef typename std::conditional<
278 std::is_base_of<Matrix_, NormalizedMatrix>::value,
290 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:259
L2NormalizedMatrix< Index_, Data_, Normalized_, Matrix_ > NormalizedMatrix
Definition L2Normalized.hpp:264
L2NormalizedBuilder(std::shared_ptr< const Builder< Index_, Normalized_, Distance_, BuilderMatrix > > builder)
Definition L2Normalized.hpp:287
Prebuilt< Index_, Data_, Distance_ > * build_raw(const Matrix_ &data) const
Definition L2Normalized.hpp:296
std::conditional< std::is_base_of< Matrix_, NormalizedMatrix >::value, Matrix_, NormalizedMatrix >::type BuilderMatrix
Definition L2Normalized.hpp:281
Wrapper around a matrix with L2 normalization.
Definition L2Normalized.hpp:213
Wrapper around a prebuilt index with L2 normalization.
Definition L2Normalized.hpp:125
L2NormalizedPrebuilt(std::unique_ptr< Prebuilt< Index_, Normalized_, Distance_ > > prebuilt)
Definition L2Normalized.hpp:130
std::unique_ptr< Searcher< Index_, Data_, Distance_ > > initialize() const
Definition L2Normalized.hpp:153
Wrapper around a search interface with L2 normalization.
Definition L2Normalized.hpp:61
L2NormalizedSearcher(std::unique_ptr< Searcher< Index_, Normalized_, Distance_ > > searcher, std::size_t num_dimensions)
Definition L2Normalized.hpp:67
Interface for matrix data.
Definition Matrix.hpp:56
virtual std::size_t num_dimensions() const=0
virtual std::unique_ptr< MatrixExtractor< Normalized_ > > new_extractor() const=0
virtual Index_ num_observations() const=0
Interface for prebuilt nearest-neighbor search indices.
Definition Prebuilt.hpp:28
virtual Index_ num_observations() const =0
virtual std::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:24