Documentation
¶
Overview ¶
Package collection provides commonly used collection data structures and algorithms that are not currently in the Go standard libraries.
Index ¶
- func JustValues[K comparable, V any](pairs []KeyValue[K, V]) []V
- func MapDifference[K comparable, V any](a map[K]V, b map[K]V) map[K]V
- func MapIntersection[K comparable, V any](a map[K]V, b map[K]V) map[K]V
- func MapPairIntersection[K comparable, V comparable](a map[K]V, b map[K]V) map[K]V
- func MapSymmetricDifference[K comparable, V any](a map[K]V, b map[K]V) map[K]V
- func MapUnion[K comparable, V any](a map[K]V, b map[K]V) map[K]V
- func SliceRemoveAt[T any](s []T, index int) []T
- func SliceRemoveAtFast[T any](s []T, index int) []T
- func SliceSafeRemoveAt[T any](s []T, index int) ([]T, error)
- func SliceSafeRemoveAtFast[T any](s []T, index int) ([]T, error)
- type KeyValue
- type Pair
- type Set
- func (s Set[T]) Contains(item T) bool
- func (s Set[T]) ContainsSlice(items []T) bool
- func (a Set[T]) Difference(b Set[T]) Set[T]
- func (s Set[T]) Insert(item T) bool
- func (s Set[T]) InsertSlice(items []T)
- func (a Set[T]) Intersection(b Set[T]) Set[T]
- func (s Set[T]) Items() []T
- func (s Set[T]) Len() int
- func (s Set[T]) Remove(item T) bool
- func (s Set[T]) RemoveSlice(items []T)
- func (a Set[T]) SymmetricDifference(b Set[T]) Set[T]
- func (a Set[T]) Union(b Set[T]) Set[T]
- type SortOrder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JustValues ¶
func JustValues[K comparable, V any](pairs []KeyValue[K, V]) []V
JustValues return a slice of just the values from the given slice of KeyValue pairs.
func MapDifference ¶
func MapDifference[K comparable, V any](a map[K]V, b map[K]V) map[K]V
Return a new map that contains only the items that are present in a but not in b.
func MapIntersection ¶
func MapIntersection[K comparable, V any](a map[K]V, b map[K]V) map[K]V
Return a new map that contains only the keys that are present in both sets. Only values from a will be used.
func MapPairIntersection ¶
func MapPairIntersection[K comparable, V comparable](a map[K]V, b map[K]V) map[K]V
Return a new map that contains only the key-value pairs that are present in both sets. The keys and values of both a and b need to match to qualify for the new map.
func MapSymmetricDifference ¶
func MapSymmetricDifference[K comparable, V any](a map[K]V, b map[K]V) map[K]V
Return a new map that contains only the items that are present in one or the other map but not the items that appear in both maps.
func MapUnion ¶
func MapUnion[K comparable, V any](a map[K]V, b map[K]V) map[K]V
Return a new map that is the union of a and b that will contain all of the keys from both a and b. If b has the same key as a, then the value of a will be used.
func SliceRemoveAt ¶
Remove the element at the specified index while preserving the element order. NOTE: The underlying array is modified. This function does not check that the index is within bounds.
func SliceRemoveAtFast ¶
Remove the element at the specified index without preserving the element order. The last element in the slice is copied to s[index]. NOTE: The underlying array is modified. This function does not check that the index is within bounds.
func SliceSafeRemoveAt ¶
Remove the element at the specified index while preserving the element order. NOTE: The underlying array is modified. An error will be returned if the index is out of bounds.
func SliceSafeRemoveAtFast ¶
Remove the element at the specified index without preserving the element order. NOTE: The underlying array is modified. An error will be returned if the index is out of bounds.
Types ¶
type KeyValue ¶
type KeyValue[K comparable, V any] struct { Key K Value V }
KeyValue is a tuple of a Key and Value pair.
func MapSortedByKeys ¶
Return a slice of KeyValue pairs by sorting the keys from the specified map The value type has to be one of the cmp.Ordered constraints (types that implement <).
func MapSortedByValue ¶
func MapSortedByValue[K comparable, V cmp.Ordered](m map[K]V, order SortOrder) []KeyValue[K, V]
Return a slice of KeyValue pairs by sorting the values from the specified map The value type has to be one of the cmp.Ordered constraints (types that implement <).
func MapSortedByValueFunc ¶
func MapSortedByValueFunc[K comparable, V any](m map[K]V, less func(lhs V, rhs V) bool) []KeyValue[K, V]
Return a slice of KeyValue pairs by sorting the values from the specified map using the less function provided.
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set contains a collection of unique items. See https://en.wikipedia.org/wiki/Set_(mathematics) for set theory.
func NewSetFrom ¶
func NewSetFrom[T comparable](args ...[]T) Set[T]
Create a new set that contains only the unique items from a number of slices.
func NewSetWithCapacity ¶
func NewSetWithCapacity[T comparable](capacity int) Set[T]
Create a new set that can store items of type T with the capacity pre-allocated.
func (Set[T]) ContainsSlice ¶
ContainsSlice returns true if all items in the slice is present in the set.
func (Set[T]) Difference ¶
Return a new set that contains only the items that are present in this set but not in b.
func (Set[T]) Insert ¶
Insert a new item into the set. Returns true if the item could be inserted and false if the item is already in the set.
func (Set[T]) InsertSlice ¶
func (s Set[T]) InsertSlice(items []T)
Insert a slice of items into the set.
func (Set[T]) Intersection ¶
Return a new set that contains only the items that are present in both sets.
func (Set[T]) Remove ¶
Remove the item from the set. Returns true if the item was in the set before removing.
func (Set[T]) RemoveSlice ¶
func (s Set[T]) RemoveSlice(items []T)
Remove a slice of items from the set.
func (Set[T]) SymmetricDifference ¶
Return a new set that contains only the items that are present in one or the other set but not the items that appear in both sets.
type SortOrder ¶
type SortOrder bool
SortOrder determines if the collection should be sorted Ascending or Descending.