end
end
+# live unanchored coloring
+class UnanchoredTypeColoring
+
+ private var coloration_result: Map[MClassType, Int] = new HashMap[MClassType, Int]
+ private var conflicts_graph: Map[MClassType, Set[MClassType]] = new HashMap[MClassType, Set[MClassType]]
+
+ init do end
+
+ fun colorize(elements: Map[MClassType, Set[MClassType]]): Map[MClassType, Int] do
+ build_conflicts_graph(elements)
+ colorize_elements(elements)
+ return coloration_result
+ end
+
+ fun build_tables(elements: Map[MClassType, Set[MClassType]]): Map[MClassType, Array[nullable MClassType]] do
+ var tables = new HashMap[MClassType, Array[nullable MClassType]]
+
+ for mclasstype, mtypes in elements do
+ var table = new Array[nullable MClassType]
+ for mtype in mtypes do
+ var color = self.coloration_result[mtype.mclass.mclass_type]
+ if table.length <= color then
+ for i in [table.length .. color[ do
+ table[i] = null
+ end
+ end
+ table[color] = mtype
+ end
+ tables[mclasstype] = table
+ end
+ return tables
+ end
+
+ # Colorize a collection of elements
+ fun colorize_elements(elements: Map[MClassType, Set[MClassType]]) do
+ var min_color = 0
+ for mclasstype, mclasstypes in elements do
+ for element in mclasstypes do
+ if self.coloration_result.has_key(element.mclass.mclass_type) then continue
+ var color = min_color
+ while not self.is_color_free(element.mclass.mclass_type, color) do
+ color += 1
+ end
+ coloration_result[element.mclass.mclass_type] = color
+ color = min_color
+ end
+ end
+ end
+
+ # Check if a related element to the element already use the color
+ private fun is_color_free(element: MClassType, color: Int): Bool do
+ if conflicts_graph.has_key(element) then
+ for st in conflicts_graph[element] do
+ if coloration_result.has_key(st) and coloration_result[st] == color then return false
+ end
+ end
+ return true
+ end
+
+ # look for unanchored types generated by the same type
+ private fun build_conflicts_graph(elements: Map[MClassType, Set[MClassType]]) do
+ for mclasstype, mtypes in elements do
+ for mtype in mtypes do
+ for otype in mtypes do
+ if otype == mtype then continue
+ self.add_conflict(mtype.mclass.mclass_type, otype.mclass.mclass_type)
+ end
+ end
+ end
+ end
+
+ private fun add_conflict(mtype: MClassType, otype: MClassType) do
+ if mtype == otype then return
+ if not self.conflicts_graph.has_key(mtype) then self.conflicts_graph[mtype] = new HashSet[MClassType]
+ self.conflicts_graph[mtype].add(otype)
+ if not self.conflicts_graph.has_key(otype) then self.conflicts_graph[otype] = new HashSet[MClassType]
+ self.conflicts_graph[otype].add(mtype)
+ end
+end
+
+class NaiveUnanchoredTypeColoring
+ super UnanchoredTypeColoring
+
+ init do end
+
+ redef fun colorize_elements(elements: Map[MClassType, Set[MClassType]]) do
+ var color = 0
+ for mclasstype, mclasstypes in elements do
+ for element in mclasstypes do
+ coloration_result[element.mclass.mclass_type] = color
+ color += 1
+ end
+ end
+ end
+end
+
+abstract class UnanchoredTypePerfectHashing
+ super NaiveUnanchoredTypeColoring
+
+ private var masks: Map[MClassType, Int] = new HashMap[MClassType, Int]
+
+ init do end
+
+ redef fun colorize_elements(elements: Map[MClassType, Set[MClassType]]) do
+ var color = 1
+ for mclasstype, mclasstypes in elements do
+ for element in mclasstypes do
+ coloration_result[element.mclass.mclass_type] = color
+ color += 1
+ end
+ end
+ end
+
+ fun compute_masks(elements: Map[MClassType, Set[MClassType]]): Map[MClassType, Int] do
+ for mclasstype, mtypes in elements do
+ self.masks[mclasstype] = compute_mask(mtypes)
+ end
+ return self.masks
+ end
+
+ private fun compute_mask(mtypes: Set[MClassType]): Int do
+ var mask = 0
+ loop
+ var used = new List[Int]
+ for mtype in mtypes do
+ var res = op(mask, self.coloration_result[mtype.mclass.mclass_type])
+ 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
+
+ redef fun build_tables(elements: Map[MClassType, Set[MClassType]]): Map[MClassType, Array[nullable MClassType]] do
+ var tables = new HashMap[MClassType, Array[nullable MClassType]]
+
+ for mclasstype, mtypes in elements do
+ var table = new Array[nullable MClassType]
+ for mtype in mtypes do
+ var color = phash(self.coloration_result[mtype.mclass.mclass_type], masks[mclasstype])
+ if table.length <= color then
+ for i in [table.length .. color[ do
+ table[i] = null
+ end
+ end
+ table[color] = mtype
+ end
+ tables[mclasstype] = table
+ end
+ return tables
+ 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 UnanchoredTypeModPerfectHashing
+ super UnanchoredTypePerfectHashing
+ init do end
+ redef fun op(mask, id) do return mask % id
+end
+
+class UnanchoredTypeAndPerfectHashing
+ super UnanchoredTypePerfectHashing
+ init do end
+ redef fun op(mask, id) do return mask.bin_and(id)
+end
+
# Utils