Documentation ¶
Overview ¶
slices implements a set of useful functions for manipulating slices that have not provided by golang.org/x/exp/slices yet This package is just a temporary solution for internal use. It will be removed when golang.org/x/exp/slices provides the same functions.
Index ¶
- func FindNext[E any](s []E, pivot int, iter Iterator[E]) int
- func FindPrev[E any](s []E, pivot int, iter Iterator[E]) int
- func FindRange[E any](s []E, start, end int, iter Iterator[E]) int
- func IndexGreaterOrEqual[E any](s []E, pivot int, iter Iterator[E]) int
- func IndexLessThan[E any](s []E, pivot int, iter Iterator[E]) int
- func IndexRange[E any](s []E, start, end int, iter Iterator[E]) int
- func LongestPrefixIndex[T comparable](s1, s2 []T) (_ int)
- func MismatchIndex[T comparable](s1, s2 []T) (_ int)
- func Range[S ~[]E, E constraints.Integer](stop E) S
- func RangeG[S ~[]E, E constraints.Ordered](start, stop E, N int, step func(i int, e E) E) S
- func RangeInteger[S ~[]E, E constraints.Integer](start, stop, step E) S
- func Reset[T any](s []T, i, j int)
- func SubClone[T any](s []T, start, end int) []T
- func Walk[E comparable](s []E, i int, v E) []E
- func WalkParallel[E comparable](f func(i int) bool, s ...[][]E) int
- type Iterator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindNext ¶
FindNext calls iter on each element in s starting from pivot, and returns the index of the first element for which iter returns true.
func FindPrev ¶
FindPrev calls iter on each element in s starting from pivot, and returns the index of the first element for which iter returns true.
func FindRange ¶
FindRange calls iter on each element in s[start:end], and returns the index of the first element for which iter returns true.
func IndexGreaterOrEqual ¶
[pivot, len(s))
func LongestPrefixIndex ¶
func LongestPrefixIndex[T comparable](s1, s2 []T) (_ int)
LongestPrefixIndex returns the index of the last element in the longest common prefix of s1 and s2.
func MismatchIndex ¶
func MismatchIndex[T comparable](s1, s2 []T) (_ int)
MismatchIndex returns the first index of mismatched value of s1 and s2. MismatchIndex will assume that s1 and s2 have the same length. If s1 and s2 have different length, it will conceptually pad the shorter slice with an imagination value that can't be matched. With that logic, if s1 is a prefix of s2, returns len(s1). If s1 and s2 have no mismatched value and len(s1) == len(s2), returns -1.
func Range ¶
func Range[S ~[]E, E constraints.Integer](stop E) S
Range generates a slice of integers from 0 to `stop` with a step of 1. Range return nil if `stop` is negative. For example, Range[int](10) returns []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.
func RangeG ¶
func RangeG[S ~[]E, E constraints.Ordered](start, stop E, N int, step func(i int, e E) E) S
RangeG generates a slice of ordered values with cap `N` from `start` to `stop` with a step of `step`. In detail, Range pushes `start` to the slice, then repeatedly calls `step` with last value in the slice to generate the next last value in the slice, until the next value is greater than or equal to `stop` or the size of the slice reaches `N`. RangeG panics if `step` is nil. RangeG maybe considered expensive if `N` is large because it always allocates a slice of cap `N` even if the size of the returned slice is smaller.
func RangeInteger ¶
func RangeInteger[S ~[]E, E constraints.Integer](start, stop, step E) S
RangeInteger generates a sequence of integers from `start` to `stop` with a step of `step`. For example, RangeInt[int](0, 10, 1) returns []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. RangeInt can also accept negative `step` to generate a sequence of integers in reverse order. For example, RangeInteger[int](10, 0, -1) returns []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}.
func Reset ¶
Reset modifies the slice s by setting all elements to zero value of type T from index i to j.
func Walk ¶
func Walk[E comparable](s []E, i int, v E) []E
Walk walks through the slice s from position i until it finds the value v. If v is found, returns the list of elements from i to the position of v. If v is not found, returns empty slice.
func WalkParallel ¶
func WalkParallel[E comparable](f func(i int) bool, s ...[][]E) int