]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Finally get control flow nesting right in ProducerStatement.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Sat, 7 Apr 2012 20:12:25 +0000 (21:12 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Sat, 7 Apr 2012 20:12:25 +0000 (21:12 +0100)
src/ofc/codegen/ProducerStatement.scala

index d42d11d7b155f1c1014ae06af9235554487f5040..a1a301d94ea5da066d2a93ded31ddcd85e4f849f 100644 (file)
@@ -1,21 +1,17 @@
 package ofc.codegen
-import ofc.util.Ordering
+import ofc.LogicError
 import ofc.util.DirectedGraph
 
 class ProducerStatement extends Statement {
   object Context {
-    def preferenceOrdering(ordering: (Context, Context) => Boolean) : (Context, Context) => Boolean = {
+    private def priority(context: Context) : Int = {
       // This ensures that the nesting ordering is Predicate, DerivedExpression, VariableRange
       // when no other dependencies exist.
-      (left, right) => if (ordering(left, right)) 
-        true 
-      else if (ordering(right, left)) 
-        false
-      else (left, right) match {
-        case (_: Predicate, _: DerivedExpression) => true
-        case (_: Predicate, _: VariableRange) => true
-        case (_: DerivedExpression, _: VariableRange) => true
-        case _ => false
+      context match {
+        case (_: Predicate) => 1
+        case (_: DerivedExpression) => 2
+        case (_: VariableRange) => 3
+        case _ => throw new LogicError("Unknown context type.")
       }
     }
 
@@ -37,8 +33,9 @@ class ProducerStatement extends Statement {
         graph.addEdge(contextMapping.get(c1).get, contextMapping.get(c2).get)
       }
 
-      val sortedVertices = DirectedGraph.topoSort(graph)
       val flippedMapping = contextMapping.map(_.swap).toMap
+      val vertexPriorityFunction = (v: DirectedGraph#Vertex) => priority(flippedMapping.get(v).get)
+      val sortedVertices = DirectedGraph.topoSort(graph, vertexPriorityFunction)
       val sortedContexts = { for (v <- sortedVertices) yield flippedMapping.get(v).get }
 
       sortedContexts.toSeq