container

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2025 License: Apache-2.0 Imports: 5 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]) List

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

List returns the internal list.

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 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.

type Sorted

type Sorted[T SortedElement] struct {
	// contains filtered or unexported fields
}

Sorted is a sorted container.

func NewSorted

func NewSorted[T SortedElement]() *Sorted[T]

NewSorted creates a new container that holds elements of type T sorted. Adds are costly as it sorts on each add, calls to Sorted() are free.

func (*Sorted[T]) Add

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

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

func (*Sorted[T]) Sorted

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

Sorted returns the internal list.

type SortedElement

type SortedElement interface {
	fmt.Stringer

	Priority() int
}

SortedElement needs to be implemented by every interface.

Jump to

Keyboard shortcuts

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