Report a bug
If you spot a problem with this page, click here to create a Github issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.ndslice.sorting

This is a submodule of mir.ndslice.

Note: The combination of pairwise  with lambda "a <= b" ("a < b") and all  can be used to check if an ndslice is sorted (strictly monotonic). topology iota  can be used to make an index. topology map  and topology zip  can be used to create Schwartzian transform. See also the examples in the module.

See Also:
flattened 
isSorted and isStrictlyMonotonic
Authors:
Ilya Yaroshenko, Andrei Alexandrescu
Examples:
Check if ndslice is sorted, or strictly monotonic.
import mir.ndslice.algorithm: all;
import mir.ndslice.slice: sliced;
import mir.ndslice.sorting: sort;
import mir.ndslice.topology: pairwise;

auto arr = [1, 1, 2].sliced;

assert(arr.pairwise!"a <= b".all);
assert(!arr.pairwise!"a < b".all);

arr = [4, 3, 2, 1].sliced;

assert(!arr.pairwise!"a <= b".all);
assert(!arr.pairwise!"a < b".all);

sort(arr);

assert(arr.pairwise!"a <= b".all);
assert(arr.pairwise!"a < b".all);
Examples:
Create index
import mir.ndslice.algorithm: all;
import mir.ndslice.allocation: slice;
import mir.ndslice.slice: sliced;
import mir.ndslice.sorting: sort;
import mir.ndslice.topology: iota, pairwise;

auto arr = [4, 2, 3, 1].sliced;

auto index = arr.length.iota.slice;
index.sort!((a, b) => arr[a] < arr[b]);

assert(arr[index].pairwise!"a <= b".all);
Examples:
Schwartzian transform
import mir.ndslice.algorithm: all;
import mir.ndslice.allocation: slice;
import mir.ndslice.slice: sliced;
import mir.ndslice.sorting: sort;
import mir.ndslice.topology: zip, map, pairwise;

alias transform = (a) => (a - 3) ^^ 2;

auto arr = [4, 2, 3, 1].sliced;

arr.map!transform.slice.zip(arr).sort!((l, r) => l.a < r.a);

assert(arr.map!transform.pairwise!"a <= b".all);
template sort(alias less = "a < b")
Sorts 1D ndslice.
See Also:
Examples:
import mir.ndslice.algorithm: all;
import mir.ndslice.slice;
import mir.ndslice.sorting: sort;
import mir.ndslice.topology: pairwise;

int[10] arr = [7,1,3,2,9,0,5,4,8,6];

auto data = arr[].ptr.sliced(arr.length);
data.sort();
assert(data.pairwise!"a <= b".all);
Slice!(kind, packs, Iterator) sort(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Iterator) slice)
if (packs.length == 1);