Numeric sequences v0.1.0

Source
Running sums and scans, counting sequences, and an overflow-free midpoint.

PartialSum<T>

pub func PartialSum<T>(
    items: T[..],
    into: var T[..]
) -> bool

Writes the running sums of items into into: element i becomes the sum of the first i + 1 elements. into may be items itself. Requires + on the element type, with whatever overflow it has. Runs in time proportional to the length and allocates nothing.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the sequence to sum
intovar T[..]where the sums go, at least as long as items

Returns: whether into was long enough; nothing is written when it was not

InclusiveScan<T>

pub func InclusiveScan<T>(
    items: T[..],
    into: var T[..],
    combine: func(T, T) -> T
) -> bool

Writes the running combination of items into into: element i becomes the first i + 1 elements combined left to right with combine. PartialSum is this scan with +. into may be items itself. Runs in time proportional to the length and allocates nothing beyond whatever combine does.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the sequence to scan
intovar T[..]where the results go, at least as long as items
combinefunc(T, T) -> Thow two values combine

Returns: whether into was long enough; nothing is written when it was not

ExclusiveScan<T>

pub func ExclusiveScan<T>(
    items: T[..],
    into: var T[..],
    initial: T,
    combine: func(T, T) -> T
) -> bool

Writes the running combination of items before each element into into: element i becomes initial combined with the first i elements, so the first result is initial and the last element of items takes part in none. into may be items itself. Runs in time proportional to the length and allocates nothing beyond whatever combine does.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the sequence to scan
intovar T[..]where the results go, at least as long as items
initialTthe value before anything is combined
combinefunc(T, T) -> Thow two values combine

Returns: whether into was long enough; nothing is written when it was not

Iota<T>

pub func Iota<T>(into: var T[..], start: T, step: T)

Fills into with start, start + step, start + 2 * step and so on. Requires + on the element type, with whatever overflow it has. Runs in time proportional to the length and allocates nothing.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
intovar T[..]the sequence to fill
startTthe first value
stepTwhat each value adds to the one before

Midpoint

pub func Midpoint(a: int64, b: int64) -> int64
pub func Midpoint(a: int32, b: int32) -> int32
pub func Midpoint(a: uint64, b: uint64) -> uint64
pub func Midpoint(a: uint32, b: uint32) -> uint32
pub func Midpoint(a: float64, b: float64) -> float64

Halfway between a and b, without overflowing to infinity when both are near the largest finite value.

OverloadSummary
pub func Midpoint(a: int64, b: int64) -> int64Halfway between a and b, rounded toward a, without overflowing however far apart they are.
pub func Midpoint(a: int32, b: int32) -> int32Halfway between a and b, rounded toward a, without overflowing however far apart they are.
pub func Midpoint(a: uint64, b: uint64) -> uint64Halfway between a and b, rounded toward a, without overflowing however far apart they are.
pub func Midpoint(a: uint32, b: uint32) -> uint32Halfway between a and b, rounded toward a, without overflowing however far apart they are.
pub func Midpoint(a: float64, b: float64) -> float64Halfway between a and b, without overflowing to infinity when both are near the largest finite value.

Source

Parameters

NameTypeDescription
aint64 / int32 / uint64 / uint32 / float64one end
bint64 / int32 / uint64 / uint32 / float64the other end

Returns: the midpoint