fun sort: Array[E]
do
var res = map.keys.to_a
- var sorter = new CounterSorter[E](self)
+ var sorter = new CounterComparator[E](self)
sorter.sort(res)
return res
end
end
end
-private class CounterSorter[E: Object]
- super AbstractSorter[E]
+private class CounterComparator[E: Object]
+ super Comparator[E]
var counter: Counter[E]
redef fun compare(a,b) do return self.counter.map[a] <=> self.counter.map[b]
end
private fun show_counter(c: Counter[Int])
do
var list = c.sort
- (new ComparableSorter[Int]).sort(list)
+ default_comparator.sort(list)
for e in list do
print " {e} -> {c[e]} times ({div(c[e]*100, c.sum)}%)"
end
# Sort roots and other elements using a comparator method
# This method basically sorts roots then each group of children
- fun sort_with(comparator: AbstractSorter[E])
+ fun sort_with(comparator: Comparator[E])
do
comparator.sort(roots)
for a in sub.values do
module pipeline
redef interface Collection[E]
- # Filter: sort with ComparableSorter.
+ # Filter: sort with `default_comparator`.
# SEE: `sort_with` for details
# REQUIRE: self isa Iterator[Comparable]
#
fun sort_filter: Collection[E]
do
assert self isa Collection[Comparable]
- var sorter = new ComparableSorter[Comparable]
var a = self.to_a
- sorter.sort(a)
+ default_comparator.sort(a)
return a
end
- # Filter: sort with a given `sorter`.
+ # Filter: sort with a given `comparator`.
# Important: require O(n) memory.
- fun sort_with(sorter: AbstractSorter[E]): Collection[E]
+ fun sort_with(comparator: Comparator[E]): Collection[E]
do
var a = self.to_a
- sorter.sort(a)
+ comparator.sort(a)
return a
end
# * transitivity: `(self.has_edge(e,f) and self.has_edge(f,g)) implies self.has_edge(e,g)`
class POSet[E: Object]
super Collection[E]
- super AbstractSorter[E]
+ super Comparator[E]
redef fun iterator do return elements.keys.iterator
#
# Note: it caching is not usefull, see `alpha_comparator`
class CachedAlphaComparator
- super AbstractSorter[Object]
+ super Comparator[Object]
private var cache = new HashMap[Object, String]
# see `alpha_comparator`
private class AlphaComparator
- super AbstractSorter[Object]
+ super Comparator[Object]
redef fun compare(a, b) do return a.to_s <=> b.to_s
end
# var a = [1, 2, 3, 10, 20]
# alpha_comparator.sort(a)
# assert a == [1, 10, 2, 20, 3]
-fun alpha_comparator: AbstractSorter[Object] do return once new AlphaComparator
+fun alpha_comparator: Comparator[Object] do return once new AlphaComparator
# Compare modules and groups using the
# FIXME do not use Object, but a better common interface of MModule and MGroup
private class LinexComparator
- super AbstractSorter[Object]
+ super Comparator[Object]
var mins = new HashMap [MGroup, nullable MModule]
var maxs = new HashMap [MGroup, nullable MModule]
fun min(o: Object): nullable MModule do
init(i: Int) do initelt2(i)
end
-class EltSorter
- super AbstractSorter[Elt]
+class EltComparator
+ super Comparator[Elt]
redef fun compare(a: Elt, b: Elt): Int
do
if _is_val1 then
array.push(generator)
end
-var sorter = new EltSorter
+var comparator = new EltComparator
for i in [0..n[ do
- sorter.sort(array)
- sorter.toggle
+ comparator.sort(array)
+ comparator.toggle
end
# limitations under the License.
-class BackIntSorter
- super AbstractSorter[Int]
+class BackIntComparator
+ super Comparator[Int]
redef fun compare(a: Int, b: Int): Int
do
return b <=> a
init do end
end
-class DecimalSorter
- super AbstractSorter[Int]
+class DecimalComparator
+ super Comparator[Int]
redef fun compare(a: Int, b: Int): Int
do
return (a%10) <=> (b%10)
var q = get_an_array(50)
print(q.join(" "))
-(new ComparableSorter[Int]).sort(q)
+(new DefaultComparator[Int]).sort(q)
print(q.join(" "))
-(new DecimalSorter).sort(q)
+(new DecimalComparator).sort(q)
print(q.join(" "))
-(new BackIntSorter).sort(q)
+(new BackIntComparator).sort(q)
print(q.join(" "))
-(new DecimalSorter).sort(q)
+(new DecimalComparator).sort(q)
print(q.join(" "))