package ofc.codegen
import ofc.util.Ordering
+import ofc.util.DirectedGraph
class ProducerStatement extends Statement {
object Context {
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
}
}