From d68798ae363733266777598fbae447ff21165210 Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Sat, 7 Apr 2012 20:20:12 +0100 Subject: [PATCH] Switch to new sorting code in ProducerStatement. --- src/ofc/codegen/ProducerStatement.scala | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/ofc/codegen/ProducerStatement.scala b/src/ofc/codegen/ProducerStatement.scala index 23ba91f..d42d11d 100644 --- a/src/ofc/codegen/ProducerStatement.scala +++ b/src/ofc/codegen/ProducerStatement.scala @@ -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 } } -- 2.47.3