# another product.
# This module contains classes used to sorts arrays.
-# In order to provide your own sort class you should define a subclass of AbstractSorter with
-# a custom `compare' function.
-package sorter
+# In order to provide your own sort class you should define a subclass of `Comparator` with
+# a custom `Comparator::compare` function.
+module sorter
import array
# This abstract class generalizes ways to sort an array
-# TODO: rename *Sorter to *Comparator
-interface AbstractSorter[E: Object]
- # Compare `a' and `b'.
+interface Comparator[E]
+ # Compare `a` and `b`.
# Returns:
# -1 if a < b
# 0 if a = b
# 1 if a > b
fun compare(a: E, b: E): Int is abstract
- # Sort `array' using the `compare' function.
+ # Sort `array` using the `compare` function.
fun sort(array: Array[E]) do sub_sort(array, 0, array.length-1)
- # Sort `array' between `from' and `to' indices
+ # Sort `array` between `from` and `to` indices
private fun sub_sort(array: Array[E], from: Int, to: Int)
do
if from >= to then
end
end
- # Quick-sort `array' between `from' and `to' indices
+ # Quick-sort `array` between `from` and `to` indices
private fun quick_sort(array: Array[E], from: Int, to: Int)
do
var pivot = array[from]
sub_sort(array, i, to)
end
- # Bubble-sort `array' between `from' and `to' indices
+ # Bubble-sort `array` between `from` and `to` indices
private fun bubble_sort(array: Array[E], from: Int, to: Int)
do
var i = from
end
end
-# This class uses the operator <=> to sort arrays.
-# You can also use the `sort' method of the `Array' class.
-class ComparableSorter[E: Comparable]
- super AbstractSorter[E]
+# Deprecated class, use `Comparator` instead
+interface AbstractSorter[E]
+ super Comparator[E]
+end
+
+# This comparator uses the operator `<=>` to compare objects.
+class DefaultComparator[E: Comparable]
+ super Comparator[E]
# Return a <=> b
redef fun compare(a, b) do return a <=> b
init do end
end
+# Deprecated class, use `DefaultComparator` instead
+class ComparableSorter[E: Comparable]
+ super DefaultComparator[E]
+end