compiling: moves up the compiling_writer module to be used by the new FFI
[nit.git] / src / coloring.nit
index a0dcae8..57ca211 100644 (file)
@@ -1315,11 +1315,184 @@ class NaiveLiveEntryColoring
        end
 end
 
+# live unanchored coloring
+class UnanchoredTypeColoring
+
+       private var coloration_result: Map[MType, Int] = new HashMap[MType, Int]
+       private var conflicts_graph: Map[MType, Set[MType]] = new HashMap[MType, Set[MType]]
+
+       init do end
+
+       fun colorize(elements: Map[MClassType, Set[MType]]): Map[MType, Int] do
+               build_conflicts_graph(elements)
+               colorize_elements(elements)
+               return coloration_result
+       end
+
+       fun build_tables(elements: Map[MClassType, Set[MType]]): Map[MClassType, Array[nullable MType]] do
+               var tables = new HashMap[MClassType, Array[nullable MType]]
+
+               for mclasstype, mtypes in elements do
+                       var table = new Array[nullable MType]
+                       for mtype in mtypes do
+                               var color = self.coloration_result[mtype]
+                               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[MType]]) do
+               var min_color = 0
+               for mclasstype, mclasstypes in elements do
+                       for element in mclasstypes do
+                               if self.coloration_result.has_key(element) then continue
+                               var color = min_color
+                               while not self.is_color_free(element, color) do
+                                       color += 1
+                               end
+                               coloration_result[element] = 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: MType, 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[MType]]) 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, otype)
+                               end
+                       end
+               end
+       end
+
+       private fun add_conflict(mtype: MType, otype: MType) do
+               if mtype == otype then return
+               if not self.conflicts_graph.has_key(mtype) then  self.conflicts_graph[mtype] = new HashSet[MType]
+               self.conflicts_graph[mtype].add(otype)
+               if not self.conflicts_graph.has_key(otype) then  self.conflicts_graph[otype] = new HashSet[MType]
+               self.conflicts_graph[otype].add(mtype)
+       end
+end
+
+class NaiveUnanchoredTypeColoring
+       super UnanchoredTypeColoring
+
+       init do end
+
+       redef fun colorize_elements(elements: Map[MClassType, Set[MType]]) do
+               var color = 0
+               for mclasstype, mclasstypes in elements do
+                       for element in mclasstypes do
+                               coloration_result[element] = 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[MType]]) do
+               var color = 1
+               for mclasstype, mclasstypes in elements do
+                       for element in mclasstypes do
+                               coloration_result[element] = color
+                               color += 1
+                       end
+               end
+       end
+
+       fun compute_masks(elements: Map[MClassType, Set[MType]]): 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[MType]): Int do
+               var mask = 0
+               loop
+                       var used = new List[Int]
+                       for mtype in mtypes do
+                               var res = op(mask, self.coloration_result[mtype])
+                               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[MType]]): Map[MClassType, Array[nullable MType]] do
+               var tables = new HashMap[MClassType, Array[nullable MType]]
+
+               for mclasstype, mtypes in elements do
+                       var table = new Array[nullable MType]
+                       for mtype in mtypes do
+                               var color = phash(self.coloration_result[mtype], 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
 
 # An ordered set
-class OrderedSet[E]
+class OrderedSet[E: Object]
        super Array[E]
 
        init do end
@@ -1345,4 +1518,4 @@ class OrderedSet[E]
        fun linearize(sorter: AbstractSorter[E]) do
                sorter.sort(self)
        end
-end
\ No newline at end of file
+end