ds

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package ds provides generic data structure implementations. All containers are non-concurrent-safe; use external synchronization when sharing across goroutines.

OrderedMap[K,V] preserves insertion order with O(1) access and zero-allocation iteration. Set[T] offers set algebra (union, intersection, difference) and relation checks (subset, disjoint). BiMap[K,V] provides bidirectional O(1) lookup. Stack[T] and Heap[T] offer LIFO and priority-queue semantics respectively. SortedMap[K,V] combines O(1) random access by key with O(log n) ordered iteration; the sort key and the map lookup key can be different fields of V.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BiMap

type BiMap[K comparable, V comparable] struct {
	// contains filtered or unexported fields
}

BiMap is a bidirectional map with O(1) lookup in both directions. Each key maps to exactly one value and each value maps to exactly one key. It is not safe for concurrent use.

func NewBiMap

func NewBiMap[K, V comparable]() *BiMap[K, V]

NewBiMap creates an empty BiMap.

func NewBiMapWithCapacity

func NewBiMapWithCapacity[K, V comparable](n int) *BiMap[K, V]

NewBiMapWithCapacity creates a BiMap with pre-allocated capacity. Panics if n is negative.

func (*BiMap[K, V]) All

func (m *BiMap[K, V]) All() iter.Seq2[K, V]

All returns an iterator over all key-value pairs.

func (*BiMap[K, V]) Clear

func (m *BiMap[K, V]) Clear()

Clear removes all mappings, keeping the underlying map storage.

func (*BiMap[K, V]) Clone

func (m *BiMap[K, V]) Clone() *BiMap[K, V]

Clone returns a shallow copy of the BiMap.

func (*BiMap[K, V]) DeleteByKey

func (m *BiMap[K, V]) DeleteByKey(key K) bool

DeleteByKey removes the mapping for key. Returns true if the key existed.

func (*BiMap[K, V]) DeleteByValue

func (m *BiMap[K, V]) DeleteByValue(value V) bool

DeleteByValue removes the mapping for value. Returns true if the value existed.

func (*BiMap[K, V]) GetByKey

func (m *BiMap[K, V]) GetByKey(key K) (V, bool)

GetByKey returns the value associated with key.

func (*BiMap[K, V]) GetByValue

func (m *BiMap[K, V]) GetByValue(value V) (K, bool)

GetByValue returns the key associated with value.

func (*BiMap[K, V]) Keys

func (m *BiMap[K, V]) Keys() []K

Keys returns all keys.

func (*BiMap[K, V]) Len

func (m *BiMap[K, V]) Len() int

Len returns the number of mappings.

func (*BiMap[K, V]) Set

func (m *BiMap[K, V]) Set(key K, value V)

Set associates key with value. If key or value already has a mapping, the old mapping is removed to maintain the 1:1 invariant.

func (*BiMap[K, V]) Values

func (m *BiMap[K, V]) Values() []V

Values returns all values.

type Heap

type Heap[T any] struct {
	// contains filtered or unexported fields
}

Heap is a binary heap ordered by the compare function. compare(a, b) < 0 means a has higher priority (min-heap default). It is not safe for concurrent use.

func NewHeap

func NewHeap[T any](compare func(T, T) int) *Heap[T]

NewHeap creates an empty heap with the given compare function. Panics if compare is nil.

func NewHeapFrom

func NewHeapFrom[T any](compare func(T, T) int, s []T) *Heap[T]

NewHeapFrom creates a heap from an existing slice in O(n) time. The slice ownership is transferred to the heap. Panics if compare is nil.

func NewMaxHeap

func NewMaxHeap[T cmp.Ordered]() *Heap[T]

NewMaxHeap creates a max-heap for ordered types.

func NewMinHeap

func NewMinHeap[T cmp.Ordered]() *Heap[T]

NewMinHeap creates a min-heap for ordered types using cmp.Compare.

func (*Heap[T]) All

func (h *Heap[T]) All() iter.Seq[T]

All returns an iterator over all elements in arbitrary order. Does not modify the heap.

func (*Heap[T]) Clear

func (h *Heap[T]) Clear()

Clear removes all elements.

func (*Heap[T]) Clone

func (h *Heap[T]) Clone() *Heap[T]

Clone returns a shallow copy of the heap. The heap property is preserved.

func (*Heap[T]) Drain

func (h *Heap[T]) Drain() iter.Seq[T]

Drain returns an iterator that pops elements in sorted order until the heap is empty.

func (*Heap[T]) Len

func (h *Heap[T]) Len() int

Len returns the number of elements.

func (*Heap[T]) Peek

func (h *Heap[T]) Peek() (T, bool)

Peek returns the top element without removing it. Returns (zero, false) if empty.

func (*Heap[T]) Pop

func (h *Heap[T]) Pop() (T, bool)

Pop removes and returns the top element. Returns (zero, false) if empty.

func (*Heap[T]) Push

