Two elements cannot have the same color if they both appear in the same bucket No coloring order is garantied
Example:
Conflicts:
Possible colors:
nitc :: BucketsColorer :: _conflicts
nitc :: BucketsColorer :: conflicts=
nitc :: BucketsColorer :: defaultinit
nitc :: BucketsColorer :: is_color_free
nitc $ BucketsColorer :: SELF
Type of this instance, automatically specialized in every classnitc :: BucketsColorer :: _conflicts
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
nitc :: BucketsColorer :: conflicts=
nitc :: BucketsColorer :: defaultinit
core :: Object :: defaultinit
nitc :: BucketsColorer :: is_color_free
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: native_class_name
The class name of the object in CString format.core :: Object :: output_class_name
Display class name on stdout (debug only).
# Colorize a collection of buckets
# Two elements cannot have the same color if they both appear in the same bucket
# No coloring order is garantied
#
# Example:
#
# * buckets[A] = {x1, x2}
# * buckets[B] = {x1, x3, x4}
# * buckets[C] = {x2, x3}
#
# Conflicts:
#
# * x1: {x2, x3, x4}
# * x2: {x1, x3}
# * x3: {x1, x2, x4}
# * x4: {x1, x3}
#
# Possible colors:
#
# * x1: 0, x2: 1, x3: 2, x4: 1
class BucketsColorer[H: Object, E: Object]
private var colors = new HashMap[E, Int]
private var conflicts = new HashMap[E, Set[E]]
# Start bucket coloring
fun colorize(buckets: Map[H, Set[E]]): Map[E, Int] do
compute_conflicts(buckets)
var min_color = 0
for holder, hbuckets in buckets do
for bucket in hbuckets do
if colors.has_key(bucket) then continue
var color = min_color
while not is_color_free(bucket, color) do
color += 1
end
colors[bucket] = color
color = min_color
end
end
return colors
end
private fun is_color_free(bucket: E, color: Int): Bool do
if conflicts.has_key(bucket) then
for other in conflicts[bucket] do
if colors.has_key(other) and colors[other] == color then return false
end
end
return true
end
private fun compute_conflicts(buckets: Map[H, Set[E]]) do
conflicts.clear
for holder, hbuckets in buckets do
for bucket in hbuckets do
if not conflicts.has_key(bucket) then conflicts[bucket] = new HashSet[E]
for obucket in hbuckets do
if obucket == bucket then continue
if not conflicts.has_key(obucket) then conflicts[obucket] = new HashSet[E]
conflicts[bucket].add(obucket)
conflicts[obucket].add(bucket)
end
end
end
end
end
src/compiler/coloring.nit:447,1--512,3