Skip to content
volt
GitHub

std::slice

Algorithms on any T[..]: sort, search, reverse, min and max. They order elements with cmp and match them with eq (see std::compare).

Source: std/slice.volt

Methods on other types

Attached to built-in types, or to every type (T).

<T: type, A: std::mem::allocator = std::mem::default_allocator> attach fn sort(this: T[..]&, allocator: A = {}) -> void

sorts in place, smallest first by cmp; stable (equal elements keep their order). Past 16 elements it takes scratch room for n elements from allocator. Integers sort by their bits (a radix sort, in a few passes over the elements, with no compares), the rest with a merge sort

<T: type, F: type, A: std::mem::allocator = std::mem::default_allocator> attach fn sort_by(this: T[..]&, order: F, allocator: A = {}) -> void

sorts in place by order(a, b) (negative: a goes first); stable

<T: type> attach fn is_sorted(this: T[..]&) -> bool

whether every element sorts no earlier than the one before it

<T: type> attach fn lower_bound(this: T[..]&, x: T) -> usize

in a sorted slice: the first index whose element doesn't sort before x (len if none)

<T: type> attach fn binary_search(this: T[..]&, x: T) -> usize?

in a sorted slice: an index holding x, if one does

<T: type> attach fn swap(this: T[..]&, i: usize, j: usize) -> void

swaps elements i and j

<T: type> attach fn reverse(this: T[..]&) -> void

reverses the order in place

<T: type> attach fn index_of(this: T[..]&, x: T) -> usize?

the index of the first element equal to x (by eq), if any

<T: type> attach fn contains(this: T[..]&, x: T) -> bool

whether an element equals x (by eq)

<T: type> attach fn min(this: T[..]&) -> T*

the smallest element by cmp (the first of equals), or null when empty

<T: type> attach fn max(this: T[..]&) -> T*

the largest element by cmp (the first of equals), or null when empty