collections API

collections

package

API reference for the collections package.

S
struct

MultiMap

MultiMap is a thread-safe map from keys to slices of values.

pkg/collections/multimap.go:15-18
type MultiMap struct

Example

mm := collections.NewMultiMap[string, int]()
mm.Add("a", 1)
mm.Add("a", 2)
vals := mm.Get("a") // []int{1, 2}

Fields

Name Type Description
mu sync.RWMutex
data map[K][]V
F
function

NewMultiMap

NewMultiMap creates a new MultiMap.

Returns

*MultiMap[K,
V]
pkg/collections/multimap.go:21-25
func NewMultiMap[K comparable, V any]() *MultiMap[K, V]

{
	return &MultiMap[K, V]{
		data: make(map[K][]V),
	}
}
S
struct

OrderedSet

OrderedSet is a thread-safe set that preserves insertion order.

pkg/collections/ordered_set.go:14-18
type OrderedSet struct

Example

s := collections.NewOrderedSet[string]()
s.Add("a", "b")
for _, v := range s.Items() { ... }

Fields

Name Type Description
mu sync.RWMutex
items map[T]struct{}
order []T
F
function

NewOrderedSet

NewOrderedSet creates a new OrderedSet.

Returns

*OrderedSet[T]
pkg/collections/ordered_set.go:21-25
func NewOrderedSet[T comparable]() *OrderedSet[T]

{
	return &OrderedSet[T]{
		items: make(map[T]struct{}),
	}
}
S
struct

Queue

Queue is a generic thread-safe FIFO queue.

pkg/collections/queue.go:14-17
type Queue struct

Example

q := collections.NewQueue[string]()
q.Enqueue("a")
for v, ok := q.Dequeue(); ok; v, ok = q.Dequeue() { ... }

Fields

Name Type Description
mu sync.Mutex
items []T
F
function

NewQueue

NewQueue creates a new Queue.

Returns

*Queue[T]
pkg/collections/queue.go:20-22
func NewQueue[T any]() *Queue[T]

{
	return &Queue[T]{}
}
S
struct

Set

Set is a generic thread-safe set.

pkg/collections/set.go:12-15
type Set struct

Example

s := collections.NewSet[string]()
s.Add("a", "b")
if s.Has("a") { ... }

Fields

Name Type Description
items map[T]struct{}
mu sync.RWMutex
F
function

NewSet

NewSet creates a new empty Set.

Returns

*Set[T]
pkg/collections/set.go:18-22
func NewSet[T comparable]() *Set[T]

{
	return &Set[T]{
		items: make(map[T]struct{}),
	}
}
F
function

TestSet

Parameters

pkg/collections/set_test.go:7-33
func TestSet(t *testing.T)

{
	s := NewSet[int]()

	s.Add(1, 2, 3)
	if s.Len() != 3 {
		t.Errorf("Len: got %d, want 3", s.Len())
	}

	if !s.Has(2) {
		t.Error("Has 2: should be true")
	}

	s.Remove(2)
	if s.Has(2) {
		t.Error("Has 2: should be false after removal")
	}

	items := s.Items()
	if len(items) != 2 {
		t.Errorf("Items: got %d, want 2", len(items))
	}

	s.Clear()
	if s.Len() != 0 {
		t.Error("Clear should empty the set")
	}
}
S
struct

BiMap

BiMap is a thread-safe bidirectional map.

pkg/collections/bimap.go:15-19
type BiMap struct

Example

bm := collections.NewBiMap[string, int]()
bm.Put("a", 1)
v, _ := bm.Get("a")   // 1
k, _ := bm.Inverse(1) // "a"

Fields

Name Type Description
mu sync.RWMutex
forward map[K1]K2
inverse map[K2]K1
F
function

NewBiMap

NewBiMap creates a new BiMap.

Returns

*BiMap[K1,
K2]
pkg/collections/bimap.go:22-27
func NewBiMap[K1 comparable, K2 comparable]() *BiMap[K1, K2]

{
	return &BiMap[K1, K2]{
		forward: make(map[K1]K2),
		inverse: make(map[K2]K1),
	}
}