Documentation ¶
Overview ¶
Package ds provides various data structure implementations with generics.
Index ¶
- type Heap
- type List
- func (l *List[T]) Add(item T)
- func (l *List[T]) Clear()
- func (l *List[T]) Clip()
- func (l *List[T]) Contains(item T) bool
- func (l *List[T]) Each(iterator func(item T))
- func (l *List[T]) Get(index int) T
- func (l *List[T]) IndexOf(item T) int
- func (l *List[T]) IsEmpty() bool
- func (l *List[T]) Items() []T
- func (l *List[T]) Remove(item T) bool
- func (l *List[T]) Size() int
- type Pool
- type Set
- func NewSet[T comparable](initialCapacity int) *Set[T]
- func SetDifference[T comparable](first, second *Set[T]) *Set[T]
- func SetFromMapKeys[T comparable, V any](m map[T]V) *Set[T]
- func SetFromMapValues[K comparable, V comparable](m map[K]V) *Set[V]
- func SetFromSlice[T comparable](slice []T) *Set[T]
- func SetIntersection[T comparable](first, second *Set[T]) *Set[T]
- func SetUnion[T comparable](first, second *Set[T]) *Set[T]
- func (s *Set[T]) Add(item T) bool
- func (s *Set[T]) AddSet(other *Set[T]) bool
- func (s *Set[T]) Clear()
- func (s *Set[T]) Clip()
- func (s *Set[T]) Contains(item T) bool
- func (s *Set[T]) ContainsSet(other *Set[T]) bool
- func (s *Set[T]) IsEmpty() bool
- func (s *Set[T]) Items() []T
- func (s *Set[T]) Remove(item T) bool
- func (s *Set[T]) RemoveSet(other *Set[T]) bool
- func (s *Set[T]) Size() int
- type Stack
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Heap ¶
type Heap[T any] struct { // contains filtered or unexported fields }
Heap is a data structure that orders items when inserted according to a specified ordering.
func NewHeap ¶
NewHeap creates a new Heap instance that is configured to use the specified better function to order items. When better returns true, the first argument will be placed higher in the heap. The specified initialCapacity is used to preallocate memory.
func (*Heap[T]) IsEmpty ¶
IsEmpty returns true if there are no items in this Heap and false otherwise.
func (*Heap[T]) Peek ¶
func (h *Heap[T]) Peek() T
Peek returns the top-most item from this Heap without removing it. This method panics if the Heap is empty so make sure to use IsEmpty beforehand.
type List ¶
type List[T comparable] struct { // contains filtered or unexported fields }
List represents a sequence of items.
Currently a List can only store comparable items. This restriction allows for Remove, IndexOf and Contains operations.
func NewList ¶
func NewList[T comparable](initialCapacity int) *List[T]
NewList creates a new List with the given capacity. The capacity can be used as a form of optimization. Regardless of the value, the initial size of the list is zero and the list can grow past the specified capacity.
func (*List[T]) Contains ¶
Contains checks whether this List has the specified item and returns true if it is contained and false otherwise.
func (*List[T]) Each ¶
func (l *List[T]) Each(iterator func(item T))
Each is a helper method allows one to iterate over all items in this List through a closure function.
func (*List[T]) Get ¶
Get returns the item in this list that is located at the specified index (starting from zero). This method will panic if the index is invalid.
func (*List[T]) IndexOf ¶
IndexOf returns the index where the specified item is located in this List. If the item is not part of this list, this method returns -1.
func (*List[T]) Items ¶
func (l *List[T]) Items() []T
Item returns all items stored in this List as a slice. The returned slice should not be mutated.
type Pool ¶ added in v0.8.0
type Pool[T any] struct { // contains filtered or unexported fields }
Pool represents a storage structure that can preseve allocated objects for faster reuse.
func (*Pool[T]) Clear ¶ added in v0.8.0
func (p *Pool[T]) Clear()
Clear removes any items that were stored for reuse.
func (*Pool[T]) Fetch ¶ added in v0.8.0
func (p *Pool[T]) Fetch() *T
Fetch retrieves an available item from the pool or creates a new one if one is not available.
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set represents a set data structure, where only one instance of a given item is stored.
Note: Using the standard map[T]struct{} approach will likely yield faster performance and should be preferred in performance-critical code. This type makes usage of sets more human-readable.
func NewSet ¶
func NewSet[T comparable](initialCapacity int) *Set[T]
NewSet creates a new Set instance with the specified initial capacity, which is only used to preallocate memory and does not act as an upper bound.
func SetDifference ¶ added in v0.6.0
func SetDifference[T comparable](first, second *Set[T]) *Set[T]
SetDifference creates a new Set that holds the difference between the first and the second specified sets.
func SetFromMapKeys ¶ added in v0.5.0
func SetFromMapKeys[T comparable, V any](m map[T]V) *Set[T]
SetFromMapKeys creates a new Set instance based on the keys of the provided map.
func SetFromMapValues ¶ added in v0.5.0
func SetFromMapValues[K comparable, V comparable](m map[K]V) *Set[V]
SetFromMapValues creates a new Set instance based on the values of the provided map.
func SetFromSlice ¶ added in v0.5.0
func SetFromSlice[T comparable](slice []T) *Set[T]
SetFromSlice creates a new Set instance based on the elements contained in the provided slice.
func SetIntersection ¶ added in v0.6.0
func SetIntersection[T comparable](first, second *Set[T]) *Set[T]
SetIntersection creates a new Set that holds the intersection of the items of the two specified sets.
func SetUnion ¶ added in v0.6.0
func SetUnion[T comparable](first, second *Set[T]) *Set[T]
SetUnion creates a new Set that is the union of the specified sets.
func (*Set[T]) Add ¶
Add adds the specified item to this Set if it was not present already. This method returns true if the operation was performed and false if the item was aleady present.
func (*Set[T]) AddSet ¶ added in v0.6.0
AddSet adds the items of another Set to this Set. The operation returns true if the operation resulted in a change and false otherwise.
func (*Set[T]) ContainsSet ¶ added in v0.6.0
ContainsSet returns whether this set fully contains another set.
func (*Set[T]) Items ¶
func (s *Set[T]) Items() []T
Items returns a slice containing all of the items from this Set.
Note: The items are returned in a random order which can differ between subsequent calls.
func (*Set[T]) Remove ¶
Remove removes the specified item from this Set. This method returns true if there was in fact such an item to be removed and false otherwise.
type Stack ¶
type Stack[T any] struct { // contains filtered or unexported fields }
Stack is an implementation of a stack data structure. The last inserted item is the first one to be removed (LIFO - last in, first out).
func NewStack ¶
NewStack creates a new Stack instance with the specified initial capacity, which only serves to preallocate memory. Exceeding the initial capacity is allowed.
func (*Stack[T]) Peek ¶
func (s *Stack[T]) Peek() T
Peek returns the item that is at the top of the Stack without removing it. Make sure that the Stack is not empty, otherwise this method will panic.