Documentation
¶
Overview ¶
Package gtree provides concurrent-safe/unsafe tree containers.
Some implements are from: https://github.com/emirpasic/gods
Index ¶
- func RedBlackKVTreeInit[K comparable, V any](tree *RedBlackKVTree[K, V], comparator func(v1, v2 K) int, safe ...bool)
- func RedBlackKVTreeInitFrom[K comparable, V any](tree *RedBlackKVTree[K, V], comparator func(v1, v2 K) int, data map[K]V, ...)
- type AVLKVTree
- func NewAVLKVTree[K comparable, V any](comparator func(v1, v2 K) int, safe ...bool) *AVLKVTree[K, V]
- func NewAVLKVTreeFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, safe ...bool) *AVLKVTree[K, V]
- func NewAVLKVTreeWithChecker[K comparable, V any](comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *AVLKVTree[K, V]
- func NewAVLKVTreeWithCheckerFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], ...) *AVLKVTree[K, V]
- func (tree *AVLKVTree[K, V]) Ceiling(key K) (ceiling *AVLKVTreeNode[K, V], found bool)
- func (tree *AVLKVTree[K, V]) Clear()
- func (tree *AVLKVTree[K, V]) Clone() *AVLKVTree[K, V]
- func (tree *AVLKVTree[K, V]) Contains(key K) bool
- func (tree *AVLKVTree[K, V]) Flip(comparator ...func(v1, v2 K) int)
- func (tree *AVLKVTree[K, V]) Floor(key K) (floor *AVLKVTreeNode[K, V], found bool)
- func (tree *AVLKVTree[K, V]) Get(key K) (value V)
- func (tree *AVLKVTree[K, V]) GetOrSet(key K, value V) V
- func (tree *AVLKVTree[K, V]) GetOrSetFunc(key K, f func() V) V
- func (tree *AVLKVTree[K, V]) GetOrSetFuncLock(key K, f func() V) V
- func (tree *AVLKVTree[K, V]) GetVar(key K) *gvar.Var
- func (tree *AVLKVTree[K, V]) GetVarOrSet(key K, value V) *gvar.Var
- func (tree *AVLKVTree[K, V]) GetVarOrSetFunc(key K, f func() V) *gvar.Var
- func (tree *AVLKVTree[K, V]) GetVarOrSetFuncLock(key K, f func() V) *gvar.Var
- func (tree *AVLKVTree[K, V]) IsEmpty() bool
- func (tree *AVLKVTree[K, V]) Iterator(f func(key K, value V) bool)
- func (tree *AVLKVTree[K, V]) IteratorAsc(f func(key K, value V) bool)
- func (tree *AVLKVTree[K, V]) IteratorAscFrom(key K, match bool, f func(key K, value V) bool)
- func (tree *AVLKVTree[K, V]) IteratorDesc(f func(key K, value V) bool)
- func (tree *AVLKVTree[K, V]) IteratorDescFrom(key K, match bool, f func(key K, value V) bool)
- func (tree *AVLKVTree[K, V]) IteratorFrom(key K, match bool, f func(key K, value V) bool)
- func (tree *AVLKVTree[K, V]) Keys() []K
- func (tree *AVLKVTree[K, V]) Left() *AVLKVTreeNode[K, V]
- func (tree *AVLKVTree[K, V]) Map() map[K]V
- func (tree *AVLKVTree[K, V]) MapStrAny() map[string]any
- func (tree *AVLKVTree[K, V]) MarshalJSON() (jsonBytes []byte, err error)
- func (tree *AVLKVTree[K, V]) Print()
- func (tree *AVLKVTree[K, V]) Remove(key K) (value V)
- func (tree *AVLKVTree[K, V]) Removes(keys []K)
- func (tree *AVLKVTree[K, V]) Replace(data map[K]V)
- func (tree *AVLKVTree[K, V]) Right() *AVLKVTreeNode[K, V]
- func (tree *AVLKVTree[K, V]) Search(key K) (value V, found bool)
- func (tree *AVLKVTree[K, V]) Set(key K, value V)
- func (tree *AVLKVTree[K, V]) SetIfNotExist(key K, value V) bool
- func (tree *AVLKVTree[K, V]) SetIfNotExistFunc(key K, f func() V) bool
- func (tree *AVLKVTree[K, V]) SetIfNotExistFuncLock(key K, f func() V) bool
- func (tree *AVLKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V])
- func (tree *AVLKVTree[K, V]) Sets(data map[K]V)
- func (tree *AVLKVTree[K, V]) Size() int
- func (tree *AVLKVTree[K, V]) String() string
- func (tree *AVLKVTree[K, V]) Values() []V
- type AVLKVTreeNode
- type AVLTree
- func (tree *AVLTree) Ceiling(key any) (ceiling *AVLTreeNode, found bool)
- func (tree *AVLTree) Clear()
- func (tree *AVLTree) Clone() *AVLTree
- func (tree *AVLTree) Contains(key any) bool
- func (tree *AVLTree) Flip(comparator ...func(v1, v2 any) int)
- func (tree *AVLTree) Floor(key any) (floor *AVLTreeNode, found bool)
- func (tree *AVLTree) Get(key any) (value any)
- func (tree *AVLTree) GetOrSet(key any, value any) any
- func (tree *AVLTree) GetOrSetFunc(key any, f func() any) any
- func (tree *AVLTree) GetOrSetFuncLock(key any, f func() any) any
- func (tree *AVLTree) GetVar(key any) *gvar.Var
- func (tree *AVLTree) GetVarOrSet(key any, value any) *gvar.Var
- func (tree *AVLTree) GetVarOrSetFunc(key any, f func() any) *gvar.Var
- func (tree *AVLTree) GetVarOrSetFuncLock(key any, f func() any) *gvar.Var
- func (tree *AVLTree) IsEmpty() bool
- func (tree *AVLTree) Iterator(f func(key, value any) bool)
- func (tree *AVLTree) IteratorAsc(f func(key, value any) bool)
- func (tree *AVLTree) IteratorAscFrom(key any, match bool, f func(key, value any) bool)
- func (tree *AVLTree) IteratorDesc(f func(key, value any) bool)
- func (tree *AVLTree) IteratorDescFrom(key any, match bool, f func(key, value any) bool)
- func (tree *AVLTree) IteratorFrom(key any, match bool, f func(key, value any) bool)
- func (tree *AVLTree) Keys() []any
- func (tree *AVLTree) Left() *AVLTreeNode
- func (tree *AVLTree) Map() map[any]any
- func (tree *AVLTree) MapStrAny() map[string]any
- func (tree *AVLTree) MarshalJSON() (jsonBytes []byte, err error)
- func (tree *AVLTree) Print()
- func (tree *AVLTree) Remove(key any) (value any)
- func (tree *AVLTree) Removes(keys []any)
- func (tree *AVLTree) Replace(data map[any]any)
- func (tree *AVLTree) Right() *AVLTreeNode
- func (tree *AVLTree) Search(key any) (value any, found bool)
- func (tree *AVLTree) Set(key any, value any)
- func (tree *AVLTree) SetIfNotExist(key any, value any) bool
- func (tree *AVLTree) SetIfNotExistFunc(key any, f func() any) bool
- func (tree *AVLTree) SetIfNotExistFuncLock(key any, f func() any) bool
- func (tree *AVLTree) Sets(data map[any]any)
- func (tree *AVLTree) Size() int
- func (tree *AVLTree) String() string
- func (tree *AVLTree) Values() []any
- type AVLTreeNode
- type BKVTree
- func NewBKVTree[K comparable, V any](m int, comparator func(v1, v2 K) int, safe ...bool) *BKVTree[K, V]
- func NewBKVTreeFrom[K comparable, V any](m int, comparator func(v1, v2 K) int, data map[K]V, safe ...bool) *BKVTree[K, V]
- func NewBKVTreeWithChecker[K comparable, V any](m int, comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *BKVTree[K, V]
- func NewBKVTreeWithCheckerFrom[K comparable, V any](m int, comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], ...) *BKVTree[K, V]
- func (tree *BKVTree[K, V]) Clear()
- func (tree *BKVTree[K, V]) Clone() *BKVTree[K, V]
- func (tree *BKVTree[K, V]) Contains(key K) bool
- func (tree *BKVTree[K, V]) Get(key K) (value V)
- func (tree *BKVTree[K, V]) GetOrSet(key K, value V) V
- func (tree *BKVTree[K, V]) GetOrSetFunc(key K, f func() V) V
- func (tree *BKVTree[K, V]) GetOrSetFuncLock(key K, f func() V) V
- func (tree *BKVTree[K, V]) GetVar(key K) *gvar.Var
- func (tree *BKVTree[K, V]) GetVarOrSet(key K, value V) *gvar.Var
- func (tree *BKVTree[K, V]) GetVarOrSetFunc(key K, f func() V) *gvar.Var
- func (tree *BKVTree[K, V]) GetVarOrSetFuncLock(key K, f func() V) *gvar.Var
- func (tree *BKVTree[K, V]) Height() int
- func (tree *BKVTree[K, V]) IsEmpty() bool
- func (tree *BKVTree[K, V]) Iterator(f func(key K, value V) bool)
- func (tree *BKVTree[K, V]) IteratorAsc(f func(key K, value V) bool)
- func (tree *BKVTree[K, V]) IteratorAscFrom(key K, match bool, f func(key K, value V) bool)
- func (tree *BKVTree[K, V]) IteratorDesc(f func(key K, value V) bool)
- func (tree *BKVTree[K, V]) IteratorDescFrom(key K, match bool, f func(key K, value V) bool)
- func (tree *BKVTree[K, V]) IteratorFrom(key K, match bool, f func(key K, value V) bool)
- func (tree *BKVTree[K, V]) Keys() []K
- func (tree *BKVTree[K, V]) Left() *BKVTreeEntry[K, V]
- func (tree *BKVTree[K, V]) Map() map[K]V
- func (tree *BKVTree[K, V]) MapStrAny() map[string]any
- func (tree *BKVTree[K, V]) MarshalJSON() (jsonBytes []byte, err error)
- func (tree *BKVTree[K, V]) Print()
- func (tree *BKVTree[K, V]) Remove(key K) (value V)
- func (tree *BKVTree[K, V]) Removes(keys []K)
- func (tree *BKVTree[K, V]) Replace(data map[K]V)
- func (tree *BKVTree[K, V]) Right() *BKVTreeEntry[K, V]
- func (tree *BKVTree[K, V]) Search(key K) (value V, found bool)
- func (tree *BKVTree[K, V]) Set(key K, value V)
- func (tree *BKVTree[K, V]) SetIfNotExist(key K, value V) bool
- func (tree *BKVTree[K, V]) SetIfNotExistFunc(key K, f func() V) bool
- func (tree *BKVTree[K, V]) SetIfNotExistFuncLock(key K, f func() V) bool
- func (tree *BKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V])
- func (tree *BKVTree[K, V]) Sets(data map[K]V)
- func (tree *BKVTree[K, V]) Size() int
- func (tree *BKVTree[K, V]) String() string
- func (tree *BKVTree[K, V]) Values() []V
- type BKVTreeEntry
- type BTree
- func (tree *BTree) Clear()
- func (tree *BTree) Clone() *BTree
- func (tree *BTree) Contains(key any) bool
- func (tree *BTree) Get(key any) (value any)
- func (tree *BTree) GetOrSet(key any, value any) any
- func (tree *BTree) GetOrSetFunc(key any, f func() any) any
- func (tree *BTree) GetOrSetFuncLock(key any, f func() any) any
- func (tree *BTree) GetVar(key any) *gvar.Var
- func (tree *BTree) GetVarOrSet(key any, value any) *gvar.Var
- func (tree *BTree) GetVarOrSetFunc(key any, f func() any) *gvar.Var
- func (tree *BTree) GetVarOrSetFuncLock(key any, f func() any) *gvar.Var
- func (tree *BTree) Height() int
- func (tree *BTree) IsEmpty() bool
- func (tree *BTree) Iterator(f func(key, value any) bool)
- func (tree *BTree) IteratorAsc(f func(key, value any) bool)
- func (tree *BTree) IteratorAscFrom(key any, match bool, f func(key, value any) bool)
- func (tree *BTree) IteratorDesc(f func(key, value any) bool)
- func (tree *BTree) IteratorDescFrom(key any, match bool, f func(key, value any) bool)
- func (tree *BTree) IteratorFrom(key any, match bool, f func(key, value any) bool)
- func (tree *BTree) Keys() []any
- func (tree *BTree) Left() *BTreeEntry
- func (tree *BTree) Map() map[any]any
- func (tree *BTree) MapStrAny() map[string]any
- func (tree *BTree) MarshalJSON() (jsonBytes []byte, err error)
- func (tree *BTree) Print()
- func (tree *BTree) Remove(key any) (value any)
- func (tree *BTree) Removes(keys []any)
- func (tree *BTree) Replace(data map[any]any)
- func (tree *BTree) Right() *BTreeEntry
- func (tree *BTree) Search(key any) (value any, found bool)
- func (tree *BTree) Set(key any, value any)
- func (tree *BTree) SetIfNotExist(key any, value any) bool
- func (tree *BTree) SetIfNotExistFunc(key any, f func() any) bool
- func (tree *BTree) SetIfNotExistFuncLock(key any, f func() any) bool
- func (tree *BTree) Sets(data map[any]any)
- func (tree *BTree) Size() int
- func (tree *BTree) String() string
- func (tree *BTree) Values() []any
- type BTreeEntry
- type NilChecker
- type RedBlackKVTree
- func NewRedBlackKVTree[K comparable, V any](comparator func(v1, v2 K) int, safe ...bool) *RedBlackKVTree[K, V]
- func NewRedBlackKVTreeFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, safe ...bool) *RedBlackKVTree[K, V]
- func NewRedBlackKVTreeWithChecker[K comparable, V any](comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *RedBlackKVTree[K, V]
- func NewRedBlackKVTreeWithCheckerFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], ...) *RedBlackKVTree[K, V]
- func (tree *RedBlackKVTree[K, V]) Ceiling(key K) (ceiling *RedBlackKVTreeNode[K, V], found bool)
- func (tree *RedBlackKVTree[K, V]) Clear()
- func (tree *RedBlackKVTree[K, V]) Clone() *RedBlackKVTree[K, V]
- func (tree *RedBlackKVTree[K, V]) Contains(key K) bool
- func (tree *RedBlackKVTree[K, V]) Flip(comparator ...func(v1, v2 K) int)
- func (tree *RedBlackKVTree[K, V]) Floor(key K) (floor *RedBlackKVTreeNode[K, V], found bool)
- func (tree *RedBlackKVTree[K, V]) Get(key K) (value V)
- func (tree *RedBlackKVTree[K, V]) GetOrSet(key K, value V) V
- func (tree *RedBlackKVTree[K, V]) GetOrSetFunc(key K, f func() V) V
- func (tree *RedBlackKVTree[K, V]) GetOrSetFuncLock(key K, f func() V) V
- func (tree *RedBlackKVTree[K, V]) GetVar(key K) *gvar.Var
- func (tree *RedBlackKVTree[K, V]) GetVarOrSet(key K, value V) *gvar.Var
- func (tree *RedBlackKVTree[K, V]) GetVarOrSetFunc(key K, f func() V) *gvar.Var
- func (tree *RedBlackKVTree[K, V]) GetVarOrSetFuncLock(key K, f func() V) *gvar.Var
- func (tree *RedBlackKVTree[K, V]) IsEmpty() bool
- func (tree *RedBlackKVTree[K, V]) Iterator(f func(key K, value V) bool)
- func (tree *RedBlackKVTree[K, V]) IteratorAsc(f func(key K, value V) bool)
- func (tree *RedBlackKVTree[K, V]) IteratorAscFrom(key K, match bool, f func(key K, value V) bool)
- func (tree *RedBlackKVTree[K, V]) IteratorDesc(f func(key K, value V) bool)
- func (tree *RedBlackKVTree[K, V]) IteratorDescFrom(key K, match bool, f func(key K, value V) bool)
- func (tree *RedBlackKVTree[K, V]) IteratorFrom(key K, match bool, f func(key K, value V) bool)
- func (tree *RedBlackKVTree[K, V]) Keys() []K
- func (tree *RedBlackKVTree[K, V]) Left() *RedBlackKVTreeNode[K, V]
- func (tree *RedBlackKVTree[K, V]) Map() map[K]V
- func (tree *RedBlackKVTree[K, V]) MapStrAny() map[string]any
- func (tree RedBlackKVTree[K, V]) MarshalJSON() (jsonBytes []byte, err error)
- func (tree *RedBlackKVTree[K, V]) Print()
- func (tree *RedBlackKVTree[K, V]) Remove(key K) (value V)
- func (tree *RedBlackKVTree[K, V]) Removes(keys []K)
- func (tree *RedBlackKVTree[K, V]) Replace(data map[K]V)
- func (tree *RedBlackKVTree[K, V]) Right() *RedBlackKVTreeNode[K, V]
- func (tree *RedBlackKVTree[K, V]) Search(key K) (value V, found bool)
- func (tree *RedBlackKVTree[K, V]) Set(key K, value V)
- func (tree *RedBlackKVTree[K, V]) SetComparator(comparator func(a, b K) int)
- func (tree *RedBlackKVTree[K, V]) SetIfNotExist(key K, value V) bool
- func (tree *RedBlackKVTree[K, V]) SetIfNotExistFunc(key K, f func() V) bool
- func (tree *RedBlackKVTree[K, V]) SetIfNotExistFuncLock(key K, f func() V) bool
- func (tree *RedBlackKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V])
- func (tree *RedBlackKVTree[K, V]) Sets(data map[K]V)
- func (tree *RedBlackKVTree[K, V]) Size() int
- func (tree *RedBlackKVTree[K, V]) String() string
- func (tree *RedBlackKVTree[K, V]) UnmarshalJSON(b []byte) (err error)
- func (tree *RedBlackKVTree[K, V]) UnmarshalValue(value any) (err error)
- func (tree *RedBlackKVTree[K, V]) Values() []V
- type RedBlackKVTreeNode
- type RedBlackTree
- func (tree *RedBlackTree) Ceiling(key any) (ceiling *RedBlackTreeNode, found bool)
- func (tree *RedBlackTree) Clear()
- func (tree *RedBlackTree) Clone() *RedBlackTree
- func (tree *RedBlackTree) Contains(key any) bool
- func (tree *RedBlackTree) Flip(comparator ...func(v1, v2 any) int)
- func (tree *RedBlackTree) Floor(key any) (floor *RedBlackTreeNode, found bool)
- func (tree *RedBlackTree) Get(key any) (value any)
- func (tree *RedBlackTree) GetOrSet(key any, value any) any
- func (tree *RedBlackTree) GetOrSetFunc(key any, f func() any) any
- func (tree *RedBlackTree) GetOrSetFuncLock(key any, f func() any) any
- func (tree *RedBlackTree) GetVar(key any) *gvar.Var
- func (tree *RedBlackTree) GetVarOrSet(key any, value any) *gvar.Var
- func (tree *RedBlackTree) GetVarOrSetFunc(key any, f func() any) *gvar.Var
- func (tree *RedBlackTree) GetVarOrSetFuncLock(key any, f func() any) *gvar.Var
- func (tree *RedBlackTree) IsEmpty() bool
- func (tree *RedBlackTree) Iterator(f func(key, value any) bool)
- func (tree *RedBlackTree) IteratorAsc(f func(key, value any) bool)
- func (tree *RedBlackTree) IteratorAscFrom(key any, match bool, f func(key, value any) bool)
- func (tree *RedBlackTree) IteratorDesc(f func(key, value any) bool)
- func (tree *RedBlackTree) IteratorDescFrom(key any, match bool, f func(key, value any) bool)
- func (tree *RedBlackTree) IteratorFrom(key any, match bool, f func(key, value any) bool)
- func (tree *RedBlackTree) Keys() []any
- func (tree *RedBlackTree) Left() *RedBlackTreeNode
- func (tree *RedBlackTree) Map() map[any]any
- func (tree *RedBlackTree) MapStrAny() map[string]any
- func (tree RedBlackTree) MarshalJSON() (jsonBytes []byte, err error)
- func (tree *RedBlackTree) Print()
- func (tree *RedBlackTree) Remove(key any) (value any)
- func (tree *RedBlackTree) Removes(keys []any)
- func (tree *RedBlackTree) Replace(data map[any]any)
- func (tree *RedBlackTree) Right() *RedBlackTreeNode
- func (tree *RedBlackTree) Search(key any) (value any, found bool)
- func (tree *RedBlackTree) Set(key any, value any)
- func (tree *RedBlackTree) SetComparator(comparator func(a, b any) int)
- func (tree *RedBlackTree) SetIfNotExist(key any, value any) bool
- func (tree *RedBlackTree) SetIfNotExistFunc(key any, f func() any) bool
- func (tree *RedBlackTree) SetIfNotExistFuncLock(key any, f func() any) bool
- func (tree *RedBlackTree) Sets(data map[any]any)
- func (tree *RedBlackTree) Size() int
- func (tree *RedBlackTree) String() string
- func (tree *RedBlackTree) UnmarshalJSON(b []byte) error
- func (tree *RedBlackTree) UnmarshalValue(value any) (err error)
- func (tree *RedBlackTree) Values() []any
- type RedBlackTreeNode
Examples ¶
- AVLTree.Ceiling
- AVLTree.Clear
- AVLTree.Clone
- AVLTree.Contains
- AVLTree.Flip
- AVLTree.Floor
- AVLTree.Get
- AVLTree.GetOrSet
- AVLTree.GetOrSetFunc
- AVLTree.GetOrSetFuncLock
- AVLTree.GetVar
- AVLTree.GetVarOrSet
- AVLTree.GetVarOrSetFunc
- AVLTree.GetVarOrSetFuncLock
- AVLTree.IsEmpty
- AVLTree.Iterator
- AVLTree.IteratorAsc
- AVLTree.IteratorAscFrom (NoExistKey)
- AVLTree.IteratorAscFrom (NoExistKeyAndMatchFalse)
- AVLTree.IteratorAscFrom (Normal)
- AVLTree.IteratorDesc
- AVLTree.IteratorDescFrom
- AVLTree.IteratorFrom
- AVLTree.Keys
- AVLTree.Left
- AVLTree.Map
- AVLTree.MapStrAny
- AVLTree.MarshalJSON
- AVLTree.Print
- AVLTree.Remove
- AVLTree.Removes
- AVLTree.Replace
- AVLTree.Right
- AVLTree.Search
- AVLTree.Set
- AVLTree.SetIfNotExist
- AVLTree.SetIfNotExistFunc
- AVLTree.SetIfNotExistFuncLock
- AVLTree.Sets
- AVLTree.Size
- AVLTree.String
- AVLTree.Values
- BTree.Clear
- BTree.Clone
- BTree.Contains
- BTree.Get
- BTree.GetOrSet
- BTree.GetOrSetFunc
- BTree.GetOrSetFuncLock
- BTree.GetVar
- BTree.GetVarOrSet
- BTree.GetVarOrSetFunc
- BTree.GetVarOrSetFuncLock
- BTree.Height
- BTree.IsEmpty
- BTree.Iterator
- BTree.IteratorAsc
- BTree.IteratorAscFrom (NoExistKey)
- BTree.IteratorAscFrom (NoExistKeyAndMatchFalse)
- BTree.IteratorAscFrom (Normal)
- BTree.IteratorDesc
- BTree.IteratorDescFrom
- BTree.IteratorFrom
- BTree.Keys
- BTree.Left
- BTree.Map
- BTree.MapStrAny
- BTree.MarshalJSON
- BTree.Print
- BTree.Remove
- BTree.Removes
- BTree.Replace
- BTree.Right
- BTree.Search
- BTree.Set
- BTree.SetIfNotExist
- BTree.SetIfNotExistFunc
- BTree.SetIfNotExistFuncLock
- BTree.Sets
- BTree.Size
- BTree.String
- BTree.Values
- NewAVLTree
- NewAVLTreeFrom
- NewBTree
- NewBTreeFrom
- NewRedBlackTree
- NewRedBlackTreeFrom
- RedBlackTree.Ceiling
- RedBlackTree.Clear
- RedBlackTree.Clone
- RedBlackTree.Contains
- RedBlackTree.Flip
- RedBlackTree.Floor
- RedBlackTree.Get
- RedBlackTree.GetOrSet
- RedBlackTree.GetOrSetFunc
- RedBlackTree.GetOrSetFuncLock
- RedBlackTree.GetVar
- RedBlackTree.GetVarOrSet
- RedBlackTree.GetVarOrSetFunc
- RedBlackTree.GetVarOrSetFuncLock
- RedBlackTree.IsEmpty
- RedBlackTree.Iterator
- RedBlackTree.IteratorAsc
- RedBlackTree.IteratorAscFrom (NoExistKey)
- RedBlackTree.IteratorAscFrom (NoExistKeyAndMatchFalse)
- RedBlackTree.IteratorAscFrom (Normal)
- RedBlackTree.IteratorDesc
- RedBlackTree.IteratorDescFrom
- RedBlackTree.IteratorFrom
- RedBlackTree.Keys
- RedBlackTree.Left
- RedBlackTree.Map
- RedBlackTree.MapStrAny
- RedBlackTree.MarshalJSON
- RedBlackTree.Print
- RedBlackTree.Remove
- RedBlackTree.Removes
- RedBlackTree.Replace
- RedBlackTree.Right
- RedBlackTree.Search
- RedBlackTree.Set
- RedBlackTree.SetComparator
- RedBlackTree.SetIfNotExist
- RedBlackTree.SetIfNotExistFunc
- RedBlackTree.SetIfNotExistFuncLock
- RedBlackTree.Sets
- RedBlackTree.Size
- RedBlackTree.String
- RedBlackTree.UnmarshalJSON
- RedBlackTree.UnmarshalValue
- RedBlackTree.Values
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RedBlackKVTreeInit ¶ added in v2.9.6
func RedBlackKVTreeInit[K comparable, V any](tree *RedBlackKVTree[K, V], comparator func(v1, v2 K) int, safe ...bool)
RedBlackKVTreeInit instantiates a red-black tree with the custom key comparator. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
func RedBlackKVTreeInitFrom ¶ added in v2.9.6
func RedBlackKVTreeInitFrom[K comparable, V any](tree *RedBlackKVTree[K, V], comparator func(v1, v2 K) int, data map[K]V, safe ...bool)
RedBlackKVTreeInitFrom instantiates a red-black tree with the custom key comparator and `data` map. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
Types ¶
type AVLKVTree ¶ added in v2.9.6
type AVLKVTree[K comparable, V any] struct { // contains filtered or unexported fields }
AVLKVTree holds elements of the AVL tree.
func NewAVLKVTree ¶ added in v2.9.6
func NewAVLKVTree[K comparable, V any](comparator func(v1, v2 K) int, safe ...bool) *AVLKVTree[K, V]
NewAVLKVTree instantiates an AVL tree with the custom key comparator.
The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
func NewAVLKVTreeFrom ¶ added in v2.9.6
func NewAVLKVTreeFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, safe ...bool) *AVLKVTree[K, V]
NewAVLKVTreeFrom instantiates an AVL tree with the custom key comparator and data map.
The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
func NewAVLKVTreeWithChecker ¶ added in v2.9.8
func NewAVLKVTreeWithChecker[K comparable, V any](comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *AVLKVTree[K, V]
NewAVLKVTreeWithChecker instantiates an AVL tree with the custom key comparator and nil checker. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default. The parameter `checker` is used to specify whether the given value is nil.
func NewAVLKVTreeWithCheckerFrom ¶ added in v2.9.8
func NewAVLKVTreeWithCheckerFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *AVLKVTree[K, V]
NewAVLKVTreeWithCheckerFrom instantiates an AVL tree with the custom key comparator, nil checker and data map. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default. The parameter `checker` is used to specify whether the given value is nil.
func (*AVLKVTree[K, V]) Ceiling ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Ceiling(key K) (ceiling *AVLKVTreeNode[K, V], found bool)
Ceiling finds ceiling node of the input key, returns the ceiling node or nil if no ceiling node is found. The second return parameter `found` is true if ceiling was found, otherwise false.
Ceiling node is defined as the smallest node that is larger than or equal to the given node. A ceiling node may not be found, either because the tree is empty, or because all nodes in the tree is smaller than the given node.
Key should adhere to the comparator's type assertion, otherwise method panics.
func (*AVLKVTree[K, V]) Clear ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Clear()
Clear removes all nodes from the tree.
func (*AVLKVTree[K, V]) Clone ¶ added in v2.9.6
Clone clones and returns a new tree from current tree.
func (*AVLKVTree[K, V]) Contains ¶ added in v2.9.6
Contains checks and returns whether given `key` exists in the tree.
func (*AVLKVTree[K, V]) Flip ¶ added in v2.9.6
Flip exchanges key-value of the tree to value-key. Note that you should guarantee the value is the same type as key, or else the comparator would panic.
If the type of value is different with key, you pass the new `comparator`.
func (*AVLKVTree[K, V]) Floor ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Floor(key K) (floor *AVLKVTreeNode[K, V], found bool)
Floor Finds floor node of the input key, returns the floor node or nil if no floor node is found. The second returned parameter `found` is true if floor was found, otherwise false.
Floor node is defined as the largest node that is smaller than or equal to the given node. A floor node may not be found, either because the tree is empty, or because all nodes in the tree is larger than the given node.
Key should adhere to the comparator's type assertion, otherwise method panics.
func (*AVLKVTree[K, V]) Get ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Get(key K) (value V)
Get searches the `key` in the tree and returns its associated `value` or nil if key is not found in tree.
Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains function to do so.
func (*AVLKVTree[K, V]) GetOrSet ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) GetOrSet(key K, value V) V
GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns this value.
func (*AVLKVTree[K, V]) GetOrSetFunc ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) GetOrSetFunc(key K, f func() V) V
GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
func (*AVLKVTree[K, V]) GetOrSetFuncLock ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) GetOrSetFuncLock(key K, f func() V) V
GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex lock.
func (*AVLKVTree[K, V]) GetVar ¶ added in v2.9.6
GetVar returns a gvar.Var with the value by given `key`. Note that, the returned gvar.Var is un-concurrent safe.
Also see function Get.
func (*AVLKVTree[K, V]) GetVarOrSet ¶ added in v2.9.6
GetVarOrSet returns a gvar.Var with result from GetVarOrSet. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSet.
func (*AVLKVTree[K, V]) GetVarOrSetFunc ¶ added in v2.9.6
GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFunc.
func (*AVLKVTree[K, V]) GetVarOrSetFuncLock ¶ added in v2.9.6
GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFuncLock.
func (*AVLKVTree[K, V]) IsEmpty ¶ added in v2.9.6
IsEmpty returns true if the tree does not contain any nodes.
func (*AVLKVTree[K, V]) Iterator ¶ added in v2.9.6
Iterator is alias of IteratorAsc.
Also see IteratorAsc.
func (*AVLKVTree[K, V]) IteratorAsc ¶ added in v2.9.6
IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. If callback function `f` returns true, then it continues iterating; or false to stop.
func (*AVLKVTree[K, V]) IteratorAscFrom ¶ added in v2.9.6
IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
func (*AVLKVTree[K, V]) IteratorDesc ¶ added in v2.9.6
IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
If callback function `f` returns true, then it continues iterating; or false to stop.
func (*AVLKVTree[K, V]) IteratorDescFrom ¶ added in v2.9.6
IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
func (*AVLKVTree[K, V]) IteratorFrom ¶ added in v2.9.6
IteratorFrom is alias of IteratorAscFrom.
Also see IteratorAscFrom.
func (*AVLKVTree[K, V]) Keys ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Keys() []K
Keys returns all keys from the tree in order by its comparator.
func (*AVLKVTree[K, V]) Left ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Left() *AVLKVTreeNode[K, V]
Left returns the minimum element corresponding to the comparator of the tree or nil if the tree is empty.
func (*AVLKVTree[K, V]) Map ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Map() map[K]V
Map returns all key-value pairs as map.
func (*AVLKVTree[K, V]) MapStrAny ¶ added in v2.9.6
MapStrAny returns all key-value items as map[string]any.
func (*AVLKVTree[K, V]) MarshalJSON ¶ added in v2.9.6
MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (*AVLKVTree[K, V]) Print ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Print()
Print prints the tree to stdout.
func (*AVLKVTree[K, V]) Remove ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Remove(key K) (value V)
Remove removes the node from the tree by `key`, and returns its associated value of `key`. The given `key` should adhere to the comparator's type assertion, otherwise method panics.
func (*AVLKVTree[K, V]) Removes ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Removes(keys []K)
Removes batch deletes key-value pairs from the tree by `keys`.
func (*AVLKVTree[K, V]) Replace ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Replace(data map[K]V)
Replace clears the data of the tree and sets the nodes by given `data`.
func (*AVLKVTree[K, V]) Right ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Right() *AVLKVTreeNode[K, V]
Right returns the maximum element corresponding to the comparator of the tree or nil if the tree is empty.
func (*AVLKVTree[K, V]) Search ¶ added in v2.9.6
Search searches the tree with given `key`. Second return parameter `found` is true if key was found, otherwise false.
func (*AVLKVTree[K, V]) Set ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Set(key K, value V)
Set sets key-value pair into the tree.
func (*AVLKVTree[K, V]) SetIfNotExist ¶ added in v2.9.6
SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
func (*AVLKVTree[K, V]) SetIfNotExistFunc ¶ added in v2.9.6
SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
func (*AVLKVTree[K, V]) SetIfNotExistFuncLock ¶ added in v2.9.6
SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` within mutex lock.
func (*AVLKVTree[K, V]) SetNilChecker ¶ added in v2.10.0
func (tree *AVLKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V])
SetNilChecker registers a custom nil checker function for the map values. This function is used to determine if a value should be considered as nil. The nil checker function takes a value of type V and returns a boolean indicating whether the value should be treated as nil.
func (*AVLKVTree[K, V]) Sets ¶ added in v2.9.6
func (tree *AVLKVTree[K, V]) Sets(data map[K]V)
Sets batch sets key-values to the tree.
type AVLKVTreeNode ¶ added in v2.9.6
type AVLKVTreeNode[K comparable, V any] struct { Key K Value V }
AVLKVTreeNode is a single element within the tree.
type AVLTree ¶
AVLTree holds elements of the AVL tree.
func NewAVLTree ¶
NewAVLTree instantiates an AVL tree with the custom key comparator.
The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
avlTree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
avlTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(avlTree)
}
Output: │ ┌── key5 │ ┌── key4 └── key3 │ ┌── key2 └── key1 └── key0
func NewAVLTreeFrom ¶
NewAVLTreeFrom instantiates an AVL tree with the custom key comparator and data map.
The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
avlTree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
avlTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
otherAvlTree := gtree.NewAVLTreeFrom(gutil.ComparatorString, avlTree.Map())
fmt.Println(otherAvlTree)
// May Output:
// │ ┌── key5
// │ │ └── key4
// └── key3
// │ ┌── key2
// └── key1
// └── key0
}
Output:
func (*AVLTree) Ceiling ¶
func (tree *AVLTree) Ceiling(key any) (ceiling *AVLTreeNode, found bool)
Ceiling finds ceiling node of the input key, returns the ceiling node or nil if no ceiling node is found. The second return parameter `found` is true if ceiling was found, otherwise false.
Ceiling node is defined as the smallest node that is larger than or equal to the given node. A ceiling node may not be found, either because the tree is empty, or because all nodes in the tree is smaller than the given node.
Key should adhere to the comparator's type assertion, otherwise method panics.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorInt)
for i := 1; i < 100; i++ {
if i != 50 {
tree.Set(i, i)
}
}
node, found := tree.Ceiling(1)
if found {
fmt.Println("Ceiling 1:", node.Key)
}
node, found = tree.Ceiling(50)
if found {
fmt.Println("Ceiling 50:", node.Key)
}
node, found = tree.Ceiling(100)
if found {
fmt.Println("Ceiling 100:", node.Key)
}
node, found = tree.Ceiling(-1)
if found {
fmt.Println("Ceiling -1:", node.Key)
}
}
Output: Ceiling 1: 1 Ceiling 50: 51 Ceiling -1: 1
func (*AVLTree) Clear ¶
func (tree *AVLTree) Clear()
Clear removes all nodes from the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set(1000+i, "val"+gconv.String(i))
}
fmt.Println(tree.Size())
tree.Clear()
fmt.Println(tree.Size())
}
Output: 6 0
func (*AVLTree) Clone ¶
Clone clones and returns a new tree from current tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
avl := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
avl.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
tree := avl.Clone()
fmt.Println(tree.Map())
fmt.Println(tree.Size())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 6
func (*AVLTree) Contains ¶
Contains checks and returns whether given `key` exists in the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Contains("key1"))
fmt.Println(tree.Contains("key6"))
}
Output: true false
func (*AVLTree) Flip ¶
Flip exchanges key-value of the tree to value-key. Note that you should guarantee the value is the same type as key, or else the comparator would panic.
If the type of value is different with key, you pass the new `comparator`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorInt)
for i := 1; i < 6; i++ {
tree.Set(i, i*10)
}
fmt.Println("Before Flip", tree.Map())
tree.Flip()
fmt.Println("After Flip", tree.Map())
}
Output: Before Flip map[1:10 2:20 3:30 4:40 5:50] After Flip map[10:1 20:2 30:3 40:4 50:5]
func (*AVLTree) Floor ¶
func (tree *AVLTree) Floor(key any) (floor *AVLTreeNode, found bool)
Floor Finds floor node of the input key, returns the floor node or nil if no floor node is found. The second returned parameter `found` is true if floor was found, otherwise false.
Floor node is defined as the largest node that is smaller than or equal to the given node. A floor node may not be found, either because the tree is empty, or because all nodes in the tree is larger than the given node.
Key should adhere to the comparator's type assertion, otherwise method panics.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorInt)
for i := 1; i < 100; i++ {
if i != 50 {
tree.Set(i, i)
}
}
node, found := tree.Floor(95)
if found {
fmt.Println("Floor 95:", node.Key)
}
node, found = tree.Floor(50)
if found {
fmt.Println("Floor 50:", node.Key)
}
node, found = tree.Floor(100)
if found {
fmt.Println("Floor 100:", node.Key)
}
node, found = tree.Floor(0)
if found {
fmt.Println("Floor 0:", node.Key)
}
}
Output: Floor 95: 95 Floor 50: 49 Floor 100: 99
func (*AVLTree) Get ¶
Get searches the `key` in the tree and returns its associated `value` or nil if key is not found in tree.
Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains function to do so.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Get("key1"))
fmt.Println(tree.Get("key10"))
}
Output: val1 <nil>
func (*AVLTree) GetOrSet ¶
GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns this value.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetOrSet("key1", "newVal1"))
fmt.Println(tree.GetOrSet("key6", "val6"))
}
Output: val1 val6
func (*AVLTree) GetOrSetFunc ¶
GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetOrSetFunc("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetOrSetFunc("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*AVLTree) GetOrSetFuncLock ¶
GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex lock.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetOrSetFuncLock("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetOrSetFuncLock("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*AVLTree) GetVar ¶
GetVar returns a gvar.Var with the value by given `key`. Note that, the returned gvar.Var is un-concurrent safe.
Also see function Get.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVar("key1").String())
}
Output: val1
func (*AVLTree) GetVarOrSet ¶
GetVarOrSet returns a gvar.Var with result from GetVarOrSet. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSet.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVarOrSet("key1", "newVal1"))
fmt.Println(tree.GetVarOrSet("key6", "val6"))
}
Output: val1 val6
func (*AVLTree) GetVarOrSetFunc ¶
GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFunc.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVarOrSetFunc("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetVarOrSetFunc("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*AVLTree) GetVarOrSetFuncLock ¶
GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFuncLock.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVarOrSetFuncLock("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetVarOrSetFuncLock("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*AVLTree) IsEmpty ¶
IsEmpty returns true if the tree does not contain any nodes.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
fmt.Println(tree.IsEmpty())
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.IsEmpty())
}
Output: true false
func (*AVLTree) Iterator ¶
Iterator is alias of IteratorAsc.
Also see IteratorAsc.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}
var totalKey, totalValue int
tree.Iterator(func(key, value any) bool {
totalKey += key.(int)
totalValue += value.(int)
return totalValue < 20
})
fmt.Println("totalKey:", totalKey)
fmt.Println("totalValue:", totalValue)
}
Output: totalKey: 3 totalValue: 27
func (*AVLTree) IteratorAsc ¶
IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. If callback function `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}
tree.IteratorAsc(func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 0 , value: 10 key: 1 , value: 9 key: 2 , value: 8 key: 3 , value: 7 key: 4 , value: 6 key: 5 , value: 5 key: 6 , value: 4 key: 7 , value: 3 key: 8 , value: 2 key: 9 , value: 1
func (*AVLTree) IteratorAscFrom ¶
IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
Example (NoExistKey) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewAVLTreeFrom(gutil.ComparatorInt, m)
tree.IteratorAscFrom(0, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output:
Example (NoExistKeyAndMatchFalse) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewAVLTreeFrom(gutil.ComparatorInt, m)
tree.IteratorAscFrom(6, false, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output:
Example (Normal) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewAVLTreeFrom(gutil.ComparatorInt, m)
tree.IteratorAscFrom(1, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 1 , value: 10 key: 2 , value: 20 key: 3 , value: 30 key: 4 , value: 40 key: 5 , value: 50
func (*AVLTree) IteratorDesc ¶
IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
If callback function `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}
tree.IteratorDesc(func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 9 , value: 1 key: 8 , value: 2 key: 7 , value: 3 key: 6 , value: 4 key: 5 , value: 5 key: 4 , value: 6 key: 3 , value: 7 key: 2 , value: 8 key: 1 , value: 9 key: 0 , value: 10
func (*AVLTree) IteratorDescFrom ¶
IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewAVLTreeFrom(gutil.ComparatorInt, m)
tree.IteratorDescFrom(5, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 5 , value: 50 key: 4 , value: 40 key: 3 , value: 30 key: 2 , value: 20 key: 1 , value: 10
func (*AVLTree) IteratorFrom ¶
IteratorFrom is alias of IteratorAscFrom.
Also see IteratorAscFrom.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewAVLTreeFrom(gutil.ComparatorInt, m)
tree.IteratorFrom(1, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 1 , value: 10 key: 2 , value: 20 key: 3 , value: 30 key: 4 , value: 40 key: 5 , value: 50
func (*AVLTree) Keys ¶
Keys returns all keys from the tree in order by its comparator.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 6; i > 0; i-- {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Keys())
}
Output: [key1 key2 key3 key4 key5 key6]
func (*AVLTree) Left ¶
func (tree *AVLTree) Left() *AVLTreeNode
Left returns the minimum element corresponding to the comparator of the tree or nil if the tree is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorInt)
for i := 1; i < 100; i++ {
tree.Set(i, i)
}
fmt.Println(tree.Left().Key, tree.Left().Value)
emptyTree := gtree.NewAVLTree(gutil.ComparatorInt)
fmt.Println(emptyTree.Left())
}
Output: 1 1 <nil>
func (*AVLTree) Map ¶
Map returns all key-value pairs as map.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Map())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
func (*AVLTree) MapStrAny ¶
MapStrAny returns all key-value items as map[string]any.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set(1000+i, "val"+gconv.String(i))
}
fmt.Println(tree.MapStrAny())
}
Output: map[1000:val0 1001:val1 1002:val2 1003:val3 1004:val4 1005:val5]
func (*AVLTree) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
bytes, err := json.Marshal(tree)
if err == nil {
fmt.Println(gconv.String(bytes))
}
}
Output: {"key0":"val0","key1":"val1","key2":"val2","key3":"val3","key4":"val4","key5":"val5"}
func (*AVLTree) Print ¶
func (tree *AVLTree) Print()
Print prints the tree to stdout.
Example ¶
package main
import (
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
tree.Print()
}
Output: │ ┌── key5 │ ┌── key4 └── key3 │ ┌── key2 └── key1 └── key0
func (*AVLTree) Remove ¶
Remove removes the node from the tree by `key`, and returns its associated value of `key`. The given `key` should adhere to the comparator's type assertion, otherwise method panics.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Remove("key1"))
fmt.Println(tree.Remove("key6"))
fmt.Println(tree.Map())
}
Output: val1 <nil> map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
func (*AVLTree) Removes ¶
Removes batch deletes key-value pairs from the tree by `keys`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
removeKeys := make([]any, 2)
removeKeys = append(removeKeys, "key1")
removeKeys = append(removeKeys, "key6")
tree.Removes(removeKeys)
fmt.Println(tree.Map())
}
Output: map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
func (*AVLTree) Replace ¶
Replace clears the data of the tree and sets the nodes by given `data`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Map())
data := map[any]any{
"newKey0": "newVal0",
"newKey1": "newVal1",
"newKey2": "newVal2",
}
tree.Replace(data)
fmt.Println(tree.Map())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] map[newKey0:newVal0 newKey1:newVal1 newKey2:newVal2]
func (*AVLTree) Right ¶
func (tree *AVLTree) Right() *AVLTreeNode
Right returns the maximum element corresponding to the comparator of the tree or nil if the tree is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorInt)
for i := 1; i < 100; i++ {
tree.Set(i, i)
}
fmt.Println(tree.Right().Key, tree.Right().Value)
emptyTree := gtree.NewAVLTree(gutil.ComparatorInt)
fmt.Println(emptyTree.Right())
}
Output: 99 99 <nil>
func (*AVLTree) Search ¶
Search searches the tree with given `key`. Second return parameter `found` is true if key was found, otherwise false.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Search("key0"))
fmt.Println(tree.Search("key6"))
}
Output: val0 true <nil> false
func (*AVLTree) Set ¶
Set sets key-value pair into the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Map())
fmt.Println(tree.Size())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 6
func (*AVLTree) SetIfNotExist ¶
SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.SetIfNotExist("key1", "newVal1"))
fmt.Println(tree.SetIfNotExist("key6", "val6"))
}
Output: false true
func (*AVLTree) SetIfNotExistFunc ¶
SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.SetIfNotExistFunc("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.SetIfNotExistFunc("key6", func() any {
return "val6"
}))
}
Output: false true
func (*AVLTree) SetIfNotExistFuncLock ¶
SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` within mutex lock.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.SetIfNotExistFuncLock("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.SetIfNotExistFuncLock("key6", func() any {
return "val6"
}))
}
Output: false true
func (*AVLTree) Sets ¶
Sets batch sets key-values to the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
tree.Sets(map[any]any{
"key1": "val1",
"key2": "val2",
})
fmt.Println(tree.Map())
fmt.Println(tree.Size())
}
Output: map[key1:val1 key2:val2] 2
func (*AVLTree) Size ¶
Size returns number of nodes in the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
fmt.Println(tree.Size())
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Size())
}
Output: 0 6
func (*AVLTree) String ¶
String returns a string representation of container.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.String())
}
Output: │ ┌── key5 │ ┌── key4 └── key3 │ ┌── key2 └── key1 └── key0
func (*AVLTree) Values ¶
Values returns all values from the true in order by its comparator based on the key.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewAVLTree(gutil.ComparatorString)
for i := 6; i > 0; i-- {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Values())
}
Output: [val1 val2 val3 val4 val5 val6]
type AVLTreeNode ¶
type AVLTreeNode = AVLKVTreeNode[any, any]
AVLTreeNode is a single element within the tree.
type BKVTree ¶ added in v2.9.6
type BKVTree[K comparable, V any] struct { // contains filtered or unexported fields }
BKVTree holds elements of the B-tree.
func NewBKVTree ¶ added in v2.9.6
func NewBKVTree[K comparable, V any](m int, comparator func(v1, v2 K) int, safe ...bool) *BKVTree[K, V]
NewBKVTree instantiates a B-tree with `m` (maximum number of children) and a custom key comparator. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default. Note that the `m` must be greater or equal than 3, or else it panics.
func NewBKVTreeFrom ¶ added in v2.9.6
func NewBKVTreeFrom[K comparable, V any](m int, comparator func(v1, v2 K) int, data map[K]V, safe ...bool) *BKVTree[K, V]
NewBKVTreeFrom instantiates a B-tree with `m` (maximum number of children), a custom key comparator and data map. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
func NewBKVTreeWithChecker ¶ added in v2.9.8
func NewBKVTreeWithChecker[K comparable, V any](m int, comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *BKVTree[K, V]
NewBKVTreeWithChecker instantiates a B-tree with `m` (maximum number of children), a custom key comparator and nil checker. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default. The parameter `checker` is used to specify whether the given value is nil.
func NewBKVTreeWithCheckerFrom ¶ added in v2.9.8
func NewBKVTreeWithCheckerFrom[K comparable, V any](m int, comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *BKVTree[K, V]
NewBKVTreeWithCheckerFrom instantiates a B-tree with `m` (maximum number of children), a custom key comparator, nil checker and data map. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default. The parameter `checker` is used to specify whether the given value is nil.
func (*BKVTree[K, V]) Clear ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Clear()
Clear removes all nodes from the tree.
func (*BKVTree[K, V]) Clone ¶ added in v2.9.6
Clone clones and returns a new tree from current tree.
func (*BKVTree[K, V]) Contains ¶ added in v2.9.6
Contains checks and returns whether given `key` exists in the tree.
func (*BKVTree[K, V]) Get ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Get(key K) (value V)
Get searches the `key` in the tree and returns its associated `value` or nil if key is not found in tree.
Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains function to do so.
func (*BKVTree[K, V]) GetOrSet ¶ added in v2.9.6
func (tree *BKVTree[K, V]) GetOrSet(key K, value V) V
GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns this value.
func (*BKVTree[K, V]) GetOrSetFunc ¶ added in v2.9.6
func (tree *BKVTree[K, V]) GetOrSetFunc(key K, f func() V) V
GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
func (*BKVTree[K, V]) GetOrSetFuncLock ¶ added in v2.9.6
func (tree *BKVTree[K, V]) GetOrSetFuncLock(key K, f func() V) V
GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex lock.
func (*BKVTree[K, V]) GetVar ¶ added in v2.9.6
GetVar returns a gvar.Var with the value by given `key`. Note that, the returned gvar.Var is un-concurrent safe.
Also see function Get.
func (*BKVTree[K, V]) GetVarOrSet ¶ added in v2.9.6
GetVarOrSet returns a gvar.Var with result from GetVarOrSet. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSet.
func (*BKVTree[K, V]) GetVarOrSetFunc ¶ added in v2.9.6
GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFunc.
func (*BKVTree[K, V]) GetVarOrSetFuncLock ¶ added in v2.9.6
GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFuncLock.
func (*BKVTree[K, V]) IsEmpty ¶ added in v2.9.6
IsEmpty returns true if tree does not contain any nodes
func (*BKVTree[K, V]) Iterator ¶ added in v2.9.6
Iterator is alias of IteratorAsc.
Also see IteratorAsc.
func (*BKVTree[K, V]) IteratorAsc ¶ added in v2.9.6
IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. If callback function `f` returns true, then it continues iterating; or false to stop.
func (*BKVTree[K, V]) IteratorAscFrom ¶ added in v2.9.6
IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
func (*BKVTree[K, V]) IteratorDesc ¶ added in v2.9.6
IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
If callback function `f` returns true, then it continues iterating; or false to stop.
func (*BKVTree[K, V]) IteratorDescFrom ¶ added in v2.9.6
IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
func (*BKVTree[K, V]) IteratorFrom ¶ added in v2.9.6
IteratorFrom is alias of IteratorAscFrom.
Also see IteratorAscFrom.
func (*BKVTree[K, V]) Keys ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Keys() []K
Keys returns all keys from the tree in order by its comparator.
func (*BKVTree[K, V]) Left ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Left() *BKVTreeEntry[K, V]
Left returns the minimum element corresponding to the comparator of the tree or nil if the tree is empty.
func (*BKVTree[K, V]) Map ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Map() map[K]V
Map returns all key-value pairs as map.
func (*BKVTree[K, V]) MapStrAny ¶ added in v2.9.6
MapStrAny returns all key-value items as map[string]any.
func (*BKVTree[K, V]) MarshalJSON ¶ added in v2.9.6
MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (*BKVTree[K, V]) Print ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Print()
Print prints the tree to stdout.
func (*BKVTree[K, V]) Remove ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Remove(key K) (value V)
Remove removes the node from the tree by `key`, and returns its associated value of `key`. The given `key` should adhere to the comparator's type assertion, otherwise method panics.
func (*BKVTree[K, V]) Removes ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Removes(keys []K)
Removes batch deletes key-value pairs from the tree by `keys`.
func (*BKVTree[K, V]) Replace ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Replace(data map[K]V)
Replace clears the data of the tree and sets the nodes by given `data`.
func (*BKVTree[K, V]) Right ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Right() *BKVTreeEntry[K, V]
Right returns the maximum element corresponding to the comparator of the tree or nil if the tree is empty.
func (*BKVTree[K, V]) Search ¶ added in v2.9.6
Search searches the tree with given `key`. Second return parameter `found` is true if key was found, otherwise false.
func (*BKVTree[K, V]) Set ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Set(key K, value V)
Set sets key-value pair into the tree.
func (*BKVTree[K, V]) SetIfNotExist ¶ added in v2.9.6
SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
func (*BKVTree[K, V]) SetIfNotExistFunc ¶ added in v2.9.6
SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
func (*BKVTree[K, V]) SetIfNotExistFuncLock ¶ added in v2.9.6
SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` within mutex lock.
func (*BKVTree[K, V]) SetNilChecker ¶ added in v2.10.0
func (tree *BKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V])
SetNilChecker registers a custom nil checker function for the map values. This function is used to determine if a value should be considered as nil. The nil checker function takes a value of type V and returns a boolean indicating whether the value should be treated as nil.
func (*BKVTree[K, V]) Sets ¶ added in v2.9.6
func (tree *BKVTree[K, V]) Sets(data map[K]V)
Sets batch sets key-values to the tree.
type BKVTreeEntry ¶ added in v2.9.6
type BKVTreeEntry[K comparable, V any] struct { Key K Value V }
BKVTreeEntry represents the key-value pair contained within nodes.
type BTree ¶
BTree holds elements of the B-tree.
func NewBTree ¶
NewBTree instantiates a B-tree with `m` (maximum number of children) and a custom key comparator. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default. Note that the `m` must be greater or equal than 3, or else it panics.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
bTree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
bTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(bTree.Map())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
func NewBTreeFrom ¶
NewBTreeFrom instantiates a B-tree with `m` (maximum number of children), a custom key comparator and data map. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
bTree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
bTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
otherBTree := gtree.NewBTreeFrom(3, gutil.ComparatorString, bTree.Map())
fmt.Println(otherBTree.Map())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
func (*BTree) Clear ¶
func (tree *BTree) Clear()
Clear removes all nodes from the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set(1000+i, "val"+gconv.String(i))
}
fmt.Println(tree.Size())
tree.Clear()
fmt.Println(tree.Size())
}
Output: 6 0
func (*BTree) Clone ¶
Clone clones and returns a new tree from current tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
b := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
b.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
tree := b.Clone()
fmt.Println(tree.Map())
fmt.Println(tree.Size())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 6
func (*BTree) Contains ¶
Contains checks and returns whether given `key` exists in the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Contains("key1"))
fmt.Println(tree.Contains("key6"))
}
Output: true false
func (*BTree) Get ¶
Get searches the `key` in the tree and returns its associated `value` or nil if key is not found in tree.
Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains function to do so.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Get("key1"))
fmt.Println(tree.Get("key10"))
}
Output: val1 <nil>
func (*BTree) GetOrSet ¶
GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns this value.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetOrSet("key1", "newVal1"))
fmt.Println(tree.GetOrSet("key6", "val6"))
}
Output: val1 val6
func (*BTree) GetOrSetFunc ¶
GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetOrSetFunc("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetOrSetFunc("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*BTree) GetOrSetFuncLock ¶
GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex lock.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetOrSetFuncLock("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetOrSetFuncLock("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*BTree) GetVar ¶
GetVar returns a gvar.Var with the value by given `key`. Note that, the returned gvar.Var is un-concurrent safe.
Also see function Get.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVar("key1").String())
}
Output: val1
func (*BTree) GetVarOrSet ¶
GetVarOrSet returns a gvar.Var with result from GetVarOrSet. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSet.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVarOrSet("key1", "newVal1"))
fmt.Println(tree.GetVarOrSet("key6", "val6"))
}
Output: val1 val6
func (*BTree) GetVarOrSetFunc ¶
GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFunc.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVarOrSetFunc("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetVarOrSetFunc("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*BTree) GetVarOrSetFuncLock ¶
GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFuncLock.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVarOrSetFuncLock("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetVarOrSetFuncLock("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*BTree) Height ¶
Height returns the height of the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorInt)
for i := 0; i < 100; i++ {
tree.Set(i, i)
}
fmt.Println(tree.Height())
}
Output: 6
func (*BTree) IsEmpty ¶
IsEmpty returns true if tree does not contain any nodes
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
fmt.Println(tree.IsEmpty())
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.IsEmpty())
}
Output: true false
func (*BTree) Iterator ¶
Iterator is alias of IteratorAsc.
Also see IteratorAsc.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}
var totalKey, totalValue int
tree.Iterator(func(key, value any) bool {
totalKey += key.(int)
totalValue += value.(int)
return totalValue < 20
})
fmt.Println("totalKey:", totalKey)
fmt.Println("totalValue:", totalValue)
}
Output: totalKey: 3 totalValue: 27
func (*BTree) IteratorAsc ¶
IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. If callback function `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}
tree.IteratorAsc(func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 0 , value: 10 key: 1 , value: 9 key: 2 , value: 8 key: 3 , value: 7 key: 4 , value: 6 key: 5 , value: 5 key: 6 , value: 4 key: 7 , value: 3 key: 8 , value: 2 key: 9 , value: 1
func (*BTree) IteratorAscFrom ¶
IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
Example (NoExistKey) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
tree.IteratorAscFrom(0, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output:
Example (NoExistKeyAndMatchFalse) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
tree.IteratorAscFrom(0, false, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 1 , value: 10 key: 2 , value: 20 key: 3 , value: 30 key: 4 , value: 40 key: 5 , value: 50
Example (Normal) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
tree.IteratorAscFrom(1, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 1 , value: 10 key: 2 , value: 20 key: 3 , value: 30 key: 4 , value: 40 key: 5 , value: 50
func (*BTree) IteratorDesc ¶
IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
If callback function `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}
tree.IteratorDesc(func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 9 , value: 1 key: 8 , value: 2 key: 7 , value: 3 key: 6 , value: 4 key: 5 , value: 5 key: 4 , value: 6 key: 3 , value: 7 key: 2 , value: 8 key: 1 , value: 9 key: 0 , value: 10
func (*BTree) IteratorDescFrom ¶
IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
tree.IteratorDescFrom(5, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 5 , value: 50 key: 4 , value: 40 key: 3 , value: 30 key: 2 , value: 20 key: 1 , value: 10
func (*BTree) IteratorFrom ¶
IteratorFrom is alias of IteratorAscFrom.
Also see IteratorAscFrom.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
tree.IteratorFrom(1, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 1 , value: 10 key: 2 , value: 20 key: 3 , value: 30 key: 4 , value: 40 key: 5 , value: 50
func (*BTree) Keys ¶
Keys returns all keys from the tree in order by its comparator.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 6; i > 0; i-- {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Keys())
}
Output: [key1 key2 key3 key4 key5 key6]
func (*BTree) Left ¶
func (tree *BTree) Left() *BTreeEntry
Left returns the minimum element corresponding to the comparator of the tree or nil if the tree is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorInt)
for i := 1; i < 100; i++ {
tree.Set(i, i)
}
fmt.Println(tree.Left().Key, tree.Left().Value)
emptyTree := gtree.NewBTree(3, gutil.ComparatorInt)
fmt.Println(emptyTree.Left())
}
Output: 1 1 <nil>
func (*BTree) Map ¶
Map returns all key-value pairs as map.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Map())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
func (*BTree) MapStrAny ¶
MapStrAny returns all key-value items as map[string]any.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set(1000+i, "val"+gconv.String(i))
}
fmt.Println(tree.MapStrAny())
}
Output: map[1000:val0 1001:val1 1002:val2 1003:val3 1004:val4 1005:val5]
func (*BTree) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
bytes, err := json.Marshal(tree)
if err == nil {
fmt.Println(gconv.String(bytes))
}
}
Output: {"key0":"val0","key1":"val1","key2":"val2","key3":"val3","key4":"val4","key5":"val5"}
func (*BTree) Print ¶
func (tree *BTree) Print()
Print prints the tree to stdout.
Example ¶
package main
import (
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
tree.Print()
}
Output: key0 key1 key2 key3 key4 key5
func (*BTree) Remove ¶
Remove removes the node from the tree by `key`, and returns its associated value of `key`. The given `key` should adhere to the comparator's type assertion, otherwise method panics.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Remove("key1"))
fmt.Println(tree.Remove("key6"))
fmt.Println(tree.Map())
}
Output: val1 <nil> map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
func (*BTree) Removes ¶
Removes batch deletes key-value pairs from the tree by `keys`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
removeKeys := make([]any, 2)
removeKeys = append(removeKeys, "key1")
removeKeys = append(removeKeys, "key6")
tree.Removes(removeKeys)
fmt.Println(tree.Map())
}
Output: map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
func (*BTree) Replace ¶
Replace clears the data of the tree and sets the nodes by given `data`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Map())
data := map[any]any{
"newKey0": "newVal0",
"newKey1": "newVal1",
"newKey2": "newVal2",
}
tree.Replace(data)
fmt.Println(tree.Map())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] map[newKey0:newVal0 newKey1:newVal1 newKey2:newVal2]
func (*BTree) Right ¶
func (tree *BTree) Right() *BTreeEntry
Right returns the maximum element corresponding to the comparator of the tree or nil if the tree is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorInt)
for i := 1; i < 100; i++ {
tree.Set(i, i)
}
fmt.Println(tree.Right().Key, tree.Right().Value)
emptyTree := gtree.NewBTree(3, gutil.ComparatorInt)
fmt.Println(emptyTree.Left())
}
Output: 99 99 <nil>
func (*BTree) Search ¶
Search searches the tree with given `key`. Second return parameter `found` is true if key was found, otherwise false.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Search("key0"))
fmt.Println(tree.Search("key6"))
}
Output: val0 true <nil> false
func (*BTree) Set ¶
Set sets key-value pair into the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Map())
fmt.Println(tree.Size())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 6
func (*BTree) SetIfNotExist ¶
SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.SetIfNotExist("key1", "newVal1"))
fmt.Println(tree.SetIfNotExist("key6", "val6"))
}
Output: false true
func (*BTree) SetIfNotExistFunc ¶
SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.SetIfNotExistFunc("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.SetIfNotExistFunc("key6", func() any {
return "val6"
}))
}
Output: false true
func (*BTree) SetIfNotExistFuncLock ¶
SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` within mutex lock.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.SetIfNotExistFuncLock("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.SetIfNotExistFuncLock("key6", func() any {
return "val6"
}))
}
Output: false true
func (*BTree) Sets ¶
Sets batch sets key-values to the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
tree.Sets(map[any]any{
"key1": "val1",
"key2": "val2",
})
fmt.Println(tree.Map())
fmt.Println(tree.Size())
}
Output: map[key1:val1 key2:val2] 2
func (*BTree) Size ¶
Size returns number of nodes in the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
fmt.Println(tree.Size())
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Size())
}
Output: 0 6
func (*BTree) String ¶
String returns a string representation of container (for debugging purposes)
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.String())
}
Output: key0 key1 key2 key3 key4 key5
func (*BTree) Values ¶
Values returns all values from the true in order by its comparator based on the key.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 6; i > 0; i-- {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Values())
}
Output: [val1 val2 val3 val4 val5 val6]
type BTreeEntry ¶
type BTreeEntry = BKVTreeEntry[any, any]
BTreeEntry represents the key-value pair contained within nodes.
type NilChecker ¶ added in v2.9.8
NilChecker is a function that checks whether the given value is nil.
type RedBlackKVTree ¶ added in v2.9.6
type RedBlackKVTree[K comparable, V any] struct { // contains filtered or unexported fields }
RedBlackKVTree holds elements of the red-black tree.
func NewRedBlackKVTree ¶ added in v2.9.6
func NewRedBlackKVTree[K comparable, V any](comparator func(v1, v2 K) int, safe ...bool) *RedBlackKVTree[K, V]
NewRedBlackKVTree instantiates a red-black tree with the custom key comparator. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
func NewRedBlackKVTreeFrom ¶ added in v2.9.6
func NewRedBlackKVTreeFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, safe ...bool) *RedBlackKVTree[K, V]
NewRedBlackKVTreeFrom instantiates a red-black tree with the custom key comparator and `data` map. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
func NewRedBlackKVTreeWithChecker ¶ added in v2.9.8
func NewRedBlackKVTreeWithChecker[K comparable, V any](comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *RedBlackKVTree[K, V]
NewRedBlackKVTreeWithChecker instantiates a red-black tree with the custom key comparator and `nilChecker`. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default. The parameter `checker` is used to specify whether the given value is nil.
func NewRedBlackKVTreeWithCheckerFrom ¶ added in v2.9.8
func NewRedBlackKVTreeWithCheckerFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *RedBlackKVTree[K, V]
NewRedBlackKVTreeWithCheckerFrom instantiates a red-black tree with the custom key comparator, `data` map and `nilChecker`. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default. The parameter `checker` is used to specify whether the given value is nil.
func (*RedBlackKVTree[K, V]) Ceiling ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Ceiling(key K) (ceiling *RedBlackKVTreeNode[K, V], found bool)
Ceiling finds ceiling node of the input key, returns the ceiling node or nil if no ceiling node is found. The second return parameter `found` is true if ceiling was found, otherwise false.
Ceiling node is defined as the smallest node that is larger than or equal to the given node. A ceiling node may not be found, either because the tree is empty, or because all nodes in the tree is smaller than the given node.
Key should adhere to the comparator's type assertion, otherwise method panics.
func (*RedBlackKVTree[K, V]) Clear ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Clear()
Clear removes all nodes from the tree.
func (*RedBlackKVTree[K, V]) Clone ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Clone() *RedBlackKVTree[K, V]
Clone clones and returns a new tree from current tree.
func (*RedBlackKVTree[K, V]) Contains ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Contains(key K) bool
Contains checks and returns whether given `key` exists in the tree.
func (*RedBlackKVTree[K, V]) Flip ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Flip(comparator ...func(v1, v2 K) int)
Flip exchanges key-value of the tree to value-key. Note that you should guarantee the value is the same type as key, or else the comparator would panic.
If the type of value is different with key, you pass the new `comparator`.
func (*RedBlackKVTree[K, V]) Floor ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Floor(key K) (floor *RedBlackKVTreeNode[K, V], found bool)
Floor Finds floor node of the input key, returns the floor node or nil if no floor node is found. The second returned parameter `found` is true if floor was found, otherwise false.
Floor node is defined as the largest node that is smaller than or equal to the given node. A floor node may not be found, either because the tree is empty, or because all nodes in the tree is larger than the given node.
Key should adhere to the comparator's type assertion, otherwise method panics.
func (*RedBlackKVTree[K, V]) Get ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Get(key K) (value V)
Get searches the `key` in the tree and returns its associated `value` or nil if key is not found in tree.
Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains function to do so.
func (*RedBlackKVTree[K, V]) GetOrSet ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) GetOrSet(key K, value V) V
GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns this value.
func (*RedBlackKVTree[K, V]) GetOrSetFunc ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) GetOrSetFunc(key K, f func() V) V
GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
func (*RedBlackKVTree[K, V]) GetOrSetFuncLock ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) GetOrSetFuncLock(key K, f func() V) V
GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex lock.
func (*RedBlackKVTree[K, V]) GetVar ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) GetVar(key K) *gvar.Var
GetVar returns a gvar.Var with the value by given `key`. Note that, the returned gvar.Var is un-concurrent safe.
Also see function Get.
func (*RedBlackKVTree[K, V]) GetVarOrSet ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) GetVarOrSet(key K, value V) *gvar.Var
GetVarOrSet returns a gvar.Var with result from GetVarOrSet. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSet.
func (*RedBlackKVTree[K, V]) GetVarOrSetFunc ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) GetVarOrSetFunc(key K, f func() V) *gvar.Var
GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFunc.
func (*RedBlackKVTree[K, V]) GetVarOrSetFuncLock ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) GetVarOrSetFuncLock(key K, f func() V) *gvar.Var
GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFuncLock.
func (*RedBlackKVTree[K, V]) IsEmpty ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) IsEmpty() bool
IsEmpty returns true if tree does not contain any nodes.
func (*RedBlackKVTree[K, V]) Iterator ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Iterator(f func(key K, value V) bool)
Iterator is alias of IteratorAsc.
Also see IteratorAsc.
func (*RedBlackKVTree[K, V]) IteratorAsc ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) IteratorAsc(f func(key K, value V) bool)
IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. If callback function `f` returns true, then it continues iterating; or false to stop.
func (*RedBlackKVTree[K, V]) IteratorAscFrom ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) IteratorAscFrom(key K, match bool, f func(key K, value V) bool)
IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
func (*RedBlackKVTree[K, V]) IteratorDesc ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) IteratorDesc(f func(key K, value V) bool)
IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
If callback function `f` returns true, then it continues iterating; or false to stop.
func (*RedBlackKVTree[K, V]) IteratorDescFrom ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) IteratorDescFrom(key K, match bool, f func(key K, value V) bool)
IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
func (*RedBlackKVTree[K, V]) IteratorFrom ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) IteratorFrom(key K, match bool, f func(key K, value V) bool)
IteratorFrom is alias of IteratorAscFrom.
Also see IteratorAscFrom.
func (*RedBlackKVTree[K, V]) Keys ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Keys() []K
Keys returns all keys from the tree in order by its comparator.
func (*RedBlackKVTree[K, V]) Left ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Left() *RedBlackKVTreeNode[K, V]
Left returns the minimum element corresponding to the comparator of the tree or nil if the tree is empty.
func (*RedBlackKVTree[K, V]) Map ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Map() map[K]V
Map returns all key-value pairs as map.
func (*RedBlackKVTree[K, V]) MapStrAny ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) MapStrAny() map[string]any
MapStrAny returns all key-value items as map[string]any.
func (RedBlackKVTree[K, V]) MarshalJSON ¶ added in v2.9.6
func (tree RedBlackKVTree[K, V]) MarshalJSON() (jsonBytes []byte, err error)
MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (*RedBlackKVTree[K, V]) Print ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Print()
Print prints the tree to stdout.
func (*RedBlackKVTree[K, V]) Remove ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Remove(key K) (value V)
Remove removes the node from the tree by `key`, and returns its associated value of `key`. The given `key` should adhere to the comparator's type assertion, otherwise method panics.
func (*RedBlackKVTree[K, V]) Removes ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Removes(keys []K)
Removes batch deletes key-value pairs from the tree by `keys`.
func (*RedBlackKVTree[K, V]) Replace ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Replace(data map[K]V)
Replace clears the data of the tree and sets the nodes by given `data`.
func (*RedBlackKVTree[K, V]) Right ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Right() *RedBlackKVTreeNode[K, V]
Right returns the maximum element corresponding to the comparator of the tree or nil if the tree is empty.
func (*RedBlackKVTree[K, V]) Search ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Search(key K) (value V, found bool)
Search searches the tree with given `key`. Second return parameter `found` is true if key was found, otherwise false.
func (*RedBlackKVTree[K, V]) Set ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Set(key K, value V)
Set sets key-value pair into the tree.
func (*RedBlackKVTree[K, V]) SetComparator ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) SetComparator(comparator func(a, b K) int)
SetComparator sets/changes the comparator for sorting.
func (*RedBlackKVTree[K, V]) SetIfNotExist ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) SetIfNotExist(key K, value V) bool
SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
func (*RedBlackKVTree[K, V]) SetIfNotExistFunc ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) SetIfNotExistFunc(key K, f func() V) bool
SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
func (*RedBlackKVTree[K, V]) SetIfNotExistFuncLock ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) SetIfNotExistFuncLock(key K, f func() V) bool
SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` within mutex lock.
func (*RedBlackKVTree[K, V]) SetNilChecker ¶ added in v2.10.0
func (tree *RedBlackKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V])
SetNilChecker registers a custom nil checker function for the map values. This function is used to determine if a value should be considered as nil. The nil checker function takes a value of type V and returns a boolean indicating whether the value should be treated as nil.
func (*RedBlackKVTree[K, V]) Sets ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Sets(data map[K]V)
Sets batch sets key-values to the tree.
func (*RedBlackKVTree[K, V]) Size ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Size() int
Size returns number of nodes in the tree.
func (*RedBlackKVTree[K, V]) String ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) String() string
String returns a string representation of container
func (*RedBlackKVTree[K, V]) UnmarshalJSON ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) UnmarshalJSON(b []byte) (err error)
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*RedBlackKVTree[K, V]) UnmarshalValue ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) UnmarshalValue(value any) (err error)
UnmarshalValue is an interface implement which sets any type of value for map.
func (*RedBlackKVTree[K, V]) Values ¶ added in v2.9.6
func (tree *RedBlackKVTree[K, V]) Values() []V
Values returns all values from the true in order by its comparator based on the key.
type RedBlackKVTreeNode ¶ added in v2.9.6
type RedBlackKVTreeNode[K comparable, V any] struct { Key K Value V }
RedBlackKVTreeNode is a single element within the tree.
type RedBlackTree ¶
type RedBlackTree struct {
*RedBlackKVTree[any, any]
// contains filtered or unexported fields
}
RedBlackTree holds elements of the red-black tree.
func NewRedBlackTree ¶
func NewRedBlackTree(comparator func(v1, v2 any) int, safe ...bool) *RedBlackTree
NewRedBlackTree instantiates a red-black tree with the custom key comparator. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
rbTree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
rbTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(rbTree)
}
Output: │ ┌── key5 │ ┌── key4 │ ┌── key3 │ │ └── key2 └── key1 └── key0
func NewRedBlackTreeFrom ¶
func NewRedBlackTreeFrom(comparator func(v1, v2 any) int, data map[any]any, safe ...bool) *RedBlackTree
NewRedBlackTreeFrom instantiates a red-black tree with the custom key comparator and `data` map. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
rbTree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
rbTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
otherRBTree := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, rbTree.Map())
fmt.Println(otherRBTree)
// May Output:
// │ ┌── key5
// │ ┌── key4
// │ ┌── key3
// │ │ └── key2
// └── key1
// └── key0
}
Output:
func (*RedBlackTree) Ceiling ¶
func (tree *RedBlackTree) Ceiling(key any) (ceiling *RedBlackTreeNode, found bool)
Ceiling finds ceiling node of the input key, returns the ceiling node or nil if no ceiling node is found. The second return parameter `found` is true if ceiling was found, otherwise false.
Ceiling node is defined as the smallest node that is larger than or equal to the given node. A ceiling node may not be found, either because the tree is empty, or because all nodes in the tree is smaller than the given node.
Key should adhere to the comparator's type assertion, otherwise method panics.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
for i := 1; i < 100; i++ {
if i != 50 {
tree.Set(i, i)
}
}
node, found := tree.Ceiling(1)
if found {
fmt.Println("Ceiling 1:", node.Key)
}
node, found = tree.Ceiling(50)
if found {
fmt.Println("Ceiling 50:", node.Key)
}
node, found = tree.Ceiling(100)
if found {
fmt.Println("Ceiling 100:", node.Key)
}
node, found = tree.Ceiling(-1)
if found {
fmt.Println("Ceiling -1:", node.Key)
}
}
Output: Ceiling 1: 1 Ceiling 50: 51 Ceiling -1: 1
func (*RedBlackTree) Clear ¶
func (tree *RedBlackTree) Clear()
Clear removes all nodes from the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set(1000+i, "val"+gconv.String(i))
}
fmt.Println(tree.Size())
tree.Clear()
fmt.Println(tree.Size())
}
Output: 6 0
func (*RedBlackTree) Clone ¶
func (tree *RedBlackTree) Clone() *RedBlackTree
Clone clones and returns a new tree from current tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
b := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
b.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
tree := b.Clone()
fmt.Println(tree.Map())
fmt.Println(tree.Size())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 6
func (*RedBlackTree) Contains ¶
func (tree *RedBlackTree) Contains(key any) bool
Contains checks and returns whether given `key` exists in the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Contains("key1"))
fmt.Println(tree.Contains("key6"))
}
Output: true false
func (*RedBlackTree) Flip ¶
func (tree *RedBlackTree) Flip(comparator ...func(v1, v2 any) int)
Flip exchanges key-value of the tree to value-key. Note that you should guarantee the value is the same type as key, or else the comparator would panic.
If the type of value is different with key, you pass the new `comparator`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 1; i < 6; i++ {
tree.Set(i, i*10)
}
fmt.Println("Before Flip", tree.Map())
tree.Flip()
fmt.Println("After Flip", tree.Map())
}
Output: Before Flip map[1:10 2:20 3:30 4:40 5:50] After Flip map[10:1 20:2 30:3 40:4 50:5]
func (*RedBlackTree) Floor ¶
func (tree *RedBlackTree) Floor(key any) (floor *RedBlackTreeNode, found bool)
Floor Finds floor node of the input key, returns the floor node or nil if no floor node is found. The second returned parameter `found` is true if floor was found, otherwise false.
Floor node is defined as the largest node that is smaller than or equal to the given node. A floor node may not be found, either because the tree is empty, or because all nodes in the tree is larger than the given node.
Key should adhere to the comparator's type assertion, otherwise method panics.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
for i := 1; i < 100; i++ {
if i != 50 {
tree.Set(i, i)
}
}
node, found := tree.Floor(95)
if found {
fmt.Println("Floor 95:", node.Key)
}
node, found = tree.Floor(50)
if found {
fmt.Println("Floor 50:", node.Key)
}
node, found = tree.Floor(100)
if found {
fmt.Println("Floor 100:", node.Key)
}
node, found = tree.Floor(0)
if found {
fmt.Println("Floor 0:", node.Key)
}
}
Output: Floor 95: 95 Floor 50: 49 Floor 100: 99
func (*RedBlackTree) Get ¶
func (tree *RedBlackTree) Get(key any) (value any)
Get searches the `key` in the tree and returns its associated `value` or nil if key is not found in tree.
Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains function to do so.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Get("key1"))
fmt.Println(tree.Get("key10"))
}
Output: val1 <nil>
func (*RedBlackTree) GetOrSet ¶
func (tree *RedBlackTree) GetOrSet(key any, value any) any
GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns this value.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetOrSet("key1", "newVal1"))
fmt.Println(tree.GetOrSet("key6", "val6"))
}
Output: val1 val6
func (*RedBlackTree) GetOrSetFunc ¶
func (tree *RedBlackTree) GetOrSetFunc(key any, f func() any) any
GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetOrSetFunc("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetOrSetFunc("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*RedBlackTree) GetOrSetFuncLock ¶
func (tree *RedBlackTree) GetOrSetFuncLock(key any, f func() any) any
GetOrSetFuncLock returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not exist and then returns this value.
GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`within mutex lock.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetOrSetFuncLock("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetOrSetFuncLock("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*RedBlackTree) GetVar ¶
func (tree *RedBlackTree) GetVar(key any) *gvar.Var
GetVar returns a gvar.Var with the value by given `key`. Note that, the returned gvar.Var is un-concurrent safe.
Also see function Get.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVar("key1").String())
}
Output: val1
func (*RedBlackTree) GetVarOrSet ¶
func (tree *RedBlackTree) GetVarOrSet(key any, value any) *gvar.Var
GetVarOrSet returns a gvar.Var with result from GetVarOrSet. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSet.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVarOrSet("key1", "newVal1"))
fmt.Println(tree.GetVarOrSet("key6", "val6"))
}
Output: val1 val6
func (*RedBlackTree) GetVarOrSetFunc ¶
func (tree *RedBlackTree) GetVarOrSetFunc(key any, f func() any) *gvar.Var
GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFunc.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVarOrSetFunc("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetVarOrSetFunc("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*RedBlackTree) GetVarOrSetFuncLock ¶
func (tree *RedBlackTree) GetVarOrSetFuncLock(key any, f func() any) *gvar.Var
GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. Note that, the returned gvar.Var is un-concurrent safe.
Also see function GetOrSetFuncLock.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.GetVarOrSetFuncLock("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.GetVarOrSetFuncLock("key6", func() any {
return "val6"
}))
}
Output: val1 val6
func (*RedBlackTree) IsEmpty ¶
func (tree *RedBlackTree) IsEmpty() bool
IsEmpty returns true if tree does not contain any nodes.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
fmt.Println(tree.IsEmpty())
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.IsEmpty())
}
Output: true false
func (*RedBlackTree) Iterator ¶
func (tree *RedBlackTree) Iterator(f func(key, value any) bool)
Iterator is alias of IteratorAsc.
Also see IteratorAsc.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}
var totalKey, totalValue int
tree.Iterator(func(key, value any) bool {
totalKey += key.(int)
totalValue += value.(int)
return totalValue < 20
})
fmt.Println("totalKey:", totalKey)
fmt.Println("totalValue:", totalValue)
}
Output: totalKey: 3 totalValue: 27
func (*RedBlackTree) IteratorAsc ¶
func (tree *RedBlackTree) IteratorAsc(f func(key, value any) bool)
IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. If callback function `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}
tree.IteratorAsc(func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 0 , value: 10 key: 1 , value: 9 key: 2 , value: 8 key: 3 , value: 7 key: 4 , value: 6 key: 5 , value: 5 key: 6 , value: 4 key: 7 , value: 3 key: 8 , value: 2 key: 9 , value: 1
func (*RedBlackTree) IteratorAscFrom ¶
func (tree *RedBlackTree) IteratorAscFrom(key any, match bool, f func(key, value any) bool)
IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
Example (NoExistKey) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, m)
tree.IteratorAscFrom(0, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output:
Example (NoExistKeyAndMatchFalse) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, m)
tree.IteratorAscFrom(0, false, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 1 , value: 10 key: 2 , value: 20 key: 3 , value: 30 key: 4 , value: 40 key: 5 , value: 50
Example (Normal) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, m)
tree.IteratorAscFrom(1, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 1 , value: 10 key: 2 , value: 20 key: 3 , value: 30 key: 4 , value: 40 key: 5 , value: 50
func (*RedBlackTree) IteratorDesc ¶
func (tree *RedBlackTree) IteratorDesc(f func(key, value any) bool)
IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
If callback function `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}
tree.IteratorDesc(func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 9 , value: 1 key: 8 , value: 2 key: 7 , value: 3 key: 6 , value: 4 key: 5 , value: 5 key: 4 , value: 6 key: 3 , value: 7 key: 2 , value: 8 key: 1 , value: 9 key: 0 , value: 10
func (*RedBlackTree) IteratorDescFrom ¶
func (tree *RedBlackTree) IteratorDescFrom(key any, match bool, f func(key, value any) bool)
IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
The parameter `key` specifies the start entry for iterating. The parameter `match` specifies whether starting iterating only if the `key` is fully matched, or else using index searching iterating. If callback function `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, m)
tree.IteratorDescFrom(5, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 5 , value: 50 key: 4 , value: 40 key: 3 , value: 30 key: 2 , value: 20 key: 1 , value: 10
func (*RedBlackTree) IteratorFrom ¶
func (tree *RedBlackTree) IteratorFrom(key any, match bool, f func(key, value any) bool)
IteratorFrom is alias of IteratorAscFrom.
Also see IteratorAscFrom.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
m := make(map[any]any)
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, m)
tree.IteratorFrom(1, true, func(key, value any) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
}
Output: key: 1 , value: 10 key: 2 , value: 20 key: 3 , value: 30 key: 4 , value: 40 key: 5 , value: 50
func (*RedBlackTree) Keys ¶
func (tree *RedBlackTree) Keys() []any
Keys returns all keys from the tree in order by its comparator.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 6; i > 0; i-- {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Keys())
}
Output: [key1 key2 key3 key4 key5 key6]
func (*RedBlackTree) Left ¶
func (tree *RedBlackTree) Left() *RedBlackTreeNode
Left returns the minimum element corresponding to the comparator of the tree or nil if the tree is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
for i := 1; i < 100; i++ {
tree.Set(i, i)
}
fmt.Println(tree.Left().Key, tree.Left().Value)
emptyTree := gtree.NewRedBlackTree(gutil.ComparatorInt)
fmt.Println(emptyTree.Left())
}
Output: 1 1 <nil>
func (*RedBlackTree) Map ¶
func (tree *RedBlackTree) Map() map[any]any
Map returns all key-value pairs as map.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Map())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
func (*RedBlackTree) MapStrAny ¶
func (tree *RedBlackTree) MapStrAny() map[string]any
MapStrAny returns all key-value items as map[string]any.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set(1000+i, "val"+gconv.String(i))
}
fmt.Println(tree.MapStrAny())
}
Output: map[1000:val0 1001:val1 1002:val2 1003:val3 1004:val4 1005:val5]
func (RedBlackTree) MarshalJSON ¶
func (tree RedBlackTree) MarshalJSON() (jsonBytes []byte, err error)
MarshalJSON implements the interface MarshalJSON for json.Marshal.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
bytes, err := json.Marshal(tree)
if err == nil {
fmt.Println(gconv.String(bytes))
}
}
Output: {"key0":"val0","key1":"val1","key2":"val2","key3":"val3","key4":"val4","key5":"val5"}
func (*RedBlackTree) Print ¶
func (tree *RedBlackTree) Print()
Print prints the tree to stdout.
Example ¶
package main
import (
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
tree.Print()
}
Output: │ ┌── key5 │ ┌── key4 │ ┌── key3 │ │ └── key2 └── key1 └── key0
func (*RedBlackTree) Remove ¶
func (tree *RedBlackTree) Remove(key any) (value any)
Remove removes the node from the tree by `key`, and returns its associated value of `key`. The given `key` should adhere to the comparator's type assertion, otherwise method panics.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Remove("key1"))
fmt.Println(tree.Remove("key6"))
fmt.Println(tree.Map())
}
Output: val1 <nil> map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
func (*RedBlackTree) Removes ¶
func (tree *RedBlackTree) Removes(keys []any)
Removes batch deletes key-value pairs from the tree by `keys`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
removeKeys := make([]any, 2)
removeKeys = append(removeKeys, "key1")
removeKeys = append(removeKeys, "key6")
tree.Removes(removeKeys)
fmt.Println(tree.Map())
}
Output: map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
func (*RedBlackTree) Replace ¶
func (tree *RedBlackTree) Replace(data map[any]any)
Replace clears the data of the tree and sets the nodes by given `data`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Map())
data := map[any]any{
"newKey0": "newVal0",
"newKey1": "newVal1",
"newKey2": "newVal2",
}
tree.Replace(data)
fmt.Println(tree.Map())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] map[newKey0:newVal0 newKey1:newVal1 newKey2:newVal2]
func (*RedBlackTree) Right ¶
func (tree *RedBlackTree) Right() *RedBlackTreeNode
Right returns the maximum element corresponding to the comparator of the tree or nil if the tree is empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
for i := 1; i < 100; i++ {
tree.Set(i, i)
}
fmt.Println(tree.Right().Key, tree.Right().Value)
emptyTree := gtree.NewRedBlackTree(gutil.ComparatorInt)
fmt.Println(emptyTree.Left())
}
Output: 99 99 <nil>
func (*RedBlackTree) Search ¶
func (tree *RedBlackTree) Search(key any) (value any, found bool)
Search searches the tree with given `key`. Second return parameter `found` is true if key was found, otherwise false.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Search("key0"))
fmt.Println(tree.Search("key6"))
}
Output: val0 true <nil> false
func (*RedBlackTree) Set ¶
func (tree *RedBlackTree) Set(key any, value any)
Set sets key-value pair into the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Map())
fmt.Println(tree.Size())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 6
func (*RedBlackTree) SetComparator ¶
func (tree *RedBlackTree) SetComparator(comparator func(a, b any) int)
SetComparator sets/changes the comparator for sorting.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
var tree gtree.RedBlackTree
tree.SetComparator(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Map())
fmt.Println(tree.Size())
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 6
func (*RedBlackTree) SetIfNotExist ¶
func (tree *RedBlackTree) SetIfNotExist(key any, value any) bool
SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.SetIfNotExist("key1", "newVal1"))
fmt.Println(tree.SetIfNotExist("key6", "val6"))
}
Output: false true
func (*RedBlackTree) SetIfNotExistFunc ¶
func (tree *RedBlackTree) SetIfNotExistFunc(key any, f func() any) bool
SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.SetIfNotExistFunc("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.SetIfNotExistFunc("key6", func() any {
return "val6"
}))
}
Output: false true
func (*RedBlackTree) SetIfNotExistFuncLock ¶
func (tree *RedBlackTree) SetIfNotExistFuncLock(key any, f func() any) bool
SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and such setting key-value pair operation would be ignored.
SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` within mutex lock.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.SetIfNotExistFuncLock("key1", func() any {
return "newVal1"
}))
fmt.Println(tree.SetIfNotExistFuncLock("key6", func() any {
return "val6"
}))
}
Output: false true
func (*RedBlackTree) Sets ¶
func (tree *RedBlackTree) Sets(data map[any]any)
Sets batch sets key-values to the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
tree.Sets(map[any]any{
"key1": "val1",
"key2": "val2",
})
fmt.Println(tree.Map())
fmt.Println(tree.Size())
}
Output: map[key1:val1 key2:val2] 2
func (*RedBlackTree) Size ¶
func (tree *RedBlackTree) Size() int
Size returns number of nodes in the tree.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
fmt.Println(tree.Size())
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Size())
}
Output: 0 6
func (*RedBlackTree) String ¶
func (tree *RedBlackTree) String() string
String returns a string representation of container
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.String())
}
Output: │ ┌── key5 │ ┌── key4 │ ┌── key3 │ │ └── key2 └── key1 └── key0
func (*RedBlackTree) UnmarshalJSON ¶
func (tree *RedBlackTree) UnmarshalJSON(b []byte) error
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
bytes, err := json.Marshal(tree)
otherTree := gtree.NewRedBlackTree(gutil.ComparatorString)
err = json.Unmarshal(bytes, &otherTree)
if err == nil {
fmt.Println(otherTree.Map())
}
}
Output: map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
func (*RedBlackTree) UnmarshalValue ¶
func (tree *RedBlackTree) UnmarshalValue(value any) (err error)
UnmarshalValue is an interface implement which sets any type of value for map.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
type User struct {
Uid int
Name string
Pass1 string `gconv:"password1"`
Pass2 string `gconv:"password2"`
}
var (
user = User{
Uid: 1,
Name: "john",
Pass1: "123",
Pass2: "456",
}
)
if err := gconv.Scan(user, tree); err == nil {
fmt.Printf("%#v", tree.Map())
}
}
Output: map[interface {}]interface {}{"Name":"john", "Uid":1, "password1":"123", "password2":"456"}
func (*RedBlackTree) Values ¶
func (tree *RedBlackTree) Values() []any
Values returns all values from the true in order by its comparator based on the key.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {
tree := gtree.NewRedBlackTree(gutil.ComparatorString)
for i := 6; i > 0; i-- {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
fmt.Println(tree.Values())
}
Output: [val1 val2 val3 val4 val5 val6]
type RedBlackTreeNode ¶
type RedBlackTreeNode = RedBlackKVTreeNode[any, any]
RedBlackTreeNode is a single element within the tree.