collections

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 4 Imported by: 0

README

Go Reference

collections

This package contains the following generic (and therefore type-safe) collections:

Set

A simple wrapper around a map[T]struct{}.

Deque

A double-ended queue. Elements can be added to and removed from the back and front of the queue. Can be used as a queue, stack, or linked list.

SyncMap

A goroutine-safe map. It is nothing more than a mutex-protected map that is type-safe.

Stack

Equivalent to a Deque where elements can only be pushed to and popped from the back. Preferred to Deque when you will only perform stack operations due to performing fewer allocations and clearer intent.

Heap

A binary min-heap AKA priority queue.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Deque

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

Deque is a generic double-ended queue. A zero value is not ready to use, acquire one using NewDeque.

func NewDeque

func NewDeque[T any]() *Deque[T]

NewDeque returns a deque for type T.

func (*Deque[T]) Pop

func (d *Deque[T]) Pop() T

Pop removes and returns the item at the front of the Deque. Panics if called on an empty Deque.

func (*Deque[T]) PopBack

func (d *Deque[T]) PopBack() T

PopBack removes and returns the item at the back of the Deque. Panics if called on an empty Deque.

func (*Deque[T]) Push

func (d *Deque[T]) Push(v T)

Push appends v to the back of the Deque, such that v would be returned by an immediate call to PopBack()

func (*Deque[T]) PushFront

func (d *Deque[T]) PushFront(v T)

PushFront appends v to the front of the Deque, such that v would be returned by an immediate call to Pop()

func (*Deque[T]) Size

func (d *Deque[T]) Size() int

Size returns the count of items in this Deque.

type Heap

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

Heap implements a binary min-heap. It can be used as a priority queue. Each item is given an integer priority upon insertion. Since Heap is a min-heap, lower integers have higher priority.

func BuildHeap

func BuildHeap[T any](items []T, prios []int) *Heap[T]

BuildHeap returns a heap with the given items. items[i] is given priority prios[i].

func NewHeap

func NewHeap[T any](capacity int) *Heap[T]

func (*Heap[T]) Extract

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

Extract removes and returns the highest priority item.

func (*Heap[T]) Insert

func (h *Heap[T]) Insert(t T, prio int)

Insert inserts t into the heap.

func (*Heap[T]) Peek

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

Peek returns the highest priority item in the heap without removing it. Use Extract() if you want to get-and-remove.

func (Heap[T]) Size

func (h Heap[T]) Size() int

Size returns the number of items in the heap.

type Set

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

Set is a set data structure that keeps track of items. A zero-value Set is not ready to use, create one using NewSet.

func NewSet

func NewSet[T comparable](capacity int) Set[T]

NewSet returns a Set with the specified capacity.

func SetFromIter

func SetFromIter[T comparable](seq iter.Seq[T]) Set[T]

SetFromIter returns a Set that contains the items in the iterator.

func SetFromSlice

func SetFromSlice[T comparable](items []T) Set[T]

SetFromSlice returns a Set with all items in s present in the set. A utility to simplify set creation + initialization.

func (Set[T]) Add

func (s Set[T]) Add(items ...T)

Add adds 1 or more items to the set.

func (Set[T]) AddSet

func (s Set[T]) AddSet(other Set[T])

AddSet adds all the items in other to this set.

func (Set[T]) All

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

Items returns an iterator over s. Order of iteration should not be relied upon.

func (Set[T]) Clone

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

Clone returns a shallow copy of this set.

func (Set[T]) Del

func (s Set[T]) Del(ts ...T)

Del removes all the provided elements from the set. Has no effect when called with an item that does not exist in the set.

func (Set[T]) Equal

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

Equal returns whether s and other contain the same items.

func (Set[T]) Has

func (s Set[T]) Has(t T) bool

Has returns whether t exists in the set.

func (Set[T]) IsSubsetOf

func (s Set[T]) IsSubsetOf(other Set[T]) bool

IsSubsetOf returns whether s is a subset of other.

func (Set[T]) Size

func (s Set[T]) Size() int

Size returns the number of items in s.

func (Set[T]) Slice

func (s Set[T]) Slice() []T

ToSlice returns a slice representation of s. Order of items in resulting slice should not be relied upon.

func (Set[T]) Subtract

func (s Set[T]) Subtract(other Set[T])

Subtract removes all items in other from this set.

type Stack

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

Stack is a stack. A zero value is ready to use.

func NewStack

func NewStack[T any](capacity int) Stack[T]

NewStack returns a Stack with the specified capacity. This capacity may prevent memory allocations when working with the stack.

func (*Stack[T]) Pop

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

Pop removes the item at the top of the stack and returns it. Pop panics if called on an empty stack.

func (*Stack[T]) Push

func (s *Stack[T]) Push(t ...T)

Push puts t at the top of the stack.

func (Stack[T]) Size

func (s Stack[T]) Size() int

type SyncMap

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

SyncMap is a type-safe thread-safe map.

func NewSyncMap

func NewSyncMap[K comparable, V any](capacity int) *SyncMap[K, V]

NewSyncMap returns a new SyncMap.

func (*SyncMap[K, V]) All

func (s *SyncMap[K, V]) All() iter.Seq2[K, V]

All returns an iterator over the SyncMap. This method is intentionally not thread-safe. Meant to be used once concurrent ops are no longer being done.

func (*SyncMap[K, V]) Del

func (s *SyncMap[K, V]) Del(k K)

Del removes k from the map. Does nothing if the key does not exist.

func (*SyncMap[K, V]) Get

func (s *SyncMap[K, V]) Get(k K) (V, bool)

Get returns the value k is mapped to and whether one exists.

func (*SyncMap[K, V]) Set

func (s *SyncMap[K, V]) Set(k K, v V)

Set maps K to V.

func (*SyncMap[K, V]) SetFunc

func (s *SyncMap[K, V]) SetFunc(k K, f func(v V, exists bool) V)

SetFunc sets the mapping for k to the result of f. f is passed the existing mapping of k and whether it exists.

func (*SyncMap[K, V]) Size

func (s *SyncMap[K, V]) Size() int

Size returns the number of elements in the map.

Jump to

Keyboard shortcuts

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