collection

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package collection provides generic data structures for Go.

All data structures in this package use Go generics to work with any type that satisfies the required constraints.

Stack

A LIFO (Last-In-First-Out) data structure:

stack := collection.NewStack[int]()
stack.Push(1)
stack.Push(2)
val, ok := stack.Pop()  // val=2, ok=true

Queue

A FIFO (First-In-First-Out) data structure:

queue := collection.NewQueue[string]()
queue.Enqueue("first")
queue.Enqueue("second")
val, ok := queue.Dequeue()  // val="first", ok=true

Set

An unordered collection of unique elements:

set := collection.NewSet[int]()
set.Add(1, 2, 3)
set.Contains(2)  // true
set.Remove(2)

BST (Binary Search Tree)

An ordered tree structure for efficient search, insert, and delete:

tree := collection.NewBST[int]()
tree.Insert(5, 3, 7, 1, 4)
tree.Contains(3)  // true
values := tree.InOrder()  // [1, 3, 4, 5, 7]

Thread Safety

The data structures in this package are NOT thread-safe by default. For concurrent access, use sync primitives or SafeStack/SafeQueue variants.

Design Decisions

- Generic: All structures use type parameters - Zero values work: NewXxx() returns ready-to-use structures - Optional returns: Pop/Dequeue return (value, bool) instead of panicking - Immutable iterators: Values() returns copies, not references

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BST

