以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档:https://pkg.go.dev/github.com/gogf/gf/v2/container/gtree
NewBTree
使用m
(最大子节点数量)和自定义的比较方法创建BTree
。参数safe
用于指定是否使用并发安全的tree
,默认情况下为false
。 m
必须大于等于3,否则会panic
。 NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTree
func ExampleNewBTree() {
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]
}
NewBTreeFrom
使用m
(最大子节点数量),自定义的比较方法和类型为map[interface{}]interface{}
的data
创建BTree
。参数safe
用于指定是否使用并发安全的tree
,默认情况下为false
。 m
必须大于等于3,否则会panic
。 NewBTreeFrom(m int, comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *BTree
func ExampleNewBTreeFrom() {
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]
}
clone
返回一个值为当前tree
值的副本的新BTree
。 Clone() *BTree
func ExampleBTree_Clone() {
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
}
Set
为tree
设置key/value
。 Set(key interface{}, value interface{})
func ExampleBTree_Set() {
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
}
Sets
为tree
批量设置key/value
。 Sets(data map[interface{}]interface{})
func ExampleBTree_Sets() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
tree.Sets(map[interface{}]interface{}{
"key1": "val1",
"key2": "val2",
})
fmt.Println(tree.Map())
fmt.Println(tree.Size())
// Output:
// map[key1:val1 key2:val2]
// 2
}
Get
返回参数key
对应的值value
,如key
不存在,则返回Nil
。 Get(key interface{}) (value interface{})
func ExampleBTree_Get() {
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>
}
GetOrSet
如key
存在,则返回value
,如key
不存在,使用key
和value
设置键值,然后返回该值。 GetOrSet(key interface{}, value interface{}) interface{}
func ExampleBTree_GetOrSet() {
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
}
GetOrSetFunc
如key
存在,则返回value
,如key
不存在,使用key
和func f
的返回值设置键值,然后返回该值。 GetOrSetFunc(key interface{}, f func() interface{}) interface{}
func ExampleBTree_GetOrSetFunc() {
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() interface{} {
return "newVal1"
}))
fmt.Println(tree.GetOrSetFunc("key6", func() interface{} {
return "val6"
}))
// Output:
// val1
// val6
}
GetOrSetFunc
如key
存在,则返回value
,如key
不存在,使用key
和func f
的返回值设置键值,然后返回该值。 GetOrSetFuncLock
与GetOrSetFunc
函数的不同之处在于它在写锁中执行函数f
。 GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}
func ExampleBTree_GetOrSetFuncLock() {
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() interface{} {
return "newVal1"
}))
fmt.Println(tree.GetOrSetFuncLock("key6", func() interface{} {
return "val6"
}))
// Output:
// val1
// val6
}
GetVar
根据键名key
查询并返回对应的键值,类型为*gvar.Var
。 gvar.Var
是非并发安全的。 GetVar(key interface{}) *gvar.Var
func ExampleBTree_GetVar() {
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
}
GetVarOrSet
使用GetOrSet
的结果返回,类型为*gvar.Var
。 gvar.Var
是非并发安全的。 GetVarOrSet(key interface{}, value interface{}) *gvar.Var
func ExampleBTree_GetVarOrSet() {
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
}
GetVarOrSetFunc
使用GetOrSetFunc
的结果返回,类型为*gvar.Var
。 gvar.Var
是非并发安全的。 GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var
func ExampleBTree_GetVarOrSetFunc() {
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() interface{} {
return "newVal1"
}))
fmt.Println(tree.GetVarOrSetFunc("key6", func() interface{} {
return "val6"
}))
// Output:
// val1
// val6
}
GetVarOrSetFuncLock
使用GetOrSetFuncLock
的结果返回,类型为*gvar.Var
。 gvar.Var
是非并发安全的。 GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var
func ExampleBTree_GetVarOrSetFuncLock() {
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() interface{} {
return "newVal1"
}))
fmt.Println(tree.GetVarOrSetFuncLock("key6", func() interface{} {
return "val6"
}))
// Output:
// val1
// val6
}
key
不存在,则SetIfNotExist
为map
设置值键值对key/value
,并且返回true
。如果key
存在,则返回false
,而value
将被忽略。 SetIfNotExist(key interface{}, value interface{}) bool
func ExampleBTree_SetIfNotExist() {
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
}
SetIfNotExistFunc(key interface{}, f func() interface{}) bool
func ExampleBTree_SetIfNotExistFunc() {
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() interface{} {
return "newVal1"
}))
fmt.Println(tree.SetIfNotExistFunc("key6", func() interface{} {
return "val6"
}))
// Output:
// false
// true
}
SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool
func ExampleBTree_SetIfNotExistFuncLock() {
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() interface{} {
return "newVal1"
}))
fmt.Println(tree.SetIfNotExistFuncLock("key6", func() interface{} {
return "val6"
}))
// Output:
// false
// true
}
Contains
检查key
在tree
中是否存在。如果key
存在,则返回true
,否则返回false
。 Contains(key interface{}) bool
func ExampleBTree_Contains() {
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
}
key
从tree
中删除value
,并返回此删除的value
。 Remove(key interface{}) (value interface{})
func ExampleBTree_Remove() {
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]
}
Removes
按给定的key
批量删除tree
的value
。 Removes(keys []interface{})
func ExampleBTree_Removes() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 6; i++ {
tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
}
removeKeys := make([]interface{}, 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]
}
IsEmpty
检查tree
是否为空。如果tree
为空,则返回true
,否则返回false
。 IsEmpty() bool
func ExampleBTree_IsEmpty() {
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
}
Size
返回tree
的大小。 Size() int
func ExampleBTree_Size() {
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
}
Keys
按升序返回所有的key
。 Keys() []interface{}
func ExampleBTree_Keys() {
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]
}
Values
按key
的升序返回所有的value
。 Values() []interface{}
func ExampleBTree_Values() {
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]
}
Map
以map
的形式返回所有的key/value
。 Map() map[interface{}]interface{}
func ExampleBTree_Map() {
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]
}
MapStrAny
以map[string]interface{}
的形式返回所有的key/value
。 MapStrAny() map[string]interface{}
func ExampleBTree_MapStrAny() {
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]
}
Clear
删除tree
的所有数据。 Clear()
func ExampleBTree_Clear() {
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
}
Replace
用类型为map[interface{}]interface{}
的data
替换tree
的key/value
。 Replace(data map[interface{}]interface{})
func ExampleBTree_Replace() {
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[interface{}]interface{}{
"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]
}
Height
返回tree
的高度。 Height() int
func ExampleBTree_Height() {
tree := gtree.NewBTree(3, gutil.ComparatorInt)
for i := 0; i < 100; i++ {
tree.Set(i, i)
}
fmt.Println(tree.Height())
// Output:
// 6
}
Left
返回最左边(最小)的类型为*BTreeEntry
的node
,如果tree
是空的,则返回 nil
。 Left() *BTreeEntry
func ExampleBTree_Left() {
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>
}
Right
返回最右边(最大)的类型为*BTreeEntry
的node
,如果tree
是空的,则返回 nil
。 Right() *BTreeEntry
func ExampleBTree_Right() {
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>
}
String
返回tree
的node
显示(用于调试)。 String() string
func ExampleBTree_String() {
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
}
Search
使用参数key
搜索tree
。如果找到key
,则返回其对应的键值,并且返回参数found
为true
,否则为false
。 Search(key interface{}) (value interface{}, found bool)
func ExampleBTree_Search() {
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
}
Print
将tree
打印到标准输出。 Print()
func ExampleBTree_Print() {
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
}
Iterator
等同于 IteratorAsc
。 Iterator(f func(key, value interface{}) bool)
func ExampleBTree_Iterator() {
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 interface{}) bool {
totalKey += key.(int)
totalValue += value.(int)
return totalValue < 20
})
fmt.Println("totalKey:", totalKey)
fmt.Println("totalValue:", totalValue)
// Output:
// totalKey: 3
// totalValue: 27
}
IteratorFrom
等同于 IteratorAscFrom
。 IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool)
func ExampleBTree_IteratorFrom() {
m := make(map[interface{}]interface{})
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
tree.IteratorFrom(1, true, func(key, value interface{}) 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
}
IteratorAsc
使用自定义回调函数f
以只读方式按升序迭代tree
。如果f
返回true
,则继续迭代,返回false
则停止。 IteratorAsc(f func(key, value interface{}) bool)
func ExampleBTree_IteratorAsc() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}
tree.IteratorAsc(func(key, value interface{}) 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
}
IteratorAscFrom
使用自定义回调函数f以只读方式按升序迭代tree
。参数 key
指定从哪个key
开始迭代。参数match
为true
时,从key
完全匹配时开始迭代,否则使用索引搜索迭代。如果f
返回true
,则继续迭代,返回false
则停止。 IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool)
func ExampleBTree_IteratorAscFrom_Normal() {
m := make(map[interface{}]interface{})
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
tree.IteratorAscFrom(1, true, func(key, value interface{}) 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 ExampleBTree_IteratorAscFrom_NoExistKey() {
m := make(map[interface{}]interface{})
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
tree.IteratorAscFrom(0, true, func(key, value interface{}) bool {
fmt.Println("key:", key, ", value:", value)
return true
})
// Output:
}
func ExampleBTree_IteratorAscFrom_NoExistKeyAndMatchFalse() {
m := make(map[interface{}]interface{})
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
tree.IteratorAscFrom(0, false, func(key, value interface{}) 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
}
IteratorDesc
使用自定义回调函数f
以只读方式按降序迭代tree
。如果f
返回true
,则继续迭代,返回false
则停止。 IteratorDesc(f func(key, value interface{}) bool)
func ExampleBTree_IteratorDesc() {
tree := gtree.NewBTree(3, gutil.ComparatorString)
for i := 0; i < 10; i++ {
tree.Set(i, 10-i)
}
tree.IteratorDesc(func(key, value interface{}) 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
}
IteratorDescFrom
使用自定义回调函数f
以只读方式按降序迭代tree
。参数 key
指定从哪个key
开始迭代。参数match
为true
时,从key
完全匹配时开始迭代,否则使用索引搜索迭代。如果f
返回true
,则继续迭代,返回false
则停止。 IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool)
func ExampleBTree_IteratorDescFrom() {
m := make(map[interface{}]interface{})
for i := 1; i <= 5; i++ {
m[i] = i * 10
}
tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
tree.IteratorDescFrom(5, true, func(key, value interface{}) 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
}
MarshalJSON
实现json.Marshal
的接口。 MarshalJSON() ([]byte, error)
func ExampleBTree_MarshalJSON() {
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"}
}