collections

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2025 License: MIT Imports: 4 Imported by: 1

README

go-collections

Little collection of generic collections in golang.

heap

type CustomObject struct {
    ID int	
}

// max heap based on the ID field
h := NewHeap(func(a, b CustomObject) bool {
	return a.ID > b.ID
})

// for ordered default types there is a wrapper
h := NewOrderedHeap[int]()

queue

var q Queue[int]

q.Push(42, 43)

pop := q.Pop() // returns 42

set

var s Set[int]
s.Add(42, 43)

if s.Contains(42) { 
    // do something
}

stack

var s Stack[int]

s.Push(42, 43)

pop := s.Pop() // returns 43

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect added in v0.4.0

func Collect[T any](iter iter.Seq[T], sizeHint int) []T

Collect collects all the values from the iterator and returns a slice holding them. The sizeHint parameter can be specified to optimize the memory allocation of the slice and avoid unnecessary copies.

Types

type Heap

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

func NewHeap

func NewHeap[T any](less func(a, b T) bool) *Heap[T]

NewHeap creates a new Heap using the given less function for ordering. To create a min or a max heap, adjust the less function accordingly. For example,

h := NewHeap(func(a, b int) bool { return a < b })

to create a new min heap using the int type. And logically

h := NewHeap(func(a, b int) bool { return a > b })

for a max heap.

func NewOrderedHeap

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

NewOrderedHeap creates a new Heap using NewHeap with the default less function for the given cmp.Ordered type.

func (*Heap[T]) Ordered

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

func (*Heap[T]) Peek

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

func (*Heap[T]) Pop

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

func (*Heap[T]) Push

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

func (*Heap[T]) Size

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

func (*Heap[T]) Values

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

type Queue

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

func (*Queue[T]) Ordered

func (q *Queue[T]) Ordered() iter.Seq[T]

func (*Queue[T]) Peek

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

func (*Queue[T]) Pop

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

func (*Queue[T]) Push

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

func (*Queue[T]) Size

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

func (*Queue[T]) Values

func (q *Queue[T]) Values() iter.Seq[T]

type Set

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

func (*Set[T]) Add

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

func (*Set[T]) Contains

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

func (*Set[T]) ContainsAll

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

func (*Set[T]) Delete

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

func (*Set[T]) Eq

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

func (*Set[T]) Size

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

func (*Set[T]) Values

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

type Stack

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

func (*Stack[T]) Ordered

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

func (*Stack[T]) Peek

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

func (*Stack[T]) Pop

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

func (*Stack[T]) Push

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

func (*Stack[T]) Size

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

func (*Stack[T]) Values

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

Jump to

Keyboard shortcuts

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