datastructures

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HashFn

type HashFn[T any] func(t T) string

HashFn is a type for hashing function that converts a value of any type to a string.

type ISet

type ISet[V any] interface {
	Has(k V) bool
	Remove(k V)
	Put(k V)
	Clear()
	Copy() ISet[V]
	Size() int
	String() string
	Values() []V
	New() ISet[V]
	All() iter.Seq2[int, V]
}

ISet is the internal representation of a set, you may implement your own if desired.

type RingQueue

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

func NewRingQueue

func NewRingQueue[T any](capacity int, synchronized bool) *RingQueue[T]

NewRingQueue creates a new RingQueue with the given capacity and synchronization flag. If synchronized, operations require locks.

func (*RingQueue[T]) Items

func (r *RingQueue[T]) Items() []T

Items returns all elements in the queue. The order is oldest to newest.

func (*RingQueue[T]) Iter

func (r *RingQueue[T]) Iter() iter.Seq[T]

Iter returns an iterator that yields all elements in the ring queue oldest to newest. Empty elements are not yielded

func (*RingQueue[T]) Iter2

func (r *RingQueue[T]) Iter2() iter.Seq2[int, T]

Iter2 returns an iterator that yields all elements in the ring queue oldest to newest. Empty elements are not yielded

func (*RingQueue[T]) Pop

func (r *RingQueue[T]) Pop() T

Pop removes the oldest element in the queue and returns it. If the queue is empty, it returns the zero value of the type.

func (*RingQueue[T]) Push

func (r *RingQueue[T]) Push(elem T)

Push adds a new element to the queue. If the queue is full, it overwrites the oldest element.

func (*RingQueue[T]) Size

func (r *RingQueue[T]) Size() int

Size returns the number of non-zero elements in the queue.

type Set

type Set[V any] struct {
	// contains filtered or unexported fields
}

func NewCustomSet

func NewCustomSet[V any](iSet ISet[V]) Set[V]

NewCustomSet creates a new set with the provided ISet implementation

func NewHashSet

func NewHashSet[V any](values ...V) Set[V]

NewHashSet creates a new set with the provided values. Supports any type.

func NewHashSetWithHashFn

func NewHashSetWithHashFn[V any](hashFn HashFn[V], values ...V) Set[V]

func NewSet

func NewSet[V comparable](values ...V) Set[V]

NewSet creates a new set with the provided values. Supports only comparable types.

func (Set[V]) All

func (s Set[V]) All() iter.Seq2[int, V]

All returns an iterator that yields all elements in the set in the order they were inserted.

func (Set[V]) Clear

func (s Set[V]) Clear()

Clear removes all elements from the set.

func (Set[V]) Clone

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

Clone returns a copy of this set.

func (Set[V]) Difference

func (s Set[V]) Difference(others ...Set[V]) Set[V]

Difference returns a new set with the difference of the current set and others

func (Set[V]) Each

func (s Set[V]) Each(fn func(v V))

Each calls 'fn' on every item in the set in no particular order.

func (Set[V]) EachWithErrs

func (s Set[V]) EachWithErrs(fn func(key V) error) []error

EachWithErrs calls 'fn' on every item in the set in no particular order. All errors are aggregated and returned

func (Set[V]) Equal

func (s Set[V]) Equal(to Set[V]) bool

Equal returns true if the current set is equal to others

func (Set[V]) Has

func (s Set[V]) Has(k V) bool

Has returns true only if 'val' is in the set.

func (Set[V]) InPlaceDifference

func (s Set[V]) InPlaceDifference(others ...Set[V]) Set[V]

InPlaceDifference will modify the current set in place to become the difference with others

func (Set[V]) InPlaceIntersection

func (s Set[V]) InPlaceIntersection(others ...Set[V]) Set[V]

InPlaceIntersection will modify the current set in place to become the intersection with others

func (Set[V]) InPlaceUnion

func (s Set[V]) InPlaceUnion(others ...Set[V]) Set[V]

InPlaceUnion will modify the current set in place to become the union with others

func (Set[V]) Intersection

func (s Set[V]) Intersection(others ...Set[V]) Set[V]

Intersection returns a new set with the intersection of the current set and others

func (Set[V]) IsDisjoint

func (s Set[V]) IsDisjoint(other Set[V]) bool

IsDisjoint returns true if the current set has no elements in common with others

func (Set[V]) IsProperSubset

func (s Set[V]) IsProperSubset(to Set[V]) bool

IsProperSubset returns true if the current set is a proper subset of others

func (Set[V]) IsProperSuperset

func (s Set[V]) IsProperSuperset(to Set[V]) bool

IsProperSuperset returns true if the current set is a proper superset of others

func (Set[V]) IsSubset

func (s Set[V]) IsSubset(of Set[V]) bool

IsSubset returns true if the current set is a subset of others

func (Set[V]) IsSuperset

func (s Set[V]) IsSuperset(of Set[V]) bool

IsSuperset returns true if the current set is a superset of others

func (Set[V]) Keys

func (s Set[V]) Keys() []V

func (Set[V]) MarshalJSON

func (s Set[V]) MarshalJSON() ([]byte, error)

func (Set[V]) Put

func (s Set[V]) Put(k V)

Put adds 'val' to the set.

func (Set[V]) Remove

func (s Set[V]) Remove(k V)

Remove removes 'val' from the set.

func (Set[V]) Size

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

Size returns the number of elements in the set.

func (Set[V]) String

func (s Set[V]) String() string

String returns a string representation of the set.

func (Set[V]) SymmetricDifference

func (s Set[V]) SymmetricDifference(others ...Set[V]) Set[V]

SymmetricDifference returns a new set with the symmetric difference of the current set and others

func (Set[V]) Union

func (s Set[V]) Union(others ...Set[V]) Set[V]

Union returns a new set with the union of the current set and others

func (Set[V]) Values

func (s Set[V]) Values() []V

type Stack

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

Stack implements a LIFO stack with peeking.

func (*Stack[T]) Copy

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

Copy returns a copy of this stack.

func (*Stack[T]) HasMore

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

HasMore returns true if the stack is not empty

func (*Stack[T]) Peek

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

Peek returns the stack's top element but does not remove it. If the stack is empty the zero value is returned.

func (*Stack[T]) Pop

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

Pop removes the stack's top element and returns it. If the stack is empty it returns the zero value.

func (*Stack[T]) Push

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

Push places 'value' at the top of the stack.

func (*Stack[T]) Size

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

Size returns the number of elements in the stack.

type Tuple

type Tuple[A any, B any] struct {
	A A
	B B
}

type TupleWithErr

type TupleWithErr[A any, B any] struct {
	A A
	B B
	E error
}

func (TupleWithErr[A, B]) HasError

func (t TupleWithErr[A, B]) HasError() bool

func (TupleWithErr[A, B]) HasValue

func (t TupleWithErr[A, B]) HasValue() bool

HasValue returns true if the tuple has a non-zero value set for either A or B

Jump to

Keyboard shortcuts

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