dt

package
v0.14.1 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 18 Imported by: 23

Documentation

Overview

Package dt provides container type implementations and interfaces.

All top level structures in this package can be trivially constructed and provide high level interfaces for most common operations. These structures are not safe for access from multiple concurrent go routines (see the queue/deque in the pubsub package as an alternative for these use cases.)

Index

Constants

View Source
const ErrContainerStateImpossible ers.Error = ers.Error("impossible container state")

ErrContainerStateImpossible is returned (often wrapped,) by implementations of container datatypes that have reached an impossible or invalid state.

View Source
const ErrUninitializedContainer ers.Error = ers.Error("uninitialized container")

ErrUninitializedContainer is the content of the panic produced when you attempt to perform an operation on an uninitialized sequence.

Variables

This section is empty.

Functions

func Unwind

func Unwind[T any](in T) []T

Unwind uses the Unwrap operation to build a list of the "wrapped" objects.

func Unwrap

func Unwrap[T any](in T) (out T)

Unwrap is a generic equivalent of the `errors.Unwrap()` function for any type that implements an `Unwrap() T` method. useful in combination with Is.

Types

type Element

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

Element is the underlying component of a list, provided by iterators, the Pop operations, and the Front/Back accesses in the list. You can use the methods on this objects to iterate through the list, and the Ok() method for validating zero-valued items.

While Elements and Lists are not safe for concurrent access, it's safe to combine use of List and Element driven-mutations.

func NewElement

func NewElement[T any](val T) *Element[T]

NewElement produces an unattached Element that you can use with Append. Element.Append(NewElement()) is essentially the same as List.PushBack().

func (*Element[T]) Append

func (e *Element[T]) Append(val *Element[T]) *Element[T]

Append adds the element 'new' after the element 'e', inserting it in the next position in the list. Will return 'e' if 'new' is not valid for insertion into this list (e.g. it belongs to another list, or is attached to other elements, is already a member of this list, or is otherwise invalid.) PushBack and PushFront, are implemented in terms of Append.

func (*Element[T]) Drop

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

Drop wraps remove, and additionally, if the remove was successful, drops the value and sets the Ok value to false.

func (*Element[T]) In

func (e *Element[T]) In(l *List[T]) bool

In checks to see if an element is in the specified list. Because elements hold a pointer to their list, this is an O(1) operation.

Returns false when the element is nil.

func (*Element[T]) MarshalJSON

func (e *Element[T]) MarshalJSON() ([]byte, error)

MarshalJSON satisfies the json.Marshaler interface. Nil and unset Element values are marshaled as nil.

func (*Element[T]) Next

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

Next produces the next element. This is always non-nil, *unless* the element is not a member of a list. At the ends of a list, the value is non-nil, but would return false for Ok.

func (*Element[T]) Ok

func (e *Element[T]) Ok() bool

