You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by rx...@apache.org on 2014/04/24 07:48:02 UTC

git commit: [Fix #79] Replace Breakable For Loops By While Loops

Repository: spark
Updated Branches:
  refs/heads/master 6ab757806 -> bb68f4774


[Fix #79] Replace Breakable For Loops By While Loops

Author: Sandeep <sa...@techaddict.me>

Closes #503 from techaddict/fix-79 and squashes the following commits:

e3f6746 [Sandeep] Style changes
07a4f6b [Sandeep] for loop to While loop
0a6d8e9 [Sandeep] Breakable for loop to While loop


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/bb68f477
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/bb68f477
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/bb68f477

Branch: refs/heads/master
Commit: bb68f47745eec2954814d3da277a672d5cf89980
Parents: 6ab7578
Author: Sandeep <sa...@techaddict.me>
Authored: Wed Apr 23 22:47:59 2014 -0700
Committer: Reynold Xin <rx...@apache.org>
Committed: Wed Apr 23 22:47:59 2014 -0700

----------------------------------------------------------------------
 .../apache/spark/mllib/tree/DecisionTree.scala  | 60 ++++++++++----------
 1 file changed, 31 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/bb68f477/mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala
----------------------------------------------------------------------
diff --git a/mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala b/mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala
index 3019447..f68076f 100644
--- a/mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala
+++ b/mllib/src/main/scala/org/apache/spark/mllib/tree/DecisionTree.scala
@@ -17,8 +17,6 @@
 
 package org.apache.spark.mllib.tree
 
-import scala.util.control.Breaks._
-
 import org.apache.spark.annotation.Experimental
 import org.apache.spark.{Logging, SparkContext}
 import org.apache.spark.SparkContext._
@@ -82,31 +80,34 @@ class DecisionTree (private val strategy: Strategy) extends Serializable with Lo
      * still survived the filters of the parent nodes.
      */
 
-    // TODO: Convert for loop to while loop
-    breakable {
-      for (level <- 0 until maxDepth) {
-
-        logDebug("#####################################")
-        logDebug("level = " + level)
-        logDebug("#####################################")
-
-        // Find best split for all nodes at a level.
-        val splitsStatsForLevel = DecisionTree.findBestSplits(input, parentImpurities, strategy,
-          level, filters, splits, bins)
-
-        for ((nodeSplitStats, index) <- splitsStatsForLevel.view.zipWithIndex) {
-          // Extract info for nodes at the current level.
-          extractNodeInfo(nodeSplitStats, level, index, nodes)
-          // Extract info for nodes at the next lower level.
-          extractInfoForLowerLevels(level, index, maxDepth, nodeSplitStats, parentImpurities,
-            filters)
-          logDebug("final best split = " + nodeSplitStats._1)
-        }
-        require(scala.math.pow(2, level) == splitsStatsForLevel.length)
-        // Check whether all the nodes at the current level at leaves.
-        val allLeaf = splitsStatsForLevel.forall(_._2.gain <= 0)
-        logDebug("all leaf = " + allLeaf)
-        if (allLeaf) break // no more tree construction
+    var level = 0
+    var break = false
+    while (level < maxDepth && !break) {
+
+      logDebug("#####################################")
+      logDebug("level = " + level)
+      logDebug("#####################################")
+
+      // Find best split for all nodes at a level.
+      val splitsStatsForLevel = DecisionTree.findBestSplits(input, parentImpurities, strategy,
+        level, filters, splits, bins)
+
+      for ((nodeSplitStats, index) <- splitsStatsForLevel.view.zipWithIndex) {
+        // Extract info for nodes at the current level.
+        extractNodeInfo(nodeSplitStats, level, index, nodes)
+        // Extract info for nodes at the next lower level.
+        extractInfoForLowerLevels(level, index, maxDepth, nodeSplitStats, parentImpurities,
+          filters)
+        logDebug("final best split = " + nodeSplitStats._1)
+      }
+      require(scala.math.pow(2, level) == splitsStatsForLevel.length)
+      // Check whether all the nodes at the current level at leaves.
+      val allLeaf = splitsStatsForLevel.forall(_._2.gain <= 0)
+      logDebug("all leaf = " + allLeaf)
+      if (allLeaf) {
+        break = true // no more tree construction
+      } else {
+        level += 1
       }
     }
 
@@ -146,8 +147,8 @@ class DecisionTree (private val strategy: Strategy) extends Serializable with Lo
       parentImpurities: Array[Double],
       filters: Array[List[Filter]]): Unit = {
     // 0 corresponds to the left child node and 1 corresponds to the right child node.
-    // TODO: Convert to while loop
-    for (i <- 0 to 1) {
+    var i = 0
+    while (i <= 1) {
      // Calculate the index of the node from the node level and the index at the current level.
       val nodeIndex = scala.math.pow(2, level + 1).toInt - 1 + 2 * index + i
       if (level < maxDepth - 1) {
@@ -166,6 +167,7 @@ class DecisionTree (private val strategy: Strategy) extends Serializable with Lo
           logDebug("Filter = " + filter)
         }
       }
+      i += 1
     }
   }
 }