type BST[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

BST is a generic Binary Search Tree. Elements must be ordered (satisfy cmp.Ordered constraint).

func NewBST

func NewBST[T cmp.Ordered]() *BST[T]

NewBST creates a new empty Binary Search Tree.

Example:

tree := NewBST[int]()
tree.Insert(5, 3, 7)

func NewBSTFrom

func NewBSTFrom[T cmp.Ordered](items []T) *BST[T]

NewBSTFrom creates a BST from a slice.

func (*BST[T]) Clear

func (t *BST[T]) Clear()

Clear removes all elements from the tree.

func (*BST[T]) Contains

func (t *BST[T]) Contains(value T) bool

Contains returns true if the value is in the tree.

func (*BST[T]) ForEach

func (t *BST[T]) ForEach(fn func(T))

ForEach calls a function for each value in sorted order.

func (*BST[T]) Height

func (t *BST[T]) Height() int

Height returns the height of the tree. An empty tree has height 0; a single node has height 1.

func (*BST[T]) InOrder

func (t *BST[T]) InOrder() []T

InOrder returns values in sorted order (left, root, right).

Example:

tree.Insert(5, 3, 7, 1, 4)
tree.InOrder() // [1, 3, 4, 5, 7]

func (*BST[T]) Insert

func (t *BST[T]) Insert(values ...T)

Insert adds one or more values to the tree. Duplicate values are ignored.

Example:

tree.Insert(5, 3, 7, 1, 4)

func (*BST[T]) IsEmpty

func (t *BST[T]) IsEmpty() bool

IsEmpty returns true if the tree has no elements.

func (*BST[T]) Len

func (t *BST[T]) Len() int

Len returns the number of elements in the tree.

func (*BST[T]) LevelOrder

func (t *BST[T]) LevelOrder() []T

LevelOrder returns values level by level (breadth-first).

func (*BST[T]) Max

func (t *BST[T]) Max() (T, bool)

Max returns the maximum value in the tree. Returns zero value and false if tree is empty.

func (*BST[T]) Min

func (t *BST[T]) Min() (T, bool)

Min returns the minimum value in the tree. Returns zero value and false if tree is empty.

func (*BST[T]) PostOrder

func (t *BST[T]) PostOrder() []T

PostOrder returns values in post-order (left, right, root).

func (*BST[T]) PreOrder

func (t *BST[T]) PreOrder() []T

PreOrder returns values in pre-order (root, left, right).

func (*BST[T]) Remove

func (t *BST[T]) Remove(value T) bool

Remove deletes a value from the tree. Returns true if the value was found and removed.

func (*BST[T]) Values

func (t *BST[T]) Values() []T

Values returns all values in sorted order. Alias for InOrder().

type Queue

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

Queue is a generic FIFO (First-In-First-Out) data structure. The zero value is not usable; use NewQueue to create a Queue.

func NewQueue

func NewQueue[T any]() *Queue[T]

NewQueue creates a new empty Queue.

Example:

queue := NewQueue[string]()
queue.Enqueue("task1")

func NewQueueWithCapacity

func NewQueueWithCapacity[T any](capacity int) *Queue[T]

NewQueueWithCapacity creates a Queue with pre-allocated capacity.

func (*Queue[T]) Clear

func (q *Queue[T]) Clear()

Clear removes all elements from the queue.

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() (T, bool)

Dequeue removes and returns the front element. Returns false if the queue is empty.

Example:

val, ok := queue.Dequeue()
if !ok {
    // queue was empty
}

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(item T)

Enqueue adds an element to the back of the queue.

Example:

queue.Enqueue("task")

func (*Queue[T]) EnqueueAll

func (q *Queue[T]) EnqueueAll(items ...T)

EnqueueAll adds multiple elements to the queue. Elements are added in order.

func (*Queue[T]) IsEmpty

func (q *Queue[T]) IsEmpty() bool

IsEmpty returns true if the queue has no elements.

func (*Queue[T]) Len

func (q *Queue[T]) Len() int

Len returns the number of elements in the queue.

func (*Queue[T]) Peek

func (q *Queue[T]) Peek() (T, bool)

Peek returns the front element without removing it. Returns false if the queue is empty.

func (*Queue[T]) Values

func (q *Queue[T]) Values() []T

Values returns a copy of all elements in FIFO order.

type Set

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

Set is a generic unordered collection of unique elements. The zero value is not usable; use NewSet to create a Set.

func NewSet

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

NewSet creates a new empty Set.

Example:

set := NewSet[int]()
set.Add(1, 2, 3)

func NewSetFrom

func NewSetFrom[T comparable](items []T) *Set[T]

NewSetFrom creates a Set from a slice.

Example:

set := NewSetFrom([]int{1, 2, 2, 3}) // {1, 2, 3}

func NewSetWithCapacity

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

NewSetWithCapacity creates a Set with pre-allocated capacity.

func (*Set[T]) Add

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

Add adds one or more elements to the set. Elements already in the set are ignored.

Example:

set.Add(1, 2, 3)

func (*Set[T]) Clear

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

Clear removes all elements from the set.

func (*Set[T]) Clone

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

Clone returns a copy of the set.

func (*Set[T]) Contains

func (s *Set[T]) Contains(item T) bool

Contains returns true if the element is in the set.

func (*Set[T]) ContainsAll

func (s *Set[T]) ContainsAll(items ...T) bool

ContainsAll returns true if all elements are in the set.

func (*Set[T]) ContainsAny

func (s *Set[T]) ContainsAny(items ...T) bool

ContainsAny returns true if any element is in 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.

Example:

a := NewSetFrom([]int{1, 2, 3})
b := NewSetFrom([]int{2, 3, 4})
c := a.Difference(b) // {1}

func (*Set[T]) Equal

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

Equal returns true if both sets contain the same elements.

func (*Set[T]) Filter

func (s *Set[T]) Filter(predicate func(T) bool) *Set[T]

Filter returns a new set with elements that satisfy the predicate.

func (*Set[T]) ForEach

func (s *Set[T]) ForEach(fn func(T))

ForEach calls a function for each element in the set.

func (*Set[T]) Intersection

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

Intersection returns a new set containing elements in both sets.

Example:

a := NewSetFrom([]int{1, 2, 3})
b := NewSetFrom([]int{2, 3, 4})
c := a.Intersection(b) // {2, 3}

func (*Set[T]) IsEmpty

func (s *Set[T]) IsEmpty() bool

IsEmpty returns true if the set has no elements.

func (*Set[T]) IsSubset

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

IsSubset returns true if all elements of s are in other.

func (*Set[T]) IsSuperset

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

IsSuperset returns true if s contains all elements of other.

func (*Set[T]) Len

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

Len returns the number of elements in the set.

func (*Set[T]) Remove

func (s *Set[T]) Remove(items ...T)

Remove removes one or more elements from the set. Elements not in the set are ignored.

func (*Set[T]) SymmetricDifference

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

SymmetricDifference returns elements in either set but not both.

Example:

a := NewSetFrom([]int{1, 2, 3})
b := NewSetFrom([]int{2, 3, 4})
c := a.SymmetricDifference(b) // {1, 4}

func (*Set[T]) Union

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

Union returns a new set containing all elements from both sets.

Example:

a := NewSetFrom([]int{1, 2})
b := NewSetFrom([]int{2, 3})
c := a.Union(b) // {1, 2, 3}

func (*Set[T]) Values

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

Values returns all elements in the set. Order is not guaranteed.

type Stack

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

Stack is a generic LIFO (Last-In-First-Out) data structure. The zero value is not usable; use NewStack to create a Stack.

func NewStack

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

NewStack creates a new empty Stack.

Example:

stack := NewStack[int]()
stack.Push(1)

func NewStackWithCapacity

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

NewStackWithCapacity creates a Stack with pre-allocated capacity. Use this when you know the approximate size to reduce allocations.

func (*Stack[T]) Clear

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

Clear removes all elements from the stack.

func (*Stack[T]) IsEmpty

func (s *Stack[T]) IsEmpty() bool

IsEmpty returns true if the stack has no elements.

func (*Stack[T]) Len

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

Len returns the number of elements in the stack.

func (*Stack[T]) Peek

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

Peek returns the top element without removing it. Returns false if the stack is empty.

func (*Stack[T]) Pop

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

Pop removes and returns the top element. Returns false if the stack is empty.

Example:

val, ok := stack.Pop()
if !ok {
    // stack was empty
}

func (*Stack[T]) PopAll

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

PopAll removes and returns all elements from top to bottom. The stack will be empty after this operation.

func (*Stack[T]) Push

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

Push adds an element to the top of the stack.

Example:

stack.Push(42)

func (*Stack[T]) PushAll

func (s *Stack[T]) PushAll(items ...T)

PushAll adds multiple elements to the stack. Elements are pushed in order, so the last element becomes the top.

func (*Stack[T]) Values

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

Values returns a copy of all elements from bottom to top.

Jump to

Keyboard shortcuts

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