core/slice

slice

Types

6

Constants

1

Procedures

113

advance_slices

advance_slices :: proc(slices: S, elems: int) -> (S)Source

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]]

binary_search_by

binary_search_by :: proc(array: A, key: K, f: proc() -> (Ordering)) -> (index: int, found: bool)Source

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.

  • index: int
  • found: bool

clone

clone :: proc(a: T, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> ([]E, runtime.Allocator_Error)Source

copies a slice into a new slice

linear_search_proc

linear_search_proc :: proc(array: A, f: proc() -> (bool)) -> (index: int, found: bool)Source

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

  • array: The slice to search in.
  • f: The search condition.
  • 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

linear_search_reverse :: proc(array: A, key: T) -> (index: int, found: bool)Source

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

  • array: The slice to search in.
  • key: The element to search for.
  • 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: A, f: proc() -> (bool)) -> (index: int, found: bool)Source

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

  • array: The slice to search in.
  • f: The search condition.
  • 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: mem.Allocator = context.allocator, loc = #caller_location) -> (iter: Permutation_Iterator(T), error: runtime.Allocator_Error)Source

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.

  • slice: The slice to permute.
  • allocator: (default is context.allocator)
  • iter: The iterator, to be passed to permute.
  • error: An Allocator_Error, if allocation failed.

prefix_length

prefix_length :: proc(a: T, b: T) -> (n: int)Source

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

reinterpret

reinterpret :: proc(T: typeid, s: []V) -> ([]U)Source

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]

scanner

scanner :: proc(s: S, initializer: V, f: proc() -> (V), allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []V, err: runtime.Allocator_Error)Source

sort

sort :: proc(data: T)Source

sort sorts a slice This sort is not guaranteed to be stable

sort_by

sort_by :: proc(data: T, less: proc(i: E, j: E) -> (bool))Source

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_with_indices

sort_by_with_indices :: proc(data: T, less: proc(i: E, j: E) -> (bool), allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (indices: []int)Source

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, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (indices: []int)Source

sort sorts a slice and returns a slice of the original indices This sort is not guaranteed to be stable

stable_sort_by

stable_sort_by :: proc(data: T, less: proc(i: E, j: E) -> (bool))Source

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.

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(data: T, cmp: proc(i: E, j: E) -> (Ordering))Source

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.

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

suffix_length

suffix_length :: proc(a: T, b: T) -> (n: int)Source

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

to_bytes

to_bytes :: proc(s: []T) -> ([]u8)Source

Turn a slice into a byte slice.

See `slice.reinterpret` to go the other way.

unique

unique :: proc(s: S) -> (S)Source

'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: S, eq: proc() -> (bool)) -> (S)Source

'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

2

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.