collections API

collections

package

API reference for the collections package.

S
struct

OrderedSet

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

core/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]
core/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.

core/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]
core/collections/queue.go:20-22
func NewQueue[T any]() *Queue[T]

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

Set

Set is a generic thread-safe set.

core/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]
core/collections/set.go:18-22
func NewSet[T comparable]() *Set[T]

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

TestSet

Parameters

core/collections/set_test.go:8-34
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")
	}
}
F
function

TestSetForEachAllowsMutation

Parameters

core/collections/set_test.go:36-56
func TestSetForEachAllowsMutation(t *testing.T)

{
	set := NewSet[int]()
	set.Add(1, 2, 3)
	done := make(chan struct{})
	go func() {
		set.ForEach(func(value int) bool {
			set.Remove(value)
			return true
		})
		close(done)
	}()

	select {
	case <-done:
	case <-time.After(time.Second):
		t.Fatal("ForEach() deadlocked when the callback mutated the set")
	}
	if set.Len() != 0 {
		t.Fatalf("set length = %d, want 0", set.Len())
	}
}
F
function

TestMultiMapGetReturnsCopy

Parameters

core/collections/set_test.go:58-66
func TestMultiMapGetReturnsCopy(t *testing.T)

{
	mapping := NewMultiMap[string, int]()
	mapping.Add("key", 1)
	values := mapping.Get("key")
	values[0] = 2
	if got := mapping.Get("key")[0]; got != 1 {
		t.Fatalf("Get() exposed internal slice: %d", got)
	}
}
S
struct

BiMap

BiMap is a thread-safe bidirectional map.

core/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]
core/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),
	}
}
S
struct

MultiMap

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

core/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]
core/collections/multimap.go:21-25
func NewMultiMap[K comparable, V any]() *MultiMap[K, V]

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