|
|
@@ -14,7 +14,6 @@ import net.ranides.assira.collection.arrays.NativeArraySort;
|
|
|
import net.ranides.assira.collection.lists.IntList;
|
|
|
import net.ranides.assira.collection.lists.IntListUtils;
|
|
|
import net.ranides.assira.collection.lists.ListUtils;
|
|
|
-import net.ranides.assira.generic.CompareUtils;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collection;
|
|
|
@@ -27,36 +26,15 @@ public final class CollectionSort {
|
|
|
// utility class
|
|
|
}
|
|
|
|
|
|
- // @todo (assira #2) swapper/sorter for "connected" arrays
|
|
|
- // zrobić to za pomocą fluent-api:
|
|
|
- //
|
|
|
- // CollectionSorter
|
|
|
- // .array(a)
|
|
|
- // .list(b,c,d)
|
|
|
- // .comparator(valuecomparator) // use cmp for values from "a"
|
|
|
- // .comparator(cint) // use cmp for indexes from "a"
|
|
|
- // .orderby(b) // use default comparator in "b" (or already specified"
|
|
|
- // .sort()
|
|
|
-
|
|
|
-
|
|
|
- public static void sort1(Object collection) {
|
|
|
- // sort(comparator(collection), collection);
|
|
|
- }
|
|
|
-
|
|
|
- public static void sort2(IntComparator cmp, Object collection) {
|
|
|
- NativeArraySort.quickSort(0, size(collection), cmp, swapper(collection));
|
|
|
- }
|
|
|
-
|
|
|
- public static void msort1(Object... collections) {
|
|
|
-// msort(comparator(collections[0]), collections);
|
|
|
+ public static void isort(IntComparator cmp, Object... collections) {
|
|
|
+ NativeArraySort.quickSort(0, size(collections[0]), cmp, concat0(collections));
|
|
|
}
|
|
|
|
|
|
- public static void msort2(Comparator<?> cmp, Object... collections) {
|
|
|
- NativeArraySort.quickSort(0, size(collections[0]), comparator(collections[0], cmp), concat(collections));
|
|
|
+ public static void sort(Comparator<?> cmp, Object... collections) {
|
|
|
+ NativeArraySort.quickSort(0, size(collections[0]), comparator(collections[0], cmp), concat0(collections));
|
|
|
}
|
|
|
|
|
|
- private static Swapper concat(Object... collections) {
|
|
|
- Swapper[] swappers = Arrays.stream(collections).map(CollectionSort::swapper).toArray(Swapper[]::new);
|
|
|
+ public static Swapper concat(Swapper... swappers) {
|
|
|
return (a,b) -> {
|
|
|
for(Swapper s : swappers) {
|
|
|
s.swap(a,b);
|
|
|
@@ -64,6 +42,11 @@ public final class CollectionSort {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ private static Swapper concat0(Object... collections) {
|
|
|
+ return concat(Arrays.stream(collections).map(CollectionSort::swapper).toArray(Swapper[]::new));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
private static Swapper swapper(Object object) {
|
|
|
if(object == null) {
|
|
|
throw new IllegalArgumentException("Collection is NULL");
|
|
|
@@ -101,27 +84,6 @@ public final class CollectionSort {
|
|
|
throw new IllegalArgumentException("Unsupported collection: " + object.getClass());
|
|
|
}
|
|
|
|
|
|
- private static IntComparator comparator(Object object) {
|
|
|
- if(object == null) {
|
|
|
- throw new IllegalArgumentException("Collection is NULL");
|
|
|
- }
|
|
|
- if(object instanceof IntComparator) {
|
|
|
- return (IntComparator)object;
|
|
|
- }
|
|
|
- if(object instanceof List) {
|
|
|
- List<?> list = (List<?>) object;
|
|
|
- return (a,b) -> CompareUtils.cmp(list.get(a), list.get(b));
|
|
|
- }
|
|
|
- if(object instanceof IntList) {
|
|
|
- IntList list = (IntList) object;
|
|
|
- return (a,b) -> CompareUtils.cmp(list.getInt(a), list.getInt(b));
|
|
|
- }
|
|
|
- if(object.getClass().isArray()) {
|
|
|
- return NativeArray.wrap(object).comparator();
|
|
|
- }
|
|
|
- throw new IllegalArgumentException("Unsupported collection: " + object.getClass());
|
|
|
- }
|
|
|
-
|
|
|
private static int size(Object object) {
|
|
|
if (object instanceof Collection) {
|
|
|
return ((Collection) object).size();
|