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 MapIterator[K comparable, V any](in map[K]V) *fun.Iterator[Pair[K, V]]
- func MapKeys[K comparable, V any](in map[K]V) *fun.Iterator[K]
- func MapValues[K comparable, V any](in map[K]V) *fun.Iterator[V]
- func Unwind[T any](in T) []T
- func Unwrap[T any](in T) (out T)
- type Element
- func (e *Element[T]) Append(new *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]) 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() 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]) 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)
- func (l *List[T]) Back() *Element[T]
- func (l *List[T]) Copy() *List[T]
- func (l *List[T]) Extend(input *List[T])
- func (l *List[T]) Front() *Element[T]
- func (l *List[T]) IsSorted(lt cmp.LessThan[T]) bool
- func (l *List[T]) Iterator() *fun.Iterator[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]) PopIterator() *fun.Iterator[T]
- func (l *List[T]) PopReverse() *fun.Iterator[T]
- func (l *List[T]) Producer() fun.Producer[T]
- func (l *List[T]) ProducerPop() fun.Producer[T]
- func (l *List[T]) ProducerReverse() fun.Producer[T]
- func (l *List[T]) ProducerReversePop() fun.Producer[T]
- func (l *List[T]) PushBack(it T)
- func (l *List[T]) PushFront(it T)
- func (l *List[T]) Reverse() *fun.Iterator[T]
- func (l *List[T]) SortMerge(lt cmp.LessThan[T])
- func (l *List[T]) SortQuick(lt cmp.LessThan[T])
- func (l *List[T]) UnmarshalJSON(in []byte) error
- type Map
- func (m Map[K, V]) Add(k K, v V)
- func (m Map[K, V]) AddPair(p Pair[K, V])
- func (m Map[K, V]) Append(pairs ...Pair[K, V])
- func (m Map[K, V]) Check(key K) bool
- func (m Map[K, V]) Consume(ctx context.Context, iter *fun.Iterator[Pair[K, V]])
- func (m Map[K, V]) ConsumeMap(in Map[K, V])
- func (m Map[K, V]) ConsumeSlice(in []V, keyf func(V) K)
- func (m Map[K, V]) ConsumeValues(ctx context.Context, iter *fun.Iterator[V], keyf func(V) K)
- func (m Map[K, V]) Extend(pairs *Pairs[K, V])
- func (m Map[K, V]) Get(key K) V
- func (m Map[K, V]) Iterator() *fun.Iterator[Pair[K, V]]
- func (m Map[K, V]) Keys() *fun.Iterator[K]
- func (m Map[K, V]) Len() int
- func (m Map[K, V]) Load(key K) (V, bool)
- func (m Map[K, V]) Pairs() *Pairs[K, V]
- func (m Map[K, V]) Producer() fun.Producer[Pair[K, V]]
- func (m Map[K, V]) ProducerKeys() fun.Producer[K]
- func (m Map[K, V]) ProducerValues() fun.Producer[V]
- func (m Map[K, V]) SetDefault(key K)
- func (m Map[K, V]) Values() *fun.Iterator[V]
- type Pair
- type Pairs
- func (p *Pairs[K, V]) Add(k K, v V) *Pairs[K, V]
- func (p *Pairs[K, V]) AddPair(pair Pair[K, V]) *Pairs[K, V]
- func (p *Pairs[K, V]) Append(new ...Pair[K, V]) *Pairs[K, V]
- func (p *Pairs[K, V]) Consume(ctx context.Context, iter *fun.Iterator[Pair[K, V]]) error
- func (p *Pairs[K, V]) ConsumeMap(in map[K]V) *Pairs[K, V]
- func (p *Pairs[K, V]) ConsumeSlice(in []V, keyf func(V) K) *Pairs[K, V]
- func (p *Pairs[K, V]) ConsumeValues(ctx context.Context, iter *fun.Iterator[V], keyf func(V) K) error
- func (p *Pairs[K, V]) Extend(toAdd *Pairs[K, V])
- func (p *Pairs[K, V]) Iterator() *fun.Iterator[Pair[K, V]]
- func (p *Pairs[K, V]) Keys() *fun.Iterator[K]
- func (p *Pairs[K, V]) Len() int
- func (p *Pairs[K, V]) List() *List[Pair[K, V]]
- func (p *Pairs[K, V]) Map() map[K]V
- func (p *Pairs[K, V]) MarshalJSON() ([]byte, error)
- func (p *Pairs[K, V]) Slice() []Pair[K, V]
- func (p *Pairs[K, V]) SortMerge(c cmp.LessThan[Pair[K, V]])
- func (p *Pairs[K, V]) SortQuick(c cmp.LessThan[Pair[K, V]])
- func (p *Pairs[K, V]) UnmarshalJSON(in []byte) error
- func (p *Pairs[K, V]) Values() *fun.Iterator[V]
- type Set
- func (s *Set[T]) Add(in T)
- func (s *Set[T]) AddCheck(in T) (ok bool)
- func (s *Set[T]) Check(in T) bool
- func (s *Set[T]) Delete(in T)
- func (s *Set[T]) DeleteCheck(in T) bool
- func (s *Set[T]) Equal(other *Set[T]) bool
- func (s *Set[T]) Iterator() *fun.Iterator[T]
- func (s *Set[T]) Len() int
- func (s *Set[T]) MarshalJSON() ([]byte, error)
- func (s *Set[T]) Order()
- func (s *Set[T]) Populate(iter *fun.Iterator[T])
- func (s *Set[T]) Producer() (out fun.Producer[T])
- func (s *Set[T]) Synchronize()
- func (s *Set[T]) UnmarshalJSON(in []byte) error
- func (s *Set[T]) WithLock(mtx *sync.Mutex)
- type Slice
- func (s *Slice[T]) Add(in T)
- func (s *Slice[T]) AddWhen(cond bool, in T)
- func (s *Slice[T]) Append(in ...T)
- func (s *Slice[T]) AppendWhen(cond bool, in ...T)
- func (s Slice[T]) Cap() int
- func (s Slice[T]) Copy() Slice[T]
- func (s *Slice[T]) Empty()
- func (s *Slice[T]) Extend(in []T)
- func (s *Slice[T]) ExtendWhen(cond bool, in []T)
- func (s *Slice[T]) Filter(p func(T) bool) (o Slice[T])
- func (s *Slice[T]) FilterFuture(p func(T) bool) fun.Future[Slice[T]]
- func (s Slice[T]) IsEmpty() bool
- func (s Slice[T]) Item(index int) T
- func (s Slice[T]) Iterator() *fun.Iterator[T]
- func (s Slice[T]) Last() int
- func (s Slice[T]) Len() int
- func (s Slice[T]) Observe(of fun.Handler[T])
- func (s Slice[T]) Process(pf fun.Processor[T]) fun.Worker
- func (s Slice[T]) Ptr(index int) *T
- func (s Slice[T]) Ptrs() []*T
- func (s *Slice[T]) Reset()
- func (s *Slice[T]) Reslice(start, end int)
- func (s *Slice[T]) ResliceBeginning(start int)
- func (s *Slice[T]) ResliceEnd(end int)
- func (s Slice[T]) Sort(cp func(a, b T) bool)
- func (s *Slice[T]) Truncate(n int)
- type Stack
- func (s *Stack[T]) Append(items ...T)
- func (s *Stack[T]) Head() *Item[T]
- func (s *Stack[T]) Iterator() *fun.Iterator[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]) PopIterator() *fun.Iterator[T]
- func (s *Stack[T]) Producer() fun.Producer[T]
- func (s *Stack[T]) ProducerPop() fun.Producer[T]
- func (s *Stack[T]) Push(it T)
- func (s *Stack[T]) UnmarshalJSON(in []byte) error
Constants ¶
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 MapIterator ¶
func MapIterator[K comparable, V any](in map[K]V) *fun.Iterator[Pair[K, V]]
MapIterator converts a map into an iterator of dt.Pair objects. The iterator is panic-safe, and uses one go routine to track the progress through the map. As a result you should always, either exhaust the iterator, cancel the context that you pass to the iterator OR call iterator.Close().
To use this iterator the items in the map are not copied, and the iteration order is randomized following the convention in go.
Use in combination with other iterator processing tools (generators, observers, transformers, etc.) to limit the number of times a collection of data must be coppied.
func MapKeys ¶
func MapKeys[K comparable, V any](in map[K]V) *fun.Iterator[K]
MapKeys takes an arbitrary map and produces an iterator over only the keys.
func MapValues ¶
func MapValues[K comparable, V any](in map[K]V) *fun.Iterator[V]
MapValues takes an arbitrary map and produces an iterator over only the values.
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.
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 returns the result of json.Marshal on the value of the element. 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]) 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]) 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 NewHeapFromIterator ¶
func NewHeapFromIterator[T any](ctx context.Context, cmp cmp.LessThan[T], iter *fun.Iterator[T]) (*Heap[T], error)
NewHeapFromIterator constructs and populates a heap using the input iterator and corresponding comparison function. Will return the input iterators Close() error if one exists, and otherwise will return the populated heap.
func (*Heap[T]) Iterator ¶
Iterator provides an fun.Iterator interface to the heap. The iterator consumes items from the heap, and will return when the heap is empty.
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]) 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 NewListFromIterator ¶
NewListFromIterator builds a list from the elements in the iterator. In general, any error would be the result of the input iterators's close method
func (*List[T]) Append ¶
func (l *List[T]) Append(items ...T)
Append adds a variadic sequence of items to the end of the list.
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]) Extend ¶
Extend removes items from the front of the input list, and appends them to the end of the current list.
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]) Iterator ¶
Iterator returns an iterator over the values in the list in front-to-back order. The Iterator is not synchronized with the values in the list, and will be exhausted when you reach the end of the list.
If you add values to the list during iteration *behind* where the iterator is, these values will not be present in the iterator; however, values added ahead of the iterator, will be visible.
func (*List[T]) Len ¶
Len returns the length of the list. As the Append/Remove 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]) PopIterator ¶
PopIterator produces an iterator that consumes elements from the list as it iterates, moving front-to-back.
If you add values to the list during iteration *behind* where the iterator is, these values will not be present in the iterator; however, values added ahead of the iterator, will be visible.
func (*List[T]) PopReverse ¶
PopReverse produces an iterator that consumes elements from the list as it iterates, moving back-to-front. To access an iterator of the *values* in the list, use PopReverseValues() for an iterator over the values.
If you add values to the list during iteration *behind* where the iterator is, these values will not be present in the iterator; however, values added ahead of the iterator, will be visible.
func (*List[T]) Producer ¶
Producer provides a producer function that iterates through the contents of the list. When the producer reaches has fully iterated through the list, or iterates to an item that has been removed (likely due to concurrent access,) the producer returns io.EOF.
The Producer starts at the front of the list and iterates in order.
func (*List[T]) ProducerPop ¶
ProducerPop provides a producer function that iterates through the contents of the list, removing each item from the list as it encounters it. When the producer reaches has fully iterated through the list, or iterates to an item that has been removed (likely due to concurrent access,) the producer returns io.EOF.
In most cases, for destructive iteration, use the pubsub.Queue, pubsub.Deque, or one of the pubsub.Distributor implementations.
The Producer starts at the front of the list and iterates in order.
func (*List[T]) ProducerReverse ¶
ProducerReverse provides the same semantics and operation as the Producer operation, but starts at the end/tail of the list and works forward.
func (*List[T]) ProducerReversePop ¶
ProducerReverse provides the same semantics and operation as the ProducerPop operation, but starts at the end/tail of the list and works forward.
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]) Reverse ¶
Reverse returns an iterator that produces elements from the list, from the back to the front. The iterator is not synchronized with the values in the list, and will be exhausted when you reach the front of the list.
If you add values to the list during iteration *behind* where the iterator is, these values will not be present in the iterator; however, values added ahead of the iterator, will be visible.
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 Map ¶
type Map[K comparable, V any] map[K]V
Map is just a generic type wrapper around a map, mostly for the purpose of being able to interact with Pair[K,V] objects and Iterators.
All normal map operations are still accessible, these methods exist to provide accessible function objects for use in contexts where that may be useful and to improve the readability of some call sites, where default map access may be awkward.
func Mapify ¶
func Mapify[K comparable, V any](in map[K]V) Map[K, V]
Mapify provides a constructor that will produce a fun.Map without specifying types.
func (Map[K, V]) Add ¶
func (m Map[K, V]) Add(k K, v V)
Add adds a key value pair directly to the map.
func (Map[K, V]) Consume ¶
Consume adds items to the map from an iterator of Pair objects. Existing values for K are always overwritten.
func (Map[K, V]) ConsumeMap ¶
ConsumeMap adds all the keys from the input map the map.
func (Map[K, V]) ConsumeSlice ¶
func (m Map[K, V]) ConsumeSlice(in []V, keyf func(V) K)
ConsumeSlice adds a slice of values to the map, using the provided function to generate the key for the value. Existing values in the map are overridden.
func (Map[K, V]) ConsumeValues ¶
ConsumeValues adds items to the map, using the function to generate the keys for the values.
This operation will panic (with an ErrInvariantValidation) if the keyf panics.
func (Map[K, V]) Get ¶
func (m Map[K, V]) Get(key K) V
Get returns the value from the map, and is the same thing as:
foo := mp[key]
If the key is not present in the map, as with a normal map, this is the zero value for V.
func (Map[K, V]) Iterator ¶
Iterator converts a map into an iterator of dt.Pair objects. The iterator is panic-safe, and uses one go routine to track the progress through the map. As a result you should always, either exhaust the iterator, cancel the context that you pass to the iterator OR call iterator.Close().
To use this iterator the items in the map are not copied, and the iteration order is randomized following the convention in go.
Use in combination with other iterator processing tools (generators, observers, transformers, etc.) to limit the number of times a collection of data must be coppied.
func (Map[K, V]) Len ¶
Len returns the length. It is equivalent to len(Map), but is provided for consistency.
func (Map[K, V]) Load ¶
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 (Map[K, V]) Pairs ¶
Pairs exports a map a Pairs object, which is an alias for a slice of Pair objects.
func (Map[K, V]) Producer ¶
Producer constructs a fun.Producer function for the pairs in the map. The operation starts a goroutine on the first iteration that tracks the state of the iterator. Iteration order is randomized.
func (Map[K, V]) ProducerKeys ¶
ProducerKeys returns a generator that generates the keys of the map. The operation requires a goroutine to keep track of the state of the iteration, but does not buffer or cache keys.
func (Map[K, V]) ProducerValues ¶
ProducerValues returns a generator that generates the values of the map. The operation requires a goroutine to keep track of the state of the iteration, but does not buffer or cache values.
func (Map[K, V]) SetDefault ¶
func (m Map[K, V]) SetDefault(key K)
SetDefault set's sets the provided key in the map to the zero value for the value type.
type Pair ¶
type Pair[K comparable, V any] struct { Key K Value V }
Pair represents a key-value pair. Used by the adt synchronized map implementation and the set package to handle ordered key-value pairs.
func MakePair ¶
func MakePair[K comparable, V any](k K, v V) Pair[K, V]
MakePair constructs a pair object. This is identical to using the literal constructor but may be more ergonomic as the compiler seems to be better at inferring types in function calls over literal constructors.
type Pairs ¶
type Pairs[K comparable, V any] struct { // contains filtered or unexported fields }
Pairs implements a collection of key-value pairs.
func MakePairs ¶
func MakePairs[K comparable, V any](in ...Pair[K, V]) *Pairs[K, V]
MakePairs constructs a Pairs object from a sequence of Pairs. This is identical to using the literal constructor but may be more ergonomic as the compiler seems to be better at inferring types in function calls over literal constructors.
To build Pairs objects from other types, use the Consume methods.
func (*Pairs[K, V]) Add ¶
Add adds a new value to the underlying slice. This may add a duplicate key.
func (*Pairs[K, V]) AddPair ¶
AddPair adds a single pair to the slice of pairs. This may add a duplicate key.
func (*Pairs[K, V]) Consume ¶
Consume adds items from an iterator of pairs to the current Pairs slice.
func (*Pairs[K, V]) ConsumeMap ¶
ConsumeMap adds all of the items in a map to the Pairs object.
func (*Pairs[K, V]) ConsumeSlice ¶
ConsumeSlice adds all the values in the input slice to the Pairs object, creating the keys using the function provide.
func (*Pairs[K, V]) ConsumeValues ¶
func (p *Pairs[K, V]) ConsumeValues(ctx context.Context, iter *fun.Iterator[V], keyf func(V) K) error
ConsumeValues adds all of the values in the input iterator, generating the keys using the function provided.
func (*Pairs[K, V]) Extend ¶
Extend adds the items from a Pairs object (slice of Pair) without modifying the donating object.
func (*Pairs[K, V]) Keys ¶
Keys returns an iterator over only the keys in a sequence of iterator items.
func (*Pairs[K, V]) Map ¶
func (p *Pairs[K, V]) Map() map[K]V
Map converts a list of pairs to the equivalent map. If there are duplicate keys in the Pairs list, only the first occurrence of the key is retained.
func (*Pairs[K, V]) MarshalJSON ¶
MarshalJSON produces a JSON encoding for the Pairs object by first converting it to a map and then encoding that map as JSON. The JSON serialization does not necessarily preserve the order of the pairs object.
func (*Pairs[K, V]) SortMerge ¶ added in v0.10.2
SortMerge performs a merge sort on the collected pairs.
func (*Pairs[K, V]) SortQuick ¶ added in v0.10.2
SortQuick does a quick sort using sort.Sort. Typically faster than SortMerge, but potentially more memory intensive for some types.
func (*Pairs[K, V]) UnmarshalJSON ¶
UnmarshalJSON provides consistent JSON decoding for Pairs objects. It reads a JSON document into a map and converts it to pairs, and appends it to the existing Pairs objects without removing or resetting the current object.
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set provides a flexible generic set implementation, with optional safety for concurrent use (via Synchronize() and WithLock() methods,) and optional order tracking (via Order().)
func (*Set[T]) Add ¶
func (s *Set[T]) Add(in T)
Add attempts to add the item to the mutex, and is a noop otherwise.
func (*Set[T]) AddCheck ¶
AddCheck adds an item to the set and returns true if the item had been in the set before AddCheck. In all cases when AddCheck returns, the item is a member of the set.
func (*Set[T]) Delete ¶
func (s *Set[T]) Delete(in T)
Delete attempts to remove the item from the set.
func (*Set[T]) DeleteCheck ¶
DeleteCheck removes the item from the set, return true when the item had been in the Set, and returning false othewise
func (*Set[T]) Equal ¶
Equal tests two sets, returning true if the items in the sets have equal values. If the iterators are ordered, order is considered.
func (*Set[T]) Iterator ¶
Iterator provides a way to iterate over the items in the set. Provides items in iteration order if the set is ordered.
func (*Set[T]) MarshalJSON ¶
MarshalJSON generates a JSON array of the items in the set.
func (*Set[T]) Order ¶
func (s *Set[T]) Order()
Order enables order tracking for the Set. The method panics if there is more than one item in the map. If order tracking is enabled, this operation is a noop. tracking is enabled,
func (*Set[T]) Producer ¶
Producer will produce each item from set on successive calls. If the Set is ordered, then the producer produces items in the set's order. If the Set is synchronize, then the Producer always holds the Set's lock when called.
func (*Set[T]) Synchronize ¶
func (s *Set[T]) Synchronize()
Synchronize creates a mutex and enables its use in the Set. This operation is safe to call more than once,
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 Slice ¶
type Slice[T any] []T
Slice is just a local wrapper around a slice, providing a similarly expressive interface to the Map and Pair types in the fun package.
func Transform ¶
Transform processes a slice of one type into a slice of another type using the transformation function. Errors abort the transformation, with the exception of fun.ErrIteratorSkip. All errors are returned to the caller, except io.EOF which indicates the (early) end of iteration.
func (*Slice[T]) AddWhen ¶
AddWhen embeds a conditional check in the Add, and only adds the item to the slice when the condition is true.
func (*Slice[T]) Append ¶
func (s *Slice[T]) Append(in ...T)
Append adds all of the items to the end of the slice.
func (*Slice[T]) AppendWhen ¶
AppendWhen embeds a conditional check in the Append operation, and only adds the items to the slice when the condition is true.
func (*Slice[T]) Empty ¶
func (s *Slice[T]) Empty()
Empty re-slices the slice, to omit all items, but retain the allocation.
func (*Slice[T]) Extend ¶
func (s *Slice[T]) Extend(in []T)
Extend adds the items from the input slice to the root slice.
func (*Slice[T]) ExtendWhen ¶
ExtendWhen embeds a conditional check in the Extend operatio and only adds the items to the slice when the condition is true.
func (*Slice[T]) Filter ¶
Filter returns a new slice, having passed all the items in the input slice. Items that the filter function returns true for are included and others are skipped.
func (*Slice[T]) FilterFuture ¶
FilterFuture returns a future that generates a new slice using the filter to select items from the root slice.
func (Slice[T]) Item ¶
Item returns the item at the specified index.
If the provided index is not within the bounds of the slice the operation panics.
func (Slice[T]) Iterator ¶
Iterator returns an iterator to the items of the slice the range keyword also works for these slices.
func (Slice[T]) Last ¶
Last returns the index of the last element in the slice. Empty slices have `-1` last items.
func (Slice[T]) Process ¶
Process creates a future in the form of a work that, when called iterates through all items in the slice, returning when the processor errors. io.EOF errors are not returned, but do abort iteration early, while fun.ErrIteratorSkip is respected.
func (Slice[T]) Ptrs ¶ added in v0.10.2
func (s Slice[T]) Ptrs() []*T
Ptrs converts a slice in to a slice of pointers to the values in the original slice
func (*Slice[T]) Reset ¶
func (s *Slice[T]) Reset()
Reset constructs a new empty slice releasing the original allocation.
func (*Slice[T]) Reslice ¶
Reslice modifies the slice to set the new start and end indexes.
Slicing operations, can lead to panics if the indexes are out of bounds.
func (*Slice[T]) ResliceBeginning ¶
ResliceBeginning moves the "beginning" of the slice to the specified index.
Slicing operations, can lead to panics if the indexes are out of bounds.
func (*Slice[T]) ResliceEnd ¶
ResliceEnd moves the "end" of the slice to the specified index.
Slicing operations, can lead to panics if the indexes are out of bounds.
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 NewStackFromIterator ¶
NewListFromIterator builds a stack from the elements in the iterator. In general, any error would be the result of the input iterators's close method.
func (*Stack[T]) Append ¶
func (s *Stack[T]) Append(items ...T)
Append adds a variadic sequence of items to the list.
func (*Stack[T]) Head ¶
Head returns the item at the top of this stack. This is a non destructive operation.
func (*Stack[T]) Iterator ¶
Iterator returns a non-destructive iterator over the Items in a stack. Iterator 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]) PopIterator ¶
PopIterator returns a destructive iterator over the Items in a stack. PopIterator will not observe new items added to the stack during iteration.
func (*Stack[T]) ProducerPop ¶
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.