Skip to content

std.sort

in-place sorts (introsort, not stable; stable_* merge) and binary search

pub Asc :: struct($T: type) {
marker: u8,
}

smallest first

pub less :: fn(self: *Self, a: T, b: T) -> bool
pub Desc :: struct($T: type) {
marker: u8,
}

largest first

pub less :: fn(self: *Self, a: T, b: T) -> bool
pub By :: struct($T: type) {
f: fn(T, T) -> bool,
}

f(a, b) true when a goes before b

pub less :: fn(self: *Self, a: T, b: T) -> bool
pub asc :: fn($T: type, xs: []T)

in place, smallest first; not stable

pub desc :: fn($T: type, xs: []T)

in place, largest first; not stable

pub by :: fn($T: type, xs: []T, less: fn(T, T) -> bool)

in place by less; not stable

pub stable_asc :: fn($T: type, xs: []T, scratch: *Arena)

stable, smallest first; allocates an xs.len buffer in scratch

pub stable_by :: fn($T: type, xs: []T, less: fn(T, T) -> bool, scratch: *Arena)

stable by less; allocates an xs.len buffer in scratch

pub is_sorted :: fn($T: type, xs: []const T) -> bool

true when ascending

pub is_sorted_by :: fn($T: type, xs: []const T, less: fn(T, T) -> bool) -> bool

true when sorted by less

pub lower_bound :: fn($T: type, xs: []const T, key: T) -> usize

first index with xs[i] >= key; xs.len if none

pub lower_bound_by :: fn($T: type, xs: []const T, key: T, less: fn(T, T) -> bool) -> usize

lower_bound by less

pub upper_bound :: fn($T: type, xs: []const T, key: T) -> usize

first index with xs[i] > key; xs.len if none

pub upper_bound_by :: fn($T: type, xs: []const T, key: T, less: fn(T, T) -> bool) -> usize

upper_bound by less

pub search :: fn($T: type, xs: []const T, key: T) -> ?usize

index of the first element equal to key, or null

pub search_by :: fn($T: type, xs: []const T, key: T, less: fn(T, T) -> bool) -> ?usize

search by less; equal means neither is less