nitg-s: replaced NaiveTypeColoring by BMTypeLayoutBuilder
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 7 Feb 2013 17:36:11 +0000 (12:36 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Mon, 4 Mar 2013 18:20:00 +0000 (13:20 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/coloring.nit
src/separate_compiler.nit

index e43e26e..3f94e87 100644 (file)
@@ -18,6 +18,40 @@ module coloring
 import typing
 
 abstract class TypeLayoutBuilder
+       private var mmodule: MModule
+       init(mmodule: MModule) do self.mmodule = mmodule
+end
+
+class TypeLayout
+       # Unic ids or each Mtype
+       var ids: Map[MType, Int] = new HashMap[MType, Int]
+       # Fixed positions of each MType in all tables
+       var pos: Map[MType, Int] = new HashMap[MType, Int]
+end
+
+# Layout builder for MType using Binary Matrix (BM)
+class BMTypeLayoutBuilder
+       super TypeLayoutBuilder
+
+       init(mmodule: MModule) do super
+
+       # Compute mtypes ids and position using BM
+       fun build_layout(mtypes: Set[MType]): TypeLayout do
+               var result = new TypeLayout
+               result.ids = self.compute_ids(mtypes)
+               result.pos = result.ids
+               return result
+       end
+
+       # Give each MType a unic id using a descending linearization of the `mtypes` set
+       private fun compute_ids(mtypes: Set[MType]): Map[MType, Int] do
+               var ids = new HashMap[MType, Int]
+               var lin = self.mmodule.reverse_linearize_mtypes(mtypes)
+               for mtype in lin do
+                       ids[mtype] = ids.length
+               end
+               return ids
+       end
 end
 
 abstract class AbstractColoring[E: Object]
@@ -157,9 +191,7 @@ class TypeColoring
 
        type T: MType
 
-       private var mmodule: MModule
-
-       init(mainmodule: MModule) do self.mmodule = mainmodule
+       init(mmodule: MModule) do self.mmodule = mmodule
 
        redef fun super_elements(element, elements) do return self.mmodule.super_mtypes(element, elements)
        redef fun is_element_mi(element, elements) do return self.super_elements(element, elements).length > 1
@@ -168,19 +200,6 @@ class TypeColoring
        redef fun reverse_linearize(elements) do return self.mmodule.reverse_linearize_mtypes(elements)
 end
 
-class NaiveTypeColoring
-       super TypeColoring
-
-       init(mainmodule: MModule) do super
-
-       # naive coloring that use incremental coloring
-       redef fun colorize_elements(elements) do
-               for e in elements do
-                       self.coloration_result[e] = self.coloration_result.length
-               end
-       end
-end
-
 abstract class TypePerfectHashing
        super TypeColoring
 
index c304845..aaff5a7 100644 (file)
@@ -106,7 +106,7 @@ class SeparateCompiler
 
        private var undead_types: Set[MType] = new HashSet[MType]
        private var partial_types: Set[MType] = new HashSet[MType]
-       protected var typeids: HashMap[MType, Int] protected writable = new HashMap[MType, Int]
+       protected var typeids: Map[MType, Int] protected writable = new HashMap[MType, Int]
 
        private var type_layout_builder: TypeLayoutBuilder
        private var type_colors: Map[MType, Int] = typeids
@@ -146,7 +146,7 @@ class SeparateCompiler
        protected fun init_layout_builders do
                # Typing Layout
                if modelbuilder.toolcontext.opt_bm_typing.value then
-                       self.type_layout_builder = new NaiveTypeColoring(self.mainmodule)
+                       self.type_layout_builder = new BMTypeLayoutBuilder(self.mainmodule)
                else if modelbuilder.toolcontext.opt_phmod_typing.value then
                        self.type_layout_builder = new TypeModPerfectHashing(self.mainmodule)
                else if modelbuilder.toolcontext.opt_phand_typing.value then
@@ -316,8 +316,10 @@ class SeparateCompiler
 
                # colorize types
                var type_coloring = self.type_layout_builder
-               if type_coloring isa NaiveTypeColoring then
-                       self.type_colors = type_coloring.colorize(mtypes)
+               if type_coloring isa BMTypeLayoutBuilder then
+                       var result = type_coloring.build_layout(mtypes)
+                       self.typeids = result.ids
+                       self.type_colors = result.pos
                        self.type_tables = self.build_type_tables(mtypes, type_colors, type_coloring)
                else if type_coloring isa TypeModPerfectHashing then
                        self.type_colors = type_coloring.compute_masks(mtypes, typeids)
@@ -337,13 +339,13 @@ class SeparateCompiler
        end
 
        # Build type tables
-       fun build_type_tables(mtypes: Set[MType], colors: Map[MType, Int], colorer: TypeColoring): Map[MType, Array[nullable MType]] do
+       fun build_type_tables(mtypes: Set[MType], colors: Map[MType, Int], colorer: TypeLayoutBuilder): Map[MType, Array[nullable MType]] do
                var tables = new HashMap[MType, Array[nullable MType]]
 
                for mtype in mtypes do
                        var table = new Array[nullable MType]
                        var supers = new HashSet[MType]
-                       supers.add_all(colorer.super_elements(mtype, mtypes))
+                       supers.add_all(colorer.mmodule.super_mtypes(mtype, mtypes))
                        supers.add(mtype)
                        for sup in supers do
                                var color = colors[sup]