func (h *Heap[T]) Push(value T)

Push inserts value into the heap.

type OrderedMap

type OrderedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

OrderedMap maintains key-value pairs in insertion order. Lookup, insert, and delete are all O(1). Iteration is zero-allocation. It is not safe for concurrent use.

func NewOrderedMap

func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]

NewOrderedMap creates an empty OrderedMap.

func NewOrderedMapWithCapacity

func NewOrderedMapWithCapacity[K comparable, V any](capacity int) *OrderedMap[K, V]

NewOrderedMapWithCapacity creates an OrderedMap with pre-allocated map capacity. Panics if capacity is negative.

func (*OrderedMap[K, V]) All

func (m *OrderedMap[K, V]) All() iter.Seq2[K, V]

All returns an iterator over key-value pairs in insertion order.

func (*OrderedMap[K, V]) Backward

func (m *OrderedMap[K, V]) Backward() iter.Seq2[K, V]

Backward returns an iterator over key-value pairs in reverse insertion order.

func (*OrderedMap[K, V]) Clear

func (m *OrderedMap[K, V]) Clear()

Clear removes all key-value pairs, keeping the underlying map storage.

func (*OrderedMap[K, V]) Clone

func (m *OrderedMap[K, V]) Clone() *OrderedMap[K, V]

Clone returns a shallow copy of the map. The insertion order is preserved.

func (*OrderedMap[K, V]) Delete

func (m *OrderedMap[K, V]) Delete(key K) bool

Delete removes the key. It returns true if the key was present.

func (*OrderedMap[K, V]) Get

func (m *OrderedMap[K, V]) Get(key K) (V, bool)

Get returns the value associated with key, or the zero value with false if not found.

func (*OrderedMap[K, V]) Has

func (m *OrderedMap[K, V]) Has(key K) bool

Has reports whether the key exists.

func (*OrderedMap[K, V]) Keys

func (m *OrderedMap[K, V]) Keys() []K

Keys returns all keys in insertion order.

func (*OrderedMap[K, V]) Len

func (m *OrderedMap[K, V]) Len() int

Len returns the number of key-value pairs.

func (*OrderedMap[K, V]) Set

func (m *OrderedMap[K, V]) Set(key K, value V) bool

Set stores the key-value pair. It returns true if the key was newly inserted, false if an existing key's value was updated (position unchanged).

func (*OrderedMap[K, V]) Values

func (m *OrderedMap[K, V]) Values() []V

Values returns all values in insertion order.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is an unordered collection of unique elements. It is not safe for concurrent use.

func NewSet

func NewSet[T comparable](vals ...T) *Set[T]

NewSet creates a Set initialized with the given values. Duplicates are ignored. Accepting initial values as variadic arguments is intentional — sets are commonly constructed from a known collection of elements.

func NewSetWithCapacity

func NewSetWithCapacity[T comparable](n int) *Set[T]

NewSetWithCapacity creates an empty Set with pre-allocated capacity. Panics if n is negative.

func (*Set[T]) Add

func (s *Set[T]) Add(val T) bool

Add inserts val. It returns true if the element was newly added.

func (*Set[T]) All

func (s *Set[T]) All() iter.Seq[T]

All returns an iterator over the elements. Order is not guaranteed.

func (*Set[T]) Clear

func (s *Set[T]) Clear()

Clear removes all elements.

func (*Set[T]) Clone

func (s *Set[T]) Clone() *Set[T]

Clone returns a shallow copy of the set.

func (*Set[T]) Difference

func (s *Set[T]) Difference(other *Set[T]) *Set[T]

Difference returns a new Set containing elements in s but not in other.

func (*Set[T]) Equal

func (s *Set[T]) Equal(other *Set[T]) bool

Equal reports whether s and other contain the same elements.

func (*Set[T]) Has added in v0.3.0

func (s *Set[T]) Has(val T) bool

Has reports whether val is in the set.

func (*Set[T]) Intersect

func (s *Set[T]) Intersect(other *Set[T]) *Set[T]

Intersect returns a new Set containing elements present in both s and other.

func (*Set[T]) IsSubset

func (s *Set[T]) IsSubset(other *Set[T]) bool

IsSubset reports whether all elements of s are in other.

func (*Set[T]) IsSuperset

func (s *Set[T]) IsSuperset(other *Set[T]) bool

IsSuperset reports whether all elements of other are in s.

func (*Set[T]) Len

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

Len returns the number of elements.

func (*Set[T]) Remove

func (s *Set[T]) Remove(val T) bool

Remove removes val. It returns true if the element was present.

func (*Set[T]) SymmetricDifference

func (s *Set[T]) SymmetricDifference(other *Set[T]) *Set[T]

SymmetricDifference returns a new Set containing elements in s or other but not both.

func (*Set[T]) ToSlice

func (s *Set[T]) ToSlice() []T

