container

package
v0.1.83 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 4 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

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

Element is a thread-safe element of a linked list.

func (*Element[T]) Next

func (e *Element[T]) Next() *Element[T]

Next returns the next list element or nil.

func (*Element[T]) Prev

func (e *Element[T]) Prev() *Element[T]

Prev returns the previous list element or nil.

func (*Element[T]) Set added in v0.1.81

func (e *Element[T]) Set(v T) *Element[T]

Set assigns value v to the element and returns it.

func (*Element[T]) Value

func (e *Element[T]) Value() T

Value returns the value stored with this element.

type Int added in v0.1.82

type Int[T ~int64 | ~int32 | ~int16 | ~int8 | ~int] struct {
	// contains filtered or unexported fields
}

An Int provides atomic operations on an integer value of type T. It is a generic wrapper around atomic.Int64, allowing usage with signed integer types such as int, int8, int16, int32, and int64.

func (*Int[T]) Add added in v0.1.82

func (v *Int[T]) Add(delta T) (new T)

Add atomically adds delta to the value and returns the new value.

func (*Int[T]) And added in v0.1.82

func (v *Int[T]) And(mask T) (old T)

And atomically performs a bitwise AND operation with mask and returns the previous value.

func (*Int[T]) CompareAndSwap added in v0.1.82

func (v *Int[T]) CompareAndSwap(old T, new T) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for the Int. It compares the current value with old, and if they are equal, sets it to new and returns true. Otherwise, it returns false.

func (*Int[T]) Load added in v0.1.82

func (v *Int[T]) Load() T

Load atomically loads and returns the current value.

func (*Int[T]) Or added in v0.1.82

func (v *Int[T]) Or(mask T) (old T)

Or atomically performs a bitwise OR operation with mask and returns the previous value.

func (*Int[T]) Store added in v0.1.82

func (v *Int[T]) Store(val T)

Store atomically stores val into the Int.

func (*Int[T]) Swap added in v0.1.82

func (v *Int[T]) Swap(new T) (old T)

Swap atomically stores new into the Int and returns the previous value.

type List

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

List represents a thread-safe doubly linked list. The zero value for List is an empty list ready to use.

func NewList

func NewList[T any]() *List[T]

NewList returns an initialized list.

func (*List[T]) Back

func (l *List[T]) Back() *Element[T]

Back returns the last element of list l or nil if the list is empty.

func (*List[T]) Front

func (l *List[T]) Front() *Element[T]

Front returns the first element of list l or nil if the list is empty.

func (*List[T]) Init

func (l *List[T]) Init() *List[T]

Init initializes or clears list l.

func (*List[T]) InsertAfter

func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T]

InsertAfter inserts a new element e with value v immediately after mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.

func (*List[T]) InsertBefore

func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T]

InsertBefore inserts a new element e with value v immediately before mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.

func (*List[T]) Len

func (l *List[T]) Len() int

Len returns the number of elements of list l. The complexity is O(1).

func (*List[T]) MoveAfter

func (l *List[T]) MoveAfter(e, mark *Element[T])

MoveAfter moves element e to its new position after mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.

func (*List[T]) MoveBefore

func (l *List[T]) MoveBefore(e, mark *Element[T])

MoveBefore moves element e to its new position before mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.

func (*List[T]) MoveToBack

func (l *List[T]) MoveToBack(e *Element[T])

MoveToBack moves element e to the back of list l. If e is not an element of l, the list is not modified. The element must not be nil.

func (*List[T]) MoveToFront

func (l *List[T]) MoveToFront(e *Element[T])

MoveToFront moves element e to the front of list l. If e is not an element of l, the list is not modified. The element must not be nil.

func (*List[T]) PushBack

func (l *List[T]) PushBack(v T) *Element[T]

PushBack inserts a new element e with value v at the back of list l and returns e.

func (*List[T]) PushBackList

func (l *List[T]) PushBackList(other *List[T])

PushBackList inserts a copy of another list at the back of list l. The lists l and other may be the same. They must not be nil.

func (*List[T]) PushFront

func (l *List[T]) PushFront(v T) *Element[T]

PushFront inserts a new element e with value v at the front of list l and returns e.

func (*List[T]) PushFrontList

func (l *List[T]) PushFrontList(other *List[T])

PushFrontList inserts a copy of another list at the front of list l. The lists l and other may be the same. They must not be nil.

func (*List[T]) Remove

func (l *List[T]) Remove(e *Element[T]) T

Remove removes e from l if e is an element of list l. It returns the element value e.Value. The element must not be nil.

type Map

type Map[Key comparable, Value any] struct {
	// contains filtered or unexported fields
}

Map is a generic concurrency-safe map that wraps sync.Map and provides type-safe access for keys and values.

