以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档:https://pkg.go.dev/github.com/gogf/gf/v2/container/gtree

NewBTree

  • 说明:NewBTree使用m(最大子节点数量)和自定义的比较方法创建BTree。参数safe用于指定是否使用并发安全的tree,默认情况下为false

  • 注意:参数m必须大于等于3,否则会panic

  • 格式:

    1. NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTree
  • 示例:

    1. func ExampleNewBTree() {
    2. bTree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. bTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(bTree.Map())
    7. // Output:
    8. // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
    9. }

NewBTreeFrom

  • 说明:NewBTreeFrom使用m(最大子节点数量),自定义的比较方法和类型为map[interface{}]interface{}data创建BTree。参数safe用于指定是否使用并发安全的tree,默认情况下为false

  • 注意:参数m必须大于等于3,否则会panic

  • 格式:

    1. NewBTreeFrom(m int, comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *BTree
  • 示例:

    1. func ExampleNewBTreeFrom() {
    2. bTree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. bTree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. otherBTree := gtree.NewBTreeFrom(3, gutil.ComparatorString, bTree.Map())
    7. fmt.Println(otherBTree.Map())
    8. // Output:
    9. // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
    10. }

Clone

  • 说明:clone返回一个值为当前tree值的副本的新BTree

  • 格式:

    1. Clone() *BTree
  • 示例:

    1. func ExampleBTree_Clone() {
    2. b := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. b.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. tree := b.Clone()
    7. fmt.Println(tree.Map())
    8. fmt.Println(tree.Size())
    9. // Output:
    10. // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
    11. // 6
    12. }

Set

  • 说明:Settree设置key/value

  • 格式:

    1. Set(key interface{}, value interface{})
  • 示例:

    1. func ExampleBTree_Set() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.Map())
    7. fmt.Println(tree.Size())
    8. // Output:
    9. // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
    10. // 6
    11. }

Sets

  • 说明:Setstree批量设置key/value

  • 格式:

    1. Sets(data map[interface{}]interface{})
  • 示例:

    1. func ExampleBTree_Sets() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. tree.Sets(map[interface{}]interface{}{
    4. "key1": "val1",
    5. "key2": "val2",
    6. })
    7. fmt.Println(tree.Map())
    8. fmt.Println(tree.Size())
    9. // Output:
    10. // map[key1:val1 key2:val2]
    11. // 2
    12. }

Get

  • 说明:Get返回参数key对应的值value,如key不存在,则返回Nil

  • 格式:

    1. Get(key interface{}) (value interface{})
  • 示例:

    1. func ExampleBTree_Get() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.Get("key1"))
    7. fmt.Println(tree.Get("key10"))
    8. // Output:
    9. // val1
    10. // <nil>
    11. }

GetOrSet

  • 说明:GetOrSetkey存在,则返回value,如key不存在,使用keyvalue设置键值,然后返回该值。

  • 格式:

    1. GetOrSet(key interface{}, value interface{}) interface{}
  • 示例:

    1. func ExampleBTree_GetOrSet() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.GetOrSet("key1", "newVal1"))
    7. fmt.Println(tree.GetOrSet("key6", "val6"))
    8. // Output:
    9. // val1
    10. // val6
    11. }

GetOrSetFunc

  • 说明:GetOrSetFunckey存在,则返回value,如key不存在,使用keyfunc f的返回值设置键值,然后返回该值。

  • 格式:

    1. GetOrSetFunc(key interface{}, f func() interface{}) interface{}
  • 示例:

    1. func ExampleBTree_GetOrSetFunc() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.GetOrSetFunc("key1", func() interface{} {
    7. return "newVal1"
    8. }))
    9. fmt.Println(tree.GetOrSetFunc("key6", func() interface{} {
    10. return "val6"
    11. }))
    12. // Output:
    13. // val1
    14. // val6
    15. }

GetOrSetFuncLock

  • 说明:GetOrSetFunckey存在,则返回value,如key不存在,使用keyfunc f的返回值设置键值,然后返回该值。

  • 注意:GetOrSetFuncLockGetOrSetFunc函数的不同之处在于它在写锁中执行函数f

  • 格式:

    1. GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}
  • 示例:

    1. func ExampleBTree_GetOrSetFuncLock() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.GetOrSetFuncLock("key1", func() interface{} {
    7. return "newVal1"
    8. }))
    9. fmt.Println(tree.GetOrSetFuncLock("key6", func() interface{} {
    10. return "val6"
    11. }))
    12. // Output:
    13. // val1
    14. // val6
    15. }

GetVar

  • 说明:GetVar根据键名key查询并返回对应的键值,类型为*gvar.Var

  • 注意:返回的gvar.Var是非并发安全的。

  • 格式:

    1. GetVar(key interface{}) *gvar.Var
  • 示例:

    1. func ExampleBTree_GetVar() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.GetVar("key1").String())
    7. // Output:
    8. // val1
    9. }

GetVarOrSet

  • 说明:GetVarOrSet使用GetOrSet的结果返回,类型为*gvar.Var

  • 注意:返回的gvar.Var是非并发安全的。

  • 格式:

    1. GetVarOrSet(key interface{}, value interface{}) *gvar.Var
  • 示例:

    1. func ExampleBTree_GetVarOrSet() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.GetVarOrSet("key1", "newVal1"))
    7. fmt.Println(tree.GetVarOrSet("key6", "val6"))
    8. // Output:
    9. // val1
    10. // val6
    11. }

GetVarOrSetFunc

  • 说明:GetVarOrSetFunc使用GetOrSetFunc的结果返回,类型为*gvar.Var
  • 注意:返回的gvar.Var是非并发安全的。
  • 格式:

    1. GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var
  • 示例:

    1. func ExampleBTree_GetVarOrSetFunc() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.GetVarOrSetFunc("key1", func() interface{} {
    7. return "newVal1"
    8. }))
    9. fmt.Println(tree.GetVarOrSetFunc("key6", func() interface{} {
    10. return "val6"
    11. }))
    12. // Output:
    13. // val1
    14. // val6
    15. }

GetVarOrSetFuncLock

  • 说明:GetVarOrSetFuncLock使用GetOrSetFuncLock的结果返回,类型为*gvar.Var

  • 注意:返回的gvar.Var是非并发安全的。

  • 格式:

    1. GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var
  • 示例:

    1. func ExampleBTree_GetVarOrSetFuncLock() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.GetVarOrSetFuncLock("key1", func() interface{} {
    7. return "newVal1"
    8. }))
    9. fmt.Println(tree.GetVarOrSetFuncLock("key6", func() interface{} {
    10. return "val6"
    11. }))
    12. // Output:
    13. // val1
    14. // val6
    15. }

SetIfNotExist

  • 说明:如果key不存在,则SetIfNotExistmap设置值键值对key/value,并且返回true。如果key存在,则返回false,而value将被忽略。

  • 格式:

    1. SetIfNotExist(key interface{}, value interface{}) bool
  • 示例:

    1. func ExampleBTree_SetIfNotExist() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.SetIfNotExist("key1", "newVal1"))
    7. fmt.Println(tree.SetIfNotExist("key6", "val6"))
    8. // Output:
    9. // false
    10. // true
    11. }

SetIfNotExistFunc

  • 说明:如果key不存在,则SetIfNotExistFunc设置值为函数f的返回值,并且返回true。如果key存在,则返回false,并且value将被忽略。

  • 格式:

    1. SetIfNotExistFunc(key interface{}, f func() interface{}) bool
  • 示例:

    1. func ExampleBTree_SetIfNotExistFunc() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.SetIfNotExistFunc("key1", func() interface{} {
    7. return "newVal1"
    8. }))
    9. fmt.Println(tree.SetIfNotExistFunc("key6", func() interface{} {
    10. return "val6"
    11. }))
    12. // Output:
    13. // false
    14. // true
    15. }

SetIfNotExistFuncLock

  • 说明:如果key不存在,则SetIfNotExistFunc设置值为func c的返回值,然后返回true。如果key存在,则返回false,而value将被忽略。

  • SetIfNotExistFuncLockSetIfNotExistFunc函数的不同之处在于它在mutex.Lock中执行函数f

  • 格式:

    1. SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool
  • 示例:

    1. func ExampleBTree_SetIfNotExistFuncLock() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.SetIfNotExistFuncLock("key1", func() interface{} {
    7. return "newVal1"
    8. }))
    9. fmt.Println(tree.SetIfNotExistFuncLock("key6", func() interface{} {
    10. return "val6"
    11. }))
    12. // Output:
    13. // false
    14. // true
    15. }

Contains

  • 说明:Contains检查keytree中是否存在。如果key存在,则返回true,否则返回false

  • 格式:

    1. Contains(key interface{}) bool
  • 示例:

    1. func ExampleBTree_Contains() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.Contains("key1"))
    7. fmt.Println(tree.Contains("key6"))
    8. // Output:
    9. // true
    10. // false
    11. }

Remove

  • 说明:按给定的keytree中删除value,并返回此删除的value

  • 格式:

    1. Remove(key interface{}) (value interface{})
  • 示例:

    1. func ExampleBTree_Remove() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.Remove("key1"))
    7. fmt.Println(tree.Remove("key6"))
    8. fmt.Println(tree.Map())
    9. // Output:
    10. // val1
    11. // <nil>
    12. // map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
    13. }

Removes

  • 说明:Removes按给定的key批量删除treevalue

  • 格式:

    1. Removes(keys []interface{})
  • 示例:

    1. func ExampleBTree_Removes() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. removeKeys := make([]interface{}, 2)
    7. removeKeys = append(removeKeys, "key1")
    8. removeKeys = append(removeKeys, "key6")
    9. tree.Removes(removeKeys)
    10. fmt.Println(tree.Map())
    11. // Output:
    12. // map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
    13. }

IsEmpty

  • 说明:IsEmpty检查tree是否为空。如果tree为空,则返回true,否则返回false

  • 格式:

    1. IsEmpty() bool
  • 示例:

    1. func ExampleBTree_IsEmpty() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. fmt.Println(tree.IsEmpty())
    4. for i := 0; i < 6; i++ {
    5. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    6. }
    7. fmt.Println(tree.IsEmpty())
    8. // Output:
    9. // true
    10. // false
    11. }

Size

  • 说明:Size返回tree的大小。

  • 格式:

    1. Size() int
  • 示例:

    1. func ExampleBTree_Size() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. fmt.Println(tree.Size())
    4. for i := 0; i < 6; i++ {
    5. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    6. }
    7. fmt.Println(tree.Size())
    8. // Output:
    9. // 0
    10. // 6
    11. }

Keys

  • 说明:Keys按升序返回所有的key

  • 格式:

    1. Keys() []interface{}
  • 示例:

    1. func ExampleBTree_Keys() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 6; i > 0; i-- {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.Keys())
    7. // Output:
    8. // [key1 key2 key3 key4 key5 key6]
    9. }

Values

  • 说明:Valueskey的升序返回所有的value

  • 格式:

    1. Values() []interface{}
  • 示例:

    1. func ExampleBTree_Values() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 6; i > 0; i-- {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.Values())
    7. // Output:
    8. // [val1 val2 val3 val4 val5 val6]
    9. }

Map

  • 说明:Mapmap的形式返回所有的key/value

  • 格式:

    1. Map() map[interface{}]interface{}
  • 示例:

    1. func ExampleBTree_Map() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.Map())
    7. // Output:
    8. // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
    9. }

MapStrAny

  • 说明:MapStrAnymap[string]interface{}的形式返回所有的key/value

  • 格式:

    1. MapStrAny() map[string]interface{}
  • 示例:

    1. func ExampleBTree_MapStrAny() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set(1000+i, "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.MapStrAny())
    7. // Output:
    8. // map[1000:val0 1001:val1 1002:val2 1003:val3 1004:val4 1005:val5]
    9. }

Clear

  • 说明:Clear删除tree的所有数据。

  • 格式:

    1. Clear()
  • 示例:

    1. func ExampleBTree_Clear() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set(1000+i, "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.Size())
    7. tree.Clear()
    8. fmt.Println(tree.Size())
    9. // Output:
    10. // 6
    11. // 0
    12. }

Replace

  • 说明:Replace用类型为map[interface{}]interface{}data替换treekey/value

  • 格式:

    1. Replace(data map[interface{}]interface{})
  • 示例:

    1. func ExampleBTree_Replace() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.Map())
    7. data := map[interface{}]interface{}{
    8. "newKey0": "newVal0",
    9. "newKey1": "newVal1",
    10. "newKey2": "newVal2",
    11. }
    12. tree.Replace(data)
    13. fmt.Println(tree.Map())
    14. // Output:
    15. // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
    16. // map[newKey0:newVal0 newKey1:newVal1 newKey2:newVal2]
    17. }

Height

  • 说明:Height返回tree的高度。

  • 格式:

    1. Height() int
  • 示例:

    1. func ExampleBTree_Height() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorInt)
    3. for i := 0; i < 100; i++ {
    4. tree.Set(i, i)
    5. }
    6. fmt.Println(tree.Height())
    7. // Output:
    8. // 6
    9. }

Left

  • 说明:Left 返回最左边(最小)的类型为*BTreeEntrynode,如果tree是空的,则返回 nil

  • 格式:

    1. Left() *BTreeEntry
  • 示例:

    1. func ExampleBTree_Left() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorInt)
    3. for i := 1; i < 100; i++ {
    4. tree.Set(i, i)
    5. }
    6. fmt.Println(tree.Left().Key, tree.Left().Value)
    7. emptyTree := gtree.NewBTree(3, gutil.ComparatorInt)
    8. fmt.Println(emptyTree.Left())
    9. // Output:
    10. // 1 1
    11. // <nil>
    12. }

Right

  • 说明:Left 返回最右边(最大)的类型为*BTreeEntrynode,如果tree是空的,则返回 nil

  • 格式:

    1. Right() *BTreeEntry
  • 示例:

    1. func ExampleBTree_Right() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorInt)
    3. for i := 1; i < 100; i++ {
    4. tree.Set(i, i)
    5. }
    6. fmt.Println(tree.Right().Key, tree.Right().Value)
    7. emptyTree := gtree.NewBTree(3, gutil.ComparatorInt)
    8. fmt.Println(emptyTree.Left())
    9. // Output:
    10. // 99 99
    11. // <nil>
    12. }

String

  • 说明:String返回treenode显示(用于调试)。

  • 格式:

    1. String() string
  • 示例:

    1. func ExampleBTree_String() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.String())
    7. // Output:
    8. // key0
    9. // key1
    10. // key2
    11. // key3
    12. // key4
    13. // key5
    14. }

Search

  • 说明:Search使用参数key搜索tree。如果找到key,则返回其对应的键值,并且返回参数foundtrue,否则为false

  • 格式:

    1. Search(key interface{}) (value interface{}, found bool)
  • 示例:

    1. func ExampleBTree_Search() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. fmt.Println(tree.Search("key0"))
    7. fmt.Println(tree.Search("key6"))
    8. // Output:
    9. // val0 true
    10. // <nil> false
    11. }

Print

  • 说明:Printtree打印到标准输出。

  • 格式:

    1. Print()
  • 示例:

    1. func ExampleBTree_Print() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. tree.Print()
    7. // Output:
    8. // key0
    9. // key1
    10. // key2
    11. // key3
    12. // key4
    13. // key5
    14. }

Iterator

  • 说明:Iterator 等同于 IteratorAsc

  • 格式:

    1. Iterator(f func(key, value interface{}) bool)
  • 示例:

    1. func ExampleBTree_Iterator() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 10; i++ {
    4. tree.Set(i, 10-i)
    5. }
    6. var totalKey, totalValue int
    7. tree.Iterator(func(key, value interface{}) bool {
    8. totalKey += key.(int)
    9. totalValue += value.(int)
    10. return totalValue < 20
    11. })
    12. fmt.Println("totalKey:", totalKey)
    13. fmt.Println("totalValue:", totalValue)
    14. // Output:
    15. // totalKey: 3
    16. // totalValue: 27
    17. }

IteratorFrom

  • 说明:IteratorFrom 等同于 IteratorAscFrom

  • 格式:

    1. IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • 示例:

    1. func ExampleBTree_IteratorFrom() {
    2. m := make(map[interface{}]interface{})
    3. for i := 1; i <= 5; i++ {
    4. m[i] = i * 10
    5. }
    6. tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
    7. tree.IteratorFrom(1, true, func(key, value interface{}) bool {
    8. fmt.Println("key:", key, ", value:", value)
    9. return true
    10. })
    11. // Output:
    12. // key: 1 , value: 10
    13. // key: 2 , value: 20
    14. // key: 3 , value: 30
    15. // key: 4 , value: 40
    16. // key: 5 , value: 50
    17. }

IteratorAsc

  • 说明:IteratorAsc使用自定义回调函数f以只读方式按升序迭代tree。如果f返回true,则继续迭代,返回false则停止。

  • 格式:

    1. IteratorAsc(f func(key, value interface{}) bool)
  • 示例:

    1. func ExampleBTree_IteratorAsc() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 10; i++ {
    4. tree.Set(i, 10-i)
    5. }
    6. tree.IteratorAsc(func(key, value interface{}) bool {
    7. fmt.Println("key:", key, ", value:", value)
    8. return true
    9. })
    10. // Output:
    11. // key: 0 , value: 10
    12. // key: 1 , value: 9
    13. // key: 2 , value: 8
    14. // key: 3 , value: 7
    15. // key: 4 , value: 6
    16. // key: 5 , value: 5
    17. // key: 6 , value: 4
    18. // key: 7 , value: 3
    19. // key: 8 , value: 2
    20. // key: 9 , value: 1
    21. }