Ok checks that an element is valid. Invalid elements can be produced at the end of iterations (e.g. the list's root object,) or if you attempt to Pop an element off of an empty list.

Returns false when the element is nil.

func (*Element[T]) Previous

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

Previous produces the next element. This is always non-nil, *unless* the element is not a member of a list. At the ends of a list, the value is non-nil, but would return false for Ok.

func (*Element[T]) Push added in v0.12.0

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

Push adds a value to the list, and returns the resulting element.

func (*Element[T]) Remove

func (e *Element[T]) Remove() bool

Remove removes the elemtn from the list, returning true if the operation was successful. Remove returns false when the element is not valid to be removed (e.g. is not part of a list, is the root element of the list, etc.)

func (*Element[T]) Set

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

Set allows you to change set the value of an item in place. Returns true if the operation is successful. The operation fails if the Element is the root item in a list or not a member of a list.

Set is safe to call on nil elements.

func (*Element[T]) String

func (e *Element[T]) String() string

String returns the string form of the value of the element.

func (*Element[T]) Swap

func (e *Element[T]) Swap(with *Element[T]) bool

Swap exchanges the location of two elements in a list, returning true if the operation was successful, and false if the elements are not eligible to be swapped. It is valid/possible to swap the root element of the list with another element to "move the head", causing a wrap around effect. Swap will not operate if either element is nil, or not a member of the same list.

func (*Element[T]) UnmarshalJSON

func (e *Element[T]) UnmarshalJSON(in []byte) error

UnmarshalJSON reads the json value, and sets the value of the element to the value in the json, potentially overriding an existing value. By supporting json.Marshaler and json.Unmarshaler, Elements and lists can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

func (*Element[T]) Value

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

Value accesses the element's value.

type Heap

type Heap[T any] struct {
	CF func(T, T) int
	// contains filtered or unexported fields
}

Heap provides a min-order heap using the Heap.LT comparison operator to sort from lowest to highest. Push operations will panic if LT is not set.

func (*Heap[T]) Iterator

func (h *Heap[T]) Iterator() iter.Seq[T]

Iterator provides an iterator to the items in the heap.

func (*Heap[T]) Len

func (h *Heap[T]) Len() int

Len reports the size of the heap. Because the heap tracks its size with Push/Pop operations, this is a constant time operation.

func (*Heap[T]) Pop

func (h *Heap[T]) Pop() (T, bool)

Pop removes the element from the underlying list and returns it, with an Ok value, which is true when the value returned is valid.

func (*Heap[T]) Push

func (h *Heap[T]) Push(t T)

Push adds an item to the heap.

type Item

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

Item is a common wrapper for the elements in a stack.

func NewItem

func NewItem[T any](in T) *Item[T]

NewItem produces a valid Item object for the specified value.

func (*Item[T]) Append

func (it *Item[T]) Append(n *Item[T]) *Item[T]

Append inserts a new item after the following item in the stack, returning the new item, or if the new item is not valid, the item itself.

func (*Item[T]) Attach

func (it *Item[T]) Attach(stack *Stack[T]) bool

Attach removes items from the back of the stack and appends them to the current item. This inverts the order of items in the input stack.

func (*Item[T]) Detach

func (it *Item[T]) Detach() *Stack[T]

Detach splits a stack into two, using the current Item as the head of the new stack. The output is always non-nil: if the item is not valid or not the member of a stack Detach creates a new empty stack. If this item is currently the head of a stack, Detach returns that stack.

func (*Item[T]) In

func (it *Item[T]) In(s *Stack[T]) bool

In reports if an item is a member of a stack. Because item's track references to the stack, this is an O(1) operation.

func (*Item[T]) MarshalJSON

func (it *Item[T]) MarshalJSON() ([]byte, error)

MarshalJSON returns the result of json.Marshal on the value of the item. By supporting json.Marshaler and json.Unmarshaler, Items and stacks can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

func (*Item[T]) Next

func (it *Item[T]) Next() *Item[T]

Next returns the following item in the stack.

func (*Item[T]) Ok

func (it *Item[T]) Ok() bool

Ok return true if the Value() has been set. Returns false for incompletely initialized values.

Returns false when the item is nil.

func (*Item[T]) Push added in v0.14.0

func (it *Item[T]) Push(v T) *Item[T]

Push creates a new item and pushes it onto the stack following this item. The returned value is new/next item.

func (*Item[T]) Remove

func (it *Item[T]) Remove() bool

Remove removes the item from the stack, (at some expense, for items deeper in the stack.) If the operation isn't successful or possible the operation returns false.

func (*Item[T]) Set

func (it *Item[T]) Set(v T) bool

Set mutates the value of an Item, returning true if the operation has been successful. The operation fails if the Item is the head item in a stack or not a member of a stack.

func (*Item[T]) String

func (it *Item[T]) String() string

String implements fmt.Stringer, and returns the string value of the item's value.

func (*Item[T]) UnmarshalJSON

func (it *Item[T]) UnmarshalJSON(in []byte) error

UnmarshalJSON reads the json value, and sets the value of the item to the value in the json, potentially overriding an existing value. By supporting json.Marshaler and json.Unmarshaler, Items and stacks can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

func (*Item[T]) Value

func (it *Item[T]) Value() T

Value returns the Item's underlying value. Use the Ok method to check the validity of the zero values.

type List

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

List provides a doubly linked list. Callers are responsible for their own concurrency control and bounds checking, and should generally use with the same care as a slice.

The Deque implementation in the pubsub package provides a similar implementation with locking and a notification system.

func IteratorList added in v0.13.0

func IteratorList[T any](in iter.Seq[T]) *List[T]

IteratorList constructs a doubly-linked list from the elements of a Go standard library iterator.

func SliceList added in v0.13.0

func SliceList[T any](elems []T) *List[T]

SliceList constructs a doubly-linked list from the elements of a slice.

func VariadicList added in v0.13.0

func VariadicList[T any](elems ...T) *List[T]

VariadicList constructs a doubly-linked list from a sequence of arguments passed to the constructor.

func (*List[T]) Append

func (l *List[T]) Append(items ...T) *List[T]

Append adds a variadic sequence of items to the end of the list.

func (*List[T]) Back

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

Back returns a pointer to the last element of the list. If the list is empty, this is also the first element of the list. The operation is non-destructive. You can use this pointer to begin a c-style iteration over the list:

for e := list.Back(); e.Ok(); e = e.Previous() {
       // operate
}

func (*List[T]) Copy

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

Copy duplicates the list. The element objects in the list are distinct, though if the Values are themselves references, the values of both lists would be shared.

func (*List[T]) Extend

func (l *List[T]) Extend(seq iter.Seq[T]) *List[T]

Extend adds all of the items in a slice to the end of the list.

func (*List[T]) Front

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

Front returns a pointer to the first element of the list. If the list is empty, this is also the last element of the list. The operation is non-destructive. You can use this pointer to begin a c-style iteration over the list:

for e := list.Front(); e.Ok(); e = e.Next() {
       // operate
}

func (*List[T]) IsSorted

func (l *List[T]) IsSorted(cf func(T, T) int) bool

IsSorted reports if the list is sorted from low to high, according to the LessThan function.

func (*List[T]) IteratorBack added in v0.14.0

func (l *List[T]) IteratorBack() iter.Seq[T]

IteratorBack returns an iterator to the items in the list starting at the back and moving toward the front of the list.

If you add values to the list during iteration *behind* the current position of the iterator, these values will not be present in the iterator; however, values added ahead of the current position, will be visible.

func (*List[T]) IteratorFront added in v0.14.0

func (l *List[T]) IteratorFront() iter.Seq[T]

IteratorFront returns an iterator to the items in the list starting at the front and moving toward the back of the list.

If you add values to the list during iteration *behind* the current position of the iterator, these values will not be present in the iterator; however, values added ahead of the current position, will be visible.

func (*List[T]) IteratorPopBack added in v0.14.0

func (l *List[T]) IteratorPopBack() iter.Seq[T]

IteratorPopBack returns a destructive iterator that consumes elements from the list as it iterates, moving back-to-front.

If you add values to the list during iteration *behind* the current position of the iterator, these values will not be present in the iterator; however, values added ahead of the current position, will be visible.

func (*List[T]) IteratorPopFront added in v0.14.0

func (l *List[T]) IteratorPopFront() iter.Seq[T]

IteratorPopFront returns a destructive iterator that consumes elements from the list as it iterates, moving front-to-back.

If you add values to the list during iteration *behind* the current position of the iterator, these values will not be present in the iterator; however, values added ahead of the current position, will be visible.

func (*List[T]) Len

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

Len returns the length of the list. Because the Push/Pop operations track the length of the list, this is an O(1) operation.

func (*List[T]) MarshalJSON

func (l *List[T]) MarshalJSON() ([]byte, error)

MarshalJSON produces a JSON array representing the items in the list. By supporting json.Marshaler and json.Unmarshaler, Elements and lists can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

func (*List[T]) PopBack

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

PopBack removes the last element from the list. If the list is empty, this returns a detached non-nil value, that will report an Ok() false value. You can use this element to produce a C-style iterator over the list, that removes items during the iteration:

for e := list.PopBack(); e.Ok(); e = input.PopBack() {
	// do work
}

func (*List[T]) PopFront

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

PopFront removes the first element from the list. If the list is empty, this returns a nil value, that will report an Ok() false You can use this element to produce a C-style iterator over the list, that removes items during the iteration:

for e := list.PopFront(); e.Ok(); e = input.PopFront() {
	// do work
}

func (*List[T]) PushBack

func (l *List[T]) PushBack(it T)

PushBack creates an element and appends it to the list. The performance of PushFront and PushBack are the same.

func (*List[T]) PushFront

func (l *List[T]) PushFront(it T)

PushFront creates an element and prepends it to the list. The performance of PushFront and PushBack are the same.

func (*List[T]) Reset added in v0.13.0

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

Reset removes all members of the list, and releases all references to items in the list.

func (*List[T]) SortMerge

func (l *List[T]) SortMerge(cf func(T, T) int)

SortMerge sorts the list, using the provided comparison function and a Merge Sort operation. This is something of a novelty in most cases, as removing the elements from the list, adding to a slice and then using sort.Slice() from the standard library, and then re-adding those elements to the list, will perform better.

The operation will modify the input list, replacing it with an new list operation.

func (*List[T]) SortQuick

func (l *List[T]) SortQuick(cf func(T, T) int)

SortQuick sorts the list, by removing the elements, adding them to a slice, and then using sort.SliceStable(). In many cases this performs better than the merge sort implementation.

func (*List[T]) UnmarshalJSON

func (l *List[T]) UnmarshalJSON(in []byte) error

UnmarshalJSON reads json input and adds that to values in the list. If there are elements in the list, they are not removed. By supporting json.Marshaler and json.Unmarshaler, Elements and lists can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

type Optional added in v0.10.5

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

Optional is a wrapper type for optional value, where using a pointer is unsuitable or awkward. The type provides a reasonable interface for manipulating the value, does not need to be initialized upon construction (i.e. safe to put in structs without needing to specify an initial value.) MarshalText and MarshalBinary (with corresponding) unmarshal) interfaces make Optional types easy to embed.

