partitionAt

Partitions slice, such that all elements e1 from slice[0] to slice[nth] satisfy !less(slice[nth], e1), and all elements e2 from slice[nth] to slice[slice.length] satisfy !less(e2, slice[nth]). This effectively reorders slice such that slice[nth] refers to the element that would fall there if the range were fully sorted. Performs an expected O(slice.length) evaluations of less and swap, with a worst case of O(slice.length^^2).

This function implements the Fast, Deterministic Selection algorithm that is implemented in the `topN` function in D's standard library (as of version 2.092.0).

  1. void partitionAt(Slice!(Iterator, N, kind) slice, size_t nth)
    template partitionAt(alias less = "a < b")
    @trusted nothrow @nogc
    @optmath
    static if(__traits(isSame, naryFun!less, less))
    void
    partitionAt
    (
    Iterator
    size_t N
    SliceKind kind
    )
    (
    Slice!(Iterator, N, kind) slice
    ,
    size_t nth
    )
  2. alias partitionAt = .partitionAt!(naryFun!less)

Members

Aliases

partitionAt
alias partitionAt = .partitionAt!(naryFun!less)
Undocumented in source.

Functions

partitionAt
void partitionAt(Slice!(Iterator, N, kind) slice, size_t nth)

Parameters

less

The predicate to sort by.

Examples

Partition 1-dimensional slice at nth

import mir.ndslice.slice: sliced;

size_t nth = 2;
auto x = [3, 1, 5, 2, 0].sliced;
x.partitionAt(nth);
assert(x[nth] == 2);

Partition 2-dimensional slice

import mir.ndslice.slice: sliced;

size_t nth = 4;
auto x = [3, 1, 5, 2, 0, 7].sliced(3, 2);
x.partitionAt(nth);
assert(x[2, 0] == 5);

Can supply alternate ordering function

import mir.ndslice.slice: sliced;

size_t nth = 2;
auto x = [3, 1, 5, 2, 0].sliced;
x.partitionAt!("a > b")(nth);
assert(x[nth] == 2);

See Also

Meta