NewBTree
NewBTreeFrom
Clone
Set
Sets
Get
GetOrSet
GetOrSetFunc
GetOrSetFuncLock
- GetVar
GetVarOrSet
- GetVarOrSetFunc
GetVarOrSetFuncLock
SetIfNotExist
SetIfNotExistFunc
SetIfNotExistFuncLock
Contains
Remove
Removes
- IsEmpty
Size
Keys
Values
Map
MapStrAny
Clear
Replace
Height
Left
Right
String
Search
Print
Iterator
IteratorFrom
IteratorAsc
IteratorAscFrom
IteratorDesc
IteratorDescFrom
MarshalJson
tip
The following is a list of commonly used methods; documentation updates may lag behind new features in the code. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/container/gtree
NewBTree
Description:
NewBTree
creates aBTree
usingm
(maximum number of child nodes) and a custom comparator. Thesafe
parameter specifies whether to use a concurrency-safetree
, with a default value offalse
.Note: The
m
parameter must be greater than or equal to3
, otherwise it willpanic
.Format:
NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTree
- Example:
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
Description:
NewBTreeFrom
creates aBTree
usingm
(maximum number of child nodes), a custom comparator, and data of typemap[interface{}]interface{}
. Thesafe
parameter specifies whether to use a concurrency-safetree
, with a default value offalse
.Note: The
m
parameter must be greater than or equal to3
, otherwise it willpanic
.Format:
NewBTreeFrom(m int, comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *BTree
- Example:
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
Description:
Clone
returns a newBTree
, which is a copy of the currenttree
.Format:
Clone() *BTree
- Example:
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
Description:
Set
sets thekey/value
for thetree
.Format:
Set(key interface{}, value interface{})
- Example:
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
Description:
Sets
setskey/value
for thetree
in batches.Format:
Sets(data map[interface{}]interface{})
- Example:
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
Description:
Get
returns the valuevalue
corresponding to the parameterkey
. Ifkey
does not exist, it returnsNil
.Format:
Get(key interface{}) (value interface{})
- Example:
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
Description:
GetOrSet
returnsvalue
ifkey
exists; ifkey
does not exist, it sets the key-value withkey
andvalue
, then returns the value.Format:
GetOrSet(key interface{}, value interface{}) interface{}
- Example:
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
Description:
GetOrSetFunc
returnsvalue
ifkey
exists; ifkey
does not exist, it sets the key-value withkey
and the return value offunc f
, then returns the value.Format:
GetOrSetFunc(key interface{}, f func() interface{}) interface{}
- Example:
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
}
GetOrSetFuncLock
Description:
GetOrSetFunc
returnsvalue
ifkey
exists; ifkey
does not exist, it sets the key-value withkey
and the return value offunc f
, then returns the value.Note: The difference between
GetOrSetFuncLock
andGetOrSetFunc
is that it executes the functionf
in a write lock.Format:
GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}
- Example:
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
Description:
GetVar
searches and returns the key value corresponding to the key namekey
, the type is*gvar.Var
.Note: The returned
gvar.Var
is not concurrent safe.Format:
GetVar(key interface{}) *gvar.Var
- Example:
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
Description:
GetVarOrSet
returns the result ofGetOrSet
, the type is*gvar.Var
.Note: The returned
gvar.Var
is not concurrent safe.Format:
GetVarOrSet(key interface{}, value interface{}) *gvar.Var
- Example:
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
- Description:
GetVarOrSetFunc
returns the result ofGetOrSetFunc
, the type is*gvar.Var
. - Note: The returned
gvar.Var
is not concurrent safe. - Format:
GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var
- Example:
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
Description:
GetVarOrSetFuncLock
returns the result ofGetOrSetFuncLock
, the type is*gvar.Var
.Note: The returned
gvar.Var
is not concurrent safe.Format:
GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var
- Example:
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
}
SetIfNotExist
Description: If
key
does not exist,SetIfNotExist
sets the key-value pairkey/value
for the map and returnstrue
. Ifkey
exists, it returnsfalse
, andvalue
will be ignored.Format:
SetIfNotExist(key interface{}, value interface{}) bool
- Example:
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
Description: If
key
does not exist,SetIfNotExistFunc
sets the value to the return value of the functionf
and returnstrue
. Ifkey
exists, it returnsfalse
, andvalue
will be ignored.Format:
SetIfNotExistFunc(key interface{}, f func() interface{}) bool
- Example:
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
Description: If
key
does not exist,SetIfNotExistFunc
sets the value to the return value offunc c
, then returnstrue
. Ifkey
exists, it returnsfalse
, andvalue
will be ignored.The difference between
SetIfNotExistFuncLock
andSetIfNotExistFunc
is that it executes the functionf
withinmutex.Lock
.Format:
SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool
- Example:
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
Description:
Contains
checks ifkey
exists in thetree
. Ifkey
exists, it returnstrue
; otherwise, it returnsfalse
.Format:
Contains(key interface{}) bool
- Example:
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
}
Remove
Description: Delete the
value
fromtree
according to the givenkey
and return this deletedvalue
.Format:
Remove(key interface{}) (value interface{})
- Example:
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
Description:
Removes
deletesvalue
oftree
in batches according to the givenkey
.Format:
Removes(keys []interface{})
- Example:
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
Description:
IsEmpty
checks iftree
is empty. Iftree
is empty, it returnstrue
; otherwise, it returnsfalse
.Format:
IsEmpty() bool
- Example:
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
Description:
Size
returns the size of thetree
.Format:
Size() int
- Example:
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
Description:
Keys
returns allkey
s in ascending order.Format:
Keys() []interface{}
- Example:
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
Description:
Values
returns allvalue
s in ascending order ofkey
.Format:
Values() []interface{}
- Example:
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
Description:
Map
returns allkey/value
pairs in the form of amap
.Format:
Map() map[interface{}]interface{}
- Example:
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
Description:
MapStrAny
returns allkey/value
pairs in the form ofmap[string]interface{}
.Format:
MapStrAny() map[string]interface{}
- Example:
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
Description:
Clear
deletes all data in thetree
.Format:
Clear()
- Example:
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
Description:
Replace
replaces thekey/value
in thetree
with data of typemap[interface{}]interface{}
.Format:
Replace(data map[interface{}]interface{})
- Example:
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
Description:
Height
returns the height of thetree
.Format:
Height() int
- Example:
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
Description:
Left
returns the leftmost (smallest)node
of type*BTreeEntry
, ornil
if thetree
is empty.Format:
Left() *BTreeEntry
- Example:
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
Description:
Right
returns the rightmost (largest)node
of type*BTreeEntry
, ornil
if thetree
is empty.Format:
Right() *BTreeEntry
- Example:
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
Description:
String
returns a display (for debugging) of thenode
in thetree
.Format:
String() string
- Example:
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
Description:
Search
searches thetree
using the parameterkey
. If thekey
is found, it returns its corresponding key-value and returns the parameterfound
astrue
, otherwisefalse
.Format:
Search(key interface{}) (value interface{}, found bool)
- Example:
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
Description:
Print
prints thetree
to standard output.Format:
Print()
- Example:
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
Description:
Iterator
is equivalent toIteratorAsc
.Format:
Iterator(f func(key, value interface{}) bool)
- Example:
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
Description:
IteratorFrom
is equivalent toIteratorAscFrom
.Format:
IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool)
- Example:
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
Description:
IteratorAsc
iterates over thetree
in ascending order using a custom callback functionf
in read-only mode. Iff
returnstrue
, it continues iterating; if it returnsfalse
, it stops.Format:
IteratorAsc(f func(key, value interface{}) bool)
- Example:
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
Description:
IteratorAscFrom
iterates over thetree
in ascending order using a custom callback functionf
in read-only mode. The parameterkey
specifies from whichkey
to start iterating. Whenmatch
istrue
, iteration starts from the complete match ofkey
; otherwise, iteration uses index searching. Iff
returnstrue
, it continues iterating; if it returnsfalse
, it stops.Format:
IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool)
- Example:
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
Description:
IteratorDesc
iterates over thetree
in descending order using a custom callback functionf
in read-only mode. Iff
returnstrue
, it continues iterating; if it returnsfalse
, it stops.Format:
IteratorDesc(f func(key, value interface{}) bool)
- Example:
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
Description:
IteratorDescFrom
iterates over thetree
in descending order using a custom callback functionf
in read-only mode. The parameterkey
specifies from whichkey
to start iterating. Whenmatch
istrue
, iteration starts from the complete match ofkey
; otherwise, iteration uses index searching. Iff
returnstrue
, it continues iterating; if it returnsfalse
, it stops.Format:
IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool)
- Example:
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
Description:
MarshalJSON
implements thejson.Marshal
interface.Format:
MarshalJSON() ([]byte, error)
- Example:
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"}
}