func NewMap

func NewMap[Key comparable, Value any]() *Map[Key, Value]

NewMap creates and returns a new, empty generic concurrency-safe Map.

func (*Map[Key, Value]) Clear

func (m *Map[Key, Value]) Clear()

Clear deletes all the entries, resulting in an empty Map.

func (*Map[Key, Value]) CompareAndDelete

func (m *Map[Key, Value]) CompareAndDelete(key Key, old Value) (deleted bool)

CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type.

If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).

func (*Map[Key, Value]) CompareAndSwap

func (m *Map[Key, Value]) CompareAndSwap(key Key, old Value, new Value) (swapped bool)

CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.

func (*Map[Key, Value]) Delete

func (m *Map[Key, Value]) Delete(key Key)

Delete deletes the value for a key.

func (*Map[Key, Value]) Load

func (m *Map[Key, Value]) Load(key Key) (value Value, ok bool)

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*Map[Key, Value]) LoadAndDelete

func (m *Map[Key, Value]) LoadAndDelete(key Key) (value Value, loaded bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*Map[Key, Value]) LoadOrStore

func (m *Map[Key, Value]) LoadOrStore(key Key, value Value) (actual Value, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*Map[Key, Value]) Range

func (m *Map[Key, Value]) Range(f func(Key, Value) bool)

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

Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m.

Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.

func (*Map[Key, Value]) Store

func (m *Map[Key, Value]) Store(key Key, value Value)

Store sets the value for a key.

func (*Map[Key, Value]) Swap

func (m *Map[Key, Value]) Swap(key Key, value Value) (previous Value, loaded bool)

Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present.

type Ring

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

A Ring is an element of a thread-safe, generic circular list, or ring. Rings do not have a beginning or end; a pointer to any ring element serves as reference to the entire ring. Empty rings are represented as nil Ring pointers. The zero value for a Ring[T] is a one-element ring with a zero Value of type T.

func NewRing

func NewRing[T any](n int) *Ring[T]

NewRing creates a ring of n elements.

func (*Ring[T]) Do

func (r *Ring[T]) Do(f func(T))

Do calls function f on each element of the ring, in forward order. The behavior of Do is undefined if f changes *r.

func (*Ring[T]) Len

func (r *Ring[T]) Len() int

Len computes the number of elements in ring r. It executes in time proportional to the number of elements.

func (r *Ring[T]) Link(s *Ring[T]) *Ring[T]

Link connects ring r with ring s such that r.Next() becomes s and returns the original value for r.Next(). r must not be empty.

If r and s point to the same ring, linking them removes the elements between r and s from the ring. The removed elements form a subring and the result is a reference to that subring (if no elements were removed, the result is still the original value for r.Next(), and not nil).

If r and s point to different rings, linking them creates a single ring with the elements of s inserted after r. The result points to the element following the last element of s after insertion.

func (*Ring[T]) Move

func (r *Ring[T]) Move(n int) *Ring[T]

Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) in the ring and returns that ring element. r must not be empty.

func (*Ring[T]) Next

func (r *Ring[T]) Next() *Ring[T]

Next returns the next ring element. r must not be empty.

func (*Ring[T]) Prev

func (r *Ring[T]) Prev() *Ring[T]

Prev returns the previous ring element. r must not be empty.

func (*Ring[T]) Set

func (r *Ring[T]) Set(v T) *Ring[T]

Set assigns value v to the ring element and returns it.

func (r *Ring[T]) Unlink(n int) *Ring[T]

Unlink removes n % r.Len() elements from the ring r, starting at r.Next(). If n % r.Len() == 0, r remains unchanged. The result is the removed subring. r must not be empty.

func (*Ring[T]) Value

func (r *Ring[T]) Value() T

Value returns the value of the ring element.

type Value

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

A Value provides an atomic load and store of a specified typed value. Once Value.Store has been called, a Value must not be copied.

A Value must not be copied after first use.

func NewValue

func NewValue[T any]() *Value[T]

NewValue creates a new instance of Value.

func (*Value[T]) CompareAndSwap

func (v *Value[T]) CompareAndSwap(old, new T) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for the Value.

func (*Value[T]) Load

func (v *Value[T]) Load() (val T)

Load returns the value set by the most recent Value.Store. If there has been no call to Value.Store for this Value, it returns the zero value of T.

func (*Value[T]) Store

func (v *Value[T]) Store(val T)

Store sets the value of the Value v to val.

func (*Value[T]) Swap

func (v *Value[T]) Swap(new T) (old T)

Swap stores the new value into the Value and returns the previous value. If no value was previously stored, it returns the zero value of T.

Jump to

Keyboard shortcuts

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