collection

package
v0.0.0-...-7a2a8a2 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Package collection provides commonly used collection data structures and algorithms that are not currently in the Go standard libraries.

Index

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

func SliceRemoveAt[T any](s []T, index int) []T

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

func SliceRemoveAtFast[T any](s []T, index int) []T

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

func SliceSafeRemoveAt[T any](s []T, index int) ([]T, error)

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

func SliceSafeRemoveAtFast[T any](s []T, index int) ([]T, error)

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

func MapSortedByKeys[K cmp.Ordered, V any](m map[K]V, order SortOrder) []KeyValue[K, V]

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 Pair

type Pair[A any, B any] struct {
	First  A
	Second B
}

Pair represents a tuple of 2 values.

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 NewSet

func NewSet[T comparable]() Set[T]

Create a new set that can store items of type T.

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]) Contains

func (s Set[T]) Contains(item T) bool

Returns true if the item is in the set.

func (Set[T]) ContainsSlice

func (s Set[T]) ContainsSlice(items []T) bool

ContainsSlice returns true if all items in the slice is present in the set.

func (Set[T]) Difference

func (a Set[T]) Difference(b Set[T]) Set[T]

Return a new set that contains only the items that are present in this set but not in b.

func (Set[T]) Insert

func (s Set[T]) Insert(item T) bool

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

func (a Set[T]) Intersection(b Set[T]) Set[T]

Return a new set that contains only the items that are present in both sets.

func (Set[T]) Items

func (s Set[T]) Items() []T

The items stored in the set.

func (Set[T]) Len

func (s Set[T]) Len() int

Return the number of items stored in the set.

func (Set[T]) Remove

func (s Set[T]) Remove(item T) bool

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

func (a Set[T]) SymmetricDifference(b Set[T]) Set[T]

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.

func (Set[T]) Union

func (a Set[T]) Union(b Set[T]) Set[T]

Return a new set that is the union of this set and another.

type SortOrder

type SortOrder bool

SortOrder determines if the collection should be sorted Ascending or Descending.

const (
	// Ascending will sort the collection from smallest to biggest elements.
	Ascending SortOrder = true
	// Descending will sort the collection from biggest to smallest elements.
	Descending SortOrder = false
)

Jump to

Keyboard shortcuts

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