[mlpack-git] master: Refactor Serialize(); don't duplicate the parent. (c60e531)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Wed Oct 14 05:02:50 EDT 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/81e72d4410ae417f7a8536bd3c61865e2f62c934...ce49a4b5f0b7d12d4955c09e45c69891a6f83e8a
>---------------------------------------------------------------
commit c60e53128b8bc54254581a09334694d7e8fd3256
Author: Ryan Curtin <ryan at ratml.org>
Date: Wed Oct 14 04:15:35 2015 -0400
Refactor Serialize(); don't duplicate the parent.
This is a better approach and it should probably be applied to BinarySpaceTree
too.
>---------------------------------------------------------------
c60e53128b8bc54254581a09334694d7e8fd3256
.../core/tree/cover_tree/cover_tree_impl.hpp | 43 ++++++++++------------
1 file changed, 20 insertions(+), 23 deletions(-)
diff --git a/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp b/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp
index a36fb83..0a8bafb 100644
--- a/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp
+++ b/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp
@@ -1234,7 +1234,22 @@ void CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::Serialize(
ar & CreateNVP(base, "base");
ar & CreateNVP(stat, "stat");
ar & CreateNVP(numDescendants, "numDescendants");
- ar & CreateNVP(parent, "parent");
+
+ // Due to quirks of boost::serialization, depending on how the user
+ // serializes the tree, it's possible that the root of the tree will
+ // accidentally be serialized twice. So if we are a first-level child, we
+ // avoid serializing the parent. The true (non-duplicated) parent will fix
+ // the parent link.
+ if (Archive::is_saving::value && parent != NULL && parent->Parent() == NULL)
+ {
+ CoverTree* fakeParent = NULL;
+ ar & CreateNVP(fakeParent, "parent");
+ }
+ else
+ {
+ ar & CreateNVP(parent, "parent");
+ }
+
ar & CreateNVP(parentDistance, "parentDistance");
ar & CreateNVP(furthestDescendantDistance, "furthestDescendantDistance");
ar & CreateNVP(metric, "metric");
@@ -1257,32 +1272,14 @@ void CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::Serialize(
ar & CreateNVP(children[i], oss.str());
}
- // Due to quirks of boost::serialization, if a tree is saved as an object and
- // not a pointer, the first level of the tree will be duplicated on load.
- // Therefore, if we are the root of the tree, then we need to make sure our
- // children's parent links are correct, and delete the duplicated node if
- // necessary.
- if (Archive::is_loading::value)
+ if (Archive::is_loading::value && parent == NULL)
{
// Look through each child individually.
for (size_t i = 0; i < children.size(); ++i)
{
- if (children[i]->Parent() != this)
- {
- // Disallow the duplicate parent from deleting anything. But only
- // delete the parent if this is the first child (we are assuming that
- // each of the other children has the same incorrect parent).
- if (i == 0)
- {
- children[i]->Parent()->localMetric = false;
- children[i]->Parent()->localDataset = false;
- children[i]->Parent()->children.clear();
- delete children[i]->Parent();
- }
-
- // Fix the child's parent link.
- children[i]->Parent() = this;
- }
+ children[i]->localMetric = false;
+ children[i]->localDataset = false;
+ children[i]->Parent() = this;
}
}
}
More information about the mlpack-git
mailing list