[mlpack-git] master: Use templates to significantly simplify code and remove multiple versions of the same function. (3bde69b)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:58:13 EST 2015


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

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

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

commit 3bde69b7e0665a7152febbdd7033c434e8611d7d
Author: Ryan Curtin <ryan at ratml.org>
Date:   Sun Aug 17 06:20:07 2014 +0000

    Use templates to significantly simplify code and remove multiple versions of the
    same function.


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

3bde69b7e0665a7152febbdd7033c434e8611d7d
 src/mlpack/tests/rectangle_tree_test.cpp | 231 +++----------------------------
 1 file changed, 20 insertions(+), 211 deletions(-)

diff --git a/src/mlpack/tests/rectangle_tree_test.cpp b/src/mlpack/tests/rectangle_tree_test.cpp
index 968d9fa..98b0297 100644
--- a/src/mlpack/tests/rectangle_tree_test.cpp
+++ b/src/mlpack/tests/rectangle_tree_test.cpp
@@ -2,9 +2,9 @@
  * @file tree_traits_test.cpp
  * @author Andrew Wells
  *
- * Tests for the RectangleTree class.  This should ensure that the class works correctly
- * and that subsequent changes don't break anything.  Because it's only used to test the trees,
- * it is slow.
+ * Tests for the RectangleTree class.  This should ensure that the class works
+ * correctly and that subsequent changes don't break anything.  Because it's
+ * only used to test the trees, it is slow.
  */
 #include <mlpack/core.hpp>
 #include <mlpack/core/tree/tree_traits.hpp>
@@ -80,10 +80,8 @@ BOOST_AUTO_TEST_CASE(RectangleTreeConstructionCountTest)
  * @param tree The tree that we want to extract all of the points from.
  * @return A vector containing pointers to each point in this tree.
  */
