[mlpack-git] master: Remove a pointer to the node from the SplitType class. (52423b8)

gitdub at mlpack.org gitdub at mlpack.org
Tue May 3 12:36:42 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/4c8a8d1ccbc33916794fe0f6142fa5378ff8503d...93eb34cc4daf08cc331d1a22fccf44950b276daa

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

commit 52423b82b0765abc658c601eb423f1e8852dd260
Author: Mikhail Lozhnikov <lozhnikovma at gmail.com>
Date:   Tue May 3 19:36:42 2016 +0300

    Remove a pointer to the node from the SplitType class.


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

52423b82b0765abc658c601eb423f1e8852dd260
 .../core/tree/rectangle_tree/r_star_tree_split.hpp | 13 ++++-----
 .../tree/rectangle_tree/r_star_tree_split_impl.hpp | 26 ++++++++---------
 .../core/tree/rectangle_tree/r_tree_split.hpp      | 17 +++++------
 .../core/tree/rectangle_tree/r_tree_split_impl.hpp | 32 +++++++++------------
 .../tree/rectangle_tree/rectangle_tree_impl.hpp    |  6 ++--
 .../core/tree/rectangle_tree/x_tree_split.hpp      | 16 ++++-------
 .../core/tree/rectangle_tree/x_tree_split_impl.hpp | 33 +++++++---------------
 7 files changed, 55 insertions(+), 88 deletions(-)

diff --git a/src/mlpack/core/tree/rectangle_tree/r_star_tree_split.hpp b/src/mlpack/core/tree/rectangle_tree/r_star_tree_split.hpp
index 1b93768..908d6c2 100644
--- a/src/mlpack/core/tree/rectangle_tree/r_star_tree_split.hpp
+++ b/src/mlpack/core/tree/rectangle_tree/r_star_tree_split.hpp
@@ -26,31 +26,28 @@ class RStarTreeSplit
   RStarTreeSplit();
 
   //! Construct this with the specified node.
-  RStarTreeSplit(TreeType *node);
+  RStarTreeSplit(const TreeType *node);
 
   //! Construct this with the specified node and the parent of the node.
-  RStarTreeSplit(TreeType *node,const TreeType *parentNode);
+  RStarTreeSplit(const TreeType *node,const TreeType *parentNode);
 
   //! Create a copy of the other.split.
-  RStarTreeSplit(TreeType *node,const TreeType &other);
+  RStarTreeSplit(const TreeType &other);
 
   /**
    * Split a leaf node using the algorithm described in "The R*-tree: An
    * Efficient and Robust Access method for Points and Rectangles."  If
    * necessary, this split will propagate upwards through the tree.
    */
-  void SplitLeafNode(std::vector<bool>& relevels);
+  void SplitLeafNode(TreeType *tree,std::vector<bool>& relevels);
 
   /**
    * Split a non-leaf node using the "default" algorithm.  If this is a root
    * node, the tree increases in depth.
    */
-  bool SplitNonLeafNode(std::vector<bool>& relevels);
+  bool SplitNonLeafNode(TreeType *tree,std::vector<bool>& relevels);
 
  private:
-  //! The node which has to be split.
-  TreeType *tree;
- 
   /**
    * Class to allow for faster sorting.
    */
