diff --git a/pom.xml b/pom.xml index 5fe8f87..ab0454b 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ alda_sorting_api - 1.2.0 + 1.3.0 jar Sorting Service API diff --git a/src/main/java/sortingservice/KeySorter.java b/src/main/java/sortingservice/KeySorter.java new file mode 100644 index 0000000..ee40c8e --- /dev/null +++ b/src/main/java/sortingservice/KeySorter.java @@ -0,0 +1,37 @@ +package sortingservice; + +import java.util.function.ToIntFunction; + +/** + * A key sorter sorts its input on an integer key, without comparing elements. + * + *

+ * Where a {@link Sorter} can only compare two elements with a + * {@link java.util.Comparator Comparator}, a key sorter gets access to the + * key of every element. This restriction on the input allows sorting in + * linear time, e.g. with radix sort. + *

+ * + * The implementation is expected to return the same queue instance. + * + * @author Martijn Bonajo + * @param type of elements in queue. + */ +@FunctionalInterface +public interface KeySorter { + + /** + * Return the input queue in sorted order, based on the key of the elements. + * + *

+ * The sort is stable: elements with equal keys keep their relative + * order. + *

+ * + * @param queue to be sorted + * @param key extracts the key of an element, the key must be non-negative + * @return the queue with the elements in non-descending order of their key. + * @throws IllegalArgumentException if the key of an element is negative. + */ + Queue sort( Queue queue, ToIntFunction key ); +} diff --git a/src/main/java/sortingservice/SortKind.java b/src/main/java/sortingservice/SortKind.java index e0c5f67..671a62b 100644 --- a/src/main/java/sortingservice/SortKind.java +++ b/src/main/java/sortingservice/SortKind.java @@ -23,5 +23,9 @@ public enum SortKind { /** * Heap sort. */ - HEAP; + HEAP, + /** + * Radix sort. + */ + RADIX; } diff --git a/src/main/java/sortingservice/SorterConfiguration.java b/src/main/java/sortingservice/SorterConfiguration.java index 6e27d1a..374f87f 100644 --- a/src/main/java/sortingservice/SorterConfiguration.java +++ b/src/main/java/sortingservice/SorterConfiguration.java @@ -30,7 +30,8 @@ public interface SorterConfiguration { /** * Get queue for sorter. * @param the type of elements in the queue - * @return queue for sorter + * @return queue for sorter, or null if configuration uses a priority + * queue (see {@link #getPriorityQueue(Comparator)}). */ Queue getQueue(); @@ -44,12 +45,33 @@ public interface SorterConfiguration { PriorityQueue getPriorityQueue(Comparator comparator); /** - * Get a sorter. + * Get a sorter. + * A configuration that uses a priority queue returns a sorter that + * returns the queue as is, since the priority queue sorts by itself. * @param the type of elements in the queue - * @return sorter + * @return sorter, or null if configuration uses a key sorter (see + * {@link #getKeySorter()}). */ Sorter getSorter(); + /** + * Indicates whether this sorter uses a key sorter. + * @return true if configuration uses key sorter, false by default. + */ + default boolean usesKeySorter() { + return false; + } + + /** + * Get a key sorter. + * @param the type of elements in the queue + * @return key sorter or null if configuration does not use a key sorter, + * null by default. + */ + default KeySorter getKeySorter() { + return null; + } + /** * Indicates whether the teacher tests should test this configuration. * Your own tests should test all configurations.