Documentation
¶
Overview ¶
Package avl contains the definition of the AVL tree.
Use New to create a new AVL. The data is treated as immutable if the type implements Clonable. See NewImmutable, NewMutable and NewSimple family functions.
Use NewKey to create a new KeyAVL. You can use this struct as a custom map. The data is treated as immutable if the type implements Clonable. See NewKeyImmutable, NewKeyMutable and NewSimple family functions.
Index ¶
- type AVL
- func (a *AVL[T]) Clone() *AVL[T]
- func (a *AVL[T]) Delete(vals ...T) *AVL[T]
- func (a *AVL[T]) Get(cmp func(v T) int) *T
- func (a *AVL[T]) Insert(vals ...T) *AVL[T]
- func (a *AVL[T]) Max() *T
- func (a *AVL[T]) Min() *T
- func (a *AVL[T]) Size() uint
- func (a *AVL[T]) Sort() []T
- func (a *AVL[T]) String() string
- func (a *AVL[T]) ToImmutable(clone CloneFunc[T]) *AVL[T]
- type Clonable
- type CloneFunc
- type CompareFunc
- type KeyAVL
- func NewKey[K, V any](cmp CompareFunc[K]) *KeyAVL[K, V]
- func NewKeyImmutable[K, V any](cmp CompareFunc[K], clone CloneFunc[V]) *KeyAVL[K, V]
- func NewKeyMutable[K, V any](cmp CompareFunc[K]) *KeyAVL[K, V]
- func NewKeySimple[K cmp.Ordered, V any]() *KeyAVL[K, V]
- func NewKeySimpleImmutable[K cmp.Ordered, V any](clone CloneFunc[V]) *KeyAVL[K, V]
- func NewKeySimpleMutable[K cmp.Ordered, V any]() *KeyAVL[K, V]
- func NewKeyString[V any]() *KeyAVL[string, V]
- func (a *KeyAVL[K, V]) Clone() *KeyAVL[K, V]
- func (a *KeyAVL[K, V]) Delete(keys ...K) *KeyAVL[K, V]
- func (a *KeyAVL[K, V]) Get(k K) *V
- func (a *KeyAVL[K, V]) Has(k K) bool
- func (a *KeyAVL[K, V]) Insert(k K, v V) *KeyAVL[K, V]
- func (a *KeyAVL[K, V]) Max() *V
- func (a *KeyAVL[K, V]) Min() *V
- func (a *KeyAVL[K, V]) Size() uint
- func (a *KeyAVL[K, V]) Sort() []V
- func (a *KeyAVL[K, V]) String() string
- func (a *KeyAVL[K, V]) ToImmutable(clone CloneFunc[V]) *KeyAVL[K, V]
- type Node
- type SimpleAVL
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AVL ¶
type AVL[T any] struct { // contains filtered or unexported fields }
AVL is a standard AVL tree containing nodes.
func New ¶
func New[T any](cmp CompareFunc[T]) *AVL[T]
New creates a new AVL.
If T implements Clonable, the inserted data becomes immutable. See NewMutable to avoid this behavior. See NewImmutable to use immutable data for types that don't implements Clonable.
func NewImmutable ¶ added in v0.2.0
func NewImmutable[T any](cmp CompareFunc[T], clone CloneFunc[T]) *AVL[T]
NewImmutable creates a new AVL storing immutable data.
clone is the function used to clone data to avoid side effects.
func NewMutable ¶ added in v0.2.0
func NewMutable[T any](cmp CompareFunc[T]) *AVL[T]
NewMutable creates a new AVL storing mutable data.
func (*AVL[T]) Get ¶
Get returns the value associated with the key provided.
cmp takes the checked value and returns an integer representing the order. It returns 0 if it is the value, > 0 if the checked value is bigger than the expected one and < 0 else.
// returns value 5 if it is present
a.Get(func(v int) { return a - 5 })
If key is not found, it returns nil.
type Clonable ¶ added in v0.2.0
type Clonable[T any] interface { Clone() T }
Clonable represents a type that will be automatically cloned when used in an AVL.
type CloneFunc ¶ added in v0.2.0
type CloneFunc[T any] func(T) T
CloneFunc is a function that clone the value. It is used to avoid side effects.
type CompareFunc ¶ added in v0.2.0
CompareFunc is a function that compares two values. Returns 0 if a = b, > 0 if a > b and < 0 if a < b.
type KeyAVL ¶ added in v0.2.0
type KeyAVL[K, V any] struct { // contains filtered or unexported fields }
KeyAVL is an AVL where the comparison is made on keys (K).
func NewKey ¶ added in v0.2.0
func NewKey[K, V any](cmp CompareFunc[K]) *KeyAVL[K, V]
NewKey returns a new KeyAVL.
If V implements Clonable, the inserted data becomes immutable. See NewKeyMutable to avoid this behavior. See NewKeyImmutable to use immutable data for types that don't implements Clonable.
func NewKeyImmutable ¶ added in v0.2.0
func NewKeyImmutable[K, V any](cmp CompareFunc[K], clone CloneFunc[V]) *KeyAVL[K, V]
NewKeyImmutable creates a new AVL storing immutable data.
clone is the function used to clone data to avoid side effects.
func NewKeyMutable ¶ added in v0.2.0
func NewKeyMutable[K, V any](cmp CompareFunc[K]) *KeyAVL[K, V]
NewKeyMutable creates a new AVL storing mutable data.
func NewKeySimple ¶ added in v0.2.0
NewKeySimple returns a new KeyAVL ordered with the standard function cmp.Compare.
If V is string, use NewKeyString instead.
func NewKeySimpleImmutable ¶ added in v0.2.0
NewKeySimpleImmutable returns a new KeyAVL storing immutable data ordered with the standard function cmp.Compare.
func NewKeySimpleMutable ¶ added in v0.2.0
NewKeySimpleMutable returns a new KeyAVL storing mutable data ordered with the standard function cmp.Compare.
func NewKeyString ¶ added in v0.2.0
NewKeyString returns a new KeyAVL using strings as keys ordered with the standard function strings.Compare.
func (*KeyAVL[K, V]) Get ¶ added in v0.2.0
func (a *KeyAVL[K, V]) Get(k K) *V
Get returns the value associated with the key provided.
If key is not found, it returns nil.
func (*KeyAVL[K, V]) Max ¶ added in v0.2.0
func (a *KeyAVL[K, V]) Max() *V
Max returns the max contained in the KeyAVL.
func (*KeyAVL[K, V]) Min ¶ added in v0.2.0
func (a *KeyAVL[K, V]) Min() *V
Min returns the min contained in the KeyAVL.
type Node ¶
type Node[T any] struct { Value T // contains filtered or unexported fields }
Node is contained in an AVL.
T should be a pointer to return nil if the value is not found.
type SimpleAVL ¶ added in v0.2.0
SimpleAVL represents an AVL that stores simple values.
func NewSimple ¶ added in v0.2.0
NewSimple returns a new AVL ordered with the standard function cmp.Compare.
If T is string, use NewString instead.
func NewSimpleImmutable ¶ added in v0.2.0
NewSimpleImmutable returns a new AVL storing immutable data ordered with the standard function cmp.Compare.
func NewSimpleMutable ¶ added in v0.2.0
NewSimpleMutable returns a new AVL storing mutable data ordered with the standard function cmp.Compare.
func NewString ¶ added in v0.2.0
NewString returns a new AVL storing strings ordered with the standard function strings.Compare.