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
- func Unwind[T any](in T) []T
- func Unwrap[T any](in T) (out T)
- type Element
- func (e *Element[T]) Append(val *Element[T]) *Element[T]
- func (e *Element[T]) Drop()
- func (e *Element[T]) In(l *List[T]) bool
- func (e *Element[T]) MarshalJSON() ([]byte, error)
- func (e *Element[T]) Next() *Element[T]
- func (e *Element[T]) Ok() bool
- func (e *Element[T]) Previous() *Element[T]
- func (e *Element[T]) Push(v T) *Element[T]
- func (e *Element[T]) Remove() bool
- func (e *Element[T]) Set(v T) bool
- func (e *Element[T]) String() string
- func (e *Element[T]) Swap(with *Element[T]) bool
- func (e *Element[T]) UnmarshalJSON(in []byte) error
- func (e *Element[T]) Value() (out T)
- type Heap
- type Item
- func (it *Item[T]) Append(n *Item[T]) *Item[T]
- func (it *Item[T]) Attach(stack *Stack[T]) bool
- func (it *Item[T]) Detach() *Stack[T]
- func (it *Item[T]) In(s *Stack[T]) bool
- func (it *Item[T]) MarshalJSON() ([]byte, error)
- func (it *Item[T]) Next() *Item[T]
- func (it *Item[T]) Ok() bool
- func (it *Item[T]) Push(v T) *Item[T]
- func (it *Item[T]) Remove() bool
- func (it *Item[T]) Set(v T) bool
- func (it *Item[T]) String() string
- func (it *Item[T]) UnmarshalJSON(in []byte) error
- func (it *Item[T]) Value() T
- type List
- func (l *List[T]) Append(items ...T) *List[T]
- func (l *List[T]) Back() *Element[T]
- func (l *List[T]) Copy() *List[T]
- func (l *List[T]) Extend(seq iter.Seq[T]) *List[T]
- func (l *List[T]) Front() *Element[T]
- func (l *List[T]) IsSorted(cf func(T, T) int) bool
- func (l *List[T]) IteratorBack() iter.Seq[T]
- func (l *List[T]) IteratorFront() iter.Seq[T]
- func (l *List[T]) IteratorPopBack() iter.Seq[T]
- func (l *List[T]) IteratorPopFront() iter.Seq[T]
- func (l *List[T]) Len() int
- func (l *List[T]) MarshalJSON() ([]byte, error)
- func (l *List[T]) PopBack() *Element[T]
- func (l *List[T]) PopFront() *Element[T]
- func (l *List[T]) PushBack(it T)
- func (l *List[T]) PushFront(it T)
- func (l *List[T]) Reset()
- func (l *List[T]) SortMerge(cf func(T, T) int)
- func (l *List[T]) SortQuick(cf func(T, T) int)
- func (l *List[T]) UnmarshalJSON(in []byte) error
- type Optional
- func (o *Optional[T]) Default(in T)
- func (o *Optional[T]) Get() (T, bool)
- func (o Optional[T]) MarshalBinary() ([]byte, error)
- func (o Optional[T]) MarshalText() ([]byte, error)
- func (o Optional[T]) Ok() bool
- func (o *Optional[T]) Reset()
- func (o *Optional[T]) Resolve() T
- func (o *Optional[T]) Scan(src any) (err error)
- func (o *Optional[T]) Set(in T)
- func (o *Optional[T]) Swap(next T) (prev T)
- func (o *Optional[T]) UnmarshalBinary(in []byte) (err error)
- func (o *Optional[T]) UnmarshalText(in []byte) (err error)
- func (o Optional[T]) Value() (driver.Value, error)
- type OrderedMap
- func (m *OrderedMap[K, V]) Check(key K) bool
- func (m *OrderedMap[K, V]) Delete(k K)
- func (m *OrderedMap[K, V]) Ensure(key K) bool
- func (m *OrderedMap[K, V]) Extend(seq iter.Seq2[K, V])
- func (m *OrderedMap[K, V]) Get(key K) (out V)
- func (m *OrderedMap[K, V]) Iterator() iter.Seq2[K, V]
- func (m *OrderedMap[K, V]) Keys() iter.Seq[K]
- func (m *OrderedMap[K, V]) Len() int
- func (m *OrderedMap[K, V]) Load(key K) (V, bool)
- func (m *OrderedMap[K, V]) Set(key K, value V) bool
- func (m *OrderedMap[K, V]) Store(k K, v V)
- func (m *OrderedMap[K, V]) Values() iter.Seq[V]
- type OrderedSet
- func (s *OrderedSet[T]) Add(in T) (ok bool)
- func (s *OrderedSet[T]) Check(in T) bool
- func (s *OrderedSet[T]) Delete(in T) bool
- func (s *OrderedSet[T]) Equal(other *OrderedSet[T]) bool
- func (s *OrderedSet[T]) Extend(iter iter.Seq[T])
- func (s *OrderedSet[T]) Iterator() iter.Seq[T]
- func (s *OrderedSet[T]) Len() int
- func (s *OrderedSet[T]) MarshalJSON() ([]byte, error)
- func (s *OrderedSet[T]) SortMerge(cf func(T, T) int)
- func (s *OrderedSet[T]) SortQuick(cf func(T, T) int)
- func (s *OrderedSet[T]) UnmarshalJSON(in []byte) error
- type Ring
- func (r *Ring[T]) Cap() int
- func (r *Ring[T]) FIFO() iter.Seq[T]
- func (r *Ring[T]) Head() T
- func (r *Ring[T]) LIFO() iter.Seq[T]
- func (r *Ring[T]) Len() int
- func (r *Ring[T]) Pop() *T
- func (r *Ring[T]) PopFIFO() iter.Seq[T]
- func (r *Ring[T]) Push(val T)
- func (r *Ring[T]) Setup(size int)
- func (r *Ring[T]) Tail() T
- func (r *Ring[T]) Total() uint64
- type Set
- func (s *Set[T]) Add(in T) (existed bool)
- func (s *Set[T]) Check(in T) bool
- func (s *Set[T]) Delete(in T) (existed bool)
- func (s *Set[T]) Equal(other *Set[T]) bool
- func (s *Set[T]) Extend(iter iter.Seq[T])
- func (s *Set[T]) Iterator() iter.Seq[T]
- func (s *Set[T]) Len() int
- func (s *Set[T]) MarshalJSON() ([]byte, error)
- func (s *Set[T]) UnmarshalJSON(in []byte) error
- type Stack
- func (s *Stack[T]) Append(items ...T) *Stack[T]
- func (s *Stack[T]) Extend(seq iter.Seq[T]) *Stack[T]
- func (s *Stack[T]) Head() *Item[T]
- func (s *Stack[T]) Iterator() iter.Seq[T]
- func (s *Stack[T]) IteratorPop() iter.Seq[T]
- func (s *Stack[T]) Len() int
- func (s *Stack[T]) MarshalJSON() ([]byte, error)
- func (s *Stack[T]) Pop() *Item[T]
- func (s *Stack[T]) Push(it T)
- func (s *Stack[T]) UnmarshalJSON(in []byte) error
Constants ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
MarshalJSON satisfies the json.Marshaler interface. Nil and unset Element values are marshaled as nil.
func (*Element[T]) Next ¶
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 ¶
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 ¶
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
Push adds a value to the list, and returns the resulting element.
func (*Element[T]) Remove ¶
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 ¶
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]) Swap ¶
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 ¶
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.
type Heap ¶
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]) Len ¶
Len reports the size of the heap. Because the heap tracks its size with Push/Pop operations, this is a constant time operation.
type Item ¶
type Item[T any] struct { // contains filtered or unexported fields }
Item is a common wrapper for the elements in a stack.
func (*Item[T]) Append ¶
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 ¶
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 ¶
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 ¶
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 ¶
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]) Ok ¶
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
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 ¶
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 ¶
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 ¶
String implements fmt.Stringer, and returns the string value of the item's value.
func (*Item[T]) UnmarshalJSON ¶
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.
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
IteratorList constructs a doubly-linked list from the elements of a Go standard library iterator.
func SliceList ¶ added in v0.13.0
SliceList constructs a doubly-linked list from the elements of a slice.
func VariadicList ¶ added in v0.13.0
VariadicList constructs a doubly-linked list from a sequence of arguments passed to the constructor.
func (*List[T]) Back ¶
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 ¶
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]) Front ¶
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 ¶
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
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
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
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
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
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
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
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
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]) 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
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
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
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.
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
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
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
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
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
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
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.
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 ¶
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]) Delete ¶
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 ¶
Equal tests two sets, returning true if the items in the sets have equal values. Order is not considered.
func (*Set[T]) Iterator ¶
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]) MarshalJSON ¶
MarshalJSON generates a JSON array of the items in the set. The order of items in the array is undefined.
func (*Set[T]) UnmarshalJSON ¶
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]) Head ¶
Head returns the item at the top of this stack. This is a non destructive operation.
func (*Stack[T]) IteratorPop ¶ added in v0.14.0
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 ¶
Len returns the length of the stack. Because stack's track their own size, this is an O(1) operation.
func (*Stack[T]) MarshalJSON ¶
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 ¶
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]) UnmarshalJSON ¶
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.