container

package module
v0.0.0-...-1835b0d Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2025 License: MIT Imports: 4 Imported by: 0

README

container

A Go utilities for containers

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearchFunc

func BinarySearchFunc[U any, T Item](container *Container[T], target U, cmp func(T, U) int) (pos int, ok bool)

BinarySearchFunc is alias to slices.BinarySearchFunc, that uses container, instead of slice.

func CompareOrdered

func CompareOrdered[T Ordered](exclude ...int) func(a, b T) int

CompareOrdered returns a comparison function for Ordered elements. The returned function can be used with sorting and binary search algorithms.

func FindOrderedPosition

func FindOrderedPosition[T Ordered, S ~[]T](s S, e T, exclude ...int) (int, bool)

FindOrderedPosition finds the position where an Ordered element should be inserted to maintain sorted order. It returns the position and a boolean, indicating if an element with the exact same order was found.

func FindOrderedPositionPointer

func FindOrderedPositionPointer[T Ordered, S ~[]*T](s S, e *T, exclude ...int) (int, bool)

FindOrderedPositionPointer finds the position where a pointer to an Ordered element should be inserted. It has the same behavior as FindOrderedPosition, but works with pointer slices.

func IndexFunc

func IndexFunc[T Item](container *Container[T], f func(T) bool) (idx int)

IndexFunc is alias to slices.IndexFunc, that uses container instead of slice.

func InsertOrdered

func InsertOrdered[T Ordered, S ~[]T](h S, element T, exclude ...int) S

InsertOrdered inserts an Ordered element into a sorted slice to the correct position. Elements with orders in the exclude list are appended to the end of the slice.

func InsertOrderedPointer

func InsertOrderedPointer[T Ordered, S ~[]*T](h S, element *T, exclude ...int) S

InsertOrderedPointer inserts a pointer to an Ordered element into a sorted slice. It has the same behavior as InsertOrdered but works with pointer slices instead.

func SliceAppend

func SliceAppend[T any, S ~[]T](old S, newItems ...T) S

func SliceDelete

func SliceDelete[T Item, S ~[]T](items S, name string, deleteOne bool) S

func SliceDeleteOne

func SliceDeleteOne[T Item](items []T, name string) ([]T, bool)

func SliceDeleteOnePointer

func SliceDeleteOnePointer[T Item](items []*T, name string) ([]*T, bool)

func SliceInsert

func SliceInsert[T any, S ~[]T](items S, item T, position int) S

func SlicePointerDelete

func SlicePointerDelete[T Item, S ~[]*T](items S, name string, deleteOne bool) S

func SortOrdered

func SortOrdered[T Ordered, S ~[]T](h S, exclude ...int)

SortOrdered sorts a slice of Ordered elements by their order. If element is excluded, it'll be inserted to the end of slice.

func SortOrderedPointer

func SortOrderedPointer[T Ordered, S ~[]*T](h S, exclude ...int)

SortOrderedPointer sorts a slice of pointers to Ordered elements. It has the same behavior as SortOrdered but works with pointer slices.

func VerifySorted

func VerifySorted[T Item](container *Container[T], cmp func(T, T) int) (ok bool)

VerifySorted is alias to slices.IsSortedFunc, that uses container instead of slice.

Types

type Container

type Container[T Item] struct {
	// contains filtered or unexported fields
}

Container represents a concurrent-safe container. It uses CAS methodic to prevent data races. It must be created via New (container.New) function. Otherwise, it can easily panic because of nil pointer dereference.

func New

func New[T Item](items ...T) (container *Container[T])

New creates a new initialized container with optional items.

func (*Container[T]) Append

func (container *Container[T]) Append(items ...T)

Append appends multiple items to the container. Nothing happens, if no items provided.

func (*Container[T]) Clear

func (container *Container[T]) Clear()

Clear clears the container by storing an empty slice. Using empty slice instead of nil.

func (*Container[T]) Delete

func (container *Container[T]) Delete(name string, deleteOne bool)

Delete tries to delete the item from container by name.

func (*Container[T]) Get

func (container *Container[T]) Get(name string) (_ T, _ bool)

Get tries to get an item from container by its name.

func (*Container[T]) InsertPosition

func (container *Container[T]) InsertPosition(item T, position int)

InsertPosition inserts the item at the specified position in the container. If position is incorrect, it'll be inserted to the end of slice.

func (*Container[T]) Iterate

func (container *Container[T]) Iterate(f func(item T, index int) bool)

Iterate iterates across all container items. Stops iteration if f returns false.

func (*Container[T]) Len

func (container *Container[T]) Len() int

Len returns the length of items in container.

type Item

type Item interface {
	// Name returns name of the item.
	// Used to easily compare two items.
	Name() string
}

Item represents the container item.

type Mapped

