package core:slice
Index
Types (4)
Constants (0)
This section is empty.
Variables (0)
This section is empty.
Procedures (104)
- all_of
- all_of_proc
- any_of
- any_of_proc
- as_ptr
- binary_search
- binary_search_by
- bitset_to_enum_slice_with_buffer
- bitset_to_enum_slice_with_make
- bytes_from_ptr
- clone
- clone_to_dynamic
- cmp
- cmp_proc
- concatenate
- contains
- count
- count_proc
- destroy_permutation_iterator
- dot_product
- enum_slice_to_bitset
- enumerated_array
- equal
- fill
- filter
- filter_reverse
- first
- first_ptr
- from_ptr
- get
- get_ptr
- has_prefix
- has_suffix
- into_dynamic
- is_empty
- is_sorted
- is_sorted_by
- is_sorted_by_cmp
- is_sorted_by_key
- is_sorted_cmp
- last
- last_ptr
- length
- linear_search
- linear_search_proc
- linear_search_reverse
- linear_search_reverse_proc
- make_permutation_iterator
- map_entries
- map_entry_infos
- map_keys
- map_values
- mapper
- max
- max_index
- min
- min_index
- min_max
- none_of
- none_of_proc
- permute
- prefix_length
- ptr_add
- ptr_rotate
- ptr_sub
- ptr_swap_non_overlapping
- ptr_swap_overlapping
- reduce
- reduce_reverse
- reinterpret
- repeat
- reverse
- reverse_sort
- reverse_sort_by
- reverse_sort_by_cmp
- reverse_sort_by_key
- rotate_left
- rotate_right
- scanner
- simple_equal
- size
- sort
- sort_by
- sort_by_cmp
- sort_by_indices_allocate
- sort_by_indices_overwrite
- sort_by_key
- sort_by_with_indices
- sort_with_indices
- split_at
- split_first
- split_last
- stable_sort
- stable_sort_by
- stable_sort_by_cmp
- swap
- swap_between
- swap_with_slice
- to_bytes
- to_dynamic
- to_type
- unique
- unique_proc
- zero
Procedure Groups (2)
Types
Map_Entry ¶
Map_Entry :: struct($Key: typeid, $Value: typeid) where intrinsics.type_is_valid_map_key(Key) {}
Map_Entry_Info ¶
Map_Entry_Info :: struct($Key: typeid, $Value: typeid) where intrinsics.type_is_valid_map_key(Key) {}
Ordering ¶
Ordering :: enum int { Less = -1, Equal = 0, Greater = 1, }
Related Procedures With Returns
Permutation_Iterator ¶
Permutation_Iterator :: struct($Value: typeid) {}
An in-place permutation iterator.
Related Procedures With Parameters
Related Procedures With Returns
Constants
This section is empty.
Variables
This section is empty.
Procedures
binary_search ¶
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 ¶
binary_search_by :: proc(array: $T/[]$T, key: $T, f: proc($T, $T) -> 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 ¶
bitset_to_enum_slice_with_buffer :: proc(buf: []$T, bs: $T) -> (slice: []$T) {…}
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 ¶
bitset_to_enum_slice_with_make :: proc(bs: $T, $E: typeid, allocator := context.allocator) -> (slice: []typeid) {…}
Turn a bit_set[E]
into a []E
, allocates
e.g.:
sl := slice.bitset_to_enum_slice(bs)
bytes_from_ptr ¶
Turn a pointer and a length into a byte slice.
clone ¶
clone :: proc(a: $T/[]$T, allocator := context.allocator, loc := #caller_location) -> ([]$T, runtime.Allocator_Error) #optional_ok {…}
copies a slice into a new slice
clone_to_dynamic ¶
clone_to_dynamic :: proc(a: $T/[]$T, allocator := context.allocator, loc := #caller_location) -> ([dynamic]$T, runtime.Allocator_Error) #optional_ok {…}
copies slice into a new dynamic array
concatenate ¶
concatenate :: proc(a: []$T/[]$T, allocator := context.allocator) -> (res: $T/[]$T, err: runtime.Allocator_Error) #optional_ok {…}
contains ¶
contains :: proc(array: $T/[]$T, value: $T) -> bool {…}
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 ¶
dot_product :: proc(a, b: $T/[]$T) -> (r: $T, ok: bool) {…}
enum_slice_to_bitset ¶
enum_slice_to_bitset :: proc(enums: []$T, $T: typeid/bit_set[T]) -> (bits: T) {…}
Turn a []E
into bit_set[E]
e.g.:
bs := slice.enum_slice_to_bitset(my_flag_slice, rl.ConfigFlags)
enumerated_array ¶
enumerated_array :: proc(ptr: ^$T) -> []$T {…}
Convert a pointer to an enumerated array to a slice of the element type
filter ¶
filter :: proc(s: $T/[]$T, f: proc($T) -> bool, allocator := context.allocator) -> (res: $T/[]$T, err: runtime.Allocator_Error) #optional_ok {…}
filter_reverse ¶
filter_reverse :: proc(s: $T/[]$T, f: proc($T) -> bool, allocator := context.allocator) -> (res: $T/[]$T, err: runtime.Allocator_Error) #optional_ok {…}
first_ptr ¶
first_ptr :: proc(array: $T/[]$T) -> ^$T {…}
from_ptr ¶
from_ptr :: proc "contextless" (ptr: ^$T, count: int) -> []$T {…}
Turn a pointer and a length into a slice.
has_prefix ¶
has_prefix :: proc(array: $T/[]$T, needle: $T/[]$T) -> bool {…}
has_suffix ¶
has_suffix :: proc(array: $T/[]$T, needle: $T/[]$T) -> bool {…}
into_dynamic ¶
into_dynamic :: proc(a: $T/[]$T) -> [dynamic]$T {…}
Converts slice into a dynamic array without cloning or allocating memory
is_empty ¶
is_empty :: proc(a: $T/[]$T) -> bool {…}
is_sorted ¶
is_sorted :: proc(array: $T/[]$T) -> bool {…}
is_sorted_by_cmp ¶
is_sorted_by_cmp :: is_sorted_cmp
is_sorted_by_key ¶
is_sorted_by_key :: proc(array: $T/[]$T, key: proc($T) -> $T) -> bool {…}
last_ptr ¶
last_ptr :: proc(array: $T/[]$T) -> ^$T {…}
linear_search ¶
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 ¶
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 ¶
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 ¶
linear_search_reverse_proc :: proc(array: $T/[]$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) -> (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: $T/map[$T]$T, allocator := context.allocator) -> (entries: []Map_Entry($Key, $Value), err: runtime.Allocator_Error) {…}
map_entry_infos ¶
map_entry_infos :: proc(m: $T/map[$T]$T, allocator := context.allocator) -> (entries: []Map_Entry_Info($Key, $Value), err: runtime.Allocator_Error) {…}
map_keys ¶
map_keys :: proc(m: $T/map[$T]$T, allocator := context.allocator) -> (keys: []$T, err: runtime.Allocator_Error) {…}
map_values ¶
map_values :: proc(m: $T/map[$T]$T, allocator := context.allocator) -> (values: []$T, err: runtime.Allocator_Error) {…}
mapper ¶
mapper :: proc(s: $T/[]$T, f: proc($T) -> $T, allocator := context.allocator) -> (r: []$T, err: runtime.Allocator_Error) #optional_ok {…}
max_index ¶
Find the index of the (first) maximum element in a slice.
min_index ¶
Find the index of the (first) minimum element in a slice.
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:
iter: The iterator created by make_permutation_iterator
.
Returns:
ok: True if the permutation succeeded, false if the iteration is complete.
prefix_length ¶
prefix_length :: proc(a, b: $T/[]$T) -> (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
reduce_reverse ¶
reduce_reverse :: proc(s: $T/[]$T, initializer: $T, f: proc($T, $T) -> $T) -> $T {…}
reinterpret ¶
reinterpret :: proc "contextless" ($T: typeid/[]T, s: []T) -> []T {…}
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()
}
[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 ¶
repeat :: proc(s: $T/[]$T, count: int, allocator := context.allocator) -> (b: $T/[]$T, err: runtime.Allocator_Error) #optional_ok {…}
reverse_sort ¶
reverse_sort :: proc(data: $T/[]$T) {…}
reverse_sort_by ¶
reverse_sort_by :: proc(data: $T/[]$T, less: proc(i, j: $T) -> bool) {…}
reverse_sort_by_cmp ¶
reverse_sort_by_cmp :: proc(data: $T/[]$T, cmp: proc(i, j: $T) -> Ordering) {…}
reverse_sort_by_key ¶
reverse_sort_by_key :: proc(data: $T/[]$T, key: proc($T) -> $T) {…}
rotate_left ¶
rotate_left :: proc(array: $T/[]$T, mid: int) {…}
rotate_right ¶
rotate_right :: proc(array: $T/[]$T, k: int) {…}
scanner ¶
scanner :: proc(s: $T/[]$T, initializer: $T, f: proc($T, $T) -> $T, allocator := context.allocator) -> (res: []$T, err: runtime.Allocator_Error) #optional_ok {…}
simple_equal ¶
simple_equal :: proc(a, b: $T/[]$T) -> bool {…}
sort_by ¶
sort_by :: proc(data: $T/[]$T, less: proc(i, j: $T) -> bool) {…}
sort_by sorts a slice with a given procedure to test whether two values are ordered "i < j" This sort is not guaranteed to be stable
sort_by_cmp ¶
sort_by_cmp :: proc(data: $T/[]$T, cmp: proc(i, j: $T) -> Ordering) {…}
sort_by_indices_allocate ¶
sort_by_indices_allocate :: proc(data: $T/[]$T, indices: []int, allocator := context.allocator) -> (sorted: $T/[]$T) {…}
sort_by_indices_overwrite ¶
sort_by_indices_overwrite :: proc(data: $T/[]$T, indices: []int) {…}
sort_by_key ¶
sort_by_key :: proc(data: $T/[]$T, key: proc($T) -> $T) {…}
TODO(bill): Should sort_by_key
exist or is sort_by
more than enough?
sort_by_with_indices ¶
sort_by_with_indices :: proc(data: $T/[]$T, less: proc(i, j: $T) -> bool, allocator := context.allocator) -> (indices: []int) {…}
sort_by sorts a slice with a given procedure to test whether two values are ordered "i < j" This sort is not guaranteed to be stable
sort_with_indices ¶
sort_with_indices :: proc(data: $T/[]$T, allocator := context.allocator) -> (indices: []int) {…}
sort sorts a slice and returns a slice of the original indices This sort is not guaranteed to be stable
split_at ¶
split_at :: proc(array: $T/[]$T, index: int) -> (a, b: $T/[]$T) {…}
split_first ¶
split_first :: proc(array: $T/[]$T) -> (first: $T, rest: $T/[]$T) {…}
split_last ¶
split_last :: proc(array: $T/[]$T) -> (rest: $T/[]$T, last: $T) {…}
stable_sort_by ¶
stable_sort_by :: proc(data: $T/[]$T, less: proc(i, j: $T) -> bool) {…}
stable_sort_by sorts a slice with a given procedure to test whether two values are ordered "i < j"
stable_sort_by_cmp ¶
stable_sort_by_cmp :: proc(data: $T/[]$T, cmp: proc(i, j: $T) -> Ordering) {…}
swap_between ¶
swap_between :: proc(a, b: $T/[]$T) {…}
swap_with_slice ¶
swap_with_slice :: proc(a, b: $T/[]$T, loc := #caller_location) {…}
to_bytes ¶
to_bytes :: proc "contextless" (s: []$T) -> []u8 {…}
Turn a slice into a byte slice.
See slice.reinterpret
to go the other way.
to_type ¶
Turns a byte slice into a type.
unique ¶
unique :: proc(s: $T/[]$T) -> $T/[]$T {…}
'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 ¶
unique_proc :: proc(s: $T/[]$T, eq: proc($T, $T) -> bool) -> $T/[]$T {…}
'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.
Procedure Groups
bitset_to_enum_slice ¶
bitset_to_enum_slice :: proc{ bitset_to_enum_slice_with_make, bitset_to_enum_slice_with_buffer, }
sort_by_indices ¶
sort_by_indices :: proc{ sort_by_indices_allocate, _sort_by_indices, }
Source Files
Generation Information
Generated with odin version dev-2024-12 (vendor "odin") Windows_amd64 @ 2024-12-17 21:11:01.221228100 +0000 UTC