2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-05-10 22:29:06 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gtree
|
|
|
|
|
|
|
|
import (
|
2022-03-07 17:39:41 +08:00
|
|
|
"bytes"
|
2019-05-10 22:29:06 +08:00
|
|
|
"fmt"
|
2019-09-19 19:44:46 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/container/gvar"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/json"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/rwmutex"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2019-05-10 22:29:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// AVLTree holds elements of the AVL tree.
|
|
|
|
type AVLTree struct {
|
2020-04-07 20:06:26 +08:00
|
|
|
mu rwmutex.RWMutex
|
2019-05-10 22:29:06 +08:00
|
|
|
root *AVLTreeNode
|
|
|
|
comparator func(v1, v2 interface{}) int
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
// AVLTreeNode is a single element within the tree.
|
|
|
|
type AVLTreeNode struct {
|
|
|
|
Key interface{}
|
|
|
|
Value interface{}
|
|
|
|
parent *AVLTreeNode
|
|
|
|
children [2]*AVLTreeNode
|
|
|
|
b int8
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:53:36 +08:00
|
|
|
// NewAVLTree instantiates an AVL tree with the custom key comparator.
|
2021-09-16 20:57:59 +08:00
|
|
|
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
2019-05-10 22:29:06 +08:00
|
|
|
// which is false in default.
|
2019-07-23 23:20:27 +08:00
|
|
|
func NewAVLTree(comparator func(v1, v2 interface{}) int, safe ...bool) *AVLTree {
|
2019-05-10 22:29:06 +08:00
|
|
|
return &AVLTree{
|
2020-04-07 20:06:26 +08:00
|
|
|
mu: rwmutex.Create(safe...),
|
2019-05-10 22:29:06 +08:00
|
|
|
comparator: comparator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:53:36 +08:00
|
|
|
// NewAVLTreeFrom instantiates an AVL tree with the custom key comparator and data map.
|
2021-09-16 20:57:59 +08:00
|
|
|
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
2019-05-11 20:47:25 +08:00
|
|
|
// which is false in default.
|
2019-07-23 23:20:27 +08:00
|
|
|
func NewAVLTreeFrom(comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *AVLTree {
|
|
|
|
tree := NewAVLTree(comparator, safe...)
|
2019-05-11 20:47:25 +08:00
|
|
|
for k, v := range data {
|
|
|
|
tree.put(k, v, nil, &tree.root)
|
|
|
|
}
|
|
|
|
return tree
|
|
|
|
}
|
|
|
|
|
2019-05-10 22:29:06 +08:00
|
|
|
// Clone returns a new tree with a copy of current tree.
|
2020-04-07 20:06:26 +08:00
|
|
|
func (tree *AVLTree) Clone() *AVLTree {
|
2020-12-02 21:38:29 +08:00
|
|
|
newTree := NewAVLTree(tree.comparator, tree.mu.IsSafe())
|
2019-05-10 22:29:06 +08:00
|
|
|
newTree.Sets(tree.Map())
|
|
|
|
return newTree
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set inserts node into the tree.
|
|
|
|
func (tree *AVLTree) Set(key interface{}, value interface{}) {
|
|
|
|
tree.mu.Lock()
|
|
|
|
defer tree.mu.Unlock()
|
|
|
|
tree.put(key, value, nil, &tree.root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets batch sets key-values to the tree.
|
|
|
|
func (tree *AVLTree) Sets(data map[interface{}]interface{}) {
|
|
|
|
tree.mu.Lock()
|
|
|
|
defer tree.mu.Unlock()
|
|
|
|
for key, value := range data {
|
|
|
|
tree.put(key, value, nil, &tree.root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Search searches the tree with given `key`.
|
|
|
|
// Second return parameter `found` is true if key was found, otherwise false.
|
2019-05-10 22:29:06 +08:00
|
|
|
func (tree *AVLTree) Search(key interface{}) (value interface{}, found bool) {
|
|
|
|
tree.mu.RLock()
|
|
|
|
defer tree.mu.RUnlock()
|
2019-08-13 21:06:11 +08:00
|
|
|
if node, found := tree.doSearch(key); found {
|
|
|
|
return node.Value, true
|
2019-08-13 13:45:01 +08:00
|
|
|
}
|
|
|
|
return nil, false
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// doSearch searches the tree with given `key`.
|
|
|
|
// Second return parameter `found` is true if key was found, otherwise false.
|
2019-08-13 21:06:11 +08:00
|
|
|
func (tree *AVLTree) doSearch(key interface{}) (node *AVLTreeNode, found bool) {
|
|
|
|
node = tree.root
|
|
|
|
for node != nil {
|
2020-04-07 20:06:26 +08:00
|
|
|
cmp := tree.getComparator()(key, node.Key)
|
2019-05-10 22:29:06 +08:00
|
|
|
switch {
|
2019-06-19 09:06:52 +08:00
|
|
|
case cmp == 0:
|
2019-08-13 21:06:11 +08:00
|
|
|
return node, true
|
2019-06-19 09:06:52 +08:00
|
|
|
case cmp < 0:
|
2019-08-13 21:06:11 +08:00
|
|
|
node = node.children[0]
|
2019-06-19 09:06:52 +08:00
|
|
|
case cmp > 0:
|
2019-08-13 21:06:11 +08:00
|
|
|
node = node.children[1]
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-13 21:06:11 +08:00
|
|
|
return nil, false
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree.
|
2019-05-10 22:29:06 +08:00
|
|
|
func (tree *AVLTree) Get(key interface{}) (value interface{}) {
|
|
|
|
value, _ = tree.Search(key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
2021-09-16 20:57:59 +08:00
|
|
|
// if not exists, set value to the map with given `key`,
|
2019-05-10 22:29:06 +08:00
|
|
|
// or else just return the existing value.
|
|
|
|
//
|
2021-09-16 20:57:59 +08:00
|
|
|
// When setting value, if `value` is type of <func() interface {}>,
|
2019-05-10 22:29:06 +08:00
|
|
|
// it will be executed with mutex.Lock of the hash map,
|
2021-09-16 20:57:59 +08:00
|
|
|
// and its return value will be set to the map with `key`.
|
2019-05-10 22:29:06 +08:00
|
|
|
//
|
2021-09-16 20:57:59 +08:00
|
|
|
// It returns value with given `key`.
|
2019-05-10 22:29:06 +08:00
|
|
|
func (tree *AVLTree) doSetWithLockCheck(key interface{}, value interface{}) interface{} {
|
|
|
|
tree.mu.Lock()
|
|
|
|
defer tree.mu.Unlock()
|
2019-08-13 21:06:11 +08:00
|
|
|
if node, found := tree.doSearch(key); found {
|
|
|
|
return node.Value
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
if f, ok := value.(func() interface{}); ok {
|
2019-05-10 22:29:06 +08:00
|
|
|
value = f()
|
|
|
|
}
|
2019-10-31 23:37:33 +08:00
|
|
|
if value != nil {
|
|
|
|
tree.put(key, value, nil, &tree.root)
|
|
|
|
}
|
2019-05-10 22:29:06 +08:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrSet returns the value by key,
|
2021-09-16 20:57:59 +08:00
|
|
|
// or sets value with given `value` if it does not exist and then returns this value.
|
2019-05-10 22:29:06 +08:00
|
|
|
func (tree *AVLTree) GetOrSet(key interface{}, value interface{}) interface{} {
|
|
|
|
if v, ok := tree.Search(key); !ok {
|
|
|
|
return tree.doSetWithLockCheck(key, value)
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrSetFunc returns the value by key,
|
2021-09-16 20:57:59 +08:00
|
|
|
// or sets value with returned value of callback function `f` if it does not exist
|
2019-11-06 20:22:20 +08:00
|
|
|
// and then returns this value.
|
2019-05-10 22:29:06 +08:00
|
|
|
func (tree *AVLTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{} {
|
|
|
|
if v, ok := tree.Search(key); !ok {
|
|
|
|
return tree.doSetWithLockCheck(key, f())
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrSetFuncLock returns the value by key,
|
2021-09-16 20:57:59 +08:00
|
|
|
// or sets value with returned value of callback function `f` if it does not exist
|
2019-11-06 20:22:20 +08:00
|
|
|
// and then returns this value.
|
2019-05-10 22:29:06 +08:00
|
|
|
//
|
2021-09-16 20:57:59 +08:00
|
|
|
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
2019-05-10 22:29:06 +08:00
|
|
|
// with mutex.Lock of the hash map.
|
|
|
|
func (tree *AVLTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} {
|
|
|
|
if v, ok := tree.Search(key); !ok {
|
|
|
|
return tree.doSetWithLockCheck(key, f)
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// GetVar returns a gvar.Var with the value by given `key`.
|
2019-05-10 22:29:06 +08:00
|
|
|
// The returned gvar.Var is un-concurrent safe.
|
2020-06-29 13:40:19 +08:00
|
|
|
func (tree *AVLTree) GetVar(key interface{}) *gvar.Var {
|
2019-07-23 23:20:27 +08:00
|
|
|
return gvar.New(tree.Get(key))
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVarOrSet returns a gvar.Var with result from GetVarOrSet.
|
|
|
|
// The returned gvar.Var is un-concurrent safe.
|
2020-06-29 13:40:19 +08:00
|
|
|
func (tree *AVLTree) GetVarOrSet(key interface{}, value interface{}) *gvar.Var {
|
2019-07-23 23:20:27 +08:00
|
|
|
return gvar.New(tree.GetOrSet(key, value))
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc.
|
|
|
|
// The returned gvar.Var is un-concurrent safe.
|
2020-06-29 13:40:19 +08:00
|
|
|
func (tree *AVLTree) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var {
|
2019-07-23 23:20:27 +08:00
|
|
|
return gvar.New(tree.GetOrSetFunc(key, f))
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock.
|
|
|
|
// The returned gvar.Var is un-concurrent safe.
|
2020-06-29 13:40:19 +08:00
|
|
|
func (tree *AVLTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var {
|
2019-07-23 23:20:27 +08:00
|
|
|
return gvar.New(tree.GetOrSetFuncLock(key, f))
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
|
|
|
// It returns false if `key` exists, and `value` would be ignored.
|
2019-05-10 22:29:06 +08:00
|
|
|
func (tree *AVLTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
|
|
|
if !tree.Contains(key) {
|
|
|
|
tree.doSetWithLockCheck(key, value)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
2021-10-21 18:22:47 +08:00
|
|
|
// It returns false if `key` exists, and `value` would be ignored.
|
2019-05-10 22:29:06 +08:00
|
|
|
func (tree *AVLTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
|
|
|
if !tree.Contains(key) {
|
|
|
|
tree.doSetWithLockCheck(key, f())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
2021-10-21 18:22:47 +08:00
|
|
|
// It returns false if `key` exists, and `value` would be ignored.
|
2019-05-10 22:29:06 +08:00
|
|
|
//
|
|
|
|
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
2021-09-16 20:57:59 +08:00
|
|
|
// it executes function `f` with mutex.Lock of the hash map.
|
2019-05-10 22:29:06 +08:00
|
|
|
func (tree *AVLTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool {
|
|
|
|
if !tree.Contains(key) {
|
|
|
|
tree.doSetWithLockCheck(key, f)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Contains checks whether `key` exists in the tree.
|
2019-05-10 22:29:06 +08:00
|
|
|
func (tree *AVLTree) Contains(key interface{}) bool {
|
|
|
|
_, ok := tree.Search(key)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:53:36 +08:00
|
|
|
// Remove removes the node from the tree by key.
|
2019-05-10 22:29:06 +08:00
|
|
|
// Key should adhere to the comparator's type assertion, otherwise method panics.
|
2019-05-12 22:56:13 +08:00
|
|
|
func (tree *AVLTree) Remove(key interface{}) (value interface{}) {
|
2019-05-10 22:29:06 +08:00
|
|
|
tree.mu.Lock()
|
|
|
|
defer tree.mu.Unlock()
|
2019-05-12 22:56:13 +08:00
|
|
|
value, _ = tree.remove(key, &tree.root)
|
|
|
|
return
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Removes batch deletes values of the tree by `keys`.
|
2019-05-10 22:29:06 +08:00
|
|
|
func (tree *AVLTree) Removes(keys []interface{}) {
|
|
|
|
tree.mu.Lock()
|
|
|
|
defer tree.mu.Unlock()
|
2019-05-11 20:47:25 +08:00
|
|
|
for _, key := range keys {
|
2019-05-10 22:29:06 +08:00
|
|
|
tree.remove(key, &tree.root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true if tree does not contain any nodes.
|
|
|
|
func (tree *AVLTree) IsEmpty() bool {
|
|
|
|
return tree.Size() == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns number of nodes in the tree.
|
|
|
|
func (tree *AVLTree) Size() int {
|
|
|
|
tree.mu.RLock()
|
|
|
|
defer tree.mu.RUnlock()
|
|
|
|
return tree.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keys returns all keys in asc order.
|
|
|
|
func (tree *AVLTree) Keys() []interface{} {
|
2019-06-19 09:06:52 +08:00
|
|
|
keys := make([]interface{}, tree.Size())
|
2019-05-10 22:29:06 +08:00
|
|
|
index := 0
|
|
|
|
tree.IteratorAsc(func(key, value interface{}) bool {
|
|
|
|
keys[index] = key
|
|
|
|
index++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values returns all values in asc order based on the key.
|
|
|
|
func (tree *AVLTree) Values() []interface{} {
|
|
|
|
values := make([]interface{}, tree.Size())
|
2019-06-19 09:06:52 +08:00
|
|
|
index := 0
|
2019-05-10 22:29:06 +08:00
|
|
|
tree.IteratorAsc(func(key, value interface{}) bool {
|
2019-05-11 20:47:25 +08:00
|
|
|
values[index] = value
|
2019-05-10 22:29:06 +08:00
|
|
|
index++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
|
|
|
// Left returns the minimum element of the AVL tree
|
|
|
|
// or nil if the tree is empty.
|
|
|
|
func (tree *AVLTree) Left() *AVLTreeNode {
|
|
|
|
tree.mu.RLock()
|
|
|
|
defer tree.mu.RUnlock()
|
|
|
|
node := tree.bottom(0)
|
|
|
|
if tree.mu.IsSafe() {
|
2019-06-19 09:06:52 +08:00
|
|
|
return &AVLTreeNode{
|
|
|
|
Key: node.Key,
|
|
|
|
Value: node.Value,
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
// Right returns the maximum element of the AVL tree
|
|
|
|
// or nil if the tree is empty.
|
|
|
|
func (tree *AVLTree) Right() *AVLTreeNode {
|
|
|
|
tree.mu.RLock()
|
|
|
|
defer tree.mu.RUnlock()
|
|
|
|
node := tree.bottom(1)
|
|
|
|
if tree.mu.IsSafe() {
|
2019-06-19 09:06:52 +08:00
|
|
|
return &AVLTreeNode{
|
|
|
|
Key: node.Key,
|
|
|
|
Value: node.Value,
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:53:36 +08:00
|
|
|
// Floor Finds floor node of the input key, return the floor node or nil if no floor node is found.
|
2019-05-10 22:29:06 +08:00
|
|
|
// Second return parameter 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 (tree *AVLTree) Floor(key interface{}) (floor *AVLTreeNode, found bool) {
|
|
|
|
tree.mu.RLock()
|
|
|
|
defer tree.mu.RUnlock()
|
|
|
|
n := tree.root
|
|
|
|
for n != nil {
|
2020-04-07 20:06:26 +08:00
|
|
|
c := tree.getComparator()(key, n.Key)
|
2019-05-10 22:29:06 +08:00
|
|
|
switch {
|
2019-06-19 09:06:52 +08:00
|
|
|
case c == 0:
|
|
|
|
return n, true
|
|
|
|
case c < 0:
|
|
|
|
n = n.children[0]
|
|
|
|
case c > 0:
|
|
|
|
floor, found = n, true
|
|
|
|
n = n.children[1]
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:53:36 +08:00
|
|
|
// Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling node is found.
|
2019-05-10 22:29:06 +08:00
|
|
|
// Second return parameter 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.
|
2019-06-15 16:53:36 +08:00
|
|
|
func (tree *AVLTree) Ceiling(key interface{}) (ceiling *AVLTreeNode, found bool) {
|
2019-05-10 22:29:06 +08:00
|
|
|
tree.mu.RLock()
|
|
|
|
defer tree.mu.RUnlock()
|
|
|
|
n := tree.root
|
|
|
|
for n != nil {
|
2020-04-07 20:06:26 +08:00
|
|
|
c := tree.getComparator()(key, n.Key)
|
2019-05-10 22:29:06 +08:00
|
|
|
switch {
|
2019-06-19 09:06:52 +08:00
|
|
|
case c == 0:
|
|
|
|
return n, true
|
|
|
|
case c > 0:
|
|
|
|
n = n.children[1]
|
|
|
|
case c < 0:
|
|
|
|
ceiling, found = n, true
|
|
|
|
n = n.children[0]
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear removes all nodes from the tree.
|
|
|
|
func (tree *AVLTree) Clear() {
|
|
|
|
tree.mu.Lock()
|
|
|
|
defer tree.mu.Unlock()
|
|
|
|
tree.root = nil
|
|
|
|
tree.size = 0
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Replace the data of the tree with given `data`.
|
2019-11-04 21:26:16 +08:00
|
|
|
func (tree *AVLTree) Replace(data map[interface{}]interface{}) {
|
|
|
|
tree.mu.Lock()
|
|
|
|
defer tree.mu.Unlock()
|
|
|
|
tree.root = nil
|
|
|
|
tree.size = 0
|
|
|
|
for key, value := range data {
|
|
|
|
tree.put(key, value, nil, &tree.root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 22:29:06 +08:00
|
|
|
// String returns a string representation of container
|
|
|
|
func (tree *AVLTree) String() string {
|
2022-03-21 22:04:15 +08:00
|
|
|
if tree == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2019-05-10 22:29:06 +08:00
|
|
|
tree.mu.RLock()
|
|
|
|
defer tree.mu.RUnlock()
|
2019-08-13 13:45:01 +08:00
|
|
|
str := ""
|
2019-05-10 22:29:06 +08:00
|
|
|
if tree.size != 0 {
|
|
|
|
output(tree.root, "", true, &str)
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print prints the tree to stdout.
|
|
|
|
func (tree *AVLTree) Print() {
|
|
|
|
fmt.Println(tree.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map returns all key-value items as map.
|
|
|
|
func (tree *AVLTree) Map() map[interface{}]interface{} {
|
|
|
|
m := make(map[interface{}]interface{}, tree.Size())
|
|
|
|
tree.IteratorAsc(func(key, value interface{}) bool {
|
|
|
|
m[key] = value
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-09-19 19:44:46 +08:00
|
|
|
// MapStrAny returns all key-value items as map[string]interface{}.
|
|
|
|
func (tree *AVLTree) MapStrAny() map[string]interface{} {
|
|
|
|
m := make(map[string]interface{}, tree.Size())
|
|
|
|
tree.IteratorAsc(func(key, value interface{}) bool {
|
|
|
|
m[gconv.String(key)] = value
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-05-10 22:29:06 +08:00
|
|
|
// 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.
|
|
|
|
//
|
2021-09-16 20:57:59 +08:00
|
|
|
// If the type of value is different with key, you pass the new `comparator`.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (tree *AVLTree) Flip(comparator ...func(v1, v2 interface{}) int) {
|
2019-05-10 22:29:06 +08:00
|
|
|
t := (*AVLTree)(nil)
|
|
|
|
if len(comparator) > 0 {
|
2020-12-02 21:38:29 +08:00
|
|
|
t = NewAVLTree(comparator[0], tree.mu.IsSafe())
|
2019-05-10 22:29:06 +08:00
|
|
|
} else {
|
2020-12-02 21:38:29 +08:00
|
|
|
t = NewAVLTree(tree.comparator, tree.mu.IsSafe())
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
tree.IteratorAsc(func(key, value interface{}) bool {
|
2019-05-12 22:56:13 +08:00
|
|
|
t.put(value, key, nil, &t.root)
|
2019-05-10 22:29:06 +08:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
tree.mu.Lock()
|
|
|
|
tree.root = t.root
|
|
|
|
tree.size = t.size
|
|
|
|
tree.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-05-11 20:47:25 +08:00
|
|
|
// Iterator is alias of IteratorAsc.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (tree *AVLTree) Iterator(f func(key, value interface{}) bool) {
|
2019-05-11 20:47:25 +08:00
|
|
|
tree.IteratorAsc(f)
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:45:01 +08:00
|
|
|
// IteratorFrom is alias of IteratorAscFrom.
|
2019-08-14 22:03:52 +08:00
|
|
|
func (tree *AVLTree) IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
|
|
|
tree.IteratorAscFrom(key, match, f)
|
2019-08-13 13:45:01 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// IteratorAsc iterates the tree readonly in ascending order with given callback function `f`.
|
|
|
|
// If `f` returns true, then it continues iterating; or false to stop.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (tree *AVLTree) IteratorAsc(f func(key, value interface{}) bool) {
|
2019-05-10 22:29:06 +08:00
|
|
|
tree.mu.RLock()
|
|
|
|
defer tree.mu.RUnlock()
|
2019-08-13 13:45:01 +08:00
|
|
|
tree.doIteratorAsc(tree.bottom(0), f)
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The parameter `key` specifies the start entry for iterating. The `match` specifies whether
|
2021-09-16 20:57:59 +08:00
|
|
|
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
|
|
|
// If `f` returns true, then it continues iterating; or false to stop.
|
2019-08-14 22:03:52 +08:00
|
|
|
func (tree *AVLTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
2019-08-13 13:45:01 +08:00
|
|
|
tree.mu.RLock()
|
|
|
|
defer tree.mu.RUnlock()
|
2019-08-14 22:03:52 +08:00
|
|
|
node, found := tree.doSearch(key)
|
|
|
|
if match {
|
|
|
|
if found {
|
|
|
|
tree.doIteratorAsc(node, f)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tree.doIteratorAsc(node, f)
|
|
|
|
}
|
2019-08-13 13:45:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tree *AVLTree) doIteratorAsc(node *AVLTreeNode, f func(key, value interface{}) bool) {
|
2019-05-10 22:29:06 +08:00
|
|
|
for node != nil {
|
|
|
|
if !f(node.Key, node.Value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
node = node.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
|
|
|
|
// If `f` returns true, then it continues iterating; or false to stop.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (tree *AVLTree) IteratorDesc(f func(key, value interface{}) bool) {
|
2019-05-10 22:29:06 +08:00
|
|
|
tree.mu.RLock()
|
|
|
|
defer tree.mu.RUnlock()
|
2019-08-13 13:45:01 +08:00
|
|
|
tree.doIteratorDesc(tree.bottom(1), f)
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The parameter `key` specifies the start entry for iterating. The `match` specifies whether
|
2021-09-16 20:57:59 +08:00
|
|
|
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
|
|
|
// If `f` returns true, then it continues iterating; or false to stop.
|
2019-08-14 22:03:52 +08:00
|
|
|
func (tree *AVLTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
2019-08-13 13:45:01 +08:00
|
|
|
tree.mu.RLock()
|
|
|
|
defer tree.mu.RUnlock()
|
2019-08-14 22:03:52 +08:00
|
|
|
node, found := tree.doSearch(key)
|
|
|
|
if match {
|
|
|
|
if found {
|
|
|
|
tree.doIteratorDesc(node, f)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tree.doIteratorDesc(node, f)
|
|
|
|
}
|
2019-08-13 13:45:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tree *AVLTree) doIteratorDesc(node *AVLTreeNode, f func(key, value interface{}) bool) {
|
2019-05-10 22:29:06 +08:00
|
|
|
for node != nil {
|
|
|
|
if !f(node.Key, node.Value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
node = node.Prev()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tree *AVLTree) put(key interface{}, value interface{}, p *AVLTreeNode, qp **AVLTreeNode) bool {
|
|
|
|
q := *qp
|
|
|
|
if q == nil {
|
|
|
|
tree.size++
|
|
|
|
*qp = &AVLTreeNode{Key: key, Value: value, parent: p}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-04-07 20:06:26 +08:00
|
|
|
c := tree.getComparator()(key, q.Key)
|
2019-05-10 22:29:06 +08:00
|
|
|
if c == 0 {
|
|
|
|
q.Key = key
|
|
|
|
q.Value = value
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if c < 0 {
|
|
|
|
c = -1
|
|
|
|
} else {
|
|
|
|
c = 1
|
|
|
|
}
|
|
|
|
a := (c + 1) / 2
|
2019-05-12 22:56:13 +08:00
|
|
|
if tree.put(key, value, q, &q.children[a]) {
|
2019-05-10 22:29:06 +08:00
|
|
|
return putFix(int8(c), qp)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:56:13 +08:00
|
|
|
func (tree *AVLTree) remove(key interface{}, qp **AVLTreeNode) (value interface{}, fix bool) {
|
2019-05-10 22:29:06 +08:00
|
|
|
q := *qp
|
|
|
|
if q == nil {
|
2019-05-12 22:56:13 +08:00
|
|
|
return nil, false
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 20:06:26 +08:00
|
|
|
c := tree.getComparator()(key, q.Key)
|
2019-05-10 22:29:06 +08:00
|
|
|
if c == 0 {
|
|
|
|
tree.size--
|
2019-05-12 22:56:13 +08:00
|
|
|
value = q.Value
|
2019-06-19 09:06:52 +08:00
|
|
|
fix = true
|
2019-05-10 22:29:06 +08:00
|
|
|
if q.children[1] == nil {
|
|
|
|
if q.children[0] != nil {
|
|
|
|
q.children[0].parent = q.parent
|
|
|
|
}
|
|
|
|
*qp = q.children[0]
|
2019-05-12 22:56:13 +08:00
|
|
|
return
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
2019-05-12 22:56:13 +08:00
|
|
|
if removeMin(&q.children[1], &q.Key, &q.Value) {
|
|
|
|
return value, removeFix(-1, qp)
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
2019-05-12 22:56:13 +08:00
|
|
|
return
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if c < 0 {
|
|
|
|
c = -1
|
|
|
|
} else {
|
|
|
|
c = 1
|
|
|
|
}
|
|
|
|
a := (c + 1) / 2
|
2019-05-12 22:56:13 +08:00
|
|
|
value, fix = tree.remove(key, &q.children[a])
|
2019-05-10 22:29:06 +08:00
|
|
|
if fix {
|
2019-05-12 22:56:13 +08:00
|
|
|
return value, removeFix(int8(-c), qp)
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
2019-06-15 16:53:36 +08:00
|
|
|
return value, false
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeMin(qp **AVLTreeNode, minKey *interface{}, minVal *interface{}) bool {
|
|
|
|
q := *qp
|
|
|
|
if q.children[0] == nil {
|
|
|
|
*minKey = q.Key
|
|
|
|
*minVal = q.Value
|
|
|
|
if q.children[1] != nil {
|
|
|
|
q.children[1].parent = q.parent
|
|
|
|
}
|
|
|
|
*qp = q.children[1]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
fix := removeMin(&q.children[0], minKey, minVal)
|
|
|
|
if fix {
|
|
|
|
return removeFix(1, qp)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func putFix(c int8, t **AVLTreeNode) bool {
|
|
|
|
s := *t
|
|
|
|
if s.b == 0 {
|
|
|
|
s.b = c
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.b == -c {
|
|
|
|
s.b = 0
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.children[(c+1)/2].b == c {
|
|
|
|
s = singleRotate(c, s)
|
|
|
|
} else {
|
|
|
|
s = doubleRotate(c, s)
|
|
|
|
}
|
|
|
|
*t = s
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFix(c int8, t **AVLTreeNode) bool {
|
|
|
|
s := *t
|
|
|
|
if s.b == 0 {
|
|
|
|
s.b = c
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.b == -c {
|
|
|
|
s.b = 0
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
a := (c + 1) / 2
|
|
|
|
if s.children[a].b == 0 {
|
2019-06-19 09:06:52 +08:00
|
|
|
s = rotate(c, s)
|
2019-05-10 22:29:06 +08:00
|
|
|
s.b = -c
|
2019-06-19 09:06:52 +08:00
|
|
|
*t = s
|
2019-05-10 22:29:06 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.children[a].b == c {
|
|
|
|
s = singleRotate(c, s)
|
|
|
|
} else {
|
|
|
|
s = doubleRotate(c, s)
|
|
|
|
}
|
|
|
|
*t = s
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func singleRotate(c int8, s *AVLTreeNode) *AVLTreeNode {
|
|
|
|
s.b = 0
|
|
|
|
s = rotate(c, s)
|
|
|
|
s.b = 0
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func doubleRotate(c int8, s *AVLTreeNode) *AVLTreeNode {
|
|
|
|
a := (c + 1) / 2
|
|
|
|
r := s.children[a]
|
|
|
|
s.children[a] = rotate(-c, s.children[a])
|
|
|
|
p := rotate(c, s)
|
|
|
|
|
|
|
|
switch {
|
2019-06-19 09:06:52 +08:00
|
|
|
default:
|
|
|
|
s.b = 0
|
|
|
|
r.b = 0
|
|
|
|
case p.b == c:
|
|
|
|
s.b = -c
|
|
|
|
r.b = 0
|
|
|
|
case p.b == -c:
|
|
|
|
s.b = 0
|
|
|
|
r.b = c
|
2019-05-10 22:29:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p.b = 0
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func rotate(c int8, s *AVLTreeNode) *AVLTreeNode {
|
|
|
|
a := (c + 1) / 2
|
|
|
|
r := s.children[a]
|
|
|
|
s.children[a] = r.children[a^1]
|
|
|
|
if s.children[a] != nil {
|
|
|
|
s.children[a].parent = s
|
|
|
|
}
|
|
|
|
r.children[a^1] = s
|
|
|
|
r.parent = s.parent
|
|
|
|
s.parent = r
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tree *AVLTree) bottom(d int) *AVLTreeNode {
|
|
|
|
n := tree.root
|
|
|
|
if n == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for c := n.children[d]; c != nil; c = n.children[d] {
|
|
|
|
n = c
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prev returns the previous element in an inorder
|
|
|
|
// walk of the AVL tree.
|
|
|
|
func (node *AVLTreeNode) Prev() *AVLTreeNode {
|
|
|
|
return node.walk1(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next element in an inorder
|
|
|
|
// walk of the AVL tree.
|
|
|
|
func (node *AVLTreeNode) Next() *AVLTreeNode {
|
|
|
|
return node.walk1(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *AVLTreeNode) walk1(a int) *AVLTreeNode {
|
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
n := node
|
|
|
|
if n.children[a] != nil {
|
|
|
|
n = n.children[a]
|
|
|
|
for n.children[a^1] != nil {
|
|
|
|
n = n.children[a^1]
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
p := n.parent
|
|
|
|
for p != nil && p.children[a] == n {
|
|
|
|
n = p
|
|
|
|
p = p.parent
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func output(node *AVLTreeNode, prefix string, isTail bool, str *string) {
|
|
|
|
if node.children[1] != nil {
|
|
|
|
newPrefix := prefix
|
|
|
|
if isTail {
|
|
|
|
newPrefix += "│ "
|
|
|
|
} else {
|
|
|
|
newPrefix += " "
|
|
|
|
}
|
|
|
|
output(node.children[1], newPrefix, false, str)
|
|
|
|
}
|
|
|
|
*str += prefix
|
|
|
|
if isTail {
|
|
|
|
*str += "└── "
|
|
|
|
} else {
|
|
|
|
*str += "┌── "
|
|
|
|
}
|
2019-05-13 22:26:39 +08:00
|
|
|
*str += fmt.Sprintf("%v\n", node.Key)
|
2019-05-10 22:29:06 +08:00
|
|
|
if node.children[0] != nil {
|
|
|
|
newPrefix := prefix
|
|
|
|
if isTail {
|
|
|
|
newPrefix += " "
|
|
|
|
} else {
|
|
|
|
newPrefix += "│ "
|
|
|
|
}
|
|
|
|
output(node.children[0], newPrefix, true, str)
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-07-13 17:48:16 +08:00
|
|
|
|
|
|
|
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
2022-03-07 17:39:41 +08:00
|
|
|
func (tree AVLTree) MarshalJSON() (jsonBytes []byte, err error) {
|
|
|
|
if tree.root == nil {
|
|
|
|
return []byte("null"), nil
|
|
|
|
}
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
buffer.WriteByte('{')
|
|
|
|
tree.Iterator(func(key, value interface{}) bool {
|
|
|
|
valueBytes, valueJsonErr := json.Marshal(value)
|
|
|
|
if valueJsonErr != nil {
|
|
|
|
err = valueJsonErr
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if buffer.Len() > 1 {
|
|
|
|
buffer.WriteByte(',')
|
|
|
|
}
|
|
|
|
buffer.WriteString(fmt.Sprintf(`"%v":%s`, key, valueBytes))
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
buffer.WriteByte('}')
|
|
|
|
return buffer.Bytes(), nil
|
2019-07-13 17:48:16 +08:00
|
|
|
}
|
2020-04-07 20:06:26 +08:00
|
|
|
|
|
|
|
// getComparator returns the comparator if it's previously set,
|
|
|
|
// or else it panics.
|
|
|
|
func (tree *AVLTree) getComparator() func(a, b interface{}) int {
|
|
|
|
if tree.comparator == nil {
|
|
|
|
panic("comparator is missing for tree")
|
|
|
|
}
|
|
|
|
return tree.comparator
|
|
|
|
}
|