examples: intro a README file to emscripten examples
[nit.git] / src / coloring.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 module coloring
16
17 import poset
18
19 # Build a conflict graph from a POSet
20 class POSetConflictGraph[E: Object]
21
22 # Core is composed by:
23 # * elements that have mutiple direct parents
24 # * parents of elements that have multiple direct parents
25 # REQUIRE: is_colored
26 var core = new HashSet[E]
27
28 # Border is composed by minimal elements of the core:
29 # * that have multiple direct parents
30 # * but whose subelements are all in single inheritance
31 # REQUIRE: is_colored
32 var border = new HashSet[E]
33
34 # The crown is composed by the elements that are:
35 # * not part of the core nor the border
36 # * are in single inheritance
37 # REQUIRE: is_colored
38 var crown = new HashSet[E]
39
40 # Conflict graph of the POSet
41 # Elements X and Y are in conflict if either:
42 # * X and Y are the same element
43 # * Y is a subelement of X
44 # * X and Y have common sub elements
45 # REQUIRE: is_colored
46 var conflicts = new HashMap[E, Set[E]]
47
48 var poset: POSet[E]
49
50 init(poset: POSet[E]) do
51 self.poset = poset
52 extract_core
53 extract_border
54 extract_crown
55 compute_conflicts
56 end
57
58 # Compute the set of elements forming the core of the poset hierarchy.
59 private fun extract_core do
60 core.clear
61 for e in poset do
62 if poset[e].direct_greaters.length > 1 then
63 core.add_all(poset[e].greaters)
64 end
65 end
66 end
67
68 # Compute the set of elements composing the border of the core
69 # Elements belonging to the `border` are removed from the `core`
70 private fun extract_border do
71 border.clear
72 for e in core do
73 if not is_border(e) then continue
74 border.add(e)
75 end
76 for e in border do core.remove(e)
77 end
78
79 private fun is_border(e: E): Bool do
80 for child in poset[e].direct_smallers do
81 if core.has(child) then return false
82 end
83 return true
84 end
85
86 # Compute the set of elements belonging to the crown of the inheritance hierarchy.
87 private fun extract_crown do
88 crown.clear
89 for e in poset do
90 if not core.has(e) and not border.has(e) then crown.add(e)
91 end
92 end
93
94 # Check for conflict in the core.
95 # Start from border and tag every ancestors
96 private fun compute_conflicts do
97 conflicts.clear
98 for e in border do add_conflicts(poset[e].greaters)
99 end
100
101 private fun add_conflict(e, o: E) do
102 if not conflicts.has_key(e) then conflicts[e] = new HashSet[E]
103 if not conflicts.has_key(o) then conflicts[o] = new HashSet[E]
104 conflicts[e].add(o)
105 conflicts[o].add(e)
106 end
107
108 private fun add_conflicts(es: Collection[E]) do
109 for e1 in es do
110 for e2 in es do add_conflict(e1, e2)
111 end
112 end
113
114 # Used for debugging only
115 fun pretty_print do
116 #print "core: {core.join(" ")} ({core.length})"
117 #print "border: {border.join(" ")} ({border.length})"
118 #print "crown: {crown.join(" ")} ({crown.length})"
119 print "conflicts:"
120 for e, c in conflicts do print " {e}: {c.join(" ")}"
121 end
122 end
123
124 # Colorize elements from a POSet
125 # Two elements from a POSet cannot have the same color if they share common subelements
126 #
127 # Example:
128 # A
129 # / | \
130 # / | \
131 # B C D
132 # | /| |
133 # | / | |
134 # |/ | |
135 # E F G
136 # |
137 # H
138 # Conflicts:
139 # A: {B, C, D, E, F, G, H}
140 # B: {A, C, E, H}
141 # C: {A, E, H, F}
142 # D: {A, G}
143 # E: {A, B, C, H}
144 # F: {A, C}
145 # G: {A, D}
146 # H: {A, B, C, E}
147 # Possible colors:
148 # A:0, B:1, C: 2, D: 1, E: 3, F:3, G:2, H:4
149 #
150 # see:
151 # Ducournau, R. (2011).
152 # Coloring, a versatile technique for implementing object-oriented languages.
153 # Software: Practice and Experience, 41(6), 627–659.
154 class POSetColorer[E: Object]
155
156 # Is the poset already colored?
157 var is_colored = false
158
159 # Resulting ids
160 # REQUIRE: is_colored
161 fun ids: Map[E, Int] do
162 assert is_colored
163 return ids_cache
164 end
165 private var ids_cache = new HashMap[E, Int]
166
167 # Resulting colors
168 # REQUIRE: is_colored
169 fun colors: Map[E, Int] do
170 assert is_colored
171 return colors_cache
172 end
173 private var colors_cache = new HashMap[E, Int]
174
175 # REQUIRE: is_colored
176 fun poset: POSet[E] do
177 assert is_colored
178 return poset_cache
179 end
180 private var poset_cache: POSet[E]
181
182 # REQUIRE: is_colored
183 fun conflicts: Map[E, Set[E]] do
184 assert is_colored
185 return conflicts_cache
186 end
187 private var conflicts_cache: Map[E, Set[E]]
188
189 private var graph: POSetConflictGraph[E]
190
191 init do end
192
193 # Start coloring on given POSet
194 fun colorize(poset: POSet[E]) do
195 poset_cache = poset
196 graph = new POSetConflictGraph[E](poset)
197 allocate_ids
198 compute_colors
199 conflicts_cache = graph.conflicts
200 is_colored = true
201 end
202
203 private fun allocate_ids do
204 ids_cache.clear
205 var elements = new HashSet[E].from(poset_cache.to_a)
206 for e in poset_cache.linearize(elements) do
207 ids_cache[e] = ids_cache.length
208 end
209 end
210
211 # Colorize core, border and crown in that order
212 private fun compute_colors do
213 colors_cache.clear
214 colorize_core
215 colorize_set(graph.border)
216 colorize_set(graph.crown)
217 end
218
219 # Core elements cannot have the same color than:
220 # * one of their parents
221 # * one of their conflicting elements
222 private fun colorize_core do
223 for e in poset_cache.linearize(graph.core) do
224 var color = min_color(e)
225 var conflicts = graph.conflicts[e]
226 while not is_color_free(color, conflicts) do
227 color += 1
228 end
229 colors_cache[e] = color
230 end
231 end
232
233 # Other elements inherit color fron their direct parents
234 private fun colorize_set(set: Set[E]) do
235 for e in poset_cache.linearize(set) do colors_cache[e] = min_color(e)
236 end
237
238 # Get the next minimal color from direct parents
239 private fun min_color(e: E): Int do
240 var max_color = -1
241 for p in poset_cache[e].direct_greaters do
242 if not colors_cache.has_key(p) then continue
243 var color = colors_cache[p]
244 if color > max_color then max_color = color
245 end
246 return max_color + 1
247 end
248
249 private fun is_color_free(color: Int, set: Collection[E]): Bool do
250 for e in set do
251 if colors_cache.has_key(e) and colors_cache[e] == color then return false
252 end
253 return true
254 end
255
256 # Used for debugging only
257 fun pretty_print do
258 print "ids:"
259 for e, id in ids do print " {e}: {id}"
260 print "colors:"
261 for e, c in colors do print " {e}: {c}"
262 end
263 end
264
265 # Colorize a collection of buckets
266 # Two elements cannot have the same color if they both appear in the same bucket
267 # No coloring order is garantied
268 #
269 # Example:
270 # buckets[A] = {x1, x2}
271 # buckets[B] = {x1, x3, x4}
272 # buckets[C] = {x2, x3}
273 # Conflicts:
274 # x1: {x2, x3, x4}
275 # x2: {x1, x3}
276 # x3: {x1, x2, x4}
277 # x4: {x1, x3}
278 # Possible colors:
279 # x1: 0, x2: 1, x3: 2, x4: 1
280 class BucketsColorer[H: Object, E: Object]
281 private var colors = new HashMap[E, Int]
282 private var conflicts = new HashMap[E, Set[E]]
283
284 init do end
285
286 # Start bucket coloring
287 fun colorize(buckets: Map[H, Set[E]]): Map[E, Int] do
288 compute_conflicts(buckets)
289 var min_color = 0
290 for holder, hbuckets in buckets do
291 for bucket in hbuckets do
292 if colors.has_key(bucket) then continue
293 var color = min_color
294 while not is_color_free(bucket, color) do
295 color += 1
296 end
297 colors[bucket] = color
298 color = min_color
299 end
300 end
301 return colors
302 end
303
304 private fun is_color_free(bucket: E, color: Int): Bool do
305 if conflicts.has_key(bucket) then
306 for other in conflicts[bucket] do
307 if colors.has_key(other) and colors[other] == color then return false
308 end
309 end
310 return true
311 end
312
313 private fun compute_conflicts(buckets: Map[H, Set[E]]) do
314 conflicts.clear
315 for holder, hbuckets in buckets do
316 for bucket in hbuckets do
317 if not conflicts.has_key(bucket) then conflicts[bucket] = new HashSet[E]
318 for obucket in hbuckets do
319 if obucket == bucket then continue
320 if not conflicts.has_key(obucket) then conflicts[obucket] = new HashSet[E]
321 conflicts[bucket].add(obucket)
322 conflicts[obucket].add(bucket)
323 end
324 end
325 end
326 end
327 end
328
329 # Colorize a collection of buckets using a poset and a conflict graph
330 # Two elements cannot have the same color if they both appear in the same bucket
331 # The use of a POSet hierarchy optimize the coloring
332 # Buckets elements are colored using linearization order starting
333 class POSetBucketsColorer[H: Object, E: Object]
334 private var colors = new HashMap[E, Int]
335 private var poset: POSet[H]
336 private var conflicts: Map[H, Set[H]]
337
338 init(poset: POSet[H], conflicts: Map[H, Set[H]]) do
339 self.poset = poset
340 self.conflicts = conflicts
341 end
342
343 # Colorize buckets using the POSet and conflict graph
344 fun colorize(buckets: Map[H, Set[E]]): Map[E, Int] do
345 colors.clear
346 for h in poset.linearize(buckets.keys) do
347 var color = min_color(poset[h].direct_greaters, buckets)
348 for bucket in buckets[h] do
349 if colors.has_key(bucket) then continue
350 while not is_color_free(color, h, buckets) do color += 1
351 colors[bucket] = color
352 color += 1
353 end
354 end
355 return colors
356 end
357
358 # Get the next available color considering used colors by other buckets
359 private fun min_color(others: Collection[H], buckets: Map[H, Set[E]]): Int do
360 var min = -1
361 for holder in others do
362 var color = max_color(holder, buckets)
363 if color > min then min = color
364 end
365 return min + 1
366 end
367
368 # Return the max color used by a class
369 private fun max_color(holder: H, buckets: Map[H, Set[E]]): Int do
370 var max = -1
371 for bucket in buckets[holder] do
372 if not colors.has_key(bucket) then continue
373 var color = colors[bucket]
374 if color > max then max = color
375 end
376 return max
377 end
378
379 # Check if the color is free for this holder
380 private fun is_color_free(color: Int, holder: H, buckets: Map[H, Set[E]]): Bool do
381 if not conflicts.has_key(holder) then return true
382 for conflict in conflicts[holder] do
383 for bucket in buckets[conflict] do
384 if not colors.has_key(bucket) then continue
385 if colors[bucket] == color then return false
386 end
387 end
388 return true
389 end
390 end
391
392