Documentation
¶
Index ¶
- type Iterator
- type Ordered
- type Pair
- type Queue
- type Set
- func (s *Set[T]) Add(elem T) *Set[T]
- func (s *Set[T]) Contains(elem T) bool
- func (s *Set[T]) GetKthElement(k int) (e T, ok bool)
- func (s *Set[T]) GreatestLowerBound(value T) (T, bool)
- func (s *Set[T]) IsEmpty() bool
- func (s *Set[T]) Iter() Iterator[T]
- func (s *Set[T]) IterGte(e T) Iterator[T]
- func (s *Set[T]) LeastUpperBound(value T) (e T, ok bool)
- func (s *Set[T]) MarshalJSON() ([]byte, error)
- func (s *Set[T]) Remove(elem T) *Set[T]
- func (s *Set[T]) Size() int
- func (s *Set[T]) UnmarshalJSON(data []byte) error
- type SetEx
- func (s *SetEx[T]) Add(elem T) *SetEx[T]
- func (s *SetEx[T]) Contains(elem T) bool
- func (s *SetEx[T]) GetKthElement(k int) (e T, ok bool)
- func (s *SetEx[T]) GreatestLowerBound(value T) (T, bool)
- func (s *SetEx[T]) IsEmpty() bool
- func (s *SetEx[T]) Iter() Iterator[T]
- func (s *SetEx[T]) IterGte(e T) Iterator[T]
- func (s *SetEx[T]) LeastUpperBound(value T) (e T, ok bool)
- func (s *SetEx[T]) MarshalJSON() ([]byte, error)
- func (s *SetEx[T]) Remove(elem T) *SetEx[T]
- func (s *SetEx[T]) Size() int
- func (s *SetEx[T]) UnmarshalJSON(data []byte) error
- type SetExIterator
- type SetIterator
- type Stack
- type Tree
- func (n *Tree[K, V]) Contains(key K) bool
- func (n *Tree[K, V]) Delete(key K) *Tree[K, V]
- func (n *Tree[K, V]) Find(key K) V
- func (n *Tree[K, V]) FindOpt(key K) (V, bool)
- func (n *Tree[K, V]) GetKthElement(k int) (p Pair[K, V], ok bool)
- func (n *Tree[K, V]) GreatestLowerBound(key K) (Pair[K, V], bool)
- func (n *Tree[K, V]) Height() int
- func (n *Tree[K, V]) IsEmpty() bool
- func (n *Tree[K, V]) Iter() Iterator[Pair[K, V]]
- func (n *Tree[K, V]) IterGte(glb K) Iterator[Pair[K, V]]
- func (n *Tree[K, V]) Key() K
- func (n *Tree[K, V]) Least() (Pair[K, V], bool)
- func (n *Tree[K, V]) LeastUpperBound(key K) (Pair[K, V], bool)
- func (n *Tree[K, V]) Left() *Tree[K, V]
- func (n *Tree[K, V]) MarshalJSON() ([]byte, error)
- func (n *Tree[K, V]) Most() (Pair[K, V], bool)
- func (n *Tree[K, V]) Right() *Tree[K, V]
- func (n *Tree[K, V]) Size() int
- func (n *Tree[K, V]) UnmarshalJSON(data []byte) error
- func (n *Tree[K, V]) Update(key K, value V) *Tree[K, V]
- func (n *Tree[K, V]) Value() V
- type TreeEx
- func (n *TreeEx[K, V]) Contains(key K) bool
- func (n *TreeEx[K, V]) Delete(key K) *TreeEx[K, V]
- func (n *TreeEx[K, V]) Find(key K) V
- func (n *TreeEx[K, V]) FindOpt(key K) (V, bool)
- func (n *TreeEx[K, V]) GetKthElement(k int) (p Pair[K, V], ok bool)
- func (n *TreeEx[K, V]) GreatestLowerBound(key K) (Pair[K, V], bool)
- func (n *TreeEx[K, V]) Height() int
- func (n *TreeEx[K, V]) IsEmpty() bool
- func (n *TreeEx[K, V]) Iter() Iterator[Pair[K, V]]
- func (n *TreeEx[K, V]) IterGte(glb K) Iterator[Pair[K, V]]
- func (n *TreeEx[K, V]) Key() K
- func (n *TreeEx[K, V]) Least() (Pair[K, V], bool)
- func (n *TreeEx[K, V]) LeastUpperBound(key K) (Pair[K, V], bool)
- func (n *TreeEx[K, V]) Left() *TreeEx[K, V]
- func (n *TreeEx[K, V]) MarshalJSON() ([]byte, error)
- func (n *TreeEx[K, V]) Most() (Pair[K, V], bool)
- func (n *TreeEx[K, V]) Right() *TreeEx[K, V]
- func (n *TreeEx[K, V]) Size() int
- func (n *TreeEx[K, V]) UnmarshalJSON(data []byte) error
- func (n *TreeEx[K, V]) Update(key K, value V) *TreeEx[K, V]
- func (n *TreeEx[K, V]) Value() V
- type TreeExIterator
- type TreeIterator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Iterator ¶
type Iterator[T any] interface { //Next advances the iterator by one position, and returns true iif the new position is valid. Next() bool //Current returns the value at the current iterator position. Will panic if the iterator position is invalid. Current() T }
Iterator defines an interface for an iterator over a persistent data structure.
It is safe to use multiple iterators over the same data structure concurrently. A single Iterator instance, however, is not concurrency safe. Sharing an iterator instance between go-routines requires explicit synchronization.
Example:
var iter = tree.Iterator()
for iter.Next() {
fmt.Println(iter.Current())
}
type Ordered ¶
Ordered defines an interface for a custom key type. Any type K defining a Less method can be used with TreeEx[K, V] or OrderedSet[K]. For primitive key types see Tree[K, V] and Set[K]
type Pair ¶
type Pair[K any, V any] struct { // Key is the key associated with the pair. Key K // Value is the value associated with the pair. Value V }
Pair defines a struct for a Key / Value pair.
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue implements a persistent queue.
Persistent queues are immutable. Each mutating operation will return a pointer to a new queue with the update applied.The implementation uses structural sharing to make immutability efficient. The implementation is concurrency safe and non-blocking. A *Queue[T] instance may be accessed from multiple go-routines without synchronization. Each mutating Queue[T] operation has amortized O(1) cost.
func (*Queue[T]) Dequeue ¶
Dequeue removes the top item from the queue, and returns the dequeued value along with a new queue with the value removed. If the queue is empty, the returned 'value' will be the 0 value of T.
Dequeue is amortized O(1). This means an individual Dequeue operation may be O(n), but with a series of n calls the average runtime is O(1).
func (*Queue[T]) MarshalJSON ¶ added in v1.0.4
func (*Queue[T]) Top ¶ added in v1.0.8
func (q *Queue[T]) Top() T
Top returns the top element in the queue without removing it. This is O(1). If the queue is empty, the returned value will be the zero value of T.
func (*Queue[T]) UnmarshalJSON ¶ added in v1.0.4
type Set ¶
type Set[T constraints.Ordered] struct { // contains filtered or unexported fields }
Set defines a set interface implemented using a persistent AVL tree. It works for all types that support the < operator. For custom types see the SetEx method.
Persistent AVL trees are immutable. Each mutating operation will return the root of a new tree with the requested update applied. The implementation uses structural sharing to make immutability efficient. For any given update, at most O(log(n)) nodes will be replaced in the new tree. The implementation is also concurrency-safe and non-blocking. A *Set[T] instance may be accessed from multiple go-routines without synchronization. See the docs on Iterator[T] for notes on concurrent use of iterators.
func EmptySet ¶ added in v1.0.5
func EmptySet[T constraints.Ordered]() *Set[T]
func (*Set[T]) GetKthElement ¶ added in v1.0.3
GetKthElement returns the k'th smallest element in a set. If no such element exists, ok will be false.
func (*Set[T]) GreatestLowerBound ¶
GreatestLowerBound returns the largest element e in s, such that e <= value. If no such element exists, ok will be false and e will be set to the zero value for T.
func (*Set[T]) IterGte ¶
IterGte returns an in-order traversal iterator over all elements x in s that are >= e
func (*Set[T]) LeastUpperBound ¶
LeastUpperBound returns the smallest element e in s, such that value <= e. If no such element exists, ok will be false and e will be set to the zero value for T.
func (*Set[T]) MarshalJSON ¶
MarshalJSON marshals the set s as a json array.a
func (*Set[T]) UnmarshalJSON ¶
UnmarshalJSON unmarshals a json array into s.
type SetEx ¶
type SetEx[T Ordered[T]] struct { // contains filtered or unexported fields }
SetEx defines a set interface implemented using a persistent AVL tree. It works for custom types that implement the Ordered[T] interface. See Set[T] for types that support the < interface.
Persistent AVL trees are immutable. Each mutating operation will return the root of a new tree with the requested update applied. The implementation uses structural sharing to make immutability efficient. For any given update, at most O(log(n)) nodes will be replaced in the new tree. The implementation is also concurrency-safe and non-blocking. A *Set[T] instance may be accessed from multiple go-routines without synchronization. See the docs for Iterator[T] for notes on concurrent use of iterators.
func EmptySetEx ¶ added in v1.0.5
func (*SetEx[T]) GetKthElement ¶ added in v1.0.3
GetKthElement returns the k'th smallest element in a set. If no such element exists, ok will be false.
func (*SetEx[T]) GreatestLowerBound ¶
GreatestLowerBound returns the largest element e in s, such that e <= value. If no such element exists, ok will be false and e will be set to the zero value for T.
func (*SetEx[T]) IterGte ¶
IterGte returns an in-order traversal iterator over all elements x in s that are >= e
func (*SetEx[T]) LeastUpperBound ¶
LeastUpperBound returns the smallest element e in s, such that value <= e. If no such element exists, ok will be false and e will be set to the zero value for T.
func (*SetEx[T]) MarshalJSON ¶
MarshalJSON marshals the set s as a json array.a
func (*SetEx[T]) UnmarshalJSON ¶
UnmarshalJSON unmarshals a json array into s.
type SetExIterator ¶
type SetExIterator[T Ordered[T]] struct { // contains filtered or unexported fields }
func (*SetExIterator[T]) Current ¶
func (s *SetExIterator[T]) Current() T
func (*SetExIterator[T]) Next ¶
func (s *SetExIterator[T]) Next() bool
type SetIterator ¶
type SetIterator[T constraints.Ordered] struct { // contains filtered or unexported fields }
func (*SetIterator[T]) Current ¶
func (s *SetIterator[T]) Current() T
func (*SetIterator[T]) Next ¶
func (s *SetIterator[T]) Next() bool
type Stack ¶
type Stack[T any] struct { // contains filtered or unexported fields }
Stack implements a persistent stack.
Persistent stacks are immutable. Each mutating operation will return a pointer to a new stack with the update applied.The implementation uses structural sharing to make immutability efficient. The implementation is concurrency safe and non-blocking. A *Stack[T] instance may be accessed from multiple go-routines without synchronization. Each mutating Stack[T] operation is O(1).
func (*Stack[T]) Peek ¶
func (s *Stack[T]) Peek() T
Peek returns the top-most element of the stack. If the stack is empty, it will return the zero value for T.
func (*Stack[T]) Pop ¶
Pop returns a new stack with the top element removed. If the stack s is empty, s.Pop() is also empty.
type Tree ¶
type Tree[K constraints.Ordered, V any] struct { // contains filtered or unexported fields }
Tree implements a persistent AVL tree for keys types that support the < operator. For custom key types see TreeEx[K,V].
Note: Both an empty Tree struct and a nil *Tree are valid empty trees.
Persistent AVL trees are immutable. Each mutating operation will return the root of a new tree with the requested update applied.The implementation uses structural sharing to make immutability efficient. For any given update at most O(log(N)) nodes will be replaced in the new tree. The implementation is concurrency safe and non-blocking. A *Tree[K,V] instance may be accessed from multiple go-routines without synchronization. See the docs for Iterator[T] for notes on the concurrent use of iterators.
Example: var root *Tree[string, int] root = root.Update("Hello", 1).Update("World, 2).Update("Apple", 3).Update("Stuff", 4) root = root.Remove("Apple") iter := root.Iter()
for iter.MoveNext() {
fmt.Printf("%v == %v\n", iter.Current().Key(), iter.Current.Value())
}
func (*Tree[K, V]) Find ¶
func (n *Tree[K, V]) Find(key K) V
Find returns the value associated with key in the tree. Will return a zero value if no such item exists.
func (*Tree[K, V]) FindOpt ¶ added in v1.0.3
FindOpt returns the value associated with key in the tree. Returns true if found; otherwise false. Zero value is returned when found is false.
func (*Tree[K, V]) GetKthElement ¶ added in v1.0.3
GetKthElement returns the k'th smallest element in a Tree. If no such element exists, ok will be false.
func (*Tree[K, V]) GreatestLowerBound ¶
GreatestLowerBound returns the key-value-par for the largest node n such that n.Key() <= key. If there is no such node then boolean is false.
func (*Tree[K, V]) Height ¶
Height returns the height of the tree rooted at node n. Will return 0 if n is empty.
func (*Tree[K, V]) IterGte ¶
IterGte returns an in-order iterator for the tree for all nodes n such that n.Key() >= glb.
func (*Tree[K, V]) Key ¶
func (n *Tree[K, V]) Key() K
Key returns the key associated with the node n. If n is empty, the zero value for K is returned.
func (*Tree[K, V]) Least ¶
Least returns the key-value-pair for the lowest element in the tree. If the tree is empty then boolean is false
func (*Tree[K, V]) LeastUpperBound ¶
LeastUpperBound returns the key-value-pair for the smallest node n such that n.Key() >= key. If there is no such node then boolean is false.
func (*Tree[K, V]) Left ¶
Left returns the left subtree of the current tree. Will never be nil. For empty nodes t.Left() == t. If you are using Left() and Right() to traverse a tree, make sure to use IsEmpty() as your termination condition.
func (*Tree[K, V]) MarshalJSON ¶
func (*Tree[K, V]) Most ¶
Most returns the key-value-pair for the greatest element in the tree. If the tree is empty then boolean is false
func (*Tree[K, V]) Right ¶
Right returns the right subtree of the current tree. Will never be nil. For empty nodes t.Left() == t. If you are using Left() and Right() to traverse a tree, make sure to use IsEmpty() as your termination condition.
func (*Tree[K, V]) UnmarshalJSON ¶
type TreeEx ¶
TreeEx implements a persistent AVL tree for keys implementing the Ordered[K] interface. For built-in ordered keys (types supporting <) see Tree[K,V],
Persistent AVL trees are immutable. Each mutating operation will return the root of a new tree with the requested update applied.The implementation uses structural sharing to make immutability efficient. For any given update at most O(log(N)) nodes will be replaced in the new tree. The implementation is concurrency safe and non-blocking. A *Tree[K,V] instance may be accessed from multiple go-routines without synchronization. See the docs for Iterator[T] for notes on the concurrent use of iterators.
TreeEx also supports json encoding / decoding. To unmarshal a json map into a TreeEx[K,V] the type K should support the encoding.TextUnmarshaler interface. To marshal json, the type K should support conversion to string.
func EmptyTreeEx ¶
func (*TreeEx[K, V]) Delete ¶
Delete returns the root of a new tree with the entry for 'key' removed.
func (*TreeEx[K, V]) Find ¶
func (n *TreeEx[K, V]) Find(key K) V
Find returns the pair associated with key in the tree. Will return a zero value if no such item exists.
func (*TreeEx[K, V]) FindOpt ¶ added in v1.0.3
FindOpt returns the pair associated with key in the tree. Return true if found; otherwise false. Zero value is returned when found is false.
func (*TreeEx[K, V]) GetKthElement ¶ added in v1.0.3
GetKthElement returns the k'th smallest element in a Tree. If no such element exists, ok will be false.
func (*TreeEx[K, V]) GreatestLowerBound ¶
GreatestLowerBound returns the key-value-par for the largest node n such that n.Key() <= key. If there is no such node then false is returned.
func (*TreeEx[K, V]) Height ¶
Height returns the height of the tree rooted at node n. Will return 0 if n is empty.
func (*TreeEx[K, V]) IterGte ¶
IterGte returns an in-order iterator for the tree for all nodes n such that n.Key() >= glb.
func (*TreeEx[K, V]) Key ¶
func (n *TreeEx[K, V]) Key() K
Key returns the key associated with the node n. If n is empty, the zero value for K is returned.
func (*TreeEx[K, V]) Least ¶
Least returns the key-value-pair for the lowest element in the tree. If the tree is empty, then return false
func (*TreeEx[K, V]) LeastUpperBound ¶
LeastUpperBound returns the key-value-pair for the smallest node n such that n.Key() >= key. If there is no such node then false is returned.
func (*TreeEx[K, V]) Left ¶
Left returns the left subtree of the current tree. Will never be nil. For empty nodes t.Left() == t. If you are using Left() and Right() to traverse a tree, make sure to use IsEmpty() as your termination condition.
func (*TreeEx[K, V]) MarshalJSON ¶
func (*TreeEx[K, V]) Most ¶
Most returns the key-value-pair for the greatest element in the tree. If the tree is empty, the returned pair is also empty
func (*TreeEx[K, V]) Right ¶
Right returns the right subtree of the current tree. Will never be nil. For empty nodes t.Left() == t. If you are using Left() and Right() to traverse a tree, make sure to use IsEmpty() as your termination condition.
func (*TreeEx[K, V]) UnmarshalJSON ¶
type TreeExIterator ¶
TreeExIterator defines an iterator over a TreeEx
func (*TreeExIterator[K, V]) Current ¶
func (i *TreeExIterator[K, V]) Current() Pair[K, V]
func (*TreeExIterator[K, V]) Next ¶
func (i *TreeExIterator[K, V]) Next() bool
type TreeIterator ¶
type TreeIterator[K constraints.Ordered, V any] struct { // contains filtered or unexported fields }
TreeIterator defines an iterator over a Tree
func (*TreeIterator[K, V]) Current ¶
func (i *TreeIterator[K, V]) Current() Pair[K, V]
func (*TreeIterator[K, V]) Next ¶
func (i *TreeIterator[K, V]) Next() bool