[mlpack-git] master: Fix NSModel serialization. Use boost serialize function for variants. (b34dac8)

gitdub at mlpack.org gitdub at mlpack.org
Fri Jun 17 03:23:33 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/a7e8d3bac60d6aa2717f27e0d4f6c53ff20607f5...779556fed748819a18cc898d9a6f69900740ef23

>---------------------------------------------------------------

commit b34dac8e4bceab76ff860af035ef9d0bee1ac1fa
Author: MarcosPividori <marcos.pividori at gmail.com>
Date:   Fri Jun 17 03:03:59 2016 -0300

    Fix NSModel serialization. Use boost serialize function for variants.


>---------------------------------------------------------------

b34dac8e4bceab76ff860af035ef9d0bee1ac1fa
 src/mlpack/methods/neighbor_search/ns_model.hpp    | 17 ---------
 .../methods/neighbor_search/ns_model_impl.hpp      | 43 ++++++++++++----------
 2 files changed, 24 insertions(+), 36 deletions(-)

diff --git a/src/mlpack/methods/neighbor_search/ns_model.hpp b/src/mlpack/methods/neighbor_search/ns_model.hpp
index 0203e71..d87549e 100644
--- a/src/mlpack/methods/neighbor_search/ns_model.hpp
+++ b/src/mlpack/methods/neighbor_search/ns_model.hpp
@@ -198,23 +198,6 @@ class DeleteVisitor : public boost::static_visitor<void>
 };
 
 /**
- * SerializeVisitor serializes the given NSType instance.
- */
-template<typename Archive>
-class SerializeVisitor : public boost::static_visitor<void>
-{
- private:
-  Archive& ar;
-  const std::string& name;
-
- public:
-  template<typename NSType>
-  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 1001ff4..5ed9772 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 <boost/serialization/variant.hpp>
+
 namespace mlpack {
 namespace neighbor {
 
@@ -200,22 +202,6 @@ void DeleteVisitor::operator()(NSType* ns) const
     delete ns;
 }
 
-//! Save parameters for serialization.
-template<typename Archive>
-SerializeVisitor<Archive>::SerializeVisitor(Archive& ar,
-                                            const std::string& name) :
-    ar(ar),
-    name(name)
-{}
-
-//! Serialize the given NSType instance.
-template<typename Archive>
-template<typename NSType>
-void SerializeVisitor<Archive>::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<SortPolicy>::~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<typename Archive,
+         typename SortPolicy,
+         typename MetrType,
+         typename MatType,
+         template<typename TreeMetricType,
+                  typename TreeStatType,
+                  typename TreeMatType> class TreeType,
+         template<typename RuleType> class TraversalType>
+void serialize(
+    Archive& ar,
+    NeighborSearch<SortPolicy, MetrType, MatType, TreeType, TraversalType>& ns,
+    const unsigned int version)
+{
+  ns.Serialize(ar, version);
+}
+
 //! Serialize the kNN model.
 template<typename SortPolicy>
 template<typename Archive>
@@ -249,10 +256,8 @@ void NSModel<SortPolicy>::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<SortPolicy>::Name();
-  SerializeVisitor<Archive> s(ar, name);
-  boost::apply_visitor(s, nSearch);
+  ar & data::CreateNVP(nSearch, name);
 }
 
 //! Expose the dataset.




More information about the mlpack-git mailing list