Marshal/Unmarshal methods are provided to support using optional in primary structs and to avoid the need to duplicate structs.

func NewOptional added in v0.10.5

func NewOptional[T any](in T) Optional[T]

NewOptional is simple constructor that constructs a new populated Optional value.

func (*Optional[T]) Default added in v0.10.5

func (o *Optional[T]) Default(in T)

Default sets value of the optional to the provided value if it is not already been defined.

func (*Optional[T]) Get added in v0.10.5

func (o *Optional[T]) Get() (T, bool)

Get returns the current value of the optional and the ok value. Use this to disambiguate zero values.

func (Optional[T]) MarshalBinary added in v0.10.5

func (o Optional[T]) MarshalBinary() ([]byte, error)

MarshalBinary supports marshaling optional values into binary formats and uses the value of the optional to dictate behavior. The underlying value must implement encoding.BinaryMarshaler, bson.Marshaler or a generic Marshal() interface. byte slices are passed through.

func (Optional[T]) MarshalText added in v0.10.5

func (o Optional[T]) MarshalText() ([]byte, error)

MarshalText defines how to marshal the optional value in text-based contexts. In most cases it falls back on the value of the optional: using encoding.TextMarshaler, json.Marshaler, yaml.Marahaler, or a generic Marshal() ([]byte,error) interface are called. Strings and []byte values are through directly, and failing all of these options, this falls back to json.Marshal().

