[mlpack-git] master: Add move constructors. (55ea6aa)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Mon Oct 19 16:04:38 EDT 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/09cd0d67f2fdae252a8ab85324e71dbb4dfe0010...fecf1194c123ced12d56e7daad761c7b9aaac262

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

commit 55ea6aafd14053f793999e70d99d7402f2668a5c
Author: Ryan Curtin <ryan at ratml.org>
Date:   Sun Oct 18 10:19:55 2015 -0400

    Add move constructors.


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

55ea6aafd14053f793999e70d99d7402f2668a5c
 src/mlpack/core/tree/ballbound.hpp                 |  5 +++-
 src/mlpack/core/tree/ballbound_impl.hpp            | 17 ++++++++++-
 .../tree/binary_space_tree/binary_space_tree.hpp   |  6 ++++
 .../binary_space_tree/binary_space_tree_impl.hpp   | 35 ++++++++++++++++++++++
 src/mlpack/core/tree/hrectbound.hpp                |  3 ++
 src/mlpack/core/tree/hrectbound_impl.hpp           | 19 ++++++++++--
 6 files changed, 81 insertions(+), 4 deletions(-)

diff --git a/src/mlpack/core/tree/ballbound.hpp b/src/mlpack/core/tree/ballbound.hpp
index c2c6457..b3de613 100644
--- a/src/mlpack/core/tree/ballbound.hpp
+++ b/src/mlpack/core/tree/ballbound.hpp
@@ -70,9 +70,12 @@ class BallBound
   //! Copy constructor. To prevent memory leaks.
   BallBound(const BallBound& other);
 
-  //! For the same reason as the Copy Constructor. To prevent memory leaks.
+  //! For the same reason as the copy constructor: to prevent memory leaks.
   BallBound& operator=(const BallBound& other);
 
+  //! Move constructor: take possession of another bound.
+  BallBound(BallBound&& other);
+
   //! Destructor to release allocated memory.
   ~BallBound();
 
diff --git a/src/mlpack/core/tree/ballbound_impl.hpp b/src/mlpack/core/tree/ballbound_impl.hpp
index 292cb62..af3b768 100644
--- a/src/mlpack/core/tree/ballbound_impl.hpp
+++ b/src/mlpack/core/tree/ballbound_impl.hpp
@@ -62,7 +62,7 @@ BallBound<VecType, TMetricType>::BallBound(const BallBound& other) :
     ownsMetric(false)
 { /* Nothing to do. */ }
 
-//! For the same reason as the Copy Constructor. To prevent memory leaks.
+//! For the same reason as the copy constructor: to prevent memory leaks.
 template<typename VecType, typename TMetricType>
 BallBound<VecType, TMetricType>& BallBound<VecType, TMetricType>::operator=(
     const BallBound& other)
@@ -73,6 +73,21 @@ BallBound<VecType, TMetricType>& BallBound<VecType, TMetricType>::operator=(
   ownsMetric = false;
 }
 
+//! Move constructor.
+template<typename VecType, typename TMetricType>
+BallBound<VecType, TMetricType>::BallBound(BallBound&& other) :
+    radius(other.radius),
+    center(other.center),
+    metric(other.metric),
+    ownsMetric(other.ownsMetric)
+{
+  // Fix the other bound.
+  other.radius = 0.0;
+  other.center = VecType();
+  other.metric = NULL;
+  other.ownsMetric = false;
+}
+
 //! Destructor to release allocated memory.
 template<typename VecType, typename TMetricType>
 BallBound<VecType, TMetricType>::~BallBound()
