slices

package
v2.0.0-alpha.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clip

func Clip[S ~[]E, E any](s S) S

Clip removes unused capacity from the slice, returning s[:len(s):len(s)].

func Clone

func Clone[S ~[]E, E any](s S) S

Clone returns a copy of the slice. The elements are copied using assignment, so this is a shallow clone.

func Contains

func Contains[S ~[]E, E comparable](s S, v E) bool

Contains reports whether v is present in s.

func Delete

func Delete[S ~[]E, E any](s S, i, j int) S

Delete removes the elements s[i:j] from s, returning the modified slice. Delete panics if s[i:j] is not a valid slice of s. Delete modifies the contents of the slice s; it does not create a new slice. Delete is O(len(s)-(j-i)), so if many items must be deleted, it is better to make a single call deleting them all together than to delete one at a time.

func Diff

func Diff[S ~[]E, E comparable](a, b S) S

Diff returns a new slice containing the values which present in slice a, but not present in slice b.

func Equal

func Equal[S ~[]E, E comparable](s1, s2 S) bool

Equal reports whether two slices are equal: the same length and all elements equal. If the lengths are different, Equal returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first unequal pair. Floating point NaNs are not considered equal.

func EqualFunc

func EqualFunc[E1, E2 any](s1 []E1, s2 []E2, eq func(E1, E2) bool) bool

EqualFunc reports whether two slices are equal using a comparison function on each pair of elements. If the lengths are different, EqualFunc returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first index for which eq returns false.

func Grow

func Grow[S ~[]E, E any](slice S, n int) S

Grow increases the slice's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to the slice without another allocation. If n is negative or too large to allocate the memory, Grow panics.

func Index

func Index[S ~[]E, E comparable](s S, v E) int

Index returns the index of the first occurrence of v in s, or -1 if not present.

func IndexFunc

func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int

IndexFunc returns the first index i satisfying f(s[i]), or -1 if none do.

func Insert

func Insert[S ~[]E, E any](s S, i int, v ...E) S

Insert inserts the values v... into s at index i, returning the modified slice. In the returned slice r, r[i] == v[0]. Insert panics if i is out of range. Time complexity of this function is O(len(s) + len(v)).

func LastIndex

func LastIndex[S ~[]E, E comparable](s S, v E) int

LastIndex returns the index of the last occurrence of v in s, or -1 if not present.

func LastIndexFunc

func LastIndexFunc[S ~[]E, E any](s S, f func(E) bool) int

LastIndexFunc returns the last index i satisfying f(s[i]), or -1 if none do.

func Reverse

func Reverse[S ~[]E, E any](s S, inplace bool) S

Reverse returns a slice of the elements in reversed order. When inplace is true, it does not allocate new memory, but the slice is reversed in place.

func Split

func Split[S []E, E any](slice S, batch int) []S

Split splits a large slice []T to batches, it returns a slice of type [][]T whose elements are sub slices of slice.

func Sum

func Sum[T constraints.Integer](slice []T) int64

Sum returns the sum value of the elements in the given slice.

func Unique

func Unique[S ~[]E, E comparable](s S, inplace bool) S

Unique returns a slice containing the elements of the given slice in same order, but removes duplicate values. When inplace is true, it does not allocate new memory, the unique values will be written to the input slice from the beginning.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL