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
For generic versions of tree containers (such as AVLKVTree[K, V], BKVTree[K, V], RedBlackKVTree[K, V]), the framework provides NilChecker functions to customize "which values should be considered nil" to solve typed nil determination issues when containing pointers, interfaces, and other types. Lazy loading or conditional write methods (such as GetOrSet*, SetIfNotExist* series) will call this function before actually writing, and usually won't write the key-value pair when determined as nil; when NilChecker is not set, behavior remains consistent with historical versions.
Generic NilChecker Related Methods
-
Description: Generic versions of various tree containers provide constructors with
NilCheckerand runtime registration methods, including (mainly illustrative):NewAVLKVTreeWithChecker(comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *AVLKVTree[K, V]NewAVLKVTreeWithCheckerFrom(comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *AVLKVTree[K, V]NewBKVTreeWithChecker(m int, comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *BKVTree[K, V]NewBKVTreeWithCheckerFrom(m int, comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *BKVTree[K, V]NewRedBlackKVTreeWithChecker(comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *RedBlackKVTree[K, V]NewRedBlackKVTreeWithCheckerFrom(comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *RedBlackKVTree[K, V]func (tree *AVLKVTree[K, V]) RegisterNilChecker(checker NilChecker[V])func (tree *BKVTree[K, V]) RegisterNilChecker(checker NilChecker[V])func (tree *RedBlackKVTree[K, V]) RegisterNilChecker(checker NilChecker[V])
-
Purpose: Through
NilChecker, customize "which values are considered nil". Lazy loading/conditional write methods (such asGetOrSet*,SetIfNotExist*series) will call this function before actually writing, and usually won't write the key-value pair when determined as nil. -
Example (using RedBlackKVTree as example):
func ExampleRedBlackKVTree_NilChecker() {
type Student struct {
Name string
}
// Use constructor to specify NilChecker, treat *Student(nil) as "no value", won't write to tree
tr := gtree.NewRedBlackKVTreeWithChecker[int, *Student](gutil.ComparatorInt, func(s *Student) bool {
return s == nil
}, true)
v1 := tr.GetOrSetFunc(1, func() *Student {
return nil
})
fmt.Println(v1 == nil)
fmt.Println(tr.Contains(1))
// Can also create first, then register determination logic via RegisterNilChecker
tr2 := gtree.NewRedBlackKVTree[int, *Student](gutil.ComparatorInt, true)
tr2.RegisterNilChecker(func(s *Student) bool {
return s == nil
})
v2 := tr2.GetOrSetFunc(2, func() *Student {
return nil
})
fmt.Println(v2 == nil)
fmt.Println(tr2.Contains(2))
// Output:
// true
// false
// true
// false
}
NewBTree
-
Description:
NewBTreecreates aBTreeusingm(maximum number of child nodes) and a custom comparator. Thesafeparameter specifies whether to use a concurrency-safetree, with a default value offalse. -
Note: The
mparameter 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:
NewBTreeFromcreates aBTreeusingm(maximum number of child nodes), a custom comparator, and data of typemap[interface{}]interface{}. Thesafeparameter specifies whether to use a concurrency-safetree, with a default value offalse. -
Note: The
mparameter 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:
Clonereturns 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:
Setsets thekey/valuefor 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:
Setssetskey/valuefor thetreein 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:
Getreturns the valuevaluecorresponding to the parameterkey. Ifkeydoes 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:
GetOrSetreturnsvalueifkeyexists; ifkeydoes not exist, it sets the key-value withkeyandvalue, 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:
GetOrSetFuncreturnsvalueifkeyexists; ifkeydoes not exist, it sets the key-value withkeyand 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:
GetOrSetFuncreturnsvalueifkeyexists; ifkeydoes not exist, it sets the key-value withkeyand the return value offunc f, then returns the value. -
Note: The difference between
GetOrSetFuncLockandGetOrSetFuncis that it executes the functionfin 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:
GetVarsearches and returns the key value corresponding to the key namekey, the type is*gvar.Var. -
Note: The returned
gvar.Varis 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:
GetVarOrSetreturns the result ofGetOrSet, the type is*gvar.Var. -
Note: The returned
gvar.Varis 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:
GetVarOrSetFuncreturns the result ofGetOrSetFunc, the type is*gvar.Var. - Note: The returned
gvar.Varis 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:
GetVarOrSetFuncLockreturns the result ofGetOrSetFuncLock, the type is*gvar.Var. -
Note: The returned
gvar.Varis 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
keydoes not exist,SetIfNotExistsets the key-value pairkey/valuefor the map and returnstrue. Ifkeyexists, it returnsfalse, andvaluewill 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
keydoes not exist,SetIfNotExistFuncsets the value to the return value of the functionfand returnstrue. Ifkeyexists, it returnsfalse, andvaluewill 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
keydoes not exist,SetIfNotExistFuncsets the value to the return value offunc c, then returnstrue. Ifkeyexists, it returnsfalse, andvaluewill be ignored. -
The difference between
SetIfNotExistFuncLockandSetIfNotExistFuncis that it executes the functionfwithinmutex.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:
Containschecks ifkeyexists in thetree. Ifkeyexists, 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
valuefromtreeaccording to the givenkeyand 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:
Removesdeletesvalueoftreein 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:
IsEmptychecks iftreeis empty. Iftreeis 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:
Sizereturns 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:
Keysreturns allkeys 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:
Valuesreturns allvalues 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:
Mapreturns allkey/valuepairs 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:
MapStrAnyreturns allkey/valuepairs 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:
Cleardeletes 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:
Replacereplaces thekey/valuein thetreewith 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:
Heightreturns 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:
Leftreturns the leftmost (smallest)nodeof type*BTreeEntry, ornilif thetreeis 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:
Rightreturns the rightmost (largest)nodeof type*BTreeEntry, ornilif thetreeis 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:
Stringreturns a display (for debugging) of thenodein 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:
Searchsearches thetreeusing the parameterkey. If thekeyis found, it returns its corresponding key-value and returns the parameterfoundastrue, 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:
Printprints thetreeto 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:
Iteratoris 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:
IteratorFromis 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:
IteratorAsciterates over thetreein ascending order using a custom callback functionfin read-only mode. Iffreturnstrue, 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:
IteratorAscFromiterates over thetreein ascending order using a custom callback functionfin read-only mode. The parameterkeyspecifies from whichkeyto start iterating. Whenmatchistrue, iteration starts from the complete match ofkey; otherwise, iteration uses index searching. Iffreturnstrue, 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:
IteratorDesciterates over thetreein descending order using a custom callback functionfin read-only mode. Iffreturnstrue, 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:
IteratorDescFromiterates over thetreein descending order using a custom callback functionfin read-only mode. The parameterkeyspecifies from whichkeyto start iterating. Whenmatchistrue, iteration starts from the complete match ofkey; otherwise, iteration uses index searching. Iffreturnstrue, 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:
MarshalJSONimplements thejson.Marshalinterface. -
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"}
}