migration: use Comparator instead of Sorter in code
authorJean Privat <jean@pryen.org>
Fri, 14 Mar 2014 01:41:55 +0000 (21:41 -0400)
committerJean Privat <jean@pryen.org>
Fri, 14 Mar 2014 01:41:55 +0000 (21:41 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/counter.nit
lib/ordered_tree.nit
lib/pipeline.nit
lib/poset.nit
lib/standard/string.nit
src/model_viz.nit
tests/bench_complex_sort.nit
tests/example_sorter.nit

index df4beca..2f16944 100644 (file)
@@ -65,7 +65,7 @@ class Counter[E: Object]
        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
@@ -170,8 +170,8 @@ class Counter[E: Object]
        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
@@ -180,7 +180,7 @@ redef class POSet[E]
        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
index ede2196..15e8167 100644 (file)
@@ -77,7 +77,7 @@ class OrderedTree[E: Object]
 
        # 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
index 3765d6a..92c29d4 100644 (file)
@@ -26,7 +26,7 @@
 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]
        #
@@ -34,18 +34,17 @@ redef interface Collection[E]
        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
 
index fc6090f..65695da 100644 (file)
@@ -24,7 +24,7 @@ module poset
 #  * 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
 
index e92fea8..0b3d6d9 100644 (file)
@@ -1328,7 +1328,7 @@ end
 #
 # Note: it caching is not usefull, see `alpha_comparator`
 class CachedAlphaComparator
-       super AbstractSorter[Object]
+       super Comparator[Object]
 
        private var cache = new HashMap[Object, String]
 
@@ -1346,7 +1346,7 @@ end
 
 # 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
 
@@ -1358,4 +1358,4 @@ 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
index ba851e8..270f830 100644 (file)
@@ -62,7 +62,7 @@ end
 # 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
index ad72cc6..0ead451 100644 (file)
@@ -73,8 +73,8 @@ class E
        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
@@ -121,9 +121,9 @@ for i in [0..n[ do
        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
 
index 23da9ac..32b661c 100644 (file)
@@ -15,8 +15,8 @@
 # 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
@@ -25,8 +25,8 @@ class BackIntSorter
        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)
@@ -51,11 +51,11 @@ end
 
 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(" "))