Comparing slices v0.1.0

Source
Element-by-element equality, the first difference and lexicographic order between two slices.

Mismatch<T>

pub func Mismatch<T>(left: T[..], right: T[..]) -> uint?

The index of the first position where left and right differ. Both are borrowed and read. Reports none when neither runs out before a difference is found and the two are equal for their whole common length — so two slices of different lengths that agree on the shorter one report the shorter length, and two identical slices report none. Cannot fail. Runs in time proportional to the shorter length and allocates nothing.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
leftT[..]one of the two sequences
rightT[..]the other

Returns: the first index where the two differ, or none when neither runs out first

SequenceEqual<T>

pub func SequenceEqual<T>(left: T[..], right: T[..]) -> bool

Whether left and right hold the same elements in the same order. Both are borrowed and read. Different lengths are never equal, and two empty slices are, whatever their data pointers hold. Cannot fail. Runs in time proportional to the shorter length and allocates nothing.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
leftT[..]one of the two sequences
rightT[..]the other

Returns: whether both hold the same elements in the same order

Compare<T>

pub func Compare<T>(left: T[..], right: T[..]) -> Ordering

How left orders against right, element by element. Both are borrowed and read. The comparison is lexicographic, which is the order a dictionary uses: the first position where the two differ decides, and if neither differs before one runs out, the shorter is the smaller. Two equal-length slices that never differ are Equal. Requires < and == on the element type. Use CompareBy where the element has no ordering of its own or the wanted order is not its own. Cannot fail. Runs in time proportional to the shorter length and allocates nothing.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
leftT[..]one of the two sequences
rightT[..]the other

Returns: how left orders against right, comparing element by element then by length

CompareBy<T>

pub func CompareBy<T>(
    left: T[..],
    right: T[..],
    compare: func(T, T) -> Ordering
) -> Ordering

How left orders against right, with compare deciding each pair. Both slices are borrowed and read; compare is called at most once per position of the shorter one. Lexicographic exactly as Compare is, and the way to order a sequence whose elements have no < of their own — or to order it by something other than their own. Cannot fail. Runs in time proportional to the shorter length and allocates nothing beyond whatever compare does.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
leftT[..]one of the two sequences
rightT[..]the other
comparefunc(T, T) -> Orderinghow two elements order against each other

Returns: how left orders against right under compare, then by length