1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Graph coloring tools
23 # Unic ids or each element
24 var ids
: Map[E
, Int] = new HashMap[E
, Int]
25 # Fixed positions of each element in all tables
26 var pos
: Map[E
, Int] = new HashMap[E
, Int]
29 class PHTypingLayout[E
]
31 # Masks used by hash function
32 var masks
: Map[E
, Int] = new HashMap[E
, Int]
33 # Positions of each element for each tables
34 var hashes
: Map[E
, Map[E
, Int]] = new HashMap[E
, Map[E
, Int]]
37 class PropertyLayout[E
]
38 # Fixed positions of each element in all tables
39 var pos
: Map[E
, Int] = new HashMap[E
, Int]
42 # Layout for resolution tables
43 class ResolutionLayout
44 # Unic ids for each resolved type
45 var ids
: Map[MType, Int] = new HashMap[MType, Int]
46 # Fixed positions of resolved type
47 var pos
: Map[MType, Int] = new HashMap[MType, Int]
50 class PHResolutionLayout
51 super ResolutionLayout
52 # Masks associated to each owner of a resolution table
53 var masks
: Map[MClassType, Int] = new HashMap[MClassType, Int]
54 # Positions of each resolvec type for resolution tables
55 var hashes
: Map[MClassType, Map[MType, Int]] = new HashMap[MClassType, Map[MType, Int]]
60 abstract class TypingLayoutBuilder[E
]
62 type LAYOUT: TypingLayout[E
]
64 private var mmodule
: MModule
65 init(mmodule
: MModule) do self.mmodule
= mmodule
67 # Compute elements ids and position
68 fun build_layout
(elements
: Set[E
]): LAYOUT is abstract
70 # Give each MType a unic id using a descending linearization of the `mtypes` set
71 private fun compute_ids
(elements
: Set[E
]): Map[E
, Int] do
72 var ids
= new HashMap[E
, Int]
73 var lin
= self.reverse_linearize
(elements
)
75 ids
[element
] = ids
.length
80 private fun reverse_linearize
(elements
: Set[E
]): Array[E
] is abstract
83 # Layout builder for MType using Binary Matrix (BM)
84 class BMTypeLayoutBuilder
85 super TypingLayoutBuilder[MType]
87 init(mmodule
: MModule) do super
89 # Compute mtypes ids and position using BM
90 redef fun build_layout
(mtypes
) do
91 var result
= new TypingLayout[MType]
92 result
.ids
= self.compute_ids
(mtypes
)
93 result
.pos
= result
.ids
97 redef fun reverse_linearize
(elements
) do return self.mmodule
.reverse_linearize_mtypes
(elements
)
100 # Layout builder for MType using Coloring (CL)
101 class CLTypeLayoutBuilder
102 super TypingLayoutBuilder[MType]
104 private var colorer
: MTypeColorer
106 init(mmodule
: MModule) do
108 self.colorer
= new MTypeColorer(mmodule
)
111 # Compute mtypes ids and position using BM
112 redef fun build_layout
(mtypes
) do
113 var result
= new TypingLayout[MType]
114 result
.ids
= self.compute_ids
(mtypes
)
115 result
.pos
= self.colorer
.colorize
(mtypes
)
119 redef fun reverse_linearize
(elements
) do return self.mmodule
.reverse_linearize_mtypes
(elements
)
122 # Layout builder for MType using Perfect Hashing (PH)
123 class PHTypeLayoutBuilder
124 super TypingLayoutBuilder[MType]
126 redef type LAYOUT: PHTypingLayout[MType]
128 private var hasher
: MTypeHasher
130 init(mmodule
: MModule, operator
: PHOperator) do
132 self.hasher
= new MTypeHasher(mmodule
, operator
)
135 # Compute mtypes ids and position using BM
136 redef fun build_layout
(mtypes
) do
137 var result
= new PHTypingLayout[MType]
138 result
.ids
= self.compute_ids
(mtypes
)
139 result
.masks
= self.hasher
.compute_masks
(mtypes
, result
.ids
)
140 result
.hashes
= self.hasher
.compute_hashes
(mtypes
, result
.ids
, result
.masks
)
144 # Ids start from 1 instead of 0
145 redef fun compute_ids
(mtypes
) do
146 var ids
= new HashMap[MType, Int]
147 var lin
= self.mmodule
.reverse_linearize_mtypes
(mtypes
)
149 ids
[mtype
] = ids
.length
+ 1
154 redef fun reverse_linearize
(elements
) do return self.mmodule
.reverse_linearize_mtypes
(elements
)
157 # Layout builder for MClass using Binary Matrix (BM)
158 class BMClassLayoutBuilder
159 super TypingLayoutBuilder[MClass]
161 init(mmodule
: MModule) do super
163 # Compute mclasses ids and position using BM
164 redef fun build_layout
(mclasses
) do
165 var result
= new TypingLayout[MClass]
166 result
.ids
= self.compute_ids
(mclasses
)
167 result
.pos
= result
.ids
171 redef fun reverse_linearize
(elements
) do return self.mmodule
.reverse_linearize_mclasses
(elements
)
174 # Layout builder for MClass using Coloring (CL)
175 class CLClassLayoutBuilder
176 super TypingLayoutBuilder[MClass]
178 private var colorer
: MClassColorer
180 init(mmodule
: MModule) do
182 self.colorer
= new MClassColorer(mmodule
)
185 # Compute mclasses ids and position using BM
186 redef fun build_layout
(mclasses
) do
187 var result
= new TypingLayout[MClass]
188 result
.ids
= self.compute_ids
(mclasses
)
189 result
.pos
= self.colorer
.colorize
(mclasses
)
193 redef fun reverse_linearize
(elements
) do return self.mmodule
.reverse_linearize_mclasses
(elements
)
196 # Layout builder for MClass using Perfect Hashing (PH)
197 class PHClassLayoutBuilder
198 super TypingLayoutBuilder[MClass]
200 redef type LAYOUT: PHTypingLayout[MClass]
202 private var hasher
: MClassHasher
204 init(mmodule
: MModule, operator
: PHOperator) do
206 self.hasher
= new MClassHasher(mmodule
, operator
)
209 # Compute mclasses ids and position using BM
210 redef fun build_layout
(mclasses
) do
211 var result
= new PHTypingLayout[MClass]
212 result
.ids
= self.compute_ids
(mclasses
)
213 result
.masks
= self.hasher
.compute_masks
(mclasses
, result
.ids
)
214 result
.hashes
= self.hasher
.compute_hashes
(mclasses
, result
.ids
, result
.masks
)
218 # Ids start from 1 instead of 0
219 redef fun compute_ids
(mclasses
) do
220 var ids
= new HashMap[MClass, Int]
221 var lin
= self.mmodule
.reverse_linearize_mclasses
(mclasses
)
223 ids
[mclass
] = ids
.length
+ 1
228 redef fun reverse_linearize
(elements
) do return self.mmodule
.reverse_linearize_mclasses
(elements
)
231 abstract class PropertyLayoutBuilder[E
: MProperty]
233 type LAYOUT: PropertyLayout[E
]
235 private var mmodule
: MModule
236 init(mmodule
: MModule) do self.mmodule
= mmodule
238 # Compute properties ids and position
239 fun build_layout
(mclasses
: Set[MClass]): LAYOUT is abstract
242 # Layout builder for MProperty using Coloring (CL)
243 class CLPropertyLayoutBuilder[E
: MProperty]
244 super PropertyLayoutBuilder[E
]
246 private var colorer
: MPropertyColorer[E
]
248 init(mmodule
: MModule) do
250 self.colorer
= new MPropertyColorer[E
](mmodule
)
253 # Compute mclasses ids and position using BM
254 redef fun build_layout
(mclasses
) do
255 var result
= new PropertyLayout[E
]
256 result
.pos
= self.colorer
.colorize
(mclasses
)
261 # Layout builder for MProperty using Perfect Hashing (PH)
262 # TODO implement this class without sublcassing CL builder
263 class PHPropertyLayoutBuilder[E
: MProperty]
264 super CLPropertyLayoutBuilder[E
]
267 abstract class ResolutionLayoutBuilder
269 type LAYOUT: ResolutionLayout
273 fun build_layout
(elements
: Map[MClassType, Set[MType]]): LAYOUT is abstract
275 fun compute_ids
(elements
: Map[MClassType, Set[MType]]): Map[MType, Int] do
276 var ids
= new HashMap[MType, Int]
278 for mclasstype
, mclasstypes
in elements
do
279 for element
in mclasstypes
do
280 if ids
.has_key
(element
) then continue
289 # Layout builder for MClass using Binary Matrix (BM)
290 class BMResolutionLayoutBuilder
291 super ResolutionLayoutBuilder
295 # Compute resolved types position using BM
296 redef fun build_layout
(elements
) do
297 var result
= new ResolutionLayout
298 result
.ids
= self.compute_ids
(elements
)
299 result
.pos
= result
.ids
304 # Layout builder for resolution tables using Coloring (CL)
305 class CLResolutionLayoutBuilder
306 super ResolutionLayoutBuilder
308 private var colorer
: ResolutionColorer = new ResolutionColorer
312 # Compute resolved types colors
313 redef fun build_layout
(elements
) do
314 var result
= new ResolutionLayout
315 result
.ids
= self.compute_ids
(elements
)
316 result
.pos
= self.colorer
.colorize
(elements
)
321 # Layout builder for resolution table using Perfect Hashing (PH)
322 class PHResolutionLayoutBuilder
323 super ResolutionLayoutBuilder
325 redef type LAYOUT: PHResolutionLayout
327 private var hasher
: ResolutionHasher
329 init(operator
: PHOperator) do self.hasher
= new ResolutionHasher(operator
)
331 # Compute resolved types masks and hashes
332 redef fun build_layout
(elements
) do
333 var result
= new PHResolutionLayout
334 result
.ids
= self.compute_ids
(elements
)
335 result
.pos
= result
.ids
336 result
.masks
= self.hasher
.compute_masks
(elements
, result
.ids
)
337 result
.hashes
= self.hasher
.compute_hashes
(elements
, result
.ids
, result
.masks
)
341 redef fun compute_ids
(elements
) do
342 var ids
= new HashMap[MType, Int]
344 for mclasstype
, mclasstypes
in elements
do
345 for element
in mclasstypes
do
346 if ids
.has_key
(element
) then continue
357 abstract class AbstractColorer[E
: Object]
359 private var core
: Set[E
] = new HashSet[E
]
360 private var crown
: Set[E
] = new HashSet[E
]
361 private var border
: Set[E
] = new HashSet[E
]
363 private var coloration_result
: Map[E
, Int] = new HashMap[E
, Int]
367 fun colorize
(elements
: Set[E
]): Map[E
, Int] do
368 tag_elements
(elements
)
369 build_conflicts_graph
(elements
)
370 colorize_elements
(core
)
371 colorize_elements
(border
)
372 colorize_elements
(crown
)
373 return coloration_result
376 # Colorize a collection of elements
377 private fun colorize_elements
(elements
: Set[E
]) do
380 var lin
= reverse_linearize
(elements
)
381 for element
in lin
do
382 var color
= min_color
383 while not self.is_color_free
(element
, elements
, color
) do
386 coloration_result
[element
] = color
391 # Check if a related element to the element already use the color
392 private fun is_color_free
(element
: E
, elements
: Set[E
], color
: Int): Bool do
393 if conflicts_graph
.has_key
(element
) then
394 for st
in conflicts_graph
[element
] do
395 if coloration_result
.has_key
(st
) and coloration_result
[st
] == color
then return false
398 for st
in self.super_elements
(element
, elements
) do
399 if coloration_result
.has_key
(st
) and coloration_result
[st
] == color
then return false
404 # Tag elements as core, crown or border
405 private fun tag_elements
(elements
: Set[E
]) do
406 for element
in elements
do
407 # Check if sub elements are all in single inheritance
408 var all_subelements_si
= true
409 for subelem
in self.sub_elements
(element
, elements
) do
410 if self.is_element_mi
(subelem
, elements
) then
411 all_subelements_si
= false
416 # Tag as core, crown or border
417 if self.is_element_mi
(element
, elements
) then
418 core
.add_all
(self.super_elements
(element
, elements
))
420 if all_subelements_si
then
423 else if not all_subelements_si
then
424 core
.add_all
(self.super_elements
(element
, elements
))
432 # Conflicts graph of elements hierarchy (two types are in conflict if they have common subelements)
433 private fun build_conflicts_graph
(elements
: Set[E
]) do
434 self.conflicts_graph
= new HashMap[E
, HashSet[E
]]
435 var core
= reverse_linearize
(self.core
)
437 for i
in self.linear_extension
(t
, elements
) do
438 if t
== i
then continue
440 var lin_i
= self.linear_extension
(i
, elements
)
442 for j
in self.linear_extension
(t
, elements
) do
443 if i
== j
or j
== t
then continue
444 var lin_j
= self.linear_extension
(j
, elements
)
446 var d_ij
= lin_i
- lin_j
447 var d_ji
= lin_j
- lin_i
450 if not conflicts_graph
.has_key
(ed1
) then conflicts_graph
[ed1
] = new HashSet[E
]
451 # add ed1 x ed2 to conflicts graph
452 for ed2
in d_ji
do conflicts_graph
[ed1
].add
(ed2
)
455 if not conflicts_graph
.has_key
(ed1
) then conflicts_graph
[ed1
] = new HashSet[E
]
456 # add ed1 x ed2 to conflicts graph
457 for ed2
in d_ji
do conflicts_graph
[ed1
].add
(ed2
)
464 private var conflicts_graph
: nullable HashMap[E
, Set[E
]]
466 # cache for linear_extensions
467 private var linear_extensions_cache
: Map[E
, Array[E
]] = new HashMap[E
, Array[E
]]
469 # Return a linear_extension of super_elements of the element
470 private fun linear_extension
(element
: E
, elements
: Set[E
]): Array[E
] do
471 if not self.linear_extensions_cache
.has_key
(element
) then
472 var supers
= new HashSet[E
]
474 supers
.add_all
(self.super_elements
(element
, elements
))
475 self.linear_extensions_cache
[element
] = self.linearize
(supers
)
477 return self.linear_extensions_cache
[element
]
480 private fun super_elements
(element
: E
, elements
: Set[E
]): Set[E
] is abstract
481 private fun sub_elements
(element
: E
, elements
: Set[E
]): Set[E
] is abstract
482 private fun is_element_mi
(element
: E
, elements
: Set[E
]): Bool is abstract
483 private fun linearize
(elements
: Set[E
]): Array[E
] is abstract
484 private fun reverse_linearize
(elements
: Set[E
]): Array[E
] is abstract
488 private class MTypeColorer
489 super AbstractColorer[MType]
493 init(mmodule
: MModule) do self.mmodule
= mmodule
495 redef fun super_elements
(element
, elements
) do return self.mmodule
.super_mtypes
(element
, elements
)
496 redef fun is_element_mi
(element
, elements
) do return self.super_elements
(element
, elements
).length
> 1
497 redef fun sub_elements
(element
, elements
) do do return self.mmodule
.sub_mtypes
(element
, elements
)
498 redef fun linearize
(elements
) do return self.mmodule
.linearize_mtypes
(elements
)
499 redef fun reverse_linearize
(elements
) do return self.mmodule
.reverse_linearize_mtypes
(elements
)
503 private class MClassColorer
504 super AbstractColorer[MClass]
506 private var mmodule
: MModule
508 init(mmodule
: MModule) do self.mmodule
= mmodule
510 redef fun super_elements
(element
, elements
) do return self.mmodule
.super_mclasses
(element
)
511 fun parent_elements
(element
: MClass): Set[MClass] do return self.mmodule
.parent_mclasses
(element
)
512 redef fun is_element_mi
(element
, elements
) do return self.parent_elements
(element
).length
> 1
513 redef fun sub_elements
(element
, elements
) do do return self.mmodule
.sub_mclasses
(element
)
514 redef fun linearize
(elements
) do return self.mmodule
.linearize_mclasses
(elements
)
515 redef fun reverse_linearize
(elements
) do return self.mmodule
.reverse_linearize_mclasses
(elements
)
519 private class MPropertyColorer[E
: MProperty]
521 private var mmodule
: MModule
522 private var class_colorer
: MClassColorer
523 private var coloration_result
: Map[E
, Int] = new HashMap[E
, Int]
525 init(mmodule
: MModule) do
526 self.mmodule
= mmodule
527 self.class_colorer
= new MClassColorer(mmodule
)
530 fun colorize
(mclasses
: Set[MClass]): Map[E
, Int] do
531 self.class_colorer
.tag_elements
(mclasses
)
532 self.class_colorer
.build_conflicts_graph
(mclasses
)
533 self.colorize_core
(self.class_colorer
.core
)
534 self.colorize_crown
(self.class_colorer
.crown
)
535 return self.coloration_result
538 # Colorize properties of the core hierarchy
539 private fun colorize_core
(mclasses
: Set[MClass]) do
541 for mclass
in mclasses
do
542 var color
= min_color
544 # if the class is root, get the minimal color
545 if self.mmodule
.parent_mclasses
(mclass
).length
== 0 then
546 colorize_elements
(self.properties
(mclass
), color
)
548 # check last color used by parents
549 color
= max_color
(color
, self.mmodule
.parent_mclasses
(mclass
))
550 # check max color used in conflicts
551 if self.class_colorer
.conflicts_graph
.has_key
(mclass
) then
552 color
= max_color
(color
, self.class_colorer
.conflicts_graph
[mclass
])
554 colorize_elements
(self.properties
(mclass
), color
)
559 # Colorize properties of the crown hierarchy
560 private fun colorize_crown
(mclasses
: Set[MClass]) do
561 for mclass
in mclasses
do
562 colorize_elements
(self.properties
(mclass
), max_color
(0, self.mmodule
.parent_mclasses
(mclass
)))
566 # Colorize a collection of mproperties given a starting color
567 private fun colorize_elements
(elements
: Collection[E
], start_color
: Int) do
568 for element
in elements
do
569 if self.coloration_result
.has_key
(element
) then continue
570 self.coloration_result
[element
] = start_color
575 private fun max_color
(min_color
: Int, mclasses
: Collection[MClass]): Int do
576 var max_color
= min_color
578 for mclass
in mclasses
do
579 for mproperty
in self.properties
(mclass
) do
580 var color
= min_color
581 if self.coloration_result
.has_key
(mproperty
) then
582 color
= self.coloration_result
[mproperty
]
583 if color
>= max_color
then max_color
= color
+ 1
591 private fun properties
(mclass
: MClass): Set[E
] do
592 var properties
= new HashSet[E
]
593 for mprop
in self.mmodule
.properties
(mclass
) do
594 if mprop
isa E
then properties
.add
(mprop
)
600 # Colorer for type resolution table
601 class ResolutionColorer
603 private var coloration_result
: Map[MType, Int] = new HashMap[MType, Int]
607 fun colorize
(elements
: Map[MClassType, Set[MType]]): Map[MType, Int] do
608 self.build_conflicts_graph
(elements
)
609 self.colorize_elements
(elements
)
610 return coloration_result
613 # Colorize a collection of elements
614 fun colorize_elements
(elements
: Map[MClassType, Set[MType]]) do
616 for mclasstype
, mclasstypes
in elements
do
617 for element
in mclasstypes
do
618 if self.coloration_result
.has_key
(element
) then continue
619 var color
= min_color
620 while not self.is_color_free
(element
, color
) do
623 coloration_result
[element
] = color
629 # Check if a related element to the element already use the color
630 private fun is_color_free
(element
: MType, color
: Int): Bool do
631 if conflicts_graph
.has_key
(element
) then
632 for st
in conflicts_graph
[element
] do
633 if coloration_result
.has_key
(st
) and coloration_result
[st
] == color
then return false
639 # look for unanchored types generated by the same type
640 private fun build_conflicts_graph
(elements
: Map[MClassType, Set[MType]]) do
641 for mclasstype
, mtypes
in elements
do
642 for mtype
in mtypes
do
643 for otype
in mtypes
do
644 if otype
== mtype
then continue
645 self.add_conflict
(mtype
, otype
)
651 private var conflicts_graph
: Map[MType, Set[MType]] = new HashMap[MType, Set[MType]]
653 private fun add_conflict
(mtype
: MType, otype
: MType) do
654 if mtype
== otype
then return
655 if not self.conflicts_graph
.has_key
(mtype
) then self.conflicts_graph
[mtype
] = new HashSet[MType]
656 self.conflicts_graph
[mtype
].add
(otype
)
657 if not self.conflicts_graph
.has_key
(otype
) then self.conflicts_graph
[otype
] = new HashSet[MType]
658 self.conflicts_graph
[otype
].add
(mtype
)
664 # Abstract Perfect Hashing
665 private abstract class AbstractHasher[E
: Object]
667 var operator
: PHOperator
669 init(operator
: PHOperator) do self.operator
= operator
671 fun compute_masks
(elements
: Set[E
], ids
: Map[E
, Int]): Map[E
, Int] do
672 var masks
= new HashMap[E
, Int]
673 for element
in elements
do
674 var supers
= new HashSet[E
]
675 supers
.add_all
(self.super_elements
(element
, elements
))
677 masks
[element
] = compute_mask
(supers
, ids
)
682 fun compute_mask
(supers
: Set[E
], ids
: Map[E
, Int]): Int do
685 var used
= new List[Int]
687 var res
= operator
.op
(mask
, ids
[sup
])
688 if used
.has
(res
) then
694 if used
.length
== supers
.length
then break
700 fun compute_hashes
(elements
: Set[E
], ids
: Map[E
, Int], masks
: Map[E
, Int]): Map[E
, Map[E
, Int]] do
701 var hashes
= new HashMap[E
, Map[E
, Int]]
702 for element
in elements
do
703 var supers
= new HashSet[E
]
704 supers
.add_all
(self.super_elements
(element
, elements
))
706 var inhashes
= new HashMap[E
, Int]
707 var mask
= masks
[element
]
709 inhashes
[sup
] = operator
.op
(mask
, ids
[sup
])
711 hashes
[element
] = inhashes
716 fun super_elements
(element
: E
, elements
: Set[E
]): Set[E
] is abstract
719 # Abstract operator used for perfect hashing
720 abstract class PHOperator
721 fun op
(mask
: Int, id
:Int): Int is abstract
724 # Hashing using modulo (MOD) operator
729 redef fun op
(mask
, id
) do return mask
% id
732 # Hashing using binary and (AND) operator
737 redef fun op
(mask
, id
) do return mask
.bin_and
(id
)
740 # MType Perfect Hashing
741 private class MTypeHasher
742 super AbstractHasher[MType]
746 init(mmodule
: MModule, operator
: PHOperator) do
748 self.mmodule
= mmodule
751 redef fun super_elements
(element
, elements
) do return self.mmodule
.super_mtypes
(element
, elements
)
754 # MClass Perfect Hashing
755 private class MClassHasher
756 super AbstractHasher[MClass]
758 private var mmodule
: MModule
760 init(mmodule
: MModule, operator
: PHOperator) do
762 self.mmodule
= mmodule
765 redef fun super_elements
(element
, elements
) do return self.mmodule
.super_mclasses
(element
)
768 # Resolution tables Perfect Hashing (PH)
769 private class ResolutionHasher
771 var operator
: PHOperator
773 init(operator
: PHOperator) do self.operator
= operator
775 fun compute_masks
(elements
: Map[MClassType, Set[MType]], ids
: Map[MType, Int]): Map[MClassType, Int] do
776 var masks
= new HashMap[MClassType, Int]
777 for mclasstype
, mtypes
in elements
do
778 masks
[mclasstype
] = compute_mask
(mtypes
, ids
)
783 private fun compute_mask
(mtypes
: Set[MType], ids
: Map[MType, Int]): Int do
786 var used
= new List[Int]
787 for mtype
in mtypes
do
788 var res
= operator
.op
(mask
, ids
[mtype
])
789 if used
.has
(res
) then
795 if used
.length
== mtypes
.length
then break
801 fun compute_hashes
(elements
: Map[MClassType, Set[MType]], ids
: Map[MType, Int], masks
: Map[MClassType, Int]): Map[MClassType, Map[MType, Int]] do
802 var hashes
= new HashMap[MClassType, Map[MType, Int]]
803 for mclasstype
, mtypes
in elements
do
804 var mask
= masks
[mclasstype
]
805 var inhashes
= new HashMap[MType, Int]
806 for mtype
in mtypes
do
807 inhashes
[mtype
] = operator
.op
(mask
, ids
[mtype
])
809 hashes
[mclasstype
] = inhashes
817 redef class HashSet[E
]
818 init from
(elements
: Collection[E
]) do
820 self.add_all
(elements
)
825 init from
(elements
: Collection[E
]) do
827 self.add_all
(elements
)
830 # Return a new Array with the elements only contened in 'self' and not in 'o'
831 fun -(o
: Array[E
]): Array[E
] do
832 var res
= new Array[E
]
833 for e
in self do if not o
.has
(e
) then res
.add
(e
)
840 # Return a linearization of a set of mtypes
841 private fun linearize_mtypes
(mtypes
: Set[MType]): Array[MType] do
842 var lin
= new Array[MType].from
(mtypes
)
843 var sorter
= new TypeSorter(self)
848 # Return a reverse linearization of a set of mtypes
849 private fun reverse_linearize_mtypes
(mtypes
: Set[MType]): Array[MType] do
850 var lin
= new Array[MType].from
(mtypes
)
851 var sorter
= new ReverseTypeSorter(self)
856 # Return super types of a `mtype` in `self`
857 private fun super_mtypes
(mtype
: MType, mtypes
: Set[MType]): Set[MType] do
858 if not self.super_mtypes_cache
.has_key
(mtype
) then
859 var supers
= new HashSet[MType]
860 for otype
in mtypes
do
861 if otype
== mtype
then continue
862 if mtype
.is_subtype
(self, null, otype
) then
866 self.super_mtypes_cache
[mtype
] = supers
868 return self.super_mtypes_cache
[mtype
]
871 private var super_mtypes_cache
: Map[MType, Set[MType]] = new HashMap[MType, Set[MType]]
873 # Return all sub mtypes (directs and indirects) of a `mtype` in `self`
874 private fun sub_mtypes
(mtype
: MType, mtypes
: Set[MType]): Set[MType] do
875 if not self.sub_mtypes_cache
.has_key
(mtype
) then
876 var subs
= new HashSet[MType]
877 for otype
in mtypes
do
878 if otype
== mtype
then continue
879 if otype
.is_subtype
(self, null, mtype
) then
883 self.sub_mtypes_cache
[mtype
] = subs
885 return self.sub_mtypes_cache
[mtype
]
888 private var sub_mtypes_cache
: Map[MType, Set[MType]] = new HashMap[MType, Set[MType]]
890 # Return a linearization of a set of mclasses
891 private fun linearize_mclasses
(mclasses
: Set[MClass]): Array[MClass] do
892 var lin
= new Array[MClass].from
(mclasses
)
893 var sorter
= new ClassSorter(self)
898 # Return a reverse linearization of a set of mtypes
899 private fun reverse_linearize_mclasses
(mclasses
: Set[MClass]): Array[MClass] do
900 var lin
= new Array[MClass].from
(mclasses
)
901 var sorter
= new ReverseClassSorter(self)
906 # Return all super mclasses (directs and indirects) of a `mclass` in `self`
907 private fun super_mclasses
(mclass
: MClass): Set[MClass] do
908 if not self.super_mclasses_cache
.has_key
(mclass
) then
909 var supers
= new HashSet[MClass]
910 if self.flatten_mclass_hierarchy
.has
(mclass
) then
911 for sup
in self.flatten_mclass_hierarchy
[mclass
].greaters
do
912 if sup
== mclass
then continue
916 self.super_mclasses_cache
[mclass
] = supers
918 return self.super_mclasses_cache
[mclass
]
921 private var super_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
923 # Return all parents of a `mclass` in `self`
924 private fun parent_mclasses
(mclass
: MClass): Set[MClass] do
925 if not self.parent_mclasses_cache
.has_key
(mclass
) then
926 var parents
= new HashSet[MClass]
927 if self.flatten_mclass_hierarchy
.has
(mclass
) then
928 for sup
in self.flatten_mclass_hierarchy
[mclass
].direct_greaters
do
929 if sup
== mclass
then continue
933 self.parent_mclasses_cache
[mclass
] = parents
935 return self.parent_mclasses_cache
[mclass
]
938 private var parent_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
940 # Return all sub mclasses (directs and indirects) of a `mclass` in `self`
941 private fun sub_mclasses
(mclass
: MClass): Set[MClass] do
942 if not self.sub_mclasses_cache
.has_key
(mclass
) then
943 var subs
= new HashSet[MClass]
944 if self.flatten_mclass_hierarchy
.has
(mclass
) then
945 for sub
in self.flatten_mclass_hierarchy
[mclass
].smallers
do
946 if sub
== mclass
then continue
950 self.sub_mclasses_cache
[mclass
] = subs
952 return self.sub_mclasses_cache
[mclass
]
955 private var sub_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
957 # All 'mproperties' associated to all 'mclassdefs' of `mclass`
958 private fun properties
(mclass
: MClass): Set[MProperty] do
959 if not self.properties_cache
.has_key
(mclass
) then
960 var properties
= new HashSet[MProperty]
961 var parents
= self.super_mclasses
(mclass
)
962 for parent
in parents
do
963 properties
.add_all
(self.properties
(parent
))
966 for mclassdef
in mclass
.mclassdefs
do
967 for mpropdef
in mclassdef
.mpropdefs
do
968 properties
.add
(mpropdef
.mproperty
)
971 self.properties_cache
[mclass
] = properties
973 return properties_cache
[mclass
]
976 private var properties_cache
: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
979 # A sorter for linearize list of types
981 super AbstractSorter[MType]
983 private var mmodule
: MModule
985 init(mmodule
: MModule) do self.mmodule
= mmodule
987 redef fun compare
(a
, b
) do
990 else if a
.is_subtype
(self.mmodule
, null, b
) then
997 # A sorter for reverse linearization
998 class ReverseTypeSorter
1001 init(mmodule
: MModule) do end
1003 redef fun compare
(a
, b
) do
1006 else if a
.is_subtype
(self.mmodule
, null, b
) then
1013 # A sorter for linearize list of classes
1014 private class ClassSorter
1015 super AbstractSorter[MClass]
1017 var mmodule
: MModule
1019 redef fun compare
(a
, b
) do
1022 else if self.mmodule
.flatten_mclass_hierarchy
.has
(a
) and self.mmodule
.flatten_mclass_hierarchy
[a
].greaters
.has
(b
) then
1029 # A sorter for reverse linearization
1030 private class ReverseClassSorter
1031 super AbstractSorter[MClass]
1033 var mmodule
: MModule
1035 redef fun compare
(a
, b
) do
1038 else if self.mmodule
.flatten_mclass_hierarchy
.has
(a
) and self.mmodule
.flatten_mclass_hierarchy
[a
].greaters
.has
(b
) then