[mlpack] 241/324: Don't do base cases before recursing. This is slightly cleaner code and will properly handle the case where the tree is only one node deep.

Barak A. Pearlmutter barak+git at cs.nuim.ie
Sun Aug 17 08:22:14 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 6902f83c6568ac9c6753803201a9f1b4058967ca
Author: rcurtin <rcurtin at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date:   Tue Jul 29 15:50:38 2014 +0000

    Don't do base cases before recursing.  This is slightly cleaner code and will
    properly handle the case where the tree is only one node deep.
    
    
    git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@16917 9d5b8971-822b-0410-80eb-d18c1038ef23
---
 .../single_tree_traverser_impl.hpp                 | 100 +++++++++------------
 1 file changed, 44 insertions(+), 56 deletions(-)

diff --git a/src/mlpack/core/tree/binary_space_tree/single_tree_traverser_impl.hpp b/src/mlpack/core/tree/binary_space_tree/single_tree_traverser_impl.hpp
index 0a1e71f..7b559e0 100644
--- a/src/mlpack/core/tree/binary_space_tree/single_tree_traverser_impl.hpp
+++ b/src/mlpack/core/tree/binary_space_tree/single_tree_traverser_impl.hpp
@@ -39,77 +39,65 @@ SingleTreeTraverser<RuleType>::Traverse(
     BinarySpaceTree<BoundType, StatisticType, MatType, SplitType>&
         referenceNode)
 {
-  // If this is a leaf, the base cases have already been run.
+  // If we are a leaf, run the base case as necessary.
   if (referenceNode.IsLeaf())
-    return;
-
-  // If either score is DBL_MAX, we do not recurse into that node.
-  double leftScore = rule.Score(queryIndex, *referenceNode.Left());
-
-  // Immediately run the base case if it's not pruned.
-  if ((leftScore != DBL_MAX) && (referenceNode.Left()->IsLeaf()))
   {
-    for (size_t i = referenceNode.Left()->Begin();
-         i < referenceNode.Left()->End(); ++i)
+    for (size_t i = referenceNode.Begin(); i < referenceNode.End(); ++i)
       rule.BaseCase(queryIndex, i);
   }
-
-  double rightScore = rule.Score(queryIndex, *referenceNode.Right());
-
-  // Immediately run the base case if it's not pruned.
-  if ((rightScore != DBL_MAX) && (referenceNode.Right()->IsLeaf()))
+  else
   {
-    for (size_t i = referenceNode.Right()->Begin();
-         i < referenceNode.Right()->End(); ++i)
-      rule.BaseCase(queryIndex, i);
-  }
-
-  if (leftScore < rightScore)
-  {
-    // Recurse to the left.
-    Traverse(queryIndex, *referenceNode.Left());
-
-    // Is it still valid to recurse to the right?
-    rightScore = rule.Rescore(queryIndex, *referenceNode.Right(), rightScore);
-
-    if (rightScore != DBL_MAX)
-      Traverse(queryIndex, *referenceNode.Right()); // Recurse to the right.
-    else
-      ++numPrunes;
-  }
-  else if (rightScore < leftScore)
-  {
-    // Recurse to the right.
-    Traverse(queryIndex, *referenceNode.Right());
-
-    // Is it still valid to recurse to the left?
-    leftScore = rule.Rescore(queryIndex, *referenceNode.Left(), leftScore);
+    // If either score is DBL_MAX, we do not recurse into that node.
+    double leftScore = rule.Score(queryIndex, *referenceNode.Left());
+    double rightScore = rule.Score(queryIndex, *referenceNode.Right());
 
-    if (leftScore != DBL_MAX)
-      Traverse(queryIndex, *referenceNode.Left()); // Recurse to the left.
-    else
-      ++numPrunes;
-  }
-  else // leftScore is equal to rightScore.
-  {
-    if (leftScore == DBL_MAX)
-    {
-      numPrunes += 2; // Pruned both left and right.
-    }
-    else
+    if (leftScore < rightScore)
     {
-      // Choose the left first.
+      // Recurse to the left.
       Traverse(queryIndex, *referenceNode.Left());
 
       // Is it still valid to recurse to the right?
-      rightScore = rule.Rescore(queryIndex, *referenceNode.Right(),
-          rightScore);
+      rightScore = rule.Rescore(queryIndex, *referenceNode.Right(), rightScore);
 
       if (rightScore != DBL_MAX)
-        Traverse(queryIndex, *referenceNode.Right());
+        Traverse(queryIndex, *referenceNode.Right()); // Recurse to the right.
       else
         ++numPrunes;
     }
+    else if (rightScore < leftScore)
+    {
+      // Recurse to the right.
+      Traverse(queryIndex, *referenceNode.Right());
+
+      // Is it still valid to recurse to the left?
+      leftScore = rule.Rescore(queryIndex, *referenceNode.Left(), leftScore);
+
+      if (leftScore != DBL_MAX)
+        Traverse(queryIndex, *referenceNode.Left()); // Recurse to the left.
+      else
+        ++numPrunes;
+    }
+    else // leftScore is equal to rightScore.
+    {
+      if (leftScore == DBL_MAX)
+      {
+        numPrunes += 2; // Pruned both left and right.
+      }
+      else
+      {
+        // Choose the left first.
+        Traverse(queryIndex, *referenceNode.Left());
+
+        // Is it still valid to recurse to the right?
+        rightScore = rule.Rescore(queryIndex, *referenceNode.Right(),
+            rightScore);
+
+        if (rightScore != DBL_MAX)
+          Traverse(queryIndex, *referenceNode.Right());
+        else
+          ++numPrunes;
+      }
+    }
   }
 }
 

-- 
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