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.")
}
}
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