std.algorithm.sorting
This is a submodule of std.algorithm. It contains generic sorting algorithms.
Copyright
Types 2
Specifies whether the output of certain algorithm is desired in sorted format.
If set to SortOutput.no, the output should not be sorted.
Otherwise if set to SortOutput.yes, the output should be sorted.
Functions 40
void completeSort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
Lhs , Rhs)(SortedRange!(Lhs, less) lhs, Rhs rhs) if (hasLength!(Rhs) && hasSlicing!(Rhs)
&& hasSwappableElements!Lhs && hasSwappableElements!Rhs)Sorts the random-access range `chain(lhs, rhs)` according to predicate `less`.bool isSorted(alias less = "a < b", Range)(Range r) if (isForwardRange!(Range))Checks whether a forward range is sorted according to the comparison operation `less`. Performs r.length evaluations of `less`.bool ordered(alias less = "a < b", T...)(T values) if ((T.length == 2 && is(typeof(binaryFun!less(values[1], values[0])) : bool))
||
(T.length > 2 && is(typeof(ordered!less(values[0 .. 1 + $ / 2])))
&& is(typeof(ordered!less(values[$ / 2 .. $]))))
)Like `isSorted`, returns `true` if the given `values` are ordered according to the comparison operation `less`. Unlike `isSorted`, takes values directly instead of structured in a range.bool strictlyOrdered(alias less = "a < b", T...)(T values) if (is(typeof(ordered!less(values))))dittoRange partition(alias predicate, SwapStrategy ss, Range)(Range r) if (ss == SwapStrategy.stable && isRandomAccessRange!(Range) && hasLength!Range &&
hasSlicing!Range && hasSwappableElements!Range)Partitions a range in two using the given `predicate`.Range partition(alias predicate, SwapStrategy ss = SwapStrategy.unstable, Range)(Range r) if (ss != SwapStrategy.stable && isInputRange!Range && hasSwappableElements!Range)dittosize_t pivotPartition(alias less = "a < b", Range)(Range r, size_t pivot) if (isRandomAccessRange!Range && hasLength!Range && hasSlicing!Range && hasAssignableElements!Range)Partitions `r` around `pivot` using comparison function `less`, algorithm akin to https://en.wikipedia.org/wiki/Quicksort#Hoarepartitionscheme.bool isPartitioned(alias pred, Range)(Range r) if (isForwardRange!(Range))Params: pred = The predicate that the range should be partitioned by. r = The range to check. Returns: `true` if `r` is partitioned according to predicate `pred`.auto partition3(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range, E)(Range r, E pivot) if (ss == SwapStrategy.unstable && isRandomAccessRange!Range
&& hasSwappableElements!Range && hasLength!Range && hasSlicing!Range
&& is(typeof(binaryFun!less(r.front, pivot)) == bool)
&& is(typeof(binaryFun!less(pivot, r.front)) == bool)
&& is(typeof(binaryFun!less(r.front, r.front)) == bool))Rearranges elements in `r` in three adjacent ranges and returns them.SortedRange!(RangeIndex, (a, b) => binaryFun!less(* a, * b)) makeIndex(
alias less = "a < b",
SwapStrategy ss = SwapStrategy.unstable,
Range,
RangeIndex)(Range r, RangeIndex index) if (isForwardRange!(Range) && isRandomAccessRange!(RangeIndex)
&& is(ElementType!(RangeIndex) : ElementType!(Range) *) && hasAssignableElements!RangeIndex)Computes an index for `r` based on the comparison `less`.void makeIndex(
alias less = "a < b",
SwapStrategy ss = SwapStrategy.unstable,
Range,
RangeIndex)(Range r, RangeIndex index) if (isRandomAccessRange!Range && !isInfinite!Range &&
isRandomAccessRange!RangeIndex && !isInfinite!RangeIndex &&
isIntegral!(ElementType!RangeIndex) && hasAssignableElements!RangeIndex)DittoMerge!(less, Rs) merge(alias less = "a < b", Rs...)(Rs rs) if (Rs.length >= 2 &&
allSatisfy!(isInputRange, Rs) &&
!is(CommonType!(staticMap!(ElementType, Rs)) == void))Merge multiple sorted ranges `rs` with less-than predicate function `pred` into one single sorted output range containing the sorted union of the elements of inputs.void trustedMoveEmplace(T)(ref T source, ref T target) @trusted@trusted wrapper for moveEmplaceSortedRange!(Range, less) sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r)Sorts a random-access range according to the predicate `less`.SortedRange!(R, ((a, b) => binaryFun!less(unaryFun!transform(a),
unaryFun!transform(b)))) schwartzSort(alias transform, alias less = "a < b",
SwapStrategy ss = SwapStrategy.unstable, R)(R r) if (isRandomAccessRange!R && hasLength!R && hasSwappableElements!R &&
!is(typeof(binaryFun!less) == SwapStrategy))Alternative sorting method that should be used when comparing keys involves an expensive computation.auto schwartzSort(alias transform, SwapStrategy ss, R)(R r) if (isRandomAccessRange!R && hasLength!R && hasSwappableElements!R)dittovoid partialSort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
Range)(Range r, size_t n) if (isRandomAccessRange!(Range) && hasLength!(Range) && hasSlicing!(Range))Reorders the random-access range `r` such that the range `r[0 .. mid]` is the same as if the entire `r` were sorted, and leaves the range `r[mid .. r.length]` in no particular order.void partialSort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
Range1, Range2)(Range1 r1, Range2 r2) if (isRandomAccessRange!(Range1) && hasLength!Range1 &&
isInputRange!Range2 && is(ElementType!Range1 == ElementType!Range2) &&
hasLvalueElements!Range1 && hasLvalueElements!Range2)Stores the smallest elements of the two ranges in the left-hand range in sorted order.auto topN(alias less = "a < b",
SwapStrategy ss = SwapStrategy.unstable,
Range)(Range r, size_t nth) if (isRandomAccessRange!(Range) && hasLength!Range &&
hasSlicing!Range && hasAssignableElements!Range)Reorders the range `r` using swap such that `r[nth]` refers to the element that would fall there if the range were fully sorted.size_t topNPartitionOffMedian(alias lp, Flag!"leanRight" f, R)(R r, size_t n, bool useSampling)auto topN(alias less = "a < b",
SwapStrategy ss = SwapStrategy.unstable,
Range1, Range2)(Range1 r1, Range2 r2) if (isRandomAccessRange!(Range1) && hasLength!Range1 &&
isInputRange!Range2 && is(ElementType!Range1 == ElementType!Range2) &&
hasLvalueElements!Range1 && hasLvalueElements!Range2)Stores the smallest elements of the two ranges in the left-hand range.TRange topNCopy(alias less = "a < b", SRange, TRange)(SRange source, TRange target, SortOutput sorted = No.sortOutput) if (isInputRange!(SRange) && isRandomAccessRange!(TRange)
&& hasLength!(TRange) && hasSlicing!(TRange))Copies the top `n` elements of the input range `source` into the random-access range `target`, where `n = target.length`.void topNIndex(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
Range, RangeIndex)(Range r, RangeIndex index, SortOutput sorted = No.sortOutput) if (isRandomAccessRange!Range &&
isRandomAccessRange!RangeIndex &&
hasAssignableElements!RangeIndex)Given a range of elements, constructs an index of its top n elements (i.e., the first n elements if the range were sorted).void medianOf(
alias less = "a < b",
Flag!"leanRight" flag = No.leanRight,
Range,
Indexes...)(Range r, Indexes i) if (isRandomAccessRange!Range && hasLength!Range &&
Indexes.length >= 2 && Indexes.length <= 5 &&
allSatisfy!(isUnsigned, Indexes))bool nextPermutation(alias less = "a < b", BidirectionalRange)(BidirectionalRange range) if (isBidirectionalRange!BidirectionalRange &&
hasSwappableElements!BidirectionalRange) Permutes `range` in-place to the next lexicographically greater permutation. The predicate `less` defines the lexicographical ordering to be used on the range. If the range is currently the ...bool nextEvenPermutation(alias less = "a < b", BidirectionalRange)(BidirectionalRange range) if (isBidirectionalRange!BidirectionalRange &&
hasSwappableElements!BidirectionalRange) Permutes `range` in-place to the next lexicographically greater even permutation. The predicate `less` defines the lexicographical ordering to be used on the range. An even permutation is on...Range nthPermutation(Range)(auto ref Range range, const ulong perm) if (isRandomAccessRange!Range && hasLength!Range) refPermutes `range` into the `perm` permutation.bool nthPermutationImpl(Range)(auto ref Range range, ulong perm) if (isRandomAccessRange!Range && hasLength!Range)Returns: `true` in case the permutation worked, `false` in case `perm` had more digits in the factorial number system than range had elements. This case must not occur as this would lead to out of ...Templates 4
Sorts a range by multiple keys.
The call multiSort!("a.id < b.id", "a.date > b.date")(r) sorts the range r by id ascending, and sorts elements that have the same id by date descending. Such a call is equivalent to sort!"a.id != b.id ? a.id < b.id : a.date > b.date"(r), but multiSort is faster because it does fewer comparisons (in addition to being more convenient).
Returns
SortedRange with its predicates
converted to an equivalent single predicate.
Helper method that moves from[fIdx] into to[tIdx] if both are lvalues and uses a plain assignment if not (necessary for backwards compatibility)