-std::vector<arma::vec*> GetAllPointsInTree(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree)
+template<typename TreeType>
+std::vector<arma::vec*> GetAllPointsInTree(const TreeType& tree)
 {
   std::vector<arma::vec*> vec;
   if (tree.NumChildren() > 0)
@@ -149,10 +147,8 @@ BOOST_AUTO_TEST_CASE(RectangleTreeConstructionRepeatTest)
  *
  * @param tree The tree to check.
  */
-void CheckContainment(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree)
+template<typename TreeType>
+void CheckContainment(const TreeType& tree)
 {
   if (tree.NumChildren() == 0)
   {
@@ -175,10 +171,9 @@ void CheckContainment(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeu
 /**
  * A function to check that containment is as tight as possible.
  */
-void CheckExactContainment(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree) {
+template<typename TreeType>
+void CheckExactContainment(const TreeType& tree)
+{
   if (tree.NumChildren() == 0)
   {
     for (size_t i = 0; i < tree.Bound().Dim(); i++)
@@ -220,91 +215,10 @@ void CheckExactContainment(const RectangleTree<tree::RTreeSplit<tree::RTreeDesce
 }
 
 /**
- * A function to check that containment is as tight as possible.
- */
-void CheckExactContainment(const RectangleTree<tree::RStarTreeSplit<tree::RStarTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RStarTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree) {
-  if (tree.NumChildren() == 0)
-  {
-    for (size_t i = 0; i < tree.Bound().Dim(); i++)
-    {
-      double min = DBL_MAX;
-      double max = -1.0 * DBL_MAX;
-      for(size_t j = 0; j < tree.Count(); j++)
-      {
-  if(tree.LocalDataset().col(j)[i] < min)
-    min = tree.LocalDataset().col(j)[i];
-  if(tree.LocalDataset().col(j)[i] > max)
-    max = tree.LocalDataset().col(j)[i];
-      }
-      BOOST_REQUIRE_EQUAL(max, tree.Bound()[i].Hi());
-      BOOST_REQUIRE_EQUAL(min, tree.Bound()[i].Lo());
-    }
-  } else {
-    for(size_t i = 0; i < tree.Bound().Dim(); i++) {
-      double min = DBL_MAX;
-      double max = -1.0 * DBL_MAX;
-      for(size_t j = 0; j < tree.NumChildren(); j++) {
-  if(tree.Child(j).Bound()[i].Lo() < min)
-    min = tree.Child(j).Bound()[i].Lo();
-  if(tree.Child(j).Bound()[i].Hi() > max)
-    max = tree.Child(j).Bound()[i].Hi();
-      }
-      BOOST_REQUIRE_EQUAL(max, tree.Bound()[i].Hi());
-      BOOST_REQUIRE_EQUAL(min, tree.Bound()[i].Lo());
-    }
-    for(size_t i = 0; i < tree.NumChildren(); i++)
-      CheckExactContainment(tree.Child(i));
-  }
-}
-
-/**
- * A function to check that containment is as tight as possible.
- */
-void CheckExactContainment(const RectangleTree<tree::XTreeSplit<tree::RStarTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RStarTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree) {
-  if(tree.NumChildren() == 0) {
-    for(size_t i = 0; i < tree.Bound().Dim(); i++) {
-      double min = DBL_MAX;
-      double max = -1.0 * DBL_MAX;
-      for(size_t j = 0; j < tree.Count(); j++) {
-  if(tree.LocalDataset().col(j)[i] < min)
-    min = tree.LocalDataset().col(j)[i];
-  if(tree.LocalDataset().col(j)[i] > max)
-    max = tree.LocalDataset().col(j)[i];
-      }
-      BOOST_REQUIRE_EQUAL(max, tree.Bound()[i].Hi());
-      BOOST_REQUIRE_EQUAL(min, tree.Bound()[i].Lo());
-    }
-  } else {
-    for(size_t i = 0; i < tree.Bound().Dim(); i++) {
-      double min = DBL_MAX;
-      double max = -1.0 * DBL_MAX;
-      for(size_t j = 0; j < tree.NumChildren(); j++) {
-  if(tree.Child(j).Bound()[i].Lo() < min)
-    min = tree.Child(j).Bound()[i].Lo();
-  if(tree.Child(j).Bound()[i].Hi() > max)
-    max = tree.Child(j).Bound()[i].Hi();
-      }
-      BOOST_REQUIRE_EQUAL(max, tree.Bound()[i].Hi());
-      BOOST_REQUIRE_EQUAL(min, tree.Bound()[i].Lo());
-    }
-    for(size_t i = 0; i < tree.NumChildren(); i++)
-      CheckExactContainment(tree.Child(i));
-  }
-}
-
-/**
  * A function to check that parents and children are set correctly.
  */
-void CheckHierarchy(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree)
+template<typename TreeType>
+void CheckHierarchy(const TreeType& tree)
 {
   for (size_t i = 0; i < tree.NumChildren(); i++)
   {
@@ -313,32 +227,6 @@ void CheckHierarchy(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuri
   }
 }
 
-/**
- * A function to check that parents and children are set correctly.
- */
-void CheckHierarchy(const RectangleTree<tree::RStarTreeSplit<tree::RStarTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RStarTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree) {
-    for(size_t i = 0; i < tree.NumChildren(); i++) {
-      BOOST_REQUIRE_EQUAL(&tree, tree.Child(i).Parent());
-      CheckHierarchy(tree.Child(i));
-    }
-}
-
-/**
- * A function to check that parents and children are set correctly.
- */
-void CheckHierarchy(const RectangleTree<tree::XTreeSplit<tree::RStarTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RStarTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree) {
-    for (size_t i = 0; i < tree.NumChildren(); i++) {
-      BOOST_REQUIRE_EQUAL(&tree, tree.Child(i).Parent());
-      CheckHierarchy(tree.Child(i));
-    }
-}
-
 // Test to see if the bounds of the tree are correct. (Cover all bounds and
 // points beneath this node of the tree).
 BOOST_AUTO_TEST_CASE(RectangleTreeContainmentTest)
@@ -364,10 +252,8 @@ BOOST_AUTO_TEST_CASE(RectangleTreeContainmentTest)
  * in each leaf node are in sync.
  * @param tree The tree to check.
  */
-void CheckSync(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree)
+template<typename TreeType>
+void CheckSync(const TreeType& tree)
 {
   if (tree.IsLeaf())
   {
@@ -417,10 +303,8 @@ BOOST_AUTO_TEST_CASE(TreeLocalDatasetInSync)
  * It recurses so that it checks each node under (and including) this one.
  * @param tree The tree to check.
  */
-void CheckFills(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree)
+template<typename TreeType>
+void CheckFills(const TreeType& tree)
 {
   if (tree.IsLeaf())
   {
@@ -465,10 +349,8 @@ BOOST_AUTO_TEST_CASE(CheckMinAndMaxFills)
  * @param tree The tree for which we want the height.
  * @return The height of this tree.
  */
-int GetMaxLevel(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree)
+template<typename TreeType>
+int GetMaxLevel(const TreeType& tree)
 {
   int max = 1;
   if (!tree.IsLeaf())
@@ -494,10 +376,8 @@ int GetMaxLevel(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic
  * @param tree The tree for which we want the height.
  * @return The "shortest height" of the tree.
  */
-int GetMinLevel(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree)
+template<typename TreeType>
+int GetMinLevel(const TreeType& tree)
 {
   int min = 1;
   if (!tree.IsLeaf())
@@ -698,77 +578,6 @@ BOOST_AUTO_TEST_CASE(PointDynamicAdd)
   }
 }
 
-/**
- * A function to check that each non-leaf node fully encloses its child nodes
- * and that each leaf node encloses its points.  It recurses so that it checks
- * each node under (and including) this one.
- * @param tree The tree to check.
- */
-void CheckContainment(const RectangleTree<tree::RStarTreeSplit<tree::RStarTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RStarTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree) {
-  if (tree.NumChildren() == 0) {
-    for (size_t i = 0; i < tree.Count(); i++) {
-      BOOST_REQUIRE_EQUAL(tree.Bound().Contains(tree.Dataset().unsafe_col(tree.Points()[i])), true);
-    }
-  } else {
-    for (size_t i = 0; i < tree.NumChildren(); i++) {
-      for (size_t j = 0; j < tree.Bound().Dim(); j++) {
-        BOOST_REQUIRE_EQUAL(tree.Bound()[j].Contains(tree.Children()[i]->Bound()[j]), true);
-      }
-      CheckContainment(*(tree.Children()[i]));
-    }
-  }
-  return;
-}
-
-/**
- * A function to ensure that the dataset for the tree, and the datasets stored
- * in each leaf node are in sync.
- * @param tree The tree to check.
- */
-void CheckSync(const RectangleTree<tree::RStarTreeSplit<tree::RStarTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RStarTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree) {
-  if (tree.IsLeaf()) {
-    for (size_t i = 0; i < tree.Count(); i++) {
-      for (size_t j = 0; j < tree.LocalDataset().n_rows; j++) {
-        BOOST_REQUIRE_EQUAL(tree.LocalDataset().col(i)[j], tree.Dataset().col(tree.Points()[i])[j]);
-      }
-    }
-  }
-  else
-  {
-    for (size_t i = 0; i < tree.NumChildren(); i++)
-      CheckSync(*tree.Children()[i]);
-  }
-}
-
-/**
- * A function to ensure that the dataset for the tree, and the datasets stored
- * in each leaf node are in sync.
- * @param tree The tree to check.
- */
-void CheckSync(const RectangleTree<tree::XTreeSplit<tree::RStarTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
-        tree::RStarTreeDescentHeuristic,
-        NeighborSearchStat<NearestNeighborSort>,
-        arma::mat>& tree) {
-  if (tree.IsLeaf()) {
-    for (size_t i = 0; i < tree.Count(); i++) {
-      for (size_t j = 0; j < tree.LocalDataset().n_rows; j++) {
-        BOOST_REQUIRE_EQUAL(tree.LocalDataset().col(i)[j], tree.Dataset().col(tree.Points()[i])[j]);
-      }
-    }
-  } else {
-    for (size_t i = 0; i < tree.NumChildren(); i++) {
-      CheckSync(*tree.Children()[i]);
-    }
-  }
-  return;
-}
-
 // A test to ensure that the SingleTreeTraverser is working correctly by
 // comparing its results to the results of a naive search.
 BOOST_AUTO_TEST_CASE(SingleTreeTraverserTest)



More information about the mlpack-git mailing list