The OpenD Programming Language

minCount

Computes the minimum (respectively maximum) of range along with its number of occurrences. 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). The $(LUCKY trichotomy property of inequality) is not required: these algorithms consider elements a and b equal (for the purpose of counting) if pred puts them in the same equivalence class, i.e. !pred(a, b) && !pred(b, a).

Tuple!(ElementType!Range, size_t)
minCount
(
alias pred = "a < b"
Range
)
(
Range range
)
if (
isInputRange!Range &&
!isInfinite!Range
&&
is(typeof(binaryFun!pred(range.front, range.front)))
)

Parameters

pred

The ordering predicate to use to determine the extremum (minimum or maximum).

range Range

The input range to count.

Return Value

Type: Tuple!(ElementType!Range, size_t)

The minimum, respectively maximum element of a range together with the number it occurs in the range.

Limitations: If at least one of the arguments is NaN, the result is an unspecified value. See std.algorithm.searching.maxElement for examples on how to cope with NaNs.

Throws

Exception if range.empty.

Examples

import std.conv : text;
import std.typecons : tuple;

int[] a = [ 2, 3, 4, 1, 2, 4, 1, 1, 2 ];
// Minimum is 1 and occurs 3 times
assert(a.minCount == tuple(1, 3));
// Maximum is 4 and occurs 2 times
assert(a.maxCount == tuple(4, 2));

See Also

Meta