Documentation ¶
Overview ¶
Package slices contains common utilities to work on slices of any type.
Index ¶
- func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S
- func Diff[S ~[]T, T comparable](a, b S) []T
- func SortedUnique[S ~[]T, T constraints.Ordered](s S) S
- func SortedUniqueFunc[S ~[]T, T any](s S, less func(i, j int) bool, eq func(a, b T) bool) S
- func SubsetOf[S ~[]T, T comparable](a, b S) (bool, []T)
- func Unique[S ~[]T, T comparable](s S) S
- func UniqueFunc[S ~[]T, T any, K comparable](s S, key func(i int) K) S
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeleteFunc ¶ added in v1.12.18
DeleteFunc removes any elements from s for which del returns true, returning the modified slice. When DeleteFunc removes m elements, it might not modify the elements s[len(s)-m:len(s)]. If those elements contain pointers you might consider zeroing those elements so that objects they reference can be garbage collected.
func Diff ¶
func Diff[S ~[]T, T comparable](a, b S) []T
Diff returns a slice of elements which is the difference of a and b. The returned slice keeps the elements in the same order found in the "a" slice. Both input slices are considered as sets, that is, all elements are considered as unique when computing the difference.
func SortedUnique ¶
func SortedUnique[S ~[]T, T constraints.Ordered](s S) S
SortedUnique sorts and dedup the input slice in place. It uses the < operator to compare the elements in the slice and thus requires the elements to satisfies contraints.Ordered.
func SortedUniqueFunc ¶
SortedUniqueFunc is like SortedUnique but allows the user to specify custom functions for ordering (less function) and comparing (eq function) the elements in the slice. This is useful in all the cases where SortedUnique cannot be used: - for types that do not satisfy constraints.Ordered (e.g: composite types) - when the user wants to customize how elements are compared (e.g: user wants to enforce reverse ordering)
func SubsetOf ¶
func SubsetOf[S ~[]T, T comparable](a, b S) (bool, []T)
SubsetOf returns a boolean that indicates if slice a is a subset of slice b. In case it is not, the returned slice contains all the unique elements that are in a but not in b.
func Unique ¶
func Unique[S ~[]T, T comparable](s S) S
Unique deduplicates the elements in the input slice, preserving their ordering and modifying the slice in place. Unique relies on a map to find multiple occurrences of the same elements. For slices with a size less than 192 elements, a simpler O(N^2) search algorithm that does not allocate memory is used instead. Limit of 192 has been experimentally derived (look at BenchmarkUnique for more information).
func UniqueFunc ¶
func UniqueFunc[S ~[]T, T any, K comparable](s S, key func(i int) K) S
UniqueFunc deduplicates the elements in the input slice like Unique, but takes a function to extract the comparable "key" to compare T. This is slower than Unique, but can be used with non-comparable elements.
Types ¶
This section is empty.