Colorize elements E introduced by holders H in a POSet.

Two elements cannot have the same color if they are introduced or inherited by a same holder.

Introduced properties

private var _buckets: Map[H, Collection[E]]

nitc :: POSetGroupColorer :: _buckets

The elements to color.
private var _colors: Map[E, Int]

nitc :: POSetGroupColorer :: _colors

The resulting coloring
private var _colors_cache: HashMap[E, Int]

nitc :: POSetGroupColorer :: _colors_cache

Resulting colors
private var _graph: POSetConflictGraph[H]

nitc :: POSetGroupColorer :: _graph

The associated conflict graph containing the poset.
private var _min_colors: HashMap[H, Int]

nitc :: POSetGroupColorer :: _min_colors

The first available color for each holder.
private var _used_colors: HashMap[H, HashSet[Int]]

nitc :: POSetGroupColorer :: _used_colors

Set of known used colors
fun buckets: Map[H, Collection[E]]

nitc :: POSetGroupColorer :: buckets

The elements to color.
protected fun buckets=(buckets: Map[H, Collection[E]])

nitc :: POSetGroupColorer :: buckets=

The elements to color.
fun build_layout(h: H): Array[nullable E]

nitc :: POSetGroupColorer :: build_layout

Build table layout of elements E for the holder h.
private fun colorize_core

nitc :: POSetGroupColorer :: colorize_core

Core elements cannot have the same color than:
private fun colorize_set(set: Set[H])

nitc :: POSetGroupColorer :: colorize_set

Other elements inherit color from their direct parents
fun colors: Map[E, Int]

nitc :: POSetGroupColorer :: colors

The resulting coloring
protected fun colors=(colors: Map[E, Int])

nitc :: POSetGroupColorer :: colors=

The resulting coloring
private fun colors_cache: HashMap[E, Int]

nitc :: POSetGroupColorer :: colors_cache

Resulting colors
private fun colors_cache=(colors_cache: HashMap[E, Int])

nitc :: POSetGroupColorer :: colors_cache=

Resulting colors
private fun compute_colors

nitc :: POSetGroupColorer :: compute_colors

Colorize core, border and crown in that order
fun graph: POSetConflictGraph[H]

nitc :: POSetGroupColorer :: graph

The associated conflict graph containing the poset.
protected fun graph=(graph: POSetConflictGraph[H])

nitc :: POSetGroupColorer :: graph=

The associated conflict graph containing the poset.
private fun inherit_color(h: H): Int

nitc :: POSetGroupColorer :: inherit_color

Get the first available free color.
private fun min_colors: HashMap[H, Int]

nitc :: POSetGroupColorer :: min_colors

The first available color for each holder.
private fun min_colors=(min_colors: HashMap[H, Int])

nitc :: POSetGroupColorer :: min_colors=

The first available color for each holder.
fun poset: POSet[H]

nitc :: POSetGroupColorer :: poset

The associated poset.
fun pretty_print

nitc :: POSetGroupColorer :: pretty_print

Used for debugging only
private fun used_colors: HashMap[H, HashSet[Int]]

nitc :: POSetGroupColorer :: used_colors

Set of known used colors
private fun used_colors=(used_colors: HashMap[H, HashSet[Int]])

nitc :: POSetGroupColorer :: used_colors=

Set of known used colors

Redefined properties

redef type SELF: POSetGroupColorer[H, E]

nitc $ POSetGroupColorer :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _buckets: Map[H, Collection[E]]

nitc :: POSetGroupColorer :: _buckets

The elements to color.
private var _colors: Map[E, Int]

nitc :: POSetGroupColorer :: _colors

The resulting coloring
private var _colors_cache: HashMap[E, Int]

nitc :: POSetGroupColorer :: _colors_cache

Resulting colors
private var _graph: POSetConflictGraph[H]

nitc :: POSetGroupColorer :: _graph

The associated conflict graph containing the poset.
private var _min_colors: HashMap[H, Int]

nitc :: POSetGroupColorer :: _min_colors

The first available color for each holder.
private var _used_colors: HashMap[H, HashSet[Int]]

nitc :: POSetGroupColorer :: _used_colors

Set of known used colors
fun buckets: Map[H, Collection[E]]

nitc :: POSetGroupColorer :: buckets

The elements to color.
protected fun buckets=(buckets: Map[H, Collection[E]])

nitc :: POSetGroupColorer :: buckets=

The elements to color.
fun build_layout(h: H): Array[nullable E]

nitc :: POSetGroupColorer :: build_layout

Build table layout of elements E for the holder h.
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
private fun colorize_core

nitc :: POSetGroupColorer :: colorize_core

Core elements cannot have the same color than:
private fun colorize_set(set: Set[H])

nitc :: POSetGroupColorer :: colorize_set

Other elements inherit color from their direct parents
fun colors: Map[E, Int]

nitc :: POSetGroupColorer :: colors

The resulting coloring
protected fun colors=(colors: Map[E, Int])

nitc :: POSetGroupColorer :: colors=

The resulting coloring
private fun colors_cache: HashMap[E, Int]

nitc :: POSetGroupColorer :: colors_cache

Resulting colors
private fun colors_cache=(colors_cache: HashMap[E, Int])

nitc :: POSetGroupColorer :: colors_cache=

Resulting colors
private fun compute_colors

nitc :: POSetGroupColorer :: compute_colors

Colorize core, border and crown in that order
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun graph: POSetConflictGraph[H]

nitc :: POSetGroupColorer :: graph

The associated conflict graph containing the poset.
protected fun graph=(graph: POSetConflictGraph[H])

nitc :: POSetGroupColorer :: graph=

The associated conflict graph containing the poset.
fun hash: Int

core :: Object :: hash

The hash code of the object.
private fun inherit_color(h: H): Int

nitc :: POSetGroupColorer :: inherit_color

Get the first available free color.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
private fun min_colors: HashMap[H, Int]

nitc :: POSetGroupColorer :: min_colors

The first available color for each holder.
private fun min_colors=(min_colors: HashMap[H, Int])

nitc :: POSetGroupColorer :: min_colors=

The first available color for each holder.
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun poset: POSet[H]

nitc :: POSetGroupColorer :: poset

The associated poset.
fun pretty_print

nitc :: POSetGroupColorer :: pretty_print

Used for debugging only
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
private fun used_colors: HashMap[H, HashSet[Int]]

nitc :: POSetGroupColorer :: used_colors

Set of known used colors
private fun used_colors=(used_colors: HashMap[H, HashSet[Int]])

nitc :: POSetGroupColorer :: used_colors=

Set of known used colors
package_diagram nitc::POSetGroupColorer POSetGroupColorer core::Object Object nitc::POSetGroupColorer->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

nitc $ POSetGroupColorer
# Colorize elements `E` introduced by holders `H` in a `POSet`.
#
# Two elements cannot have the same color if they are introduced or inherited by a same holder.
class POSetGroupColorer[H: Object, E: Object]

	# The associated conflict graph containing the poset.
	#
	# The conflict graph is used instead of the original poset so that the conflict graph can be reused
	# in different coloration based on the same poset.
	var graph: POSetConflictGraph[H]

	# The elements to color.
	#
	# For each holder, the collection of introduced elements is given.
	#
	# A single element must not be introduced in more than one holder.
	var buckets: Map[H, Collection[E]]

	# The associated poset.
	#
	# alias for `graph.poset`
	fun poset: POSet[H] do return graph.poset

	# The resulting coloring
	#
	# Each element from buckets is associated to its own color
	var colors: Map[E, Int] is lazy do
		for h in graph.poset do
			used_colors[h] = new HashSet[Int]
		end
		compute_colors
		return colors_cache
	end

	# Resulting colors
	private var colors_cache = new HashMap[E, Int]

	# Set of known used colors
	private var used_colors = new HashMap[H, HashSet[Int]]

	# Build table layout of elements `E` for the holder `h`.
	#
	# `null` is used to fill places without elements (holes).
	fun build_layout(h: H): Array[nullable E]
	do
		var table = new Array[nullable E]
		for s in poset[h].greaters do
			var bucket = buckets.get_or_null(s)
			if bucket == null then continue
			for e in bucket do
				var color = colors[e]
				if table.length <= color then
					for i in [table.length .. color[ do
						table[i] = null
					end
				else
					assert table[color] == null else print "in {h}, for {color}: {table[color] or else ""} vs {e}"
				end
				table[color] = e
			end
		end
		return table
	end

	# Colorize core, border and crown in that order
	private fun compute_colors do
		colors_cache.clear
		colorize_core
		colorize_set(graph.border)
		colorize_set(graph.crown)
	end

	# Core elements cannot have the same color than:
	#  * one of their parents
	#  * one of their conflicting elements
	private fun colorize_core do
		for h in graph.order do
			if not graph.core.has(h) then continue

			var color = inherit_color(h)
			var mincolor = color
			var bucket = buckets.get_or_null(h)
			if bucket == null then continue
			var conflicts = graph.conflicts[h]
			var parents = poset[h].greaters
			for e in bucket do
				color = next_free_color(color, parents)
				color = next_free_color(color, conflicts)
				colors_cache[e] = color
				used_colors[h].add color
				#print "{h}: color[{color}] <- {e}"
				if mincolor == color then mincolor += 1
				color += 1
			end
			min_colors[h] = mincolor
		end
	end

	# Other elements inherit color from their direct parents
	private fun colorize_set(set: Set[H]) do
		for h in graph.order do
			if not set.has(h) then continue

			var color = inherit_color(h)
			var mincolor = color
			var bucket = buckets.get_or_null(h)
			if bucket == null then continue
			var parents = poset[h].greaters
			for e in bucket do
				color = next_free_color(color, parents)
				colors_cache[e] = color
				used_colors[h].add color
				#print "{h}: color[{color}] <- {e} (set)"
				if mincolor == color then mincolor += 1
				color += 1
			end
			min_colors[h] = mincolor
		end
	end

	# Get the first available free color.
	private fun inherit_color(h: H): Int
	do
		var res = 0
		for p in poset[h].direct_greaters do
			var m = min_colors[p]
			if m > res then res = m
		end
		min_colors[h] = res
		return res
	end

	# The first available color for each holder.
	#
	# Is used by children to start their coloring.
	#
	# Is updated at the end of a coloring step.
	private var min_colors = new HashMap[H, Int]

	private fun next_free_color(color: Int, set: Collection[H]): Int do
		loop
			for h in set do
				if used_colors[h].has(color) then
					#print "\tin {h}, {color} is used"
					color += 1
					continue label
				end
			end
			break
		end label
		return color
	end

	# Used for debugging only
	fun pretty_print do
		print "colors:"
		for e, c in colors do print "  {e}: {c}"
	end
end
src/compiler/coloring.nit:287,1--445,3