diff --git a/src/mlpack/core/tree/binary_space_tree/binary_space_tree.hpp b/src/mlpack/core/tree/binary_space_tree/binary_space_tree.hpp
index b4f0320..6d6f212 100644
--- a/src/mlpack/core/tree/binary_space_tree/binary_space_tree.hpp
+++ b/src/mlpack/core/tree/binary_space_tree/binary_space_tree.hpp
@@ -265,6 +265,12 @@ class BinarySpaceTree
   BinarySpaceTree(const BinarySpaceTree& other);
 
   /**
+   * Move constructor for a BinarySpaceTree; possess all the members of the
+   * given tree.
+   */
+  BinarySpaceTree(BinarySpaceTree&& other);
+
+  /**
    * Initialize the tree from a boost::serialization archive.
    *
    * @param ar Archive to load tree from.  Must be an iarchive, not an oarchive.
diff --git a/src/mlpack/core/tree/binary_space_tree/binary_space_tree_impl.hpp b/src/mlpack/core/tree/binary_space_tree/binary_space_tree_impl.hpp
index 8e0571c..9420110 100644
--- a/src/mlpack/core/tree/binary_space_tree/binary_space_tree_impl.hpp
+++ b/src/mlpack/core/tree/binary_space_tree/binary_space_tree_impl.hpp
@@ -376,6 +376,41 @@ BinarySpaceTree(
 }
 
 /**
+ * Move constructor.
+ */
+template<typename MetricType,
+         typename StatisticType,
+         typename MatType,
+         template<typename BoundMetricType> class BoundType,
+         template<typename SplitBoundType, typename SplitMatType>
+             class SplitType>
+BinarySpaceTree<MetricType, StatisticType, MatType, BoundType, SplitType>::
+BinarySpaceTree(BinarySpaceTree&& other) :
+    left(other.left),
+    right(other.right),
+    parent(other.parent),
+    begin(other.begin),
+    count(other.count),
+    bound(std::move(other.bound)),
+    stat(std::move(other.stat)),
+    parentDistance(other.parentDistance),
+    furthestDescendantDistance(other.furthestDescendantDistance),
+    minimumBoundDistance(other.minimumBoundDistance),
+    dataset(other.dataset)
+{
+  // Now we are a clone of the other tree.  But we must also clear the other
+  // tree's contents, so it doesn't delete anything when it is destructed.
+  other.left = NULL;
+  other.right = NULL;
+  other.begin = 0;
+  other.count = 0;
+  other.parentDistance = 0.0;
+  other.furthestDescendantDistance = 0.0;
+  other.minimumBoundDistance = 0.0;
+  other.dataset = NULL;
+}
+
+/**
  * Initialize the tree from an archive.
  */
 template<typename MetricType,
diff --git a/src/mlpack/core/tree/hrectbound.hpp b/src/mlpack/core/tree/hrectbound.hpp
index 151402e..330b0e4 100644
--- a/src/mlpack/core/tree/hrectbound.hpp
+++ b/src/mlpack/core/tree/hrectbound.hpp
@@ -69,6 +69,9 @@ class HRectBound
   //! Same as copy constructor; necessary to prevent memory leaks.
   HRectBound& operator=(const HRectBound& other);
 
+  //! Move constructor: take possession of another bound's information.
+  HRectBound(HRectBound&& other);
+
   //! Destructor: clean up memory.
   ~HRectBound();
 
diff --git a/src/mlpack/core/tree/hrectbound_impl.hpp b/src/mlpack/core/tree/hrectbound_impl.hpp
index 0b9f76c..9afc7fe 100644
--- a/src/mlpack/core/tree/hrectbound_impl.hpp
+++ b/src/mlpack/core/tree/hrectbound_impl.hpp
@@ -38,7 +38,7 @@ inline HRectBound<MetricType>::HRectBound(const size_t dimension) :
     minWidth(0)
 { /* Nothing to do. */ }
 
-/***
+/**
  * Copy constructor necessary to prevent memory leaks.
  */
 template<typename MetricType>
@@ -52,7 +52,7 @@ inline HRectBound<MetricType>::HRectBound(const HRectBound& other) :
     bounds[i] = other[i];
 }
 
-/***
+/**
  * Same as the copy constructor.
  */
 template<typename MetricType>
@@ -79,6 +79,21 @@ inline HRectBound<MetricType>& HRectBound<MetricType>::operator=(
 }
 
 /**
+ * Move constructor: take possession of another bound's information.
+ */
+template<typename MetricType>
+inline HRectBound<MetricType>::HRectBound(HRectBound&& other) :
+    dim(other.dim),
+    bounds(other.bounds),
+    minWidth(other.minWidth)
+{
+  // Fix the other bound.
+  other.dim = 0;
+  other.bounds = NULL;
+  other.minWidth = 0.0;
+}
+
+/**
  * Destructor: clean up memory.
  */
 template<typename MetricType>



More information about the mlpack-git mailing list