Tree - Methods - 图1tip

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 a BTree using m (maximum number of child nodes) and a custom comparator. The safe parameter specifies whether to use a concurrency-safe tree, with a default value of false.

  • Note: The m parameter must be greater than or equal to 3, otherwise it will panic.

  • Format:

  1. NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTree
  • Example:
  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

  • Description: NewBTreeFrom creates a BTree using m (maximum number of child nodes), a custom comparator, and data of type map[interface{}]interface{}. The safe parameter specifies whether to use a concurrency-safe tree, with a default value of false.

  • Note: The m parameter must be greater than or equal to 3, otherwise it will panic.

  • Format:

  1. NewBTreeFrom(m int, comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *BTree
  • Example:
  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

  • Description: Clone returns a new BTree, which is a copy of the current tree.

  • Format:

  1. Clone() *BTree
  • Example:
  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

  • Description: Set sets the key/value for the tree.

  • Format:

  1. Set(key interface{}, value interface{})
  • Example:
  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

  • Description: Sets sets key/value for the tree in batches.

  • Format:

  1. Sets(data map[interface{}]interface{})
  • Example:
  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

  • Description: Get returns the value value corresponding to the parameter key. If key does not exist, it returns Nil.

  • Format:

  1. Get(key interface{}) (value interface{})
  • Example:
  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

  • Description: GetOrSet returns value if key exists; if key does not exist, it sets the key-value with key and value, then returns the value.

  • Format:

  1. GetOrSet(key interface{}, value interface{}) interface{}
  • Example:
  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

  • Description: GetOrSetFunc returns value if key exists; if key does not exist, it sets the key-value with key and the return value of func f, then returns the value.

  • Format:

  1. GetOrSetFunc(key interface{}, f func() interface{}) interface{}
  • Example:
  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

  • Description: GetOrSetFunc returns value if key exists; if key does not exist, it sets the key-value with key and the return value of func f, then returns the value.

  • Note: The difference between GetOrSetFuncLock and GetOrSetFunc is that it executes the function f in a write lock.

  • Format:

  1. GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}
  • Example:
  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

  • Description: GetVar searches and returns the key value corresponding to the key name key, the type is *gvar.Var.

  • Note: The returned gvar.Var is not concurrent safe.

  • Format:

  1. GetVar(key interface{}) *gvar.Var
  • Example:
  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

  • Description: GetVarOrSet returns the result of GetOrSet, the type is *gvar.Var.

  • Note: The returned gvar.Var is not concurrent safe.

  • Format:

  1. GetVarOrSet(key interface{}, value interface{}) *gvar.Var
  • Example:
  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

  • Description: GetVarOrSetFunc returns the result of GetOrSetFunc, the type is *gvar.Var.
  • Note: The returned gvar.Var is not concurrent safe.
  • Format:
  1. GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var
  • Example:
  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

  • Description: GetVarOrSetFuncLock returns the result of GetOrSetFuncLock, the type is *gvar.Var.

  • Note: The returned gvar.Var is not concurrent safe.

  • Format:

  1. GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var
  • Example:
  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

  • Description: If key does not exist, SetIfNotExist sets the key-value pair key/value for the map and returns true. If key exists, it returns false, and value will be ignored.

  • Format:

  1. SetIfNotExist(key interface{}, value interface{}) bool
  • Example:
  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

  • Description: If key does not exist, SetIfNotExistFunc sets the value to the return value of the function f and returns true. If key exists, it returns false, and value will be ignored.

  • Format:

  1. SetIfNotExistFunc(key interface{}, f func() interface{}) bool
  • Example:
  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

  • Description: If key does not exist, SetIfNotExistFunc sets the value to the return value of func c, then returns true. If key exists, it returns false, and value will be ignored.

  • The difference between SetIfNotExistFuncLock and SetIfNotExistFunc is that it executes the function f within mutex.Lock.

  • Format:

  1. SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool
  • Example:
  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

  • Description: Contains checks if key exists in the tree. If key exists, it returns true; otherwise, it returns false.

  • Format:

  1. Contains(key interface{}) bool
  • Example:
  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

  • Description: Delete the value from tree according to the given key and return this deleted value.

  • Format:

  1. Remove(key interface{}) (value interface{})
  • Example:
  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

  • Description: Removes deletes value of tree in batches according to the given key.

  • Format:

  1. Removes(keys []interface{})
  • Example:
  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

  • Description: IsEmpty checks if tree is empty. If tree is empty, it returns true; otherwise, it returns false.

  • Format:

  1. IsEmpty() bool
  • Example:
  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

  • Description: Size returns the size of the tree.

  • Format:

  1. Size() int
  • Example:
  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

  • Description: Keys returns all keys in ascending order.

  • Format:

  1. Keys() []interface{}
  • Example:
  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

  • Description: Values returns all values in ascending order of key.

  • Format:

  1. Values() []interface{}
  • Example:
  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

  • Description: Map returns all key/value pairs in the form of a map.

  • Format:

  1. Map() map[interface{}]interface{}
  • Example:
  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

  • Description: MapStrAny returns all key/value pairs in the form of map[string]interface{}.

  • Format:

  1. MapStrAny() map[string]interface{}
  • Example:
  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

  • Description: Clear deletes all data in the tree.

  • Format:

  1. Clear()
  • Example:
  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

  • Description: Replace replaces the key/value in the tree with data of type map[interface{}]interface{}.

  • Format:

  1. Replace(data map[interface{}]interface{})
  • Example:
  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

  • Description: Height returns the height of the tree.

  • Format:

  1. Height() int
  • Example:
  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

  • Description: Left returns the leftmost (smallest) node of type *BTreeEntry, or nil if the tree is empty.

  • Format:

  1. Left() *BTreeEntry
  • Example:
  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

  • Description: Right returns the rightmost (largest) node of type *BTreeEntry, or nil if the tree is empty.

  • Format:

  1. Right() *BTreeEntry
  • Example:
  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

  • Description: String returns a display (for debugging) of the node in the tree.

  • Format:

  1. String() string
  • Example:
  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

  • Description: Search searches the tree using the parameter key. If the key is found, it returns its corresponding key-value and returns the parameter found as true, otherwise false.

  • Format:

  1. Search(key interface{}) (value interface{}, found bool)
  • Example:
  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

  • Description: Print prints the tree to standard output.

  • Format:

  1. Print()
  • Example:
  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

  • Description: Iterator is equivalent to IteratorAsc.

  • Format:

  1. Iterator(f func(key, value interface{}) bool)
  • Example:
  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

  • Description: IteratorFrom is equivalent to IteratorAscFrom.

  • Format:

  1. IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • Example:
  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

  • Description: IteratorAsc iterates over the tree in ascending order using a custom callback function f in read-only mode. If f returns true, it continues iterating; if it returns false, it stops.

  • Format:

  1. IteratorAsc(f func(key, value interface{}) bool)
  • Example:
  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

  • Description: IteratorAscFrom iterates over the tree in ascending order using a custom callback function f in read-only mode. The parameter key specifies from which key to start iterating. When match is true, iteration starts from the complete match of key; otherwise, iteration uses index searching. If f returns true, it continues iterating; if it returns false, it stops.

  • Format:

  1. IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • Example:
  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

  • Description: IteratorDesc iterates over the tree in descending order using a custom callback function f in read-only mode. If f returns true, it continues iterating; if it returns false, it stops.

  • Format:

  1. IteratorDesc(f func(key, value interface{}) bool)
  • Example:
  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

  • Description: IteratorDescFrom iterates over the tree in descending order using a custom callback function f in read-only mode. The parameter key specifies from which key to start iterating. When match is true, iteration starts from the complete match of key; otherwise, iteration uses index searching. If f returns true, it continues iterating; if it returns false, it stops.

  • Format:

  1. IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool)
  • Example:
  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

  • Description: MarshalJSON implements the json.Marshal interface.

  • Format:

  1. MarshalJSON() ([]byte, error)
  • Example:
  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. }