type Mapped[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Mapped represents concurrent-safe map container. It uses CAS methodic for preventing data races.

func NewMapped

func NewMapped[K comparable, V any]() (m *Mapped[K, V])

NewMapped creates a new initialized mapped container.

func (*Mapped[K, V]) Clear

func (container *Mapped[K, V]) Clear()

Clear clears the container.

func (*Mapped[K, V]) Contains

func (container *Mapped[K, V]) Contains(key K) bool

Contains returns true, if the container can find value for this key.

func (*Mapped[K, V]) Delete

func (container *Mapped[K, V]) Delete(key K)

Delete deletes a value from container by key.

func (*Mapped[K, V]) Get

func (container *Mapped[K, V]) Get(key K) (V, bool)

Get gets a value by key from the container.

func (*Mapped[K, V]) GetWithZero

func (container *Mapped[K, V]) GetWithZero(key K) V

GetWithZero gets a value by key from the container. If there's no value for this key, zero V will be returned.

func (*Mapped[K, V]) GlobalIterate

func (container *Mapped[K, V]) GlobalIterate(f func(key K, value V) bool)

GlobalIterate iterates across all key-value pairs in the container.

func (*Mapped[K, V]) Iter

func (container *Mapped[K, V]) Iter() iter.Seq2[K, V]

Iter returns iterator of the container objects.

func (*Mapped[K, V]) Keys

func (container *Mapped[K, V]) Keys() []K

Keys returns all keys in the container.

func (*Mapped[K, V]) Len

func (container *Mapped[K, V]) Len() int

Len returns the length of items in the container.

func (*Mapped[K, V]) Set

func (container *Mapped[K, V]) Set(key K, value V)

Set updates the value in container. If the value with this key already exists, it'll overwrite it.

func (*Mapped[K, V]) Values

func (container *Mapped[K, V]) Values() []V

Values returns all values in the container.

type MappedCollection

type MappedCollection[K comparable, V MappedItem] struct {
	// contains filtered or unexported fields
}

MappedCollection represents container, that groups collections by keys.

func NewMappedCollection

func NewMappedCollection[K comparable, V MappedItem]() (m *MappedCollection[K, V])

NewMappedCollection creates a new initialized mapped collection.

func (*MappedCollection[K, V]) Add

func (mc *MappedCollection[K, V]) Add(key K, value *V)

Add adds a value to the collection under the specified key.

func (*MappedCollection[K, V]) Clear

func (mc *MappedCollection[K, V]) Clear()

Clear clears the entire container.

func (*MappedCollection[K, V]) DeleteKey

func (mc *MappedCollection[K, V]) DeleteKey(key K)

DeleteKey removes all values from the collection by key.

func (*MappedCollection[K, V]) FindOrderedPosition

func (mc *MappedCollection[K, V]) FindOrderedPosition(key K, value *V, exclude ...int) (int, bool)

FindOrderedPosition finds ordered position for collection by value.

func (*MappedCollection[K, V]) Get

func (mc *MappedCollection[K, V]) Get(key K) ([]*V, bool)

Get returns all values of the collection by key.

func (*MappedCollection[K, V]) GlobalIterate

func (mc *MappedCollection[K, V]) GlobalIterate(f func(key K, value *V) bool)

GlobalIterate iterates over all collections and their values.

func (*MappedCollection[K, V]) InsertOrdered

func (mc *MappedCollection[K, V]) InsertOrdered(key K, value *V, exclude ...int)

InsertOrdered inserts a value by order into the collection by key.

func (*MappedCollection[K, V]) IterateKey

func (mc *MappedCollection[K, V]) IterateKey(key K, f func(val *V) bool)

IterateKey iterates over values of a collection by key.

func (*MappedCollection[K, V]) KeyExists

func (mc *MappedCollection[K, V]) KeyExists(key K) bool

KeyExists returns true, if container contains a collection by entered key.

func (*MappedCollection[K, V]) Keys

func (mc *MappedCollection[K, V]) Keys() (keys []K)

Keys returns all container keys.

func (*MappedCollection[K, V]) Len

func (mc *MappedCollection[K, V]) Len() (total int)

Len returns total length of all collections in the container.

func (*MappedCollection[K, V]) RemoveValue

func (mc *MappedCollection[K, V]) RemoveValue(key K, value *V)

RemoveValue removes a specific value from the collection by key.

func (*MappedCollection[K, V]) RemoveValueByName

func (mc *MappedCollection[K, V]) RemoveValueByName(key K, name string, deleteOne bool)

RemoveValueByName removes value(s) by name from the collection by key.

func (*MappedCollection[K, V]) Size

func (mc *MappedCollection[K, V]) Size(key K) int

Size returns the length of values for a collection by key.

func (*MappedCollection[K, V]) SortKey

func (mc *MappedCollection[K, V]) SortKey(key K, exclude ...int)

SortKey sorts values for a specific collection using the Ordered interface.

type MappedItem

type MappedItem interface {
	Ordered
	Item
}

MappedItem represents the mapped container item.

type Ordered

type Ordered interface {
	// Order returns the order of this element.
	Order() int
}

Ordered is implementation of ordered element.

Jump to

Keyboard shortcuts

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