Minimum and maximum v0.1.0

Source
The smallest and largest elements of a slice, and clamping a value into a range.

MinIndex<T>

pub func MinIndex<T>(items: T[..]) -> uint?

The index of the smallest element of items. items is borrowed and read. Reports none for an empty slice, and the earliest index when several elements tie. Requires < on the element type. Cannot fail. Runs in time proportional to the length, comparing each element once, and allocates nothing.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the sequence to work on

Returns: the index of the smallest element, the first when several tie, or none when empty

MaxIndex<T>

pub func MaxIndex<T>(items: T[..]) -> uint?

The index of the largest element of items. items is borrowed and read. Reports none for an empty slice, and the earliest index when several elements tie. Requires < on the element type. Cannot fail. Runs in time proportional to the length and allocates nothing.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the sequence to work on

Returns: the index of the largest element, the first when several tie, or none when empty

MinIndexBy<T>

pub func MinIndexBy<T>(
    items: T[..],
    compare: func(T, T) -> Ordering
) -> uint?

The index of the smallest element of items, with compare deciding. items is borrowed and read; compare is called once per element after the first. Reports none for an empty slice, and the earliest index when several elements tie. Cannot fail. Runs in time proportional to the length and allocates nothing beyond whatever compare does.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the sequence to work on
comparefunc(T, T) -> Orderinghow two elements order against each other

Returns: the index of the smallest element under compare, or none when empty

MaxIndexBy<T>

pub func MaxIndexBy<T>(
    items: T[..],
    compare: func(T, T) -> Ordering
) -> uint?

The index of the largest element of items, with compare deciding. items is borrowed and read; compare is called once per element after the first. Reports none for an empty slice, and the earliest index when several elements tie. Cannot fail. Runs in time proportional to the length and allocates nothing beyond whatever compare does.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the sequence to work on
comparefunc(T, T) -> Orderinghow two elements order against each other

Returns: the index of the largest element under compare, or none when empty

MinMaxIndex<T>

pub func MinMaxIndex<T>(
    items: T[..],
    smallest: *var uint,
    largest: *var uint
) -> bool

The indices of the smallest and largest elements of items, in one pass. items is borrowed and read; smallest and largest are written only when this reports true. Reports false for an empty slice and writes neither. Ties go to the earliest index on both ends, exactly as the single-ended searches promise. Two separate passes would compare each element twice; this compares them in pairs and so makes about three comparisons for every two elements. Requires < on the element type. Cannot fail. Runs in time proportional to the length and allocates nothing. smallest and largest must be non-null and writable.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the sequence to work on
smallest*var uintreceives the index of the smallest element
largest*var uintreceives the index of the largest element

Returns: whether there was anything to report, which an empty sequence is not

MinMaxIndexBy<T>

pub func MinMaxIndexBy<T>(
    items: T[..],
    compare: func(T, T) -> Ordering,
    smallest: *var uint,
    largest: *var uint
) -> bool

The indices of the smallest and largest elements of items, in one pass, with compare deciding. items is borrowed and read; smallest and largest are written only when this reports true. Reports false for an empty slice and writes neither. Ties go to the earliest index on both ends. Cannot fail. Runs in time proportional to the length and allocates nothing beyond whatever compare does. smallest and largest must be non-null and writable.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the sequence to work on
comparefunc(T, T) -> Orderinghow two elements order against each other
smallest*var uintreceives the index of the smallest element
largest*var uintreceives the index of the largest element

Returns: whether there was anything to report, which an empty sequence is not

Min<T>

pub func Min<T>(left: T, right: T) -> T

The smaller of two values, or left when neither is smaller. Both are read by value and one is returned by copy. Requires < on the type. Cannot fail. Runs in constant time and allocates nothing.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
leftTone of the two values
rightTthe other

Returns: the smaller of the two, or left when they tie

Max<T>

pub func Max<T>(left: T, right: T) -> T

The larger of two values, or left when neither is larger. Both are read by value and one is returned by copy. Requires < on the type. Cannot fail. Runs in constant time and allocates nothing.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
leftTone of the two values
rightTthe other

Returns: the larger of the two, or left when they tie

Clamp<T>

pub func Clamp<T>(value: T, low: T, high: T) -> T

value brought into the range low to high, inclusive at both ends. All three are read by value and one is returned by copy. A low above high is an empty range with no answer that could be right, so low is returned — the caller passed a range it cannot have meant. Requires < on the type. Cannot fail. Runs in constant time and allocates nothing.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
valueTthe value to bring into range
lowTthe lowest allowed value
highTthe highest allowed value

Returns: value brought into [low, high]

ClampBy<T>

pub func ClampBy<T>(
    value: T,
    low: T,
    high: T,
    compare: func(T, T) -> Ordering
) -> T

value brought into the range low to high, with compare deciding. All three values are read by value and one is returned by copy; compare is called at most three times. A low above high returns low, exactly as Clamp does. Cannot fail. Runs in constant time and allocates nothing beyond whatever compare does.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
valueTthe value to bring into range
lowTthe lowest allowed value
highTthe highest allowed value
comparefunc(T, T) -> Orderinghow two elements order against each other

Returns: value brought into [low, high] under compare