ToSlice returns the elements as a slice. Order is not guaranteed.

func (*Set[T]) Union

func (s *Set[T]) Union(other *Set[T]) *Set[T]

Union returns a new Set containing elements from both s and other.

type SortedMap added in v0.3.0

type SortedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SortedMap maintains values sorted by a caller-defined order. Backed by a hash map for O(1) random access and a B-tree for O(log n) ordered ops. The sort key and the map lookup key can be different fields of V. It is not safe for concurrent use.

V is stored by value in both the hash map and the B-tree nodes. When V is a large struct, each Set/Delete incurs a value copy inside the B-tree. In that case, prefer using SortedMap[K, *V] to avoid the copy overhead.

SortedMap does not provide Clone(), Keys(), or Values() because the sort key is extracted from V via a caller-supplied function, making generic cloning ambiguous (should the B-tree be rebuilt?), and ordered slices are better obtained via the Ascend/Descend iterators.

func NewSortedMap added in v0.3.0

func NewSortedMap[K comparable, V any](key func(V) K, less func(V, V) bool) *SortedMap[K, V]

NewSortedMap creates a SortedMap. key extracts the map lookup key from V; less defines the B-tree sort order. Both key and less must not be nil.

func (*SortedMap[K, V]) Ascend added in v0.3.0

func (m *SortedMap[K, V]) Ascend() iter.Seq[V]

Ascend returns an iterator over all values in ascending order.

func (*SortedMap[K, V]) AscendAfter added in v0.3.0

func (m *SortedMap[K, V]) AscendAfter(pivot V) iter.Seq[V]

AscendAfter returns an iterator over values > pivot in ascending order.

func (*SortedMap[K, V]) AscendFrom added in v0.3.0

func (m *SortedMap[K, V]) AscendFrom(pivot V) iter.Seq[V]

AscendFrom returns an iterator over values >= pivot in ascending order.

func (*SortedMap[K, V]) Clear added in v0.3.0

func (m *SortedMap[K, V]) Clear()

Clear removes all entries.

func (*SortedMap[K, V]) Delete added in v0.3.0

func (m *SortedMap[K, V]) Delete(key K) bool

Delete removes the entry with the given key. Returns true if the entry existed.

func (*SortedMap[K, V]) Descend added in v0.3.0

func (m *SortedMap[K, V]) Descend() iter.Seq[V]

Descend returns an iterator over all values in descending order.

func (*SortedMap[K, V]) DescendBefore added in v0.3.0

func (m *SortedMap[K, V]) DescendBefore(pivot V) iter.Seq[V]

DescendBefore returns an iterator over values < pivot in descending order.

func (*SortedMap[K, V]) DescendFrom added in v0.3.0

func (m *SortedMap[K, V]) DescendFrom(pivot V) iter.Seq[V]

DescendFrom returns an iterator over values <= pivot in descending order.

func (*SortedMap[K, V]) Get added in v0.3.0

func (m *SortedMap[K, V]) Get(key K) (V, bool)

Get returns the value associated with key and whether it was found. If key is not present, it returns the zero value of V and false.

func (*SortedMap[K, V]) Has added in v0.3.0

func (m *SortedMap[K, V]) Has(key K) bool

Has reports whether key exists.

func (*SortedMap[K, V]) Len added in v0.3.0

func (m *SortedMap[K, V]) Len() int

Len returns the number of entries.

func (*SortedMap[K, V]) Set added in v0.3.0

func (m *SortedMap[K, V]) Set(val V) bool

Set inserts or updates val. Returns true if val was newly inserted, false if an existing entry with the same key was replaced. When updating, the B-tree always performs a Delete+Set pair regardless of whether the sort key changed.

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

Stack is a LIFO stack backed by a dynamic array. It is not safe for concurrent use.

func NewStack

func NewStack[T any]() *Stack[T]

NewStack creates an empty Stack.

func NewStackWithCapacity

func NewStackWithCapacity[T any](n int) *Stack[T]

NewStackWithCapacity creates a Stack with pre-allocated capacity. Panics if n is negative.

func (*Stack[T]) All

func (s *Stack[T]) All() iter.Seq[T]

All returns an iterator over elements in LIFO order (top to bottom).

func (*Stack[T]) Clear

func (s *Stack[T]) Clear()

Clear removes all elements, keeping the underlying slice storage.

func (*Stack[T]) Clone

func (s *Stack[T]) Clone() *Stack[T]

Clone returns a shallow copy of the stack. The element order is preserved.

func (*Stack[T]) Len

func (s *Stack[T]) Len() int

Len returns the number of elements.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (T, bool)

Peek returns the top element without removing it. Returns (zero, false) if empty.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (T, bool)

Pop removes and returns the top element. Returns (zero, false) if empty.

func (*Stack[T]) Push

func (s *Stack[T]) Push(item T)

Push adds item to the top of the stack.

Jump to

Keyboard shortcuts

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