]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Work on index binding.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Tue, 17 Apr 2012 13:23:43 +0000 (14:23 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Tue, 17 Apr 2012 13:23:43 +0000 (14:23 +0100)
src/ofc/generators/onetep/CodeGenerator.scala
src/ofc/generators/onetep/IndexBindings.scala
src/ofc/generators/onetep/TreeBuilder.scala

index 0bfb8861dae31b247a946ed0aafef44eac740d9d..bd88eae89d03351b86e460463b37a425c4a83607 100644 (file)
@@ -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)
index c69b0f4e949fa85e066f2bf459267e7f8c87c14c..7cab597596ccafa2e7f80cdafca381f6b47a72b1 100644 (file)
@@ -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
 }
index 4d4723bd02c2d87f91bec43a025c18dec1ff7109..4011db11596935ff1327bd3b2769dc2737df5d7c 100644 (file)
@@ -6,6 +6,7 @@ import ofc.{InvalidInputException,UnimplementedException}
 
 case class BindingIndex(name : String) {
   override def toString()  = name
+  def getName = name
 }
 
 class Dictionary {