diff --git a/src/mlpack/methods/neighbor_search/ns_model.hpp b/src/mlpack/methods/neighbor_search/ns_model.hpp index 0203e71ccb..d87549e920 100644 --- a/src/mlpack/methods/neighbor_search/ns_model.hpp +++ b/src/mlpack/methods/neighbor_search/ns_model.hpp @@ -197,23 +197,6 @@ class DeleteVisitor : public boost::static_visitor void operator()(NSType *ns) const; }; -/** - * SerializeVisitor serializes the given NSType instance. - */ -template -class SerializeVisitor : public boost::static_visitor -{ - private: - Archive& ar; - const std::string& name; - - public: - template - void operator()(NSType *ns) const; - - SerializeVisitor(Archive& ar, const std::string& name); -}; - /** * The NSModel class provides an easy way to serialize a model, abstracts away * the different types of trees, and also reflects the NeighborSearch API. diff --git a/src/mlpack/methods/neighbor_search/ns_model_impl.hpp b/src/mlpack/methods/neighbor_search/ns_model_impl.hpp index 1001ff4b20..5ed97721cd 100644 --- a/src/mlpack/methods/neighbor_search/ns_model_impl.hpp +++ b/src/mlpack/methods/neighbor_search/ns_model_impl.hpp @@ -13,6 +13,8 @@ // In case it hasn't been included yet. #include "ns_model.hpp" +#include + namespace mlpack { namespace neighbor { @@ -200,22 +202,6 @@ void DeleteVisitor::operator()(NSType* ns) const delete ns; } -//! Save parameters for serialization. -template -SerializeVisitor::SerializeVisitor(Archive& ar, - const std::string& name) : - ar(ar), - name(name) -{} - -//! Serialize the given NSType instance. -template -template -void SerializeVisitor::operator()(NSType* ns) const -{ - ar & data::CreateNVP(ns, name); -} - /** * Initialize the NSModel with the given type and whether or not a random * basis should be used. @@ -235,6 +221,27 @@ NSModel::~NSModel() boost::apply_visitor(DeleteVisitor(), nSearch); } +/** + * Non-intrusive serialization for Neighbor Search class. We need this + * definition because we are going to use the serialize function for boost + * variant, which will look for a serialize function for its member types. + */ +template class TreeType, + template class TraversalType> +void serialize( + Archive& ar, + NeighborSearch& ns, + const unsigned int version) +{ + ns.Serialize(ar, version); +} + //! Serialize the kNN model. template template @@ -249,10 +256,8 @@ void NSModel::Serialize(Archive& ar, if (Archive::is_loading::value) boost::apply_visitor(DeleteVisitor(), nSearch); - // We'll only need to serialize one of the kNN objects, based on the type. const std::string& name = NSModelName::Name(); - SerializeVisitor s(ar, name); - boost::apply_visitor(s, nSearch); + ar & data::CreateNVP(nSearch, name); } //! Expose the dataset.