diff --git a/src/mlpack/core/tree/rectangle_tree/r_star_tree_split_impl.hpp b/src/mlpack/core/tree/rectangle_tree/r_star_tree_split_impl.hpp
index 9fda2f5..f233ff7 100644
--- a/src/mlpack/core/tree/rectangle_tree/r_star_tree_split_impl.hpp
+++ b/src/mlpack/core/tree/rectangle_tree/r_star_tree_split_impl.hpp
@@ -16,29 +16,25 @@ namespace mlpack {
 namespace tree {
 
 template<typename TreeType>
-RStarTreeSplit<TreeType>::RStarTreeSplit() :
-    tree(NULL)
+RStarTreeSplit<TreeType>::RStarTreeSplit()
 {
 
 }
 
 template<typename TreeType>
-RStarTreeSplit<TreeType>::RStarTreeSplit(TreeType *node) :
-    tree(node)
+RStarTreeSplit<TreeType>::RStarTreeSplit(const TreeType *)
 {
 
 }
 
 template<typename TreeType>
-RStarTreeSplit<TreeType>::RStarTreeSplit(TreeType *node,const TreeType *) :
-    tree(node)
+RStarTreeSplit<TreeType>::RStarTreeSplit(const TreeType *,const TreeType *)
 {
 
 }
 
 template<typename TreeType>
-RStarTreeSplit<TreeType>::RStarTreeSplit(TreeType *node,const TreeType &) :
-    tree(node)
+RStarTreeSplit<TreeType>::RStarTreeSplit(const TreeType &)
 {
 
 }
@@ -51,7 +47,7 @@ RStarTreeSplit<TreeType>::RStarTreeSplit(TreeType *node,const TreeType &) :
  * new nodes into the tree, spliting the parent if necessary.
  */
 template<typename TreeType>
-void RStarTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
+void RStarTreeSplit<TreeType>::SplitLeafNode(TreeType *tree,std::vector<bool>& relevels)
 {
   // Convenience typedef.
   typedef typename TreeType::ElemType ElemType;
@@ -70,7 +66,7 @@ void RStarTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
     tree->Children()[(tree->NumChildren())++] = copy;
     assert(tree->NumChildren() == 1);
 
-    copy->Split().SplitLeafNode(relevels);
+    copy->Split().SplitLeafNode(copy,relevels);
     return;
   }
 
@@ -87,7 +83,7 @@ void RStarTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
     size_t p = tree->MaxLeafSize() * 0.3; // The paper says this works the best.
     if (p == 0)
     {
-      tree->Split().SplitLeafNode(relevels);
+      tree->Split().SplitLeafNode(tree,relevels);
       return;
     }
 
@@ -280,7 +276,7 @@ void RStarTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
   // just in case, we use an assert.
   assert(par->NumChildren() <= par->MaxNumChildren() + 1);
   if (par->NumChildren() == par->MaxNumChildren() + 1)
-    par->Split().SplitNonLeafNode(relevels);
+    par->Split().SplitNonLeafNode(par,relevels);
 
   assert(treeOne->Parent()->NumChildren() <= treeOne->MaxNumChildren());
   assert(treeOne->Parent()->NumChildren() >= treeOne->MinNumChildren());
@@ -298,7 +294,7 @@ void RStarTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
  * higher up the tree because they were already updated if necessary.
  */
 template<typename TreeType>
-bool RStarTreeSplit<TreeType>::SplitNonLeafNode(std::vector<bool>& relevels)
+bool RStarTreeSplit<TreeType>::SplitNonLeafNode(TreeType *tree,std::vector<bool>& relevels)
 {
   // Convenience typedef.
   typedef typename TreeType::ElemType ElemType;
@@ -316,7 +312,7 @@ bool RStarTreeSplit<TreeType>::SplitNonLeafNode(std::vector<bool>& relevels)
     tree->NullifyData();
     tree->Children()[(tree->NumChildren())++] = copy;
 
-    copy->Split().SplitNonLeafNode(relevels);
+    copy->Split().SplitNonLeafNode(copy,relevels);
     return true;
   }
 
@@ -672,7 +668,7 @@ bool RStarTreeSplit<TreeType>::SplitNonLeafNode(std::vector<bool>& relevels)
   assert(par->NumChildren() <= par->MaxNumChildren() + 1);
   if (par->NumChildren() == par->MaxNumChildren() + 1)
   {
-    par->Split().SplitNonLeafNode(relevels);
+    par->Split().SplitNonLeafNode(par,relevels);
   }
 
   // We have to update the children of each of these new nodes so that they
diff --git a/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp b/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp
index 71e506f..ebb640c 100644
--- a/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp
+++ b/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp
@@ -26,39 +26,36 @@ class RTreeSplit
   RTreeSplit();
 
   //! Construct this with the specified node.
-  RTreeSplit(TreeType *node);
+  RTreeSplit(const TreeType *node);
 
   //! Construct this with the specified node and the parent of the node.
-  RTreeSplit(TreeType *node,const TreeType *parentNode);
+  RTreeSplit(const TreeType *node,const TreeType *parentNode);
 
   //! Create a copy of the other.split.
-  RTreeSplit(TreeType *node,const TreeType &other);
+  RTreeSplit(const TreeType &other);
 
   /**
    * Split a leaf node using the "default" algorithm.  If necessary, this split
    * will propagate upwards through the tree.
    */
-  void SplitLeafNode(std::vector<bool>& relevels);
+  void SplitLeafNode(TreeType *tree,std::vector<bool>& relevels);
 
   /**
    * Split a non-leaf node using the "default" algorithm.  If this is a root
    * node, the tree increases in depth.
    */
-  bool SplitNonLeafNode(std::vector<bool>& relevels);
+  bool SplitNonLeafNode(TreeType *tree,std::vector<bool>& relevels);
 
  private:
-  //! The node which has to be split.
-  TreeType *tree;
-
   /**
    * Get the seeds for splitting a leaf node.
    */
-  void GetPointSeeds(int& i, int& j);
+  static void GetPointSeeds(const TreeType *tree,int& i, int& j);
 
   /**
    * Get the seeds for splitting a non-leaf node.
    */
-  void GetBoundSeeds(int& i, int& j);
+  static void GetBoundSeeds(const TreeType *tree,int& i, int& j);
 
   /**
    * Assign points to the two new nodes.
diff --git a/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp b/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
index 66b2084..f315e3c 100644
--- a/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
+++ b/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
@@ -15,29 +15,25 @@ namespace mlpack {
 namespace tree {
 
 template<typename TreeType>
-RTreeSplit<TreeType>::RTreeSplit() :
-    tree(NULL)
+RTreeSplit<TreeType>::RTreeSplit()
 {
 
 }
 
 template<typename TreeType>
-RTreeSplit<TreeType>::RTreeSplit(TreeType *node) :
-    tree(node)
+RTreeSplit<TreeType>::RTreeSplit(const TreeType *)
 {
 
 }
 
 template<typename TreeType>
-RTreeSplit<TreeType>::RTreeSplit(TreeType *node,const TreeType *) :
-    tree(node)
+RTreeSplit<TreeType>::RTreeSplit(const TreeType *,const TreeType *)
 {
 
 }
 
 template<typename TreeType>
-RTreeSplit<TreeType>::RTreeSplit(TreeType *node,const TreeType &) :
-    tree(node)
+RTreeSplit<TreeType>::RTreeSplit(const TreeType &)
 {
 
 }
@@ -49,7 +45,7 @@ RTreeSplit<TreeType>::RTreeSplit(TreeType *node,const TreeType &) :
  * new nodes into the tree, spliting the parent if necessary.
  */
 template<typename TreeType>
-void RTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
+void RTreeSplit<TreeType>::SplitLeafNode(TreeType *tree,std::vector<bool>& relevels)
 {
   // If we are splitting the root node, we need will do things differently so
   // that the constructor and other methods don't confuse the end user by giving
@@ -63,7 +59,7 @@ void RTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
     tree->NullifyData();
     // Because this was a leaf node, numChildren must be 0.
     tree->Children()[(tree->NumChildren())++] = copy;
-    copy->Split().SplitLeafNode(relevels);
+    copy->Split().SplitLeafNode(copy,relevels);
     return;
   }
 
@@ -74,7 +70,7 @@ void RTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
   // rectangles, only points.  We assume that the tree uses Euclidean Distance.
   int i = 0;
   int j = 0;
-  GetPointSeeds(i, j);
+  RTreeSplit<TreeType>::GetPointSeeds(tree,i, j);
 
   TreeType* treeOne = new TreeType(tree->Parent());
   TreeType* treeTwo = new TreeType(tree->Parent());
@@ -94,7 +90,7 @@ void RTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
   // just in case, we use an assert.
   assert(par->NumChildren() <= par->MaxNumChildren() + 1);
   if (par->NumChildren() == par->MaxNumChildren() + 1)
-    par->Split().SplitNonLeafNode(relevels);
+    par->Split().SplitNonLeafNode(par,relevels);
 
   assert(treeOne->Parent()->NumChildren() <= treeOne->MaxNumChildren());
   assert(treeOne->Parent()->NumChildren() >= treeOne->MinNumChildren());
@@ -113,7 +109,7 @@ void RTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
  * higher up the tree because they were already updated if necessary.
  */
 template<typename TreeType>
-bool RTreeSplit<TreeType>::SplitNonLeafNode(std::vector<bool>& relevels)
+bool RTreeSplit<TreeType>::SplitNonLeafNode(TreeType *tree,std::vector<bool>& relevels)
 {
   // If we are splitting the root node, we need will do things differently so
   // that the constructor and other methods don't confuse the end user by giving
@@ -126,13 +122,13 @@ bool RTreeSplit<TreeType>::SplitNonLeafNode(std::vector<bool>& relevels)
     tree->NumChildren() = 0;
     tree->NullifyData();
     tree->Children()[(tree->NumChildren())++] = copy;
-    copy->Split().SplitNonLeafNode(relevels);
+    copy->Split().SplitNonLeafNode(copy,relevels);
     return true;
   }
 
   int i = 0;
   int j = 0;
-  GetBoundSeeds(i, j);
+  RTreeSplit<TreeType>::GetBoundSeeds(tree,i, j);
 
   assert(i != j);
 
@@ -159,7 +155,7 @@ bool RTreeSplit<TreeType>::SplitNonLeafNode(std::vector<bool>& relevels)
   assert(par->NumChildren() <= par->MaxNumChildren() + 1);
 
   if (par->NumChildren() == par->MaxNumChildren() + 1)
-    par->Split().SplitNonLeafNode(relevels);
+    par->Split().SplitNonLeafNode(par,relevels);
 
   // We have to update the children of each of these new nodes so that they
   // record the correct parent.
@@ -185,7 +181,7 @@ bool RTreeSplit<TreeType>::SplitNonLeafNode(std::vector<bool>& relevels)
  * The indices of these points will be stored in iRet and jRet.
  */
 template<typename TreeType>
-void RTreeSplit<TreeType>::GetPointSeeds(int& iRet, int& jRet)
+void RTreeSplit<TreeType>::GetPointSeeds(const TreeType *tree,int& iRet, int& jRet)
 {
   // Here we want to find the pair of points that it is worst to place in the
   // same node.  Because we are just using points, we will simply choose the two
@@ -213,7 +209,7 @@ void RTreeSplit<TreeType>::GetPointSeeds(int& iRet, int& jRet)
  * indices of the bounds will be stored in iRet and jRet.
  */
 template<typename TreeType>
-void RTreeSplit<TreeType>::GetBoundSeeds(int& iRet, int& jRet)
+void RTreeSplit<TreeType>::GetBoundSeeds(const TreeType *tree,int& iRet, int& jRet)
 {
   // Convenience typedef.
   typedef typename TreeType::ElemType ElemType;
diff --git a/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp b/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp
index 050c4b8..4e1526c 100644
--- a/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp
+++ b/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp
@@ -160,7 +160,7 @@ RectangleTree(
     points(other.Points()),
     localDataset(NULL)
 {
-  split = SplitType<RectangleTree>(this,other);
+  split = SplitType<RectangleTree>(other);
   if (deepCopy)
   {
     if (numChildren > 0)
@@ -671,7 +671,7 @@ void RectangleTree<MetricType, StatisticType, MatType, SplitType, DescentType>::
 
     // If we are full, then we need to split (or at least try).  The SplitType
     // takes care of this and of moving up the tree if necessary.
-    split.SplitLeafNode(relevels);
+    split.SplitLeafNode(this,relevels);
   }
   else
   {
@@ -681,7 +681,7 @@ void RectangleTree<MetricType, StatisticType, MatType, SplitType, DescentType>::
 
     // If we are full, then we need to split (or at least try).  The SplitType
     // takes care of this and of moving up the tree if necessary.
-    split.SplitNonLeafNode(relevels);
+    split.SplitNonLeafNode(this,relevels);
   }
 }
 
diff --git a/src/mlpack/core/tree/rectangle_tree/x_tree_split.hpp b/src/mlpack/core/tree/rectangle_tree/x_tree_split.hpp
index 66eee3f..3d112c6 100644
--- a/src/mlpack/core/tree/rectangle_tree/x_tree_split.hpp
+++ b/src/mlpack/core/tree/rectangle_tree/x_tree_split.hpp
@@ -36,34 +36,28 @@ class XTreeSplit
   XTreeSplit();
 
   //! Construct this with the specified node.
-  XTreeSplit(TreeType *node);
-
-  //! Construct this with the specified node and the specified normalNodeMaxNumChildren.
-  XTreeSplit(TreeType *node,const size_t normalNodeMaxNumChildren);
+  XTreeSplit(const TreeType *node);
 
   //! Construct this with the specified node and the parent of the node.
-  XTreeSplit(TreeType *node,const TreeType *parentNode);
+  XTreeSplit(const TreeType *node,const TreeType *parentNode);
 
   //! Create a copy of the other.split.
-  XTreeSplit(TreeType *node,const TreeType &other);
+  XTreeSplit(const TreeType &other);
 
   /**
    * Split a leaf node using the algorithm described in "The R*-tree: An
    * Efficient and Robust Access method for Points and Rectangles."  If
    * necessary, this split will propagate upwards through the tree.
    */
-  void SplitLeafNode(std::vector<bool>& relevels);
+  void SplitLeafNode(TreeType *tree,std::vector<bool>& relevels);
 
   /**
    * Split a non-leaf node using the "default" algorithm.  If this is a root
    * node, the tree increases in depth.
    */
-  bool SplitNonLeafNode(std::vector<bool>& relevels);
+  bool SplitNonLeafNode(TreeType *tree,std::vector<bool>& relevels);
 
  private:
-  //! The node which has to be split.
-  TreeType *tree;
-
   //! The max number of child nodes a non-leaf normal node can have.
   size_t normalNodeMaxNumChildren;
 
diff --git a/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp b/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp
index 3d99cf0..b1929d0 100644
--- a/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp
+++ b/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp
@@ -16,40 +16,27 @@ namespace tree {
 
 template<typename TreeType>
 XTreeSplit<TreeType>::XTreeSplit() :
-    tree(NULL),
     normalNodeMaxNumChildren(0)
 {
 
 }
 
 template<typename TreeType>
-XTreeSplit<TreeType>::XTreeSplit(TreeType *node) :
-    tree(node),
+XTreeSplit<TreeType>::XTreeSplit(const TreeType *node) :
     normalNodeMaxNumChildren(node->MaxNumChildren())
 {
 
 }
 
-
-template<typename TreeType>
-XTreeSplit<TreeType>::XTreeSplit(TreeType *node,const size_t normalNodeMaxNumChildren) :
-    tree(node),
-    normalNodeMaxNumChildren(normalNodeMaxNumChildren)
-{
-
-}
-
 template<typename TreeType>
-XTreeSplit<TreeType>::XTreeSplit(TreeType *node,const TreeType *parent) :
-    tree(node),
+XTreeSplit<TreeType>::XTreeSplit(const TreeType *,const TreeType *parent) :
     normalNodeMaxNumChildren(parent->Split().NormalNodeMaxNumChildren())
 {
 
 }
 
 template<typename TreeType>
-XTreeSplit<TreeType>::XTreeSplit(TreeType *node,const TreeType &other) :
-    tree(node),
+XTreeSplit<TreeType>::XTreeSplit(const TreeType &other) :
     normalNodeMaxNumChildren(other.Split().NormalNodeMaxNumChildren())
 {
 
@@ -63,7 +50,7 @@ XTreeSplit<TreeType>::XTreeSplit(TreeType *node,const TreeType &other) :
  * new nodes into the tree, spliting the parent if necessary.
  */
 template<typename TreeType>
-void XTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
+void XTreeSplit<TreeType>::SplitLeafNode(TreeType *tree,std::vector<bool>& relevels)
 {
   // Convenience typedef.
   typedef typename TreeType::ElemType ElemType;
@@ -81,7 +68,7 @@ void XTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
     // Because this was a leaf node, numChildren must be 0.
     tree->Children()[(tree->NumChildren())++] = copy;
     assert(tree->NumChildren() == 1);
-    copy->Split().SplitLeafNode(relevels);
+    copy->Split().SplitLeafNode(copy,relevels);
     return;
   }
 
@@ -99,7 +86,7 @@ void XTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
     size_t p = tree->MaxLeafSize() * 0.3;
     if (p == 0)
     {
-      tree->Split().SplitLeafNode(relevels);
+      tree->Split().SplitLeafNode(tree,relevels);
       return;
     }
 
@@ -312,7 +299,7 @@ void XTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
   // in case, we use an assert.
   assert(par->NumChildren() <= par->MaxNumChildren() + 1);
   if (par->NumChildren() == par->MaxNumChildren() + 1)
-    par->Split().SplitNonLeafNode(relevels);
+    par->Split().SplitNonLeafNode(par,relevels);
 
   assert(treeOne->Parent()->NumChildren() <=
       treeOne->Parent()->MaxNumChildren());
@@ -334,7 +321,7 @@ void XTreeSplit<TreeType>::SplitLeafNode(std::vector<bool>& relevels)
  * higher up the tree because they were already updated if necessary.
  */
 template<typename TreeType>
-bool XTreeSplit<TreeType>::SplitNonLeafNode(std::vector<bool>& relevels)
+bool XTreeSplit<TreeType>::SplitNonLeafNode(TreeType *tree,std::vector<bool>& relevels)
 {
   // Convenience typedef.
   typedef typename TreeType::ElemType ElemType;
@@ -351,7 +338,7 @@ bool XTreeSplit<TreeType>::SplitNonLeafNode(std::vector<bool>& relevels)
     tree->NumChildren() = 0;
     tree->NullifyData();
     tree->Children()[(tree->NumChildren())++] = copy;
-    copy->Split().SplitNonLeafNode(relevels);
+    copy->Split().SplitNonLeafNode(copy,relevels);
     return true;
   }
 
@@ -845,7 +832,7 @@ bool XTreeSplit<TreeType>::SplitNonLeafNode(std::vector<bool>& relevels)
 
   if (par->NumChildren() == par->MaxNumChildren() + 1)
   {
-    par->Split().SplitNonLeafNode(relevels);
+    par->Split().SplitNonLeafNode(par,relevels);
   }
 
   // We have to update the children of each of these new nodes so that they




More information about the mlpack-git mailing list