slices

package
v1.15.4 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package slices contains common utilities to work on slices of any type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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

func SortedUniqueFunc[S ~[]T, T any](
	s S,
	less func(i, j int) bool,
	eq func(a, b T) bool,
) S

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.

Jump to

Keyboard shortcuts

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