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]
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
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
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
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
# 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)
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]