func (Optional[T]) Ok added in v0.10.9

func (o Optional[T]) Ok() bool

Ok returns true when the optional.

func (*Optional[T]) Reset added in v0.10.5

func (o *Optional[T]) Reset()

Reset unsets the OK value of the optional, and unsets the reference to the existing value.

func (*Optional[T]) Resolve added in v0.10.5

func (o *Optional[T]) Resolve() T

Resolve returns the current value of the optional. Zero values of T are ambiguous.

func (*Optional[T]) Scan added in v0.10.5

func (o *Optional[T]) Scan(src any) (err error)

Scan implements the sql.Scanner interface. This is invalid if the type of the optional value is not a primitive value type.

func (*Optional[T]) Set added in v0.10.5

func (o *Optional[T]) Set(in T)

Set marks the optional value as defined, and sets the optional value. You can set an optional to the zero value for type T. To "unset" a value use the Reset().

func (*Optional[T]) Swap added in v0.10.5

func (o *Optional[T]) Swap(next T) (prev T)

Swap returns the previous value of the optional and replaces it with the provided value.

func (*Optional[T]) UnmarshalBinary added in v0.10.5

func (o *Optional[T]) UnmarshalBinary(in []byte) (err error)

UnmarshalBinary provides a compliment to MarshalBinary, and serves as a passthrough for encoding.BinaryUnmarshaler, bson.Unmarshaler and the generic Unmarshal interface.

func (*Optional[T]) UnmarshalText added in v0.10.5

func (o *Optional[T]) UnmarshalText(in []byte) (err error)

