[mlpack] 05/58: Use templates to significantly simplify code and remove multiple versions of the same function.

Barak A. Pearlmutter barak+git at cs.nuim.ie
Tue Sep 9 13:19:38 UTC 2014


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch svn-trunk
in repository mlpack.

commit 5dc3bfc69b8f88a31efe166c307b9d687ba15460
Author: rcurtin <rcurtin at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date:   Sun Aug 17 06:20:07 2014 +0000

    Use templates to significantly simplify code and remove multiple versions of the
    same function.
    
    
    git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@17050 9d5b8971-822b-0410-80eb-d18c1038ef23
---
 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)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list