Documentation
¶
Overview ¶
Package list implements a doubly linked list.
To iterate over a list (where l is a *List):
for e := l.Front(); e != nil; e = e.Next() {
// do something with e.Value
}
Index ¶
- func Batches[T any](values []T, batchSize int) (batches [][]T)
- func BatchesIntoChan[T any](values []T, batchSize int, channel chan []T)
- func BatchesIntoChanWithRemainder[T any](values []T, batchSize int, channel chan []T) (remainder []T)
- func BatchesWithRemainder[T any](values []T, batchSize int) (batches [][]T, remainder []T)
- func CopyMap[Key comparable, Value any](in map[Key]Value) map[Key]Value
- func CopySlice[T any](src []T) []T
- func FilterSlice[T any](in []T, filterFunc func(value T) (include bool)) []T
- func Flatten2D[T any](slice [][]T) []T
- func Flatten3D[T any](slice [][][]T) []T
- func ForEachMap[Key comparable, Value any](in map[Key]Value, f func(key Key, value Value))
- func ForEachSlice[Value any](in []Value, f func(value Value))
- func IntersectionUnique[T comparable](slices ...[]T) []T
- func MapAscending[Key constraints.Ordered, Value any](in map[Key]Value) func() (k Key, v Value, ok bool)
- func MapDescending[Key constraints.Ordered, Value any](in map[Key]Value) func() (k Key, v Value, ok bool)
- func MapKeys[Key comparable, Value any](in map[Key]Value) []Key
- func MapValues[Key comparable, Value any](in map[Key]Value) []Value
- func MapValuesByAscendingKey[Key constraints.Ordered, Value any](in map[Key]Value) []Value
- func MapValuesByDescendingKey[Key constraints.Ordered, Value any](in map[Key]Value) []Value
- func MergeMaps[Key comparable, Value any](maps ...map[Key]Value) map[Key]Value
- func Range[T constraints.Integer](start T, end T) []T
- func Repeat[T any, C constraints.Integer](value T, count C) []T
- func RepeatDynamic[T any, C constraints.Integer](creationFunc func(index C) T, count C) []T
- func RepeatZero[T any, C constraints.Integer](count C) []T
- func SliceContains[T comparable](slice []T, value T) bool
- func SliceContainsCaseInsensitive(slice []string, value string) bool
- func SliceConversion[OldType constraints.Simple, NewType constraints.Simple](in []OldType) []NewType
- func SliceDiff[SliceType comparable](a []SliceType, b []SliceType) (inAButNotInB []SliceType)
- func SliceDiffUnique[SliceType comparable](a []SliceType, b []SliceType) (inAButNotInB []SliceType)
- func SliceEqual[SliceType any](in1 []SliceType, in2 []SliceType, ...) bool
- func SliceEqualWithErr[SliceType any](in1 []SliceType, in2 []SliceType, ...) (bool, stackerr.Error)
- func SliceFind[T comparable](slice []T, value T) (int, bool)
- func SliceFindCaseInsensitive(slice []string, value string) (int, bool)
- func SliceUnique[T comparable](in []T) (out []T)
- func SortSliceAscendingCopy[SliceType constraints.Ordered](in []SliceType) (sorted []SliceType)
- func SortSliceAscendingInPlace[SliceType constraints.Ordered](in []SliceType)
- func SortSliceDescendingCopy[SliceType constraints.Ordered](in []SliceType) (sorted []SliceType)
- func SortSliceDescendingInPlace[SliceType constraints.Ordered](in []SliceType)
- func TransformMap[InKey comparable, InValue any, OutKey comparable, OutValue any](in map[InKey]InValue, ...) map[OutKey]OutValue
- func TransformMapToSlice[MapKeyType comparable, MapValueType any, SliceType any](in map[MapKeyType]MapValueType, ...) (out []SliceType)
- func TransformMapWithErr[InKey comparable, InValue any, OutKey comparable, OutValue any](in map[InKey]InValue, ...) (map[OutKey]OutValue, stackerr.Error)
- func TransformSlice[In any, Out any](in []In, transformationFunc func(value In) (transformed Out)) (out []Out)
- func TransformSliceToMap[SliceType any, MapKeyType comparable, MapValueType any](in []SliceType, ...) (out map[MapKeyType]MapValueType)
- func TransformSliceToMapWithErr[SliceType any, MapKeyType comparable, MapValueType any](in []SliceType, ...) (out map[MapKeyType]MapValueType, err stackerr.Error)
- func TransformSliceWithErr[In any, Out any](in []In, ...) (out []Out, err stackerr.Error)
- func UnionUnique[T comparable](slices ...[]T) []T
- type HashMap
- func NewHashMap[T comparable](initial []T) HashMap[T]
- func NewHashMapPreallocated[T comparable](size int) HashMap[T]
- func TransformSliceToHashMap[SliceType any, MapKeyType comparable](in []SliceType, ...) (out HashMap[MapKeyType])
- func TransformSliceToHashMapWithErr[SliceType any, MapKeyType comparable](in []SliceType, ...) (out HashMap[MapKeyType], err stackerr.Error)
- type LinkedList
- type LinkedListElement
- type PriorityQueue
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BatchesIntoChan ¶
func BatchesWithRemainder ¶
func CopyMap ¶
func CopyMap[Key comparable, Value any](in map[Key]Value) map[Key]Value
CopyMap creates a copy of the input map
func CopySlice ¶
func CopySlice[T any](src []T) []T
CopySlice will create a copy of the given slice.
func FilterSlice ¶
FilterSlice creates a new slice of elements that meet a given condition function.
func Flatten2D ¶
func Flatten2D[T any](slice [][]T) []T
Flatten2D flattens a 2-dimensional slice of type T into a 1-dimensional slice of type T
func Flatten3D ¶
func Flatten3D[T any](slice [][][]T) []T
Flatten3D flattens a 3-dimensional slice of type T into a 1-dimensional slice of type T
func ForEachMap ¶
func ForEachMap[Key comparable, Value any](in map[Key]Value, f func(key Key, value Value))
func ForEachSlice ¶
func ForEachSlice[Value any](in []Value, f func(value Value))
func IntersectionUnique ¶
func IntersectionUnique[T comparable](slices ...[]T) []T
IntersectionUnique returns the set of unique values (no duplicates) that are present in each of the given slices.
func MapAscending ¶
func MapAscending[Key constraints.Ordered, Value any](in map[Key]Value) func() (k Key, v Value, ok bool)
MapAscending will return a closure (iterator) that will return the next element of the map (in ascending order by key) each time it's called. After the last element has been returned, the closure will return zero-values and false for 'ok'.
func MapDescending ¶
func MapDescending[Key constraints.Ordered, Value any](in map[Key]Value) func() (k Key, v Value, ok bool)
MapDescending will return a closure (iterator) that will return the next element of the map (in descending order by key) each time it's called.
func MapKeys ¶
func MapKeys[Key comparable, Value any](in map[Key]Value) []Key
MapKeys gets all keys of the input map as a slice.
func MapValues ¶
func MapValues[Key comparable, Value any](in map[Key]Value) []Value
MapValues gets all values of the input map as a slice.
func MapValuesByAscendingKey ¶
func MapValuesByAscendingKey[Key constraints.Ordered, Value any](in map[Key]Value) []Value
MapValuesByAscendingKey gets all values of the input map as a slice, sorted in ascending order of the map's keys.
func MapValuesByDescendingKey ¶
func MapValuesByDescendingKey[Key constraints.Ordered, Value any](in map[Key]Value) []Value
MapValuesByDescendingKey gets all values of the input map as a slice, sorted in descending order of the map's keys.
func MergeMaps ¶
func MergeMaps[Key comparable, Value any](maps ...map[Key]Value) map[Key]Value
MergeMaps will merge multiple maps together, with values for keys in later maps overwriting values with the same keys in previous maps. If no maps are passed in, it returns nil. If one map is passed in, it will create a copy of that map.
func Range ¶
func Range[T constraints.Integer](start T, end T) []T
Range creates a slice of integer values from `start` (inclusive) to `end` (exclusive).
func Repeat ¶
func Repeat[T any, C constraints.Integer](value T, count C) []T
Repeat creates a slice that repeats the given value a certain number of times.
func RepeatDynamic ¶
func RepeatDynamic[T any, C constraints.Integer](creationFunc func(index C) T, count C) []T
func RepeatZero ¶
func RepeatZero[T any, C constraints.Integer](count C) []T
RepeatZero creates a slice of a certain number of copies of the zero value of the given type. Only the first type parameter (T) must be provided; the second (C) will be inferred from the input argument.
func SliceContains ¶
func SliceContains[T comparable](slice []T, value T) bool
func SliceConversion ¶
func SliceConversion[OldType constraints.Simple, NewType constraints.Simple](in []OldType) []NewType
SliceConversion will convert a slice from one simple numeric type to another.
func SliceDiff ¶
func SliceDiff[SliceType comparable](a []SliceType, b []SliceType) (inAButNotInB []SliceType)
SliceDiff will get a slice of all elements that are present in `a` but not in `b`. If an element is in `a` N times and is not in `b`, it will appear in the output N times as well.
func SliceDiffUnique ¶
func SliceDiffUnique[SliceType comparable](a []SliceType, b []SliceType) (inAButNotInB []SliceType)
SliceDiff will get a slice of all unique elements that are present in `a` but not in `b`. If an element is in `a` several times and is not in `b`, it will only appear in the output once.
func SliceEqual ¶
func SliceEqual[SliceType any](in1 []SliceType, in2 []SliceType, comparisonFunc func(val1 SliceType, val2 SliceType) bool) bool
SliceEqual checks whether two slices are equal by using a comparison function on each pair of elements. If the slices are of unequal length, it will return false.
func SliceEqualWithErr ¶
func SliceEqualWithErr[SliceType any](in1 []SliceType, in2 []SliceType, comparisonFunc func(val1 SliceType, val2 SliceType) (bool, stackerr.Error)) (bool, stackerr.Error)
SliceEqualWithErr checks whether two slices are equal by using a comparison function (which can return an error) on each pair of elements. If the slices are of unequal length, it will return false.
func SliceFind ¶
func SliceFind[T comparable](slice []T, value T) (int, bool)
func SliceUnique ¶
func SliceUnique[T comparable](in []T) (out []T)
SliceUnique will get a new slice containing all unique/distinct values in the input slice, in the order that they appear.
func SortSliceAscendingCopy ¶
func SortSliceAscendingCopy[SliceType constraints.Ordered](in []SliceType) (sorted []SliceType)
SortSliceAscendingCopy will return a sorted (in ascending order) copy of the given slice, leaving elements with equal values where they are (stable sort). The original slice will not be modified.
func SortSliceAscendingInPlace ¶
func SortSliceAscendingInPlace[SliceType constraints.Ordered](in []SliceType)
SortSliceAscendingInPlace will sort the given slice in ascending order, leaving elements with equal values where they are (stable sort).
func SortSliceDescendingCopy ¶
func SortSliceDescendingCopy[SliceType constraints.Ordered](in []SliceType) (sorted []SliceType)
SortSliceDescendingCopy will return a sorted (in descending order) copy of the given slice, leaving elements with equal values where they are (stable sort). The original slice will not be modified.
func SortSliceDescendingInPlace ¶
func SortSliceDescendingInPlace[SliceType constraints.Ordered](in []SliceType)
SortSliceDescendingInPlace will sort the given slice in descending order, leaving elements with equal values where they are (stable sort).
func TransformMap ¶
func TransformMap[InKey comparable, InValue any, OutKey comparable, OutValue any](in map[InKey]InValue, transformationFunc func(key InKey, value InValue) (transformedKey OutKey, transformedValue OutValue)) map[OutKey]OutValue
TransformMap maps an input map to an output map using a transformation function.
func TransformMapToSlice ¶
func TransformMapToSlice[MapKeyType comparable, MapValueType any, SliceType any](in map[MapKeyType]MapValueType, transformationFunc func(key MapKeyType, value MapValueType) SliceType) (out []SliceType)
TransformMapToSlice transforms map into a slice using the given transformation function.
func TransformMapWithErr ¶
func TransformMapWithErr[InKey comparable, InValue any, OutKey comparable, OutValue any](in map[InKey]InValue, transformationFunc func(key InKey, value InValue) (transformedKey OutKey, transformedValue OutValue, err stackerr.Error)) (map[OutKey]OutValue, stackerr.Error)
TransformMapWithErr maps an input map to an output map using a transformation function that can return an error.
func TransformSlice ¶
func TransformSlice[In any, Out any](in []In, transformationFunc func(value In) (transformed Out)) (out []Out)
TransformSlice maps an input slice to an output slice using a transformation function.
func TransformSliceToMap ¶
func TransformSliceToMap[SliceType any, MapKeyType comparable, MapValueType any](in []SliceType, transformationFunc func(sliceIndex int, sliceValue SliceType) (mapKey MapKeyType, mapValue MapValueType)) (out map[MapKeyType]MapValueType)
TransformSliceToMap transforms a slice of elements to a map of elements using a given transformation function.
func TransformSliceToMapWithErr ¶
func TransformSliceToMapWithErr[SliceType any, MapKeyType comparable, MapValueType any](in []SliceType, transformationFunc func(sliceIndex int, sliceValue SliceType) (mapKey MapKeyType, mapValue MapValueType, err stackerr.Error)) (out map[MapKeyType]MapValueType, err stackerr.Error)
TransformSliceToMapWithErr transforms a slice of elements to a map of elements using a given transformation function, and allows the transformation function to return an error that will cancel the execution.
func TransformSliceWithErr ¶
func TransformSliceWithErr[In any, Out any](in []In, transformationFunc func(value In) (transformed Out, err stackerr.Error)) (out []Out, err stackerr.Error)
TransformSliceWithErr maps an input slice to an output slice using a transformation function and allows returning an error.
func UnionUnique ¶
func UnionUnique[T comparable](slices ...[]T) []T
UnionUnique returns the set of unique values (no duplicates) that are present in at least one of the given slices.
Types ¶
type HashMap ¶
type HashMap[T comparable] interface { // Store will store a key in the hash map. Store(key T) // Delete will delete a key from the hash map if it exists, and returns a bool of whether // the key existed and was deleted. Delete(key T) bool // Has returns a bool of whether the key exists in the hash map. Has(key T) bool // Length returns the length of the hash map Length() int // Keys returns a slice of all keys in the hash map. Keys() []T }
HashMap is an interface that represents a keys-only map. It is useful for tracking unique IDs and checking if they exist in a high-performance way.
func NewHashMap ¶
func NewHashMap[T comparable](initial []T) HashMap[T]
func NewHashMapPreallocated ¶
func NewHashMapPreallocated[T comparable](size int) HashMap[T]
func TransformSliceToHashMap ¶
func TransformSliceToHashMap[SliceType any, MapKeyType comparable](in []SliceType, transformationFunc func(sliceIndex int, sliceValue SliceType) (mapKey MapKeyType)) (out HashMap[MapKeyType])
TransformSliceToHashMap transforms a slice of elements to a hash (lookup) map using a given transformation function.
func TransformSliceToHashMapWithErr ¶
func TransformSliceToHashMapWithErr[SliceType any, MapKeyType comparable](in []SliceType, transformationFunc func(sliceIndex int, sliceValue SliceType) (mapKey MapKeyType, err stackerr.Error)) (out HashMap[MapKeyType], err stackerr.Error)
TransformSliceToHashMapWithErr transforms a slice of elements to a hash (lookup) map using a given transformation function, and allows the transformation function to return an error that will cancel the execution.
type LinkedList ¶
type LinkedList[T any] interface { // Init initializes or clears list l. Init() LinkedList[T] // Len returns the number of elements of list l. // The complexity is O(1). Len() int // Front returns the first element of list l or nil if the list is empty. Front() LinkedListElement[T] // Back returns the last element of list l or nil if the list is empty. Back() LinkedListElement[T] // Remove removes e from l if e is an element of list l. // It returns the element value e.Value. // The element must not be nil. Remove(e LinkedListElement[T]) T // PushFront inserts a new element e with value v at the front of list l and returns e. PushFront(v T) LinkedListElement[T] // PushBack inserts a new element e with value v at the back of list l and returns e. PushBack(v T) LinkedListElement[T] // InsertBefore inserts a new element e with value v immediately before mark and returns e. // If mark is not an element of l, the list is not modified. // The mark must not be nil. InsertBefore(v T, mark LinkedListElement[T]) LinkedListElement[T] // InsertAfter inserts a new element e with value v immediately after mark and returns e. // If mark is not an element of l, the list is not modified. // The mark must not be nil. InsertAfter(v T, mark LinkedListElement[T]) LinkedListElement[T] // MoveToFront moves element e to the front of list l. // If e is not an element of l, the list is not modified. // The element must not be nil. MoveToFront(e LinkedListElement[T]) // MoveToBack moves element e to the back of list l. // If e is not an element of l, the list is not modified. // The element must not be nil. MoveToBack(e LinkedListElement[T]) // MoveBefore moves element e to its new position before mark. // If e or mark is not an element of l, or e == mark, the list is not modified. // The element and mark must not be nil. MoveBefore(e, mark LinkedListElement[T]) // MoveAfter moves element e to its new position after mark. // If e or mark is not an element of l, or e == mark, the list is not modified. // The element and mark must not be nil. MoveAfter(e, mark LinkedListElement[T]) // PushBackList inserts a copy of another list at the back of list l. // The lists l and other may be the same. They must not be nil. PushBackList(other LinkedList[T]) // PushFrontList inserts a copy of another list at the front of list l. // The lists l and other may be the same. They must not be nil. PushFrontList(other LinkedList[T]) }
type LinkedListElement ¶
type LinkedListElement[T any] interface { List() LinkedList[T] Prev() LinkedListElement[T] Next() LinkedListElement[T] Value() T // contains filtered or unexported methods }
type PriorityQueue ¶
type PriorityQueue[T any, P constraints.Ordered] interface { // Push adds a new item to the PriorityQueue, automatically inserting it // at the right position based on its priority value. Push(T, P) // Pop retrieves the item with the highest priority value. If the queue is // empty, it will panic (always check with Empty() first). Pop() (T, P) // Len will return the number of items in the queue. Len() int // Empty will return true if there are no items remaining in the queue. Empty() bool }
A PriorityQueue is a data structure that pops elements in order of descending priority. It's closer to a self-sorting Stack.
func NewPriorityQueue ¶
func NewPriorityQueue[T any, P constraints.Ordered]() PriorityQueue[T, P]