container

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: Apache-2.0 Imports: 7 Imported by: 2

Documentation

Overview

Package container contains generic containers.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrExists is returned when the given element exists in the container.
	ErrExists = errors.New("element exists already")
	// ErrUnknown is returned when the given name doesn't exist in the container.
	ErrUnknown = errors.New("unknown element given")
)

Functions

This section is empty.

Types

type Hashable added in v0.1.0

type Hashable interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string
}

Hashable is used as the key for maps.

type List

type List[T fmt.Stringer] struct {
	// contains filtered or unexported fields
}

List is a list container of T.

func NewList

func NewList[T fmt.Stringer]() *List[T]

NewList creates a new container that holds element's of type T.

func (*List[T]) Add

func (c *List[T]) Add(element T) error

Add adds a new element to the container.

func (*List[T]) Clear

func (c *List[T]) Clear()

Clear clears the internal list.

func (*List[T]) Clone added in v0.2.0

func (c *List[T]) Clone() *List[T]

Clone makes a deep copy of the current list.

func (*List[T]) List

func (c *List[T]) List() []T

List returns the internal list.

func (*List[T]) Set added in v0.2.0

func (c *List[T]) Set(element T)

Set overwrites the given item in the list or appends it.

type Map

type Map[K Hashable, T any] struct {
	// contains filtered or unexported fields
}

Map is a map container for function factories.

func NewMap

func NewMap[K Hashable, T any]() *Map[K, T]

NewMap creates a new map of any type. This is not concurrent safe.

func (*Map[K, T]) Add

func (c *Map[K, T]) Add(name K, element T) bool

Add adds a new factory function to this container. It returns false if the element already exists.

func (*Map[K, T]) All

func (c *Map[K, T]) All() map[K]T

All returns the internal map.

func (*Map[K, T]) Del added in v0.1.0

func (c *Map[K, T]) Del(key K) bool

Del calls delete(me, key) and returns true if the element existed.

func (*Map[K, T]) Get

func (c *Map[K, T]) Get(name K) (T, bool)

Get returns a single item by its name.

func (*Map[K, T]) Len added in v0.1.0

func (c *Map[K, T]) Len() int

Len returns the length of the internal map.

func (*Map[K, T]) Range added in v0.1.0

func (c *Map[K, T]) Range(f func(k K, v T) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

func (*Map[K, T]) Set

func (c *Map[K, T]) Set(name K, element T)

Set will set a value regardless of whether it already exists in the map.

type Pool added in v0.1.0

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

A Pool is a generic wrapper around a sync.Pool.

func NewPool added in v0.1.0

func NewPool[T any](fn func() T) Pool[T]

NewPool creates a new Pool with the provided new function.

The equivalent sync.Pool construct is "sync.Pool{New: fn}".

func (*Pool[T]) Get added in v0.1.0

func (p *Pool[T]) Get() T

Get is a generic wrapper around sync.Pool's Get method.

func (*Pool[T]) Put added in v0.1.0

func (p *Pool[T]) Put(x T)

Put is a generic wrapper around sync.Pool's Put method.

type PriorityList added in v0.1.1

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

PriorityList is a priority list of elements.

func NewPriorityList added in v0.1.1

func NewPriorityList[T any]() *PriorityList[T]

NewPriorityList creates a new priority list of elements.

func (*PriorityList[T]) Add added in v0.1.1

func (c *PriorityList[T]) Add(element T, priority int) error

Add adds a new element with the given priority to the list.

func (*PriorityList[T]) Iterate added in v0.1.1

func (c *PriorityList[T]) Iterate(reversed bool) iter.Seq2[int, T]

Iterate clones the internal list, sorts it and then returns a new iterator over the elements of the priority list. Requires Go 1.23 or later.

type PriorityListElement added in v0.1.1

type PriorityListElement[T any] struct {
	Item     T
	Priority int
}

PriorityListElement needs to be implemented by every interface.

type Priorizeable added in v0.1.0

type Priorizeable interface {
	Priority() int
}

Priorizeable has to be implemented by elements that use container.Priorize*.

type SafeMap

type SafeMap[K Hashable, T any] struct {
	*hashmap.Map[K, T]
}

SafeMap is a wrapper for "github.com/cornelk/hashmap".

func NewSafeMap

func NewSafeMap[K Hashable, T any]() *SafeMap[K, T]

NewSafeMap creates a wrapper for "github.com/cornelk/hashmap".

func (*SafeMap[K, T]) Add

func (m *SafeMap[K, T]) Add(key K, value T) bool

Add is wrapper for hasmap.Insert.

Jump to

Keyboard shortcuts

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