github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/avl/tree.gno (about) 1 package avl 2 3 type IterCbFn func(key string, value interface{}) bool 4 5 //---------------------------------------- 6 // Tree 7 8 // The zero struct can be used as an empty tree. 9 type Tree struct { 10 node *Node 11 } 12 13 // NewTree creates a new empty AVL tree. 14 func NewTree() *Tree { 15 return &Tree{ 16 node: nil, 17 } 18 } 19 20 // Size returns the number of key-value pair in the tree. 21 func (tree *Tree) Size() int { 22 return tree.node.Size() 23 } 24 25 // Has checks whether a key exists in the tree. 26 // It returns true if the key exists, otherwise false. 27 func (tree *Tree) Has(key string) (has bool) { 28 return tree.node.Has(key) 29 } 30 31 // Get retrieves the value associated with the given key. 32 // It returns the value and a boolean indicating whether the key exists. 33 func (tree *Tree) Get(key string) (value interface{}, exists bool) { 34 _, value, exists = tree.node.Get(key) 35 return 36 } 37 38 // GetByIndex retrieves the key-value pair at the specified index in the tree. 39 // It returns the key and value at the given index. 40 func (tree *Tree) GetByIndex(index int) (key string, value interface{}) { 41 return tree.node.GetByIndex(index) 42 } 43 44 // Set inserts a key-value pair into the tree. 45 // If the key already exists, the value will be updated. 46 // It returns a boolean indicating whether the key was newly inserted or updated. 47 func (tree *Tree) Set(key string, value interface{}) (updated bool) { 48 newnode, updated := tree.node.Set(key, value) 49 tree.node = newnode 50 return updated 51 } 52 53 // Remove removes a key-value pair from the tree. 54 // It returns the removed value and a boolean indicating whether the key was found and removed. 55 func (tree *Tree) Remove(key string) (value interface{}, removed bool) { 56 newnode, _, value, removed := tree.node.Remove(key) 57 tree.node = newnode 58 return value, removed 59 } 60 61 // Iterate performs an in-order traversal of the tree within the specified key range. 62 // It calls the provided callback function for each key-value pair encountered. 63 // If the callback returns true, the iteration is stopped. 64 func (tree *Tree) Iterate(start, end string, cb IterCbFn) bool { 65 return tree.node.TraverseInRange(start, end, true, true, 66 func(node *Node) bool { 67 return cb(node.Key(), node.Value()) 68 }, 69 ) 70 } 71 72 // ReverseIterate performs a reverse in-order traversal of the tree within the specified key range. 73 // It calls the provided callback function for each key-value pair encountered. 74 // If the callback returns true, the iteration is stopped. 75 func (tree *Tree) ReverseIterate(start, end string, cb IterCbFn) bool { 76 return tree.node.TraverseInRange(start, end, false, true, 77 func(node *Node) bool { 78 return cb(node.Key(), node.Value()) 79 }, 80 ) 81 } 82 83 // IterateByOffset performs an in-order traversal of the tree starting from the specified offset. 84 // It calls the provided callback function for each key-value pair encountered, up to the specified count. 85 // If the callback returns true, the iteration is stopped. 86 func (tree *Tree) IterateByOffset(offset int, count int, cb IterCbFn) bool { 87 return tree.node.TraverseByOffset(offset, count, true, true, 88 func(node *Node) bool { 89 return cb(node.Key(), node.Value()) 90 }, 91 ) 92 } 93 94 // ReverseIterateByOffset performs a reverse in-order traversal of the tree starting from the specified offset. 95 // It calls the provided callback function for each key-value pair encountered, up to the specified count. 96 // If the callback returns true, the iteration is stopped. 97 func (tree *Tree) ReverseIterateByOffset(offset int, count int, cb IterCbFn) bool { 98 return tree.node.TraverseByOffset(offset, count, false, true, 99 func(node *Node) bool { 100 return cb(node.Key(), node.Value()) 101 }, 102 ) 103 }