From: Francis Russell Date: Tue, 17 Apr 2012 13:23:43 +0000 (+0100) Subject: Work on index binding. X-Git-Url: https://git.unchartedbackwaters.co.uk/w/?a=commitdiff_plain;h=3cc1e902e7e1872e0abea25990faf835e1606120;p=francis%2Fofc.git Work on index binding. --- diff --git a/src/ofc/generators/onetep/CodeGenerator.scala b/src/ofc/generators/onetep/CodeGenerator.scala index 0bfb886..bd88eae 100644 --- a/src/ofc/generators/onetep/CodeGenerator.scala +++ b/src/ofc/generators/onetep/CodeGenerator.scala @@ -29,6 +29,11 @@ class NameManager { */ class CodeGenerator(indexBindings: IndexBindings) { + private val indexSymbols = { + def createMapping(index: BindingIndex) = (index, new NamedUnboundVarSymbol[IntType](index.getName)) + indexBindings.getBindingIndices.map(createMapping(_)).toMap + } + def apply(assignment: Assignment) { //val declarations = collectDeclarations(assignment) //for(declaration <- declarations) code append declaration+"\n" @@ -62,16 +67,20 @@ class CodeGenerator(indexBindings: IndexBindings) { for(operand <- space.getOperands) { val opStatement = buildStatement(operand) - for(discreteIndex <- operand.getDiscreteIndices; if indexBindings.contains(discreteIndex)) { - //TODO: store the symbol! - val symbol = new NamedUnboundVarSymbol[IntType](discreteIndex.getName) - val newData = opStatement.addPredicate(symbol |==| discreteIndex.getValue) + for(discreteIndex <- operand.getDiscreteIndices) indexBindings.getBindingIndex(discreteIndex) match { + case Some(bindingIndex) => { + val symbol = indexSymbols.get(bindingIndex).get + val newData = opStatement.addPredicate(symbol |==| discreteIndex.getValue) + } + case _ => () } - for(spatialIndex <- operand.getSpatialIndices; if indexBindings.contains(spatialIndex)) { - //TODO: store the symbol! - val symbol = new NamedUnboundVarSymbol[IntType](spatialIndex.getName) - opStatement.addPredicate(symbol |==| spatialIndex.getValue) + for(spatialIndex <- operand.getSpatialIndices) indexBindings.getBindingIndex(spatialIndex) match { + case Some(bindingIndex) => { + val symbol = indexSymbols.get(bindingIndex).get + val newData = opStatement.addPredicate(symbol |==| spatialIndex.getValue) + } + case _ => () } result.merge(opStatement) diff --git a/src/ofc/generators/onetep/IndexBindings.scala b/src/ofc/generators/onetep/IndexBindings.scala index c69b0f4..7cab597 100644 --- a/src/ofc/generators/onetep/IndexBindings.scala +++ b/src/ofc/generators/onetep/IndexBindings.scala @@ -1,4 +1,5 @@ package ofc.generators.onetep +import ofc.LogicError class IndexBindings { import scala.collection.mutable.{Set,HashSet, HashMap} @@ -11,17 +12,29 @@ class IndexBindings { def add(binding: BindingIndex, index: SpatialIndex) = spatial.getOrElseUpdate(binding, new HashSet()) += index def add(binding: BindingIndex, index: DiscreteIndex) = discrete.getOrElseUpdate(binding, new HashSet()) += index - def contains(index: SpatialIndex) : Boolean = { + def contains(index: SpatialIndex) : Boolean = getBindingIndex(index) match { + case Some(_) => true + case _ => false + } + + def contains(index: DiscreteIndex) : Boolean = getBindingIndex(index) match { + case Some(_) => true + case _ => false + } + + def getBindingIndex(index: SpatialIndex) : Option[BindingIndex] = { for((bindingIndex, spatialIndices) <- spatial; if spatialIndices.contains(index)) - return true + return Some(bindingIndex) - false + None } - def contains(index: DiscreteIndex) : Boolean = { + def getBindingIndex(index: DiscreteIndex) : Option[BindingIndex] = { for((bindingIndex, discreteIndices) <- discrete; if discreteIndices.contains(index)) - return true + return Some(bindingIndex) - false + None } + + def getBindingIndices = spatial.keys ++ discrete.keys } diff --git a/src/ofc/generators/onetep/TreeBuilder.scala b/src/ofc/generators/onetep/TreeBuilder.scala index 4d4723b..4011db1 100644 --- a/src/ofc/generators/onetep/TreeBuilder.scala +++ b/src/ofc/generators/onetep/TreeBuilder.scala @@ -6,6 +6,7 @@ import ofc.{InvalidInputException,UnimplementedException} case class BindingIndex(name : String) { override def toString() = name + def getName = name } class Dictionary {