IteratorAscFrom

  • 说明:IteratorAscFrom使用自定义回调函数f以只读方式按升序迭代tree。参数 key 指定从哪个key开始迭代。参数matchtrue时,从key完全匹配时开始迭代,否则使用索引搜索迭代。如果f返回true,则继续迭代,返回false则停止。

  • 格式:

    1. IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • 示例:

    1. func ExampleBTree_IteratorAscFrom_Normal() {
    2. m := make(map[interface{}]interface{})
    3. for i := 1; i <= 5; i++ {
    4. m[i] = i * 10
    5. }
    6. tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
    7. tree.IteratorAscFrom(1, true, func(key, value interface{}) bool {
    8. fmt.Println("key:", key, ", value:", value)
    9. return true
    10. })
    11. // Output:
    12. // key: 1 , value: 10
    13. // key: 2 , value: 20
    14. // key: 3 , value: 30
    15. // key: 4 , value: 40
    16. // key: 5 , value: 50
    17. }
    1. func ExampleBTree_IteratorAscFrom_NoExistKey() {
    2. m := make(map[interface{}]interface{})
    3. for i := 1; i <= 5; i++ {
    4. m[i] = i * 10
    5. }
    6. tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
    7. tree.IteratorAscFrom(0, true, func(key, value interface{}) bool {
    8. fmt.Println("key:", key, ", value:", value)
    9. return true
    10. })
    11. // Output:
    12. }
    1. func ExampleBTree_IteratorAscFrom_NoExistKeyAndMatchFalse() {
    2. m := make(map[interface{}]interface{})
    3. for i := 1; i <= 5; i++ {
    4. m[i] = i * 10
    5. }
    6. tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
    7. tree.IteratorAscFrom(0, false, func(key, value interface{}) bool {
    8. fmt.Println("key:", key, ", value:", value)
    9. return true
    10. })
    11. // Output:
    12. // key: 1 , value: 10
    13. // key: 2 , value: 20
    14. // key: 3 , value: 30
    15. // key: 4 , value: 40
    16. // key: 5 , value: 50
    17. }

IteratorDesc

  • 说明:IteratorDesc使用自定义回调函数f以只读方式按降序迭代tree。如果f返回true,则继续迭代,返回false则停止。

  • 格式:

    1. IteratorDesc(f func(key, value interface{}) bool)
  • 示例:

    1. func ExampleBTree_IteratorDesc() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 10; i++ {
    4. tree.Set(i, 10-i)
    5. }
    6. tree.IteratorDesc(func(key, value interface{}) bool {
    7. fmt.Println("key:", key, ", value:", value)
    8. return true
    9. })
    10. // Output:
    11. // key: 9 , value: 1
    12. // key: 8 , value: 2
    13. // key: 7 , value: 3
    14. // key: 6 , value: 4
    15. // key: 5 , value: 5
    16. // key: 4 , value: 6
    17. // key: 3 , value: 7
    18. // key: 2 , value: 8
    19. // key: 1 , value: 9
    20. // key: 0 , value: 10
    21. }

IteratorDescFrom

  • 说明:IteratorDescFrom使用自定义回调函数f以只读方式按降序迭代tree。参数 key 指定从哪个key开始迭代。参数matchtrue时,从key完全匹配时开始迭代,否则使用索引搜索迭代。如果f返回true,则继续迭代,返回false则停止。

  • 格式:

    1. IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • 示例:

    1. func ExampleBTree_IteratorDescFrom() {
    2. m := make(map[interface{}]interface{})
    3. for i := 1; i <= 5; i++ {
    4. m[i] = i * 10
    5. }
    6. tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m)
    7. tree.IteratorDescFrom(5, true, func(key, value interface{}) bool {
    8. fmt.Println("key:", key, ", value:", value)
    9. return true
    10. })
    11. // Output:
    12. // key: 5 , value: 50
    13. // key: 4 , value: 40
    14. // key: 3 , value: 30
    15. // key: 2 , value: 20
    16. // key: 1 , value: 10
    17. }

MarshalJson

  • 说明:MarshalJSON实现json.Marshal的接口。

  • 格式:

    1. MarshalJSON() ([]byte, error)
  • 示例:

    1. func ExampleBTree_MarshalJSON() {
    2. tree := gtree.NewBTree(3, gutil.ComparatorString)
    3. for i := 0; i < 6; i++ {
    4. tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    5. }
    6. bytes, err := json.Marshal(tree)
    7. if err == nil {
    8. fmt.Println(gconv.String(bytes))
    9. }
    10. // Output:
    11. // {"key0":"val0","key1":"val1","key2":"val2","key3":"val3","key4":"val4","key5":"val5"}
    12. }