1010#include < limits>
1111#include < cmath>
1212#include < cstddef>
13+ #include < type_traits>
1314
1415/* *
15- * @file Kmknn.hpp
16- *
16+ * @file knncolle_kmknn.hpp
1717 * @brief Implements the k-means with k-nearest neighbors (KMKNN) algorithm.
1818 */
1919
@@ -201,35 +201,21 @@ struct KmknnOptions {
201201 std::shared_ptr<Refine<Index_, Data_, Distance_, KmeansMatrix_> > refine_algorithm;
202202};
203203
204+ /* *
205+ * @cond
206+ */
204207template <typename Index_, typename Data_, typename Distance_, class DistanceMetric_ >
205208class KmknnPrebuilt ;
206209
207- /* *
208- * @brief KMKNN searcher.
209- *
210- * Instances of this class are usually constructed using `KmknnPrebuilt::initialize()`.
211- *
212- * @tparam Index_ Integer type for the observation indices.
213- * @tparam Data_ Numeric type for the input and query data.
214- * @tparam Distance_ Floating-point type for the distances.
215- * @tparam DistanceMetric_ Class implementing the distance metric calculation.
216- * This should satisfy the `knncolle::DistanceMetric` interface.
217- */
218210template <typename Index_, typename Data_, typename Distance_, class DistanceMetric_ = DistanceMetric<Data_, Distance_> >
219211class KmknnSearcher final : public knncolle::Searcher<Index_, Data_, Distance_> {
220212public:
221- /* *
222- * @cond
223- */
224213 KmknnSearcher (const KmknnPrebuilt<Index_, Data_, Distance_, DistanceMetric_>& parent) : my_parent(parent) {
225214 my_center_order.reserve (my_parent.my_sizes .size ());
226215 if constexpr (needs_conversion) {
227216 my_conversion_buffer.resize (my_parent.my_dim );
228217 }
229218 }
230- /* *
231- * @endcond
232- */
233219
234220private:
235221 const KmknnPrebuilt<Index_, Data_, Distance_, DistanceMetric_>& my_parent;
@@ -315,17 +301,6 @@ class KmknnSearcher final : public knncolle::Searcher<Index_, Data_, Distance_>
315301 }
316302};
317303
318- /* *
319- * @brief Index for a KMKNN search.
320- *
321- * Instances of this class are usually constructed using `KmknnBuilder::build_raw()`.
322- *
323- * @tparam Index_ Integer type for the indices.
324- * @tparam Data_ Numeric type for the input and query data.
325- * @tparam Distance_ Floating-point type for the distances.
326- * @tparam DistanceMetric_ Class implementing the distance metric calculation.
327- * This should satisfy the `knncolle::DistanceMetric` interface.
328- */
329304template <typename Index_, typename Data_, typename Distance_, class DistanceMetric_ = DistanceMetric<Data_, Distance_> >
330305class KmknnPrebuilt final : public knncolle::Prebuilt<Index_, Data_, Distance_> {
331306private:
@@ -354,9 +329,6 @@ class KmknnPrebuilt final : public knncolle::Prebuilt<Index_, Data_, Distance_>
354329 std::vector<Distance_> my_dist_to_centroid;
355330
356331public:
357- /* *
358- * @cond
359- */
360332 template <class KmeansMatrix_ = kmeans::SimpleMatrix<Index_, Data_> >
361333 KmknnPrebuilt (
362334 std::size_t num_dim,
@@ -490,9 +462,6 @@ class KmknnPrebuilt final : public knncolle::Prebuilt<Index_, Data_, Distance_>
490462
491463 return ;
492464 }
493- /* *
494- * @endcond
495- */
496465
497466private:
498467 void search_nn (const Common<Data_, Distance_>* target, knncolle::NeighborQueue<Index_, Distance_>& nearest, std::vector<std::pair<Distance_, Index_> >& center_order) const {
@@ -640,13 +609,17 @@ class KmknnPrebuilt final : public knncolle::Prebuilt<Index_, Data_, Distance_>
640609 friend class KmknnSearcher <Index_, Data_, Distance_, DistanceMetric_>;
641610
642611public:
643- /* *
644- * Creates a `KmknnSearcher` instance.
645- */
646612 std::unique_ptr<knncolle::Searcher<Index_, Data_, Distance_> > initialize () const {
613+ return initialize_known ();
614+ }
615+
616+ auto initialize_known () const {
647617 return std::make_unique<KmknnSearcher<Index_, Data_, Distance_, DistanceMetric_> >(*this );
648618 }
649619};
620+ /* *
621+ * @endcond
622+ */
650623
651624/* *
652625 * @brief Perform a nearest neighbor search based on k-means clustering.
@@ -722,11 +695,16 @@ class KmknnBuilder final : public knncolle::Builder<Index_, Data_, Distance_, Ma
722695 return my_options;
723696 }
724697
698+ public:
699+ knncolle::Prebuilt<Index_, Data_, Distance_>* build_raw (const Matrix_& data) const {
700+ return build_known_raw (data);
701+ }
702+
725703public:
726704 /* *
727- * Creates a `KmknnPrebuilt` instance .
705+ * Override to assist devirtualization .
728706 */
729- knncolle::Prebuilt<Index_, Data_, Distance_>* build_raw (const Matrix_& data) const {
707+ auto build_known_raw (const Matrix_& data) const {
730708 std::size_t ndim = data.num_dimensions ();
731709 auto nobs = data.num_observations ();
732710
@@ -739,6 +717,20 @@ class KmknnBuilder final : public knncolle::Builder<Index_, Data_, Distance_, Ma
739717
740718 return new KmknnPrebuilt<Index_, Data_, Distance_, DistanceMetric_>(ndim, nobs, std::move (store), my_metric, my_options);
741719 }
720+
721+ /* *
722+ * Override to assist devirtualization.
723+ */
724+ auto build_known_unique (const Matrix_& data) const {
725+ return std::unique_ptr<std::remove_reference_t <decltype (*build_known_raw (data))> >(build_known_raw (data));
726+ }
727+
728+ /* *
729+ * Override to assist devirtualization.
730+ */
731+ auto build_known_shared (const Matrix_& data) const {
732+ return std::shared_ptr<std::remove_reference_t <decltype (*build_known_raw (data))> >(build_known_raw (data));
733+ }
742734};
743735
744736}
0 commit comments