UnmarshalText provides an inverse version of MarshalText for the encoding.UnmarshalText interface. encoding.TextUnmarshaler, json.Unmarshaler, yaml.Unmarshaler, and a generic Unmarshler interface. Strings and bytes slices pass through directly, and json.Unmarshal() is used in all other situations.

func (Optional[T]) Value added in v0.10.5

func (o Optional[T]) Value() (driver.Value, error)

Value implements the SQL driver.Valuer interface. Scan implements the sql Scanner interface. This is invalid if the type of the optional value is not a primitive value type.

type OrderedMap added in v0.14.0

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

OrderedMap provides a map implementation that maintains insertion order. It has the same interface as stw.Map but iterates in insertion order. This implementation uses a mutex to ensure consistency between the underlying hash map and the insertion order list.

func (*OrderedMap[K, V]) Check added in v0.14.0

func (m *OrderedMap[K, V]) Check(key K) bool

Check returns true if the value K is in the map.

func (*OrderedMap[K, V]) Delete added in v0.14.0

func (m *OrderedMap[K, V]) Delete(k K)

Delete removes a key from the map.

func (*OrderedMap[K, V]) Ensure added in v0.14.0

func (m *OrderedMap[K, V]) Ensure(key K) bool

Ensure sets the provided key in the map to the zero value for the value type. If the key already exists, it is not modified.

func (*OrderedMap[K, V]) Extend added in v0.14.0

func (m *OrderedMap[K, V]) Extend(seq iter.Seq2[K, V])

Extend adds a sequence of key-value pairs to the map.

func (*OrderedMap[K, V]) Get added in v0.14.0

func (m *OrderedMap[K, V]) Get(key K) (out V)

Get returns the value from the map. If the key is not present in the map, this returns the zero value for V.

func (*OrderedMap[K, V]) Iterator added in v0.14.0

func (m *OrderedMap[K, V]) Iterator() iter.Seq2[K, V]

Iterator returns a standard Go iterator interface to the key-value pairs of the map in insertion order.

func (*OrderedMap[K, V]) Keys added in v0.14.0

func (m *OrderedMap[K, V]) Keys() iter.Seq[K]

Keys provides an iterator over just the keys in the map in insertion order.

func (*OrderedMap[K, V]) Len added in v0.14.0

func (m *OrderedMap[K, V]) Len() int

Len returns the length of the map.

func (*OrderedMap[K, V]) Load added in v0.14.0

func (m *OrderedMap[K, V]) Load(key K) (V, bool)

Load returns the value in the map for the key, and an "ok" value which is true if that item is present in the map.

func (*OrderedMap[K, V]) Set added in v0.14.0

func (m *OrderedMap[K, V]) Set(key K, value V) bool

Set adds a key-value pair directly to the map. If the key already exists, it updates the value but does not change the insertion order. When the return value is true, the key existed in the map before the operation.

func (*OrderedMap[K, V]) Store added in v0.14.0

func (m *OrderedMap[K, V]) Store(k K, v V)

Store adds a key-value pair directly to the map. Alias for Add.

func (*OrderedMap[K, V]) Values added in v0.14.0

func (m *OrderedMap[K, V]) Values() iter.Seq[V]

Values provides an iterator over just the values in the map in insertion order.

type OrderedSet added in v0.14.0

type OrderedSet[T comparable] struct {
	// contains filtered or unexported fields
}

OrderedSet provides a generic set implementation that always maintains insertion order. This implementation is not thread-safe. For a synchronized version, use adt.OrderedSet.

func MakeOrderedSet added in v0.14.0

func MakeOrderedSet[T comparable](in iter.Seq[T]) *OrderedSet[T]

MakeOrderedSet constructs an ordered set and adds all items from the input sequence to the set in order.

func (*OrderedSet[T]) Add added in v0.14.0

func (s *OrderedSet[T]) Add(in T) (ok bool)

Add adds an item to the set and returns true if the item had been in the set before Add. In all cases when Add returns, the item is a member of the set.

func (*OrderedSet[T]) Check added in v0.14.0

func (s *OrderedSet[T]) Check(in T) bool

Check returns true if the item is in the set.

func (*OrderedSet[T]) Delete added in v0.14.0

func (s *OrderedSet[T]) Delete(in T) bool

Delete removes the item from the set, returning true when the item had been in the Set, and returning false otherwise.

