package core:slice

⌘K
Ctrl+K
or
/

    Overview

    Utility procedures for working with slices, including sorting and searching them.

    Packages 1

    heapA generic in-place max heap on a slice for any type.

    Index

    Constants (0)

    This section is empty.

    Variables (0)

    This section is empty.

    Procedures (118)

    Types

    Generic_Cmp ¶

    Generic_Cmp :: proc(lhs, rhs: rawptr, user_data: rawptr) -> Ordering

    Map_Entry ¶

    Map_Entry :: struct($Key: typeid, $Value: typeid) where intrinsics.type_is_valid_map_key(Key) {
    	… // See source for fields
    }

    Map_Entry_Info ¶

    Map_Entry_Info :: struct($Key: typeid, $Value: typeid) where intrinsics.type_is_valid_map_key(Key) {
    	… // See source for fields
    }

    Ordering ¶

    Ordering :: enum int {
    	Less    = -1, 
    	Equal   = 0, 
    	Greater = +1, 
    }

    Permutation_Iterator ¶

    Permutation_Iterator :: struct($T: typeid) {
    	… // See source for fields
    }
     

    An in-place permutation iterator.

    Related Procedures With Parameters

    Constants

    This section is empty.

    Variables

    This section is empty.

    Procedures

    advance_slices ¶

    advance_slices :: proc(slices: $S/[][]$T, elems: int) -> S {…}
     

    Removes the first n elements (elems) from a slice of slices, spanning inner slices and dropping empty ones.

    If elems is out of bounds (more than the total) this will trigger a bounds check.

    Example:
    import "core:fmt"
    import "core:slice"
    
    advance_slices_example :: proc() {
    	slices := [][]byte {
    		{1, 2, 3, 4},
    		{5, 6, 7},
    	}
    
    	fmt.println(slice.advance_slices(slices, 4))
    }
    
    Output:
    [[5, 6, 7]]
    

    all_of ¶

    @(require_results)
    all_of :: proc "contextless" (s: $S/[]$T, value: $T) -> bool {…}

    all_of_proc ¶

    @(require_results)
    all_of_proc :: proc(s: $S/[]$T, f: proc($T) -> bool) -> bool {…}

    any_of ¶

    @(require_results)
    any_of :: proc "contextless" (s: $S/[]$T, value: $T) -> bool {…}

    any_of_proc ¶

    @(require_results)
    any_of_proc :: proc(s: $S/[]$T, f: proc($T) -> bool) -> bool {…}

    as_ptr ¶

    @(require_results)
    as_ptr :: proc "contextless" (array: $T/[]$E) -> [^]E {…}
    @(require_results)
    binary_search :: proc(array: $A/[]$T, key: $T) -> (index: int, found: bool) {…}
     

    Searches the given slice for the given element. If the slice is not sorted, the returned index is unspecified and meaningless.

    If the value is found then the returned int is the index of the matching element. If there are multiple matches, then any one of the matches could be returned.

    If the value is not found then the returned int is the index where a matching element could be inserted while maintaining sorted order.

    For slices of more complex types see: binary_search_by

    Example:
    /*
    Looks up a series of four elements. The first is found, with a
    uniquely determined position; the second and third are not
    found; the fourth could match any position in `[1, 4]`.
    */
    
    index: int
    found: bool
    
    s := []i32{0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
    
    index, found = slice.binary_search(s, 13)
    assert(index == 9 && found == true)
    
    index, found = slice.binary_search(s, 4)
    assert(index == 7 && found == false)
    
    index, found = slice.binary_search(s, 100)
    assert(index == 13 && found == false)
    
    index, found = slice.binary_search(s, 1)
    assert(index >= 1 && index <= 4 && found == true)
    

    binary_search_by ¶

    @(require_results)
    binary_search_by :: proc(
    	array: $A/[]$T, 
    	key:   $K, 
    	f:     proc($T, $K) -> Ordering, 
    ) -> (index: int, found: bool) {…}
     

    Searches the given slice for the given element. If the slice is not sorted, the returned index is unspecified and meaningless.

    If the value is found then the returned int is the index of the matching element. If there are multiple matches, then any one of the matches could be returned.

    If the value is not found then the returned int is the index where a matching element could be inserted while maintaining sorted order.

    The array elements and key may be different types. This allows the filter procedure to compare keys against a slice of structs, one struct value at a time.

    Returns:

    • index int
    • found bool

    bitset_to_enum_slice_with_buffer ¶

    @(require_results)
    bitset_to_enum_slice_with_buffer :: proc(buf: []$E, bs: $T) -> (slice: []E) {…}
     

    Turn a bit_set[E] into a []E e.g.: sl := slice.bitset_to_enum_slice(flag_buf[:], bs)

    bitset_to_enum_slice_with_make ¶

    @(require_results)
    bitset_to_enum_slice_with_make :: proc(
    	bs:        $T, 
    	$E:         typeid, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (slice: []E) {…}
     

    Turn a bit_set[E] into a []E, allocates e.g.: sl := slice.bitset_to_enum_slice(bs)

    bytes_from_ptr ¶

    @(require_results)
    bytes_from_ptr :: proc "contextless" (ptr: rawptr, byte_count: int) -> []u8 {…}
     

    Turn a pointer and a length into a byte slice.

    clone ¶

    @(require_results)
    clone :: proc(
    	a:         $T/[]$E, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> ([]E, runtime.Allocator_Error) #optional_ok {…}
     

    copies a slice into a new slice

    clone_to_dynamic ¶

    @(require_results)
    clone_to_dynamic :: proc(
    	a:         $T/[]$E, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> ([dynamic]E, runtime.Allocator_Error) #optional_ok {…}
     

    copies slice into a new dynamic array

    cmp ¶

    @(require_results)
    cmp :: proc(a, b: $E) -> Ordering {…}

    cmp_proc ¶

    @(require_results)
    cmp_proc :: proc($E: typeid) -> proc(E, E) -> Ordering {…}

    cold_sort ¶

    cold_sort :: proc(data: $T/[]$E) {…}
     

    cold_sort, cold_sort_by, and cold_sort_by_cmp sort like sort, sort_by, and sort_by_cmp with a small part of their code, and more slowly on large inputs: for sorts that rarely run, or only on a few items. Equal elements keep their original order

    cold_sort_by ¶

    cold_sort_by :: proc(data: $T/[]$E, less: proc(i, j: $E) -> bool) {…}
     

    less must be a strict weak order. In particular less(x, x) must be false: use <, never <=. With any other less, the result is a permutation of data in no particular order.

    cold_sort_by_cmp ¶

    cold_sort_by_cmp :: proc(data: $T/[]$E, cmp: proc(i, j: $E) -> Ordering) {…}
     

    cmp must be a strict weak order. In particular cmp(x, x) must return .Equal. With any other cmp, the result is a permutation of data in no particular order.

    concatenate ¶

    @(require_results)
    concatenate :: proc(
    	a:         []$T/[]$E, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (res: T, err: runtime.Allocator_Error) #optional_ok {…}

    contains ¶

    @(require_results)
    contains :: proc "contextless" (array: $T/[]$E, value: $E) -> bool {…}

    count ¶

    @(require_results)
    count :: proc "contextless" (s: $S/[]$T, value: $T) -> (n: int) {…}

    count_proc ¶

    @(require_results)
    count_proc :: proc(s: $S/[]$T, f: proc($T) -> bool) -> (n: int) {…}

    destroy_permutation_iterator ¶

    destroy_permutation_iterator :: proc(
    	iter:      Permutation_Iterator($T), 
    	allocator := context.allocator, 
    ) {…}
     

    Free the state allocated by make_permutation_iterator.

    Inputs:

    • iter The iterator created by make_permutation_iterator.
    • allocator The allocator used to create the iterator. (default is context.allocator)

    dot_product ¶

    @(require_results)
    dot_product :: proc "contextless" (a, b: $S/[]$T) -> (r: T, ok: bool) {…}

    enum_slice_to_bitset ¶

    @(require_results)
    enum_slice_to_bitset :: proc "contextless" (enums: []$E, $T: typeid/bit_set[$E]) -> (bits: T) {…}
     

    Turn a []E into bit_set[E] e.g.: bs := slice.enum_slice_to_bitset(my_flag_slice, rl.ConfigFlags)

    enumerated_array ¶

    @(require_results)
    enumerated_array :: proc "contextless" (ptr: ^$T) -> []intrinsics.type_elem_type(T) {…}
     

    Convert a pointer to an enumerated array to a slice of the element type

    equal ¶

    @(require_results)
    equal :: proc "contextless" (a, b: $T/[]$E) -> bool {…}

    fill ¶

    fill :: proc "contextless" (array: $T/[]$E, value: $E) {…}

    filter ¶

    @(require_results)
    filter :: proc(
    	s:         $S/[]$U, 
    	f:         proc($U) -> bool, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (res: S, err: runtime.Allocator_Error) #optional_ok {…}

    filter_reverse ¶

    @(require_results)
    filter_reverse :: proc(
    	s:         $S/[]$U, 
    	f:         proc($U) -> bool, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (res: S, err: runtime.Allocator_Error) #optional_ok {…}

    first ¶

    @(require_results)
    first :: proc(array: $T/[]$E) -> E {…}

    first_ptr ¶

    @(require_results)
    first_ptr :: proc(array: $T/[]$E) -> ^E {…}

    from_ptr ¶

    @(require_results)
    from_ptr :: proc "contextless" (ptr: ^$T, count: int) -> []T {…}
     

    Turn a pointer and a length into a slice.

    get ¶

    @(require_results)
    get :: proc "contextless" (array: $T/[]$E, index: int) -> (value: E, ok: bool) {…}

    get_ptr ¶

    @(require_results)
    get_ptr :: proc "contextless" (array: $T/[]$E, index: int) -> (value: ^E, ok: bool) {…}

    has_prefix ¶

    @(require_results)
    has_prefix :: proc "contextless" (array: $T/[]$E, needle: $T/[]$E) -> bool {…}

    has_suffix ¶

    @(require_results)
    has_suffix :: proc "contextless" (array: $T/[]$E, needle: $T/[]$E) -> bool {…}

    into_dynamic ¶

    @(require_results)
    into_dynamic :: proc(a: $T/[]$E) -> [dynamic]E {…}
     

    Converts slice into a dynamic array without cloning or allocating memory

    is_empty ¶

    @(require_results)
    is_empty :: proc "contextless" (a: $T/[]$E) -> bool {…}

    is_sorted ¶

    @(require_results)
    is_sorted :: proc(array: $T/[]$E) -> bool {…}

    is_sorted_by ¶

    @(require_results)
    is_sorted_by :: proc(array: $T/[]$E, less: proc(i, j: $E) -> bool) -> bool {…}

    is_sorted_by_key ¶

    @(require_results)
    is_sorted_by_key :: proc(array: $T/[]$E, key: proc($E) -> K) -> bool {…}

    is_sorted_cmp ¶

    @(require_results)
    is_sorted_cmp :: proc(array: $T/[]$E, cmp: proc(i, j: $E) -> Ordering) -> bool {…}

    last ¶

    @(require_results)
    last :: proc(array: $T/[]$E) -> E {…}

    last_ptr ¶

    @(require_results)
    last_ptr :: proc(array: $T/[]$E) -> ^E {…}

    length ¶

    @(require_results)
    length :: proc "contextless" (a: $T/[]$E) -> int {…}
    @(require_results)
    linear_search :: proc "contextless" (array: $A/[]$T, key: $T) -> (index: int, found: bool) {…}
     

    Searches the given slice for the given element in O(n) time.

    If you need a custom search condition, see linear_search_proc

    Inputs:

    • array The slice to search in.
    • key The element to search for.

    Returns:

    • index The index i, such that array[i] is the first occurrence of key in array, or -1 if key is not present in array.
    Example:
    index: int
    found: bool
    
    a := []i32{10, 10, 10, 20}
    
    index, found = linear_search_reverse(a, 10)
    assert(index == 0 && found == true)
    
    index, found = linear_search_reverse(a, 30)
    assert(index == -1 && found == false)
    
    // Note that `index == 1`, since it is relative to `a[2:]`
    index, found = linear_search_reverse(a[2:], 20)
    assert(index == 1 && found == true)
    

    linear_search_proc ¶

    @(require_results)
    linear_search_proc :: proc(array: $A/[]$T, f: proc($T) -> bool) -> (index: int, found: bool) {…}
     

    Searches the given slice for the first element satisfying predicate f in O(n) time.

    Inputs:

    • array The slice to search in.
    • f The search condition.

    Returns:

    • index The index i, such that array[i] is the first x in array for which f(x) == true, or -1 if such x does not exist.

    linear_search_reverse ¶

    @(require_results)
    linear_search_reverse :: proc "contextless" (
    	array: $A/[]$T, 
    	key:   $T, 
    ) -> (index: int, found: bool) {…}
     

    Searches the given slice for the given element in O(n) time, starting from the slice end.

    If you need a custom search condition, see linear_search_reverse_proc

    Inputs:

    • array The slice to search in.
    • key The element to search for.

    Returns:

    • index The index i, such that array[i] is the last occurrence of key in array, or -1 if key is not present in array.
    Example:
    index: int
    found: bool
    
    a := []i32{10, 10, 10, 20}
    
    index, found = linear_search_reverse(a, 20)
    assert(index == 3 && found == true)
    
    index, found = linear_search_reverse(a, 10)
    assert(index == 2 && found == true)
    
    index, found = linear_search_reverse(a, 30)
    assert(index == -1 && found == false)
    
    // Note that `index == 1`, since it is relative to `a[2:]`
    index, found = linear_search_reverse(a[2:], 20)
    assert(index == 1 && found == true)
    

    linear_search_reverse_proc ¶

    @(require_results)
    linear_search_reverse_proc :: proc(
    	array: $A/[]$T, 
    	f:     proc($T) -> bool, 
    ) -> (index: int, found: bool) {…}
     

    Searches the given slice for the last element satisfying predicate f in O(n) time, starting from the slice end.

    Inputs:

    • array The slice to search in.
    • f The search condition.

    Returns:

    • index The index i, such that array[i] is the last x in array for which f(x) == true, or -1 if such x does not exist.

    make_permutation_iterator ¶

    make_permutation_iterator :: proc(
    	slice:     []$T, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (iter: Permutation_Iterator(T), error: runtime.Allocator_Error) #optional_ok {…}
     

    Make an iterator to permute a slice in-place.

    Allocates Using Provided Allocator

    This procedure allocates some state to assist in permutation and does not make a copy of the underlying slice. If you want to permute a slice without altering the underlying data, use clone to create a copy, then permute that instead.

    Inputs:

    • slice The slice to permute.
    • allocator (default is context.allocator)

    Returns:

    • iter The iterator, to be passed to permute.
    • error An Allocator_Error, if allocation failed.

    map_entries ¶

    map_entries :: proc(
    	m:         $M/map[$K]$V, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (entries: []Map_Entry(K, V), err: runtime.Allocator_Error) {…}

    map_entry_infos ¶

    map_entry_infos :: proc(
    	m:         $M/map[$K]$V, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (entries: []Map_Entry_Info(K, V), err: runtime.Allocator_Error) {…}

    map_keys ¶

    map_keys :: proc(
    	m:         $M/map[$K]$V, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (keys: []K, err: runtime.Allocator_Error) {…}

    map_values ¶

    map_values :: proc(
    	m:         $M/map[$K]$V, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (values: []V, err: runtime.Allocator_Error) {…}

    mapper ¶

    @(require_results)
    mapper :: proc(
    	s:         $S/[]$U, 
    	f:         proc($U) -> V, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (r: []V, err: runtime.Allocator_Error) #optional_ok {…}

    max ¶

    @(require_results)
    max :: proc "contextless" (s: $S/[]$T) -> (res: T, ok: bool) #optional_ok {…}

    max_index ¶

    @(require_results)
    max_index :: proc "contextless" (s: $S/[]$T) -> (max_index: int, ok: bool) #optional_ok {…}
     

    Find the index of the (first) maximum element in a slice.

    min ¶

    @(require_results)
    min :: proc "contextless" (s: $S/[]$T) -> (res: T, ok: bool) #optional_ok {…}

    min_index ¶

    @(require_results)
    min_index :: proc "contextless" (s: $S/[]$T) -> (min_index: int, ok: bool) #optional_ok {…}
     

    Find the index of the (first) minimum element in a slice.

    min_max ¶

    @(require_results)
    min_max :: proc "contextless" (s: $S/[]$T) -> (min, max: T, ok: bool) {…}

    none_of ¶

    @(require_results)
    none_of :: proc "contextless" (s: $S/[]$T, value: $T) -> bool {…}

    none_of_proc ¶

    @(require_results)
    none_of_proc :: proc(s: $S/[]$T, f: proc($T) -> bool) -> bool {…}

    permute ¶

    permute :: proc(iter: ^Permutation_Iterator($T)) -> (ok: bool) {…}
     

    Permute a slice in-place.

    Note that the first iteration will always be the original, unpermuted slice.

    Inputs:

    Returns:

    • ok True if the permutation succeeded, false if the iteration is complete.

    prefix_length ¶

    @(require_results)
    prefix_length :: proc "contextless" (a, b: $T/[]$E) -> (n: int) {…}
     

    return the prefix length common between slices a and b.

    slice.prefix_length([]u8{1, 2, 3, 4}, []u8{1}) -> 1 slice.prefix_length([]u8{1, 2, 3, 4}, []u8{1, 2, 3}) -> 3 slice.prefix_length([]u8{1, 2, 3, 4}, []u8{2, 3, 4}) -> 0

    ptr_add ¶

    ptr_add :: proc "contextless" (p: $P/^$T, x: int) -> ^T {…}

    ptr_rotate ¶

    ptr_rotate :: proc "contextless" (left: int, mid: ^$T, right: int) {…}

    ptr_sub ¶

    ptr_sub :: proc "contextless" (p: $P/^$T, x: int) -> ^T {…}

    ptr_swap_non_overlapping ¶

    ptr_swap_non_overlapping :: proc "contextless" (x, y: rawptr, len: int) {…}

    ptr_swap_overlapping ¶

    ptr_swap_overlapping :: proc "contextless" (x, y: rawptr, len: int) {…}

    reduce ¶

    @(require_results)
    reduce :: proc(s: $S/[]$U, initializer: $V, f: proc($V, $U) -> V) -> V {…}

    reduce_reverse ¶

    @(require_results)
    reduce_reverse :: proc(s: $S/[]$U, initializer: $V, f: proc($V, $U) -> V) -> V {…}

    reinterpret ¶

    @(require_results)
    reinterpret :: proc "contextless" ($T: typeid/[]$U, s: []$V) -> []U {…}
     

    Turn a slice of one type, into a slice of another type.

    Only converts the type and length of the slice itself. The length is rounded down to the nearest whole number of items.

    Example:
    import "core:fmt"
    import "core:slice"
    
    i64s_as_i32s :: proc() {
    	large_items := []i64{1, 2, 3, 4}
    	small_items := slice.reinterpret([]i32, large_items)
    	assert(len(small_items) == 8)
    	fmt.println(large_items, "->", small_items)
    }
    
    bytes_as_i64s :: proc() {
    	small_items := [12]byte{}
    	small_items[0] = 1
    	small_items[8] = 2
    	large_items := slice.reinterpret([]i64, small_items[:])
    	assert(len(large_items) == 1) // only enough bytes to make 1 x i64; two would need at least 8 bytes.
    	fmt.println(small_items, "->", large_items)
    }
    
    reinterpret_example :: proc() {
    	i64s_as_i32s()
    	bytes_as_i64s()
    }
    
    Output:
    [1, 2, 3, 4] -> [1, 0, 2, 0, 3, 0, 4, 0]
    [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0] -> [1]
    

    repeat ¶

    @(require_results)
    repeat :: proc(
    	s:         $S/[]$U, 
    	count:     int, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (b: S, err: runtime.Allocator_Error) #optional_ok {…}

    retain_slice ¶

    @(require_results)
    retain_slice :: proc(s: $S/[]$T, keep: []bool, loc := #caller_location) -> S {…}
     

    'retain_slice' keeps the elements of s whose value in keep is true, in order, and drops the rest. keep must be as long as s. The procedure modifies the slice in-place and returns the modified slice.

    retain_soa ¶

    @(require_results)
    retain_soa :: proc(s: $S/#soa[]$T, keep: []bool, loc := #caller_location) -> S {…}
     

    'retain_soa' keeps the elements of the #soa slice s whose value in keep is true, in order, and drops the rest. keep must be as long as s. The procedure modifies the slice in-place and returns the modified slice.

    reverse ¶

    reverse :: proc(array: $T/[]$E) {…}

    reverse_sort ¶

    reverse_sort :: proc(data: $T/[]$E) {…}

    reverse_sort_by_cmp_constant ¶

    reverse_sort_by_cmp_constant :: proc(data: $T/[]$E, $cmp: proc(i, j: $E) -> Ordering) {…}
     

    cmp must be a strict weak order. In particular cmp(x, x) must return .Equal. With any other cmp, the result is a permutation of data in no particular order.

    reverse_sort_by_cmp_runtime ¶

    reverse_sort_by_cmp_runtime :: proc(data: $T/[]$E, cmp: proc(i, j: $E) -> Ordering) {…}
     

    reverse_sort_by_cmp_runtime and reverse_sort_by_cmp_constant are the forms of reverse_sort_by_cmp. cmp must be a strict weak order. In particular cmp(x, x) must return .Equal. With any other cmp, the result is a permutation of data in no particular order.

    reverse_sort_by_constant ¶

    reverse_sort_by_constant :: proc(data: $T/[]$E, $less: proc(i, j: $E) -> bool) {…}
     

    less must be a strict weak order. In particular less(x, x) must be false: use <, never <=. With any other less, the result is a permutation of data in no particular order.

    reverse_sort_by_key ¶

    reverse_sort_by_key :: proc(data: $T/[]$E, key: proc($E) -> K) {…}

    reverse_sort_by_runtime ¶

    reverse_sort_by_runtime :: proc(data: $T/[]$E, less: proc(i, j: $E) -> bool) {…}
     

    reverse_sort_by_runtime and reverse_sort_by_constant are the forms of reverse_sort_by. less must be a strict weak order. In particular less(x, x) must be false: use <, never <=. With any other less, the result is a permutation of data in no particular order.

    rotate_left ¶

    rotate_left :: proc "contextless" (array: $T/[]$E, mid: int) {…}

    rotate_right ¶

    rotate_right :: proc "contextless" (array: $T/[]$E, k: int) {…}

    scanner ¶

    @(require_results)
    scanner :: proc(
    	s:           $S/[]$U, 
    	initializer: $V, 
    	f:           proc($V, $U) -> V, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (res: []V, err: runtime.Allocator_Error) #optional_ok {…}

    simple_equal ¶

    @(require_results)
    simple_equal :: proc "contextless" (a, b: $T/[]$E) -> bool {…}

    size ¶

    @(require_results)
    size :: proc "contextless" (a: $T/[]$E) -> int {…}
     

    Gets the byte size of the backing data

    sort ¶

    sort :: proc(data: $T/[]$E) {…}
     

    sort sorts a slice Equal elements keep their original order

    sort_by_cmp_constant ¶

    sort_by_cmp_constant :: proc(data: $T/[]$E, $cmp: proc(i, j: $E) -> Ordering) {…}
     

    cmp must be a strict weak order. In particular cmp(x, x) must return .Equal. With any other cmp, the result is a permutation of data in no particular order.

    sort_by_cmp_runtime ¶

    sort_by_cmp_runtime :: proc(data: $T/[]$E, cmp: proc(i, j: $E) -> Ordering) {…}
     

    sort_by_cmp_runtime and sort_by_cmp_constant are the forms of sort_by_cmp. cmp must be a strict weak order. In particular cmp(x, x) must return .Equal. With any other cmp, the result is a permutation of data in no particular order.

    Related Procedure Groups

    sort_by_cmp_with_data ¶

    sort_by_cmp_with_data :: proc(
    	data:      $T/[]$E, 
    	cmp:       proc(i, j: $E, user_data: rawptr) -> Ordering, 
    	user_data: rawptr, 
    ) {…}
     

    cmp must be a strict weak order. In particular cmp(x, x, user_data) must return .Equal. With any other cmp, the result is a permutation of data in no particular order.

    sort_by_constant ¶

    sort_by_constant :: proc(data: $T/[]$E, $less: proc(i, j: $E) -> bool) {…}
     

    less must be a strict weak order. In particular less(x, x) must be false: use <, never <=. With any other less, the result is a permutation of data in no particular order.

    sort_by_generic_cmp ¶

    sort_by_generic_cmp :: proc(data: $T/[]$E, cmp: Generic_Cmp, user_data: rawptr) {…}
     

    cmp must be a strict weak order. In particular cmp(&x, &x, user_data) must return .Equal. With any other cmp, the result is a permutation of data in no particular order.

    sort_by_indices_allocate ¶

    sort_by_indices_allocate :: proc(
    	data:      $T/[]$E, 
    	indices:   []int, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (sorted: T) {…}

    sort_by_indices_overwrite ¶

    sort_by_indices_overwrite :: proc(data: $T/[]$E, indices: []int) {…}

    sort_by_key ¶

    sort_by_key :: proc(data: $T/[]$E, key: proc($E) -> K) {…}
     

    TODO(bill): Should sort_by_key exist or is sort_by more than enough?

    sort_by_runtime ¶

    sort_by_runtime :: proc(data: $T/[]$E, less: proc(i, j: $E) -> bool) {…}
     

    sort_by_runtime and sort_by_constant are the forms of sort_by. less must be a strict weak order. In particular less(x, x) must be false: use <, never <=. With any other less, the result is a permutation of data in no particular order.

    Related Procedure Groups

    sort_by_with_data ¶

    sort_by_with_data :: proc(
    	data:      $T/[]$E, 
    	less:      proc(i, j: $E, user_data: rawptr) -> bool, 
    	user_data: rawptr, 
    ) {…}
     

    less must be a strict weak order. In particular less(x, x, user_data) must be false: use <, never <=. With any other less, the result is a permutation of data in no particular order.

    sort_by_with_indices ¶

    sort_by_with_indices :: proc(
    	data:      $T/[]$E, 
    	less:      proc(i, j: $E) -> bool, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (indices: []int) {…}
     

    sort_by_with_indices sorts a slice with a given procedure to test whether two values are ordered "i < j", and returns a slice of the original indices Equal elements keep their original order less must be a strict weak order. In particular less(x, x) must be false: use <, never <=. With any other less, the result is a permutation of data in no particular order.

    sort_by_with_indices_with_data ¶

    sort_by_with_indices_with_data :: proc(
    	data:      $T/[]$E, 
    	less:      proc(i, j: $E, user_data: rawptr) -> bool, 
    	user_data: rawptr, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (indices: []int) {…}
     

    less must be a strict weak order. In particular less(x, x, user_data) must be false: use <, never <=. With any other less, the result is a permutation of data in no particular order.

    sort_from_permutation_indices ¶

    sort_from_permutation_indices :: proc(data: $T/[]$E, indices: []int) {…}

    sort_with_indices ¶

    sort_with_indices :: proc(
    	data:      $T/[]$E, 
    	allocator := context.allocator, 
    	loc := #caller_location, 
    ) -> (indices: []int) {…}
     

    sort_with_indices sorts a slice and returns a slice of the original indices Equal elements keep their original order

    split_at ¶

    @(require_results)
    split_at :: proc(array: $T/[]$E, index: int) -> (a, b: T) {…}

    split_first ¶

    @(require_results)
    split_first :: proc(array: $T/[]$E) -> (first: E, rest: T) {…}

    split_last ¶

    @(require_results)
    split_last :: proc(array: $T/[]$E) -> (rest: T, last: E) {…}

    stable_sort ¶

    stable_sort :: sort
     

    sort sorts a slice Equal elements keep their original order

    suffix_length ¶

    @(require_results)
    suffix_length :: proc "contextless" (a, b: $T/[]$E) -> (n: int) {…}
     

    return the suffix length common between slices a and b.

    slice.suffix_length([]u8{1, 2, 3, 4}, []u8{1, 2, 3, 4}) -> 4 slice.suffix_length([]u8{1, 2, 3, 4}, []u8{3, 4}) -> 2 slice.suffix_length([]u8{1, 2, 3, 4}, []u8{1}) -> 0 slice.suffix_length([]u8{1, 2, 3, 4}, []u8{1, 3, 5}) -> 0 slice.suffix_length([]u8{3, 4, 5}, []u8{3, 5}) -> 1

    swap ¶

    swap :: proc(array: $T/[]$E, a, b: int) {…}

    swap_between ¶

    swap_between :: proc "contextless" (a, b: $T/[]$E) {…}

    swap_with_slice ¶

    swap_with_slice :: proc(a, b: $T/[]$E, loc := #caller_location) {…}

    to_bytes ¶

    @(require_results)
    to_bytes :: proc "contextless" (s: []$T) -> []u8 {…}
     

    Turn a slice into a byte slice.

    See slice.reinterpret to go the other way.

    to_dynamic ¶

    to_dynamic :: clone_to_dynamic
     

    copies slice into a new dynamic array

    to_type ¶

    @(require_results)
    to_type :: proc "contextless" (buf: []u8, $T: typeid) -> (T, bool) #optional_ok {…}
     

    Turns a byte slice into a type.

    unique ¶

    @(require_results)
    unique :: proc "contextless" (s: $S/[]$T) -> S {…}
     

    'unique' replaces consecutive runs of equal elements with a single copy. The procedures modifies the slice in-place and returns the modified slice.

    unique_proc ¶

    @(require_results)
    unique_proc :: proc(s: $S/[]$T, eq: proc($T, $T) -> bool) -> S {…}
     

    'unique_proc' replaces consecutive runs of equal elements with a single copy using a comparison procedure The procedures modifies the slice in-place and returns the modified slice.

    zero ¶

    zero :: proc "contextless" (array: $T/[]$E) {…}

    Procedure Groups

    retain ¶

    retain :: proc{
    	retain_slice,
    	retain_soa,
    }
    
     

    'retain' keeps the elements of a slice or #soa slice whose value in keep is true, in order, and drops the rest. The procedures modify the slice in-place and return the modified slice.

    reverse_sort_by ¶

    reverse_sort_by :: proc{
    	reverse_sort_by_runtime,
    }
    
     

    reverse_sort_by sorts a slice in the reverse of the order less gives Equal elements keep their original order less must be a strict weak order. In particular less(x, x) must be false: use <, never <=. With any other less, the result is a permutation of data in no particular order.

    reverse_sort_by_cmp ¶

    reverse_sort_by_cmp :: proc{
    	reverse_sort_by_cmp_runtime,
    }
    
     

    reverse_sort_by_cmp sorts a slice in the reverse of the order cmp gives Equal elements keep their original order cmp must be a strict weak order. In particular cmp(x, x) must return .Equal. With any other cmp, the result is a permutation of data in no particular order.

    sort_by ¶

    sort_by :: proc{
    	sort_by_runtime,
    }
    
     

    sort_by sorts a slice with a given procedure to test whether two values are ordered "i < j" Equal elements keep their original order less must be a strict weak order. In particular less(x, x) must be false: use <, never <=. With any other less, the result is a permutation of data in no particular order.

    sort_by_cmp ¶

    sort_by_cmp :: proc{
    	sort_by_cmp_runtime,
    }
    
     

    sort_by_cmp sorts a slice with a given procedure that orders two values Equal elements keep their original order cmp must be a strict weak order. In particular cmp(x, x) must return .Equal. With any other cmp, the result is a permutation of data in no particular order.

    stable_sort_by ¶

    stable_sort_by :: proc{
    	sort_by_runtime,
    }
    
     

    Sorts a slice while maintaining the relative order of elements with the same key. Two items i and j are ordered if less(i, j) returns true.

    less must be a strict weak order. In particular less(x, x) must be false: use <, never <=. With any other less, the result is a permutation of data in no particular order.

    Example:
    import "core:slice"
    import "core:fmt"
    
    stable_sort_by_example :: proc() {
    	Example :: struct { n: int, s: string }
    
    	arr := []Example {
    		{2, "name"},
    		{3, "Bill"},
    		{1, "My"},
    		{2, "is"}
    	}
    	slice.stable_sort_by(arr, proc(i, j: Example) -> bool {
    		return i.n < j.n
    	})
    	
    	for e in arr do  fmt.printf("%s ", e.s)
    	fmt.println()
    }
    
    Output:
    My name is Bill
    

    stable_sort_by_cmp ¶

    stable_sort_by_cmp :: proc{
    	sort_by_cmp_runtime,
    }
    
     

    Sorts a slice while maintaining the relative order of elements with the same key. The ordering of the any two items is defined by the user-provided cmp.

    cmp must be a strict weak order. In particular cmp(x, x) must return .Equal. With any other cmp, the result is a permutation of data in no particular order.

    Example:
    import "core:slice"
    import "core:fmt"
    
    stable_sort_by_cmp_example :: proc() {
    	Example :: struct { n: int, s: string }
    
    	arr := []Example {
    		{2, "name"},
    		{3, "Bill"},
    		{1, "My"},
    		{2, "is"}
    	}
    	slice.stable_sort_by_cmp(arr, proc(i, j: Example) -> slice.Ordering {
    		return slice.cmp(i.n, j.n)
    	})
    	
    	for e in arr do  fmt.printf("%s ", e.s)
    	fmt.println()
    }
    
    Output:
    My name is Bill
    

    Source Files

    Generation Information

    Generated with odin version dev-2026-12 (vendor "odin") Windows_amd64 @ 2026-12-03 21:06:39.177575100 +0000 UTC