- return tables
- end
-
- private fun compute_mask(mtypes: Set[T], ids: Map[T, Int]): Int do
- var mask = 0
- loop
- var used = new List[Int]
- for sup in mtypes do
- var res = op(mask, ids[sup])
- if used.has(res) then
- break
- else
- used.add(res)
- end
- end
- if used.length == mtypes.length then break
- mask += 1
- end
- return mask
- end
-
- private fun op(mask: Int, id:Int): Int is abstract
- private fun phash(id: Int, mask: Int): Int do return op(mask, id)
-end
-
-class ClassModPerfectHashing
- super ClassPerfectHashing
- init(mainmodule: MModule) do
- super(mainmodule)
- end
- redef fun op(mask, id) do return mask % id
-end
-
-class ClassAndPerfectHashing
- super ClassPerfectHashing
- init(mainmodule: MModule) do
- super(mainmodule)
- end
- redef fun op(mask, id) do return mask.bin_and(id)
-end
-
-# MProperty coloring
-class PropertyColoring
-
- type MPROP: MProperty
- type MPROPDEF: MPropDef
-
- private var class_coloring: ClassColoring
- private var coloration_result: Map[MPROP, Int] = new HashMap[MPROP, Int]
-
- init(class_coloring: ClassColoring) do
- self.class_coloring = class_coloring
- end
-
- fun colorize: Map[MPROP, Int] do
- colorize_core_properties
- colorize_crown_properties
- return self.coloration_result
- end
-
- fun build_property_tables: Map[MClass, Array[nullable MPROPDEF]] do
- var tables = new HashMap[MClass, Array[nullable MPROPDEF]]
-
- for mclass in self.class_coloring.coloration_result.keys do
- var table = new Array[nullable MPROPDEF]
- # first, fill table from parents by reverse linearization order
- var parents = self.class_coloring.super_elements(mclass)
- var lin = self.class_coloring.reverse_linearize(parents)
- for parent in lin do
- for mproperty in self.properties(parent) do
- var color = self.coloration_result[mproperty]
- if table.length <= color then
- for i in [table.length .. color[ do
- table[i] = null
- end
- end
- for mpropdef in mproperty.mpropdefs do
- if mpropdef.mclassdef.mclass == parent then
- table[color] = mpropdef
- end
- end
- end
- end
-
- # then override with local properties
- for mproperty in self.properties(mclass) do
- var color = self.coloration_result[mproperty]
- if table.length <= color then
- for i in [table.length .. color[ do
- table[i] = null
- end
- end
- for mpropdef in mproperty.mpropdefs do
- if mpropdef.mclassdef.mclass == mclass then
- table[color] = mpropdef
- end
- end
- end
- tables[mclass] = table
- end
- return tables
- end
-
- # Colorize properties of the core hierarchy
- private fun colorize_core_properties do
- var mclasses = self.class_coloring.core
- var min_color = 0
-
- for mclass in mclasses do
- var color = min_color
-
- # if the class is root, get the minimal color
- if self.class_coloring.parent_elements(mclass).length == 0 then
- colorize_elements(self.properties(mclass), color)
- else
- # check last color used by parents
- color = max_color(color, self.class_coloring.parent_elements(mclass))
- # check max color used in conflicts
- if self.class_coloring.conflicts_graph.has_key(mclass) then
- color = max_color(color, self.class_coloring.conflicts_graph[mclass])
- end
-
- # colorize
- colorize_elements(self.properties(mclass), color)
- end
- end
- end
-
- # Colorize properties of the crown hierarchy
- private fun colorize_crown_properties do
- for mclass in self.class_coloring.crown do
- colorize_elements(self.properties(mclass), max_color(0, self.class_coloring.parent_elements(mclass)))
- end
- end
-
- # Colorize a collection of properties given a starting color
- private fun colorize_elements(elements: Collection[MPROP], start_color: Int) do
- for element in elements do
- if self.coloration_result.has_key(element) then continue
- self.coloration_result[element] = start_color
- start_color += 1
- end
- end
-
- private fun max_color(min_color: Int, mclasses: Collection[MClass]): Int do
- var max_color = min_color
-
- for mclass in mclasses do
- for mproperty in self.properties(mclass) do
- var color = min_color
- if self.coloration_result.has_key(mproperty) then
- color = self.coloration_result[mproperty]
- if color >= max_color then max_color = color + 1
- end
- end
- end
- return max_color
- end
-
- # properties cache
- private var properties_cache: Map[MClass, Set[MPROP]] = new HashMap[MClass, Set[MPROP]]
-
- # All 'mproperties' associated to all 'mclassdefs' of the class
- private fun properties(mclass: MClass): Set[MPROP] do
- if not self.properties_cache.has_key(mclass) then
- var properties = new HashSet[MPROP]
- var parents = self.class_coloring.super_elements(mclass)
- for parent in parents do
- properties.add_all(self.properties(parent))
- end
-
- for mclassdef in mclass.mclassdefs do
- for mpropdef in mclassdef.mpropdefs do
- var mproperty = mpropdef.mproperty
- if mproperty isa MPROP then
- properties.add(mproperty)
- end
- end
- end
- self.properties_cache[mclass] = properties
- end
- return properties_cache[mclass]