View source code
Display the source code in std/algorithm/searching.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla 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 local clone.

Function std.algorithm.searching.minPos

Computes a subrange of range starting at the first occurrence of range's minimum (respectively maximum) and with the same ending as range, or the empty range if range itself is empty.

Range minPos(alias pred, Range) (
  Range range
)
if (isForwardRange!Range && !isInfinite!Range && is(typeof(binaryFun!pred(range.front, range.front))));

Formally, the minimum is a value x in range such that pred(a, x) is false for all values a in range. Conversely, the maximum is a value x in range such that pred(x, a) is false for all values a in range (note the swapped arguments to pred).

These functions may be used for computing arbitrary extrema by choosing pred appropriately. For corrrect functioning, pred must be a strict partial order, i.e. transitive (if pred(a, b) && pred(b, c) then pred(a, c)) and irreflexive (pred(a, a) is false).

Parameters

NameDescription
pred The ordering predicate to use to determine the extremum (minimum or maximum) element.
range The forward range to search.

Returns

The position of the minimum (respectively maximum) element of forward range range, i.e. a subrange of range starting at the position of its smallest (respectively largest) element and with the same ending as range.

See Also

max, minCount, minIndex, minElement

Example

int[] a = [ 2, 3, 4, 1, 2, 4, 1, 1, 2 ];
// Minimum is 1 and first occurs in position 3
writeln(a.minPos); // [1, 2, 4, 1, 1, 2]
// Maximum is 4 and first occurs in position 2
writeln(a.maxPos); // [4, 1, 2, 4, 1, 1, 2]

Authors

Andrei Alexandrescu

License

Boost License 1.0.