]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Switch to new sorting code in ProducerStatement.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Sat, 7 Apr 2012 19:20:12 +0000 (20:20 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Sat, 7 Apr 2012 19:20:12 +0000 (20:20 +0100)
src/ofc/codegen/ProducerStatement.scala

index 23ba91f10e079d6a68285ff0b07904072547fa7a..d42d11d7b155f1c1014ae06af9235554487f5040 100644 (file)
@@ -1,5 +1,6 @@
 package ofc.codegen
 import ofc.util.Ordering
+import ofc.util.DirectedGraph
 
 class ProducerStatement extends Statement {
   object Context {
@@ -24,9 +25,23 @@ class ProducerStatement extends Statement {
         case _ => false
       }
 
-      val partialOrdering = Ordering.transitiveClosure(contexts, pathFunction(_: Context, _: Context))
-      val augmentedOrdering = preferenceOrdering((a,b) => partialOrdering.contains(a,b))
-      contexts.sortWith(augmentedOrdering)
+      val graph = new DirectedGraph
+      val contextMapping = scala.collection.mutable.Map[Context, DirectedGraph#Vertex]()
+
+      for(context <- contexts) {
+        val vertex = graph.addVertex
+        contextMapping += (context -> vertex)
+      }
+
+      for(c1 <- contexts; c2 <- contexts; if pathFunction(c1, c2)) {
+        graph.addEdge(contextMapping.get(c1).get, contextMapping.get(c2).get)
+      }
+
+      val sortedVertices = DirectedGraph.topoSort(graph)
+      val flippedMapping = contextMapping.map(_.swap).toMap
+      val sortedContexts = { for (v <- sortedVertices) yield flippedMapping.get(v).get }
+
+      sortedContexts.toSeq
     }
   }