func (*OrderedSet[T]) Equal added in v0.14.0

func (s *OrderedSet[T]) Equal(other *OrderedSet[T]) bool

Equal tests two sets, returning true if the items in the sets have equal values in the same order.

func (*OrderedSet[T]) Extend added in v0.14.0

func (s *OrderedSet[T]) Extend(iter iter.Seq[T])

Extend adds all items encountered in the iterator to the set.

func (*OrderedSet[T]) Iterator added in v0.14.0

func (s *OrderedSet[T]) Iterator() iter.Seq[T]

Iterator returns a new-style native Go iterator for the items in the set in insertion order.

func (*OrderedSet[T]) Len added in v0.14.0

func (s *OrderedSet[T]) Len() int

Len returns the number of items tracked in the set.

func (*OrderedSet[T]) MarshalJSON added in v0.14.0

func (s *OrderedSet[T]) MarshalJSON() ([]byte, error)

MarshalJSON generates a JSON array of the items in the set in insertion order.

func (*OrderedSet[T]) SortMerge added in v0.14.0

func (s *OrderedSet[T]) SortMerge(cf func(T, T) int)

SortMerge sorts the elements in the set using a merge sort algorithm.

func (*OrderedSet[T]) SortQuick added in v0.14.0

func (s *OrderedSet[T]) SortQuick(cf func(T, T) int)

SortQuick sorts the elements in the set using sort.StableSort. Typically faster than SortMerge, but potentially more memory intensive for some types.

func (*OrderedSet[T]) UnmarshalJSON added in v0.14.0

func (s *OrderedSet[T]) UnmarshalJSON(in []byte) error

UnmarshalJSON reads input JSON data, constructs an array in memory and then adds items from the array to existing set in order. Items that are in the set when UnmarshalJSON begins are not modified.

type Ring added in v0.12.0

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

Ring is a simple generic ring-buffer implemented on top of a slice/array with a few conveniences: there are forward and backward iterators; you can pop items from the "end" (oldest) in the buffer. The Total() method maintains a count of the total number of items added to the buffer.

Operations on the Ring are NOT safe for concurrent use from multiple go routines.

func (*Ring[T]) Cap added in v0.12.0

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

Cap returns the capacity of the ring buffer. This is either the size passed to Setup() or the default 1024.

func (*Ring[T]) FIFO added in v0.12.0

func (r *Ring[T]) FIFO() iter.Seq[T]

FIFO returns an iterator that begins at the first (oldest; Head) element and iterates forward to the current or most recently added element in the buffer.

func (*Ring[T]) Head added in v0.12.0

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

Head returns the oldest element in the buffer.

func (*Ring[T]) LIFO added in v0.12.0

func (r *Ring[T]) LIFO() iter.Seq[T]

LIFO returns the element that was most recently added to buffer and iterates backwords to the oldest element in the buffer.

func (*Ring[T]) Len added in v0.12.0

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

Len returns the number of elements in the buffer. This is, some number between the capacity and 0. It is decremented when items are popped from the buffer, but only incremented when a previously empty position is filled.

func (*Ring[T]) Pop added in v0.12.0

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

Pop returns the oldest element in the buffer to the caller. If the buffer is empty, then Pop() returns nil.

The returned value is (effectively) owned by the caller of Pop() and is independent of the value stored in the ring.

func (*Ring[T]) PopFIFO added in v0.12.0

func (r *Ring[T]) PopFIFO() iter.Seq[T]

PopFIFO returns a FIFO iterator that consumes elements in the buffer, starting with the oldest element in the buffer and moving through all elements. The iterator is exhusted when the buffer is empty.

func (*Ring[T]) Push added in v0.12.0

func (r *Ring[T]) Push(val T)

Push adds an element to the buffer in the next position, potentially overwriting the oldest element in the buffer once the buffer is full.

Elements are always pushed to the "next" position in the buffer, even if elements are removed using Pop().

func (*Ring[T]) Setup added in v0.12.0

func (r *Ring[T]) Setup(size int)

Setup sets the size of the ring buffer and initializes the buffer, if the buffer hasn't been used. Using the buffer initializes it with a size of 1024.

func (*Ring[T]) Tail added in v0.12.0

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

Tail returns the newest (or most recently added) element in the buffer.

func (*Ring[T]) Total added in v0.12.0

func (r *Ring[T]) Total() uint64

Total returns the number of elements that have ever been added to the buffer. This number is never decremented.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set provides a generic unordered set implementation. This implementation is not thread-safe. For a synchronized version, use adt.Set. For a set that maintains insertion order, use OrderedSet or adt.OrderedSet.

func MakeSet added in v0.14.0

func MakeSet[T comparable](in iter.Seq[T]) *Set[T]

MakeSet constructs an unordered set and adds all items from the input sequence to the set.

func (*Set[T]) Add

func (s *Set[T]) Add(in T) (existed bool)

Add adds an item to the set and returns true if the item had been in the set before Add. In all cases when Add returns, the item is a member of the set.

func (*Set[T]) Check

func (s *Set[T]) Check(in T) bool

Check returns true if the item is in the set.

func (*Set[T]) Delete

func (s *Set[T]) Delete(in T) (existed bool)

Delete removes the item from the set, returning true when the item had been in the Set, and returning false otherwise.

func (*Set[T]) Equal

func (s *Set[T]) Equal(other *Set[T]) bool

Equal tests two sets, returning true if the items in the sets have equal values. Order is not considered.

func (*Set[T]) Extend added in v0.10.3

func (s *Set[T]) Extend(iter iter.Seq[T])

Extend adds all items from the iterator to the set.

func (*Set[T]) Iterator

func (s *Set[T]) Iterator() iter.Seq[T]

Iterator returns a new-style native Go iterator for the items in the set. The iteration order is undefined and may vary between calls.

func (*Set[T]) Len

func (s *Set[T]) Len() int

Len returns the number of items tracked in the set.

func (*Set[T]) MarshalJSON

func (s *Set[T]) MarshalJSON() ([]byte, error)

MarshalJSON generates a JSON array of the items in the set. The order of items in the array is undefined.

func (*Set[T]) UnmarshalJSON

func (s *Set[T]) UnmarshalJSON(in []byte) error

UnmarshalJSON reads input JSON data, constructs an array in memory and then adds items from the array to existing set. Items that are in the set when UnmarshalJSON begins are not modified.

type Stack

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

Stack provides a generic singly linked list, with an interface that is broadly similar to dt.List.

func (*Stack[T]) Append

func (s *Stack[T]) Append(items ...T) *Stack[T]

Append adds a variadic sequence of items to the list.

func (*Stack[T]) Extend added in v0.14.0

func (s *Stack[T]) Extend(seq iter.Seq[T]) *Stack[T]

Extend appends all items from the iterator to the stack.

func (*Stack[T]) Head

func (s *Stack[T]) Head() *Item[T]

Head returns the item at the top of this stack. This is a non destructive operation.

func (*Stack[T]) Iterator

func (s *Stack[T]) Iterator() iter.Seq[T]

Iterator returns a native go iterator function for the items in a set.

func (*Stack[T]) IteratorPop added in v0.14.0

func (s *Stack[T]) IteratorPop() iter.Seq[T]

IteratorPop returns a destructive iterator over the Items in a stack. IteratorPop will not observe new items added to the stack during iteration.

func (*Stack[T]) Len

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

Len returns the length of the stack. Because stack's track their own size, this is an O(1) operation.

func (*Stack[T]) MarshalJSON

func (s *Stack[T]) MarshalJSON() ([]byte, error)

MarshalJSON produces a JSON array representing the items in the stack. By supporting json.Marshaler and json.Unmarshaler, Items and stacks can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

func (*Stack[T]) Pop

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

Pop removes the item on the top of the stack, and returns it. If the stack is empty, this will return, but not detach, the root item of the stack, which will report a false Ok() value.

func (*Stack[T]) Push

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

Push appends an item to the stack.

func (*Stack[T]) UnmarshalJSON

func (s *Stack[T]) UnmarshalJSON(in []byte) error

UnmarshalJSON reads json input and adds that to values in the stack. If there are items in the stack, they are not removed. By supporting json.Marshaler and json.Unmarshaler, Items and stacks can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

Directories

Path Synopsis
Package hdrhist provides an implementation of Gil Tene's HDR Histogram data structure.
Package hdrhist provides an implementation of Gil Tene's HDR Histogram data structure.

Jump to

Keyboard shortcuts

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