github.com/gogf/gf/v2@v2.7.4/container/gtree/gtree.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // Package gtree provides concurrent-safe/unsafe tree containers. 8 // 9 // Some implements are from: https://github.com/emirpasic/gods 10 package gtree 11 12 import "github.com/gogf/gf/v2/container/gvar" 13 14 // iTree defines the interface for basic operations of a tree. 15 type iTree interface { 16 // Set inserts node into the tree. 17 Set(key interface{}, value interface{}) 18 19 // Sets batch sets key-values to the tree. 20 Sets(data map[interface{}]interface{}) 21 22 // SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. 23 // It returns false if `key` exists, and `value` would be ignored. 24 SetIfNotExist(key interface{}, value interface{}) bool 25 26 // SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. 27 // It returns false if `key` exists, and `value` would be ignored. 28 SetIfNotExistFunc(key interface{}, f func() interface{}) bool 29 30 // SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. 31 // It returns false if `key` exists, and `value` would be ignored. 32 // SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that 33 // it executes function `f` with mutex.Lock of the hash map. 34 SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool 35 36 // Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree. 37 Get(key interface{}) (value interface{}) 38 39 // GetOrSet returns the value by key, 40 // or sets value with given `value` if it does not exist and then returns this value. 41 GetOrSet(key interface{}, value interface{}) interface{} 42 43 // GetOrSetFunc returns the value by key, 44 // or sets value with returned value of callback function `f` if it does not exist 45 // and then returns this value. 46 GetOrSetFunc(key interface{}, f func() interface{}) interface{} 47 48 // GetOrSetFuncLock returns the value by key, 49 // or sets value with returned value of callback function `f` if it does not exist 50 // and then returns this value. 51 // GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` 52 // with mutex.Lock of the hash map. 53 GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} 54 55 // GetVar returns a gvar.Var with the value by given `key`. 56 // The returned gvar.Var is un-concurrent safe. 57 GetVar(key interface{}) *gvar.Var 58 59 // GetVarOrSet returns a gvar.Var with result from GetVarOrSet. 60 // The returned gvar.Var is un-concurrent safe. 61 GetVarOrSet(key interface{}, value interface{}) *gvar.Var 62 63 // GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc. 64 // The returned gvar.Var is un-concurrent safe. 65 GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var 66 67 // GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock. 68 // The returned gvar.Var is un-concurrent safe. 69 GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var 70 71 // Search searches the tree with given `key`. 72 // Second return parameter `found` is true if key was found, otherwise false. 73 Search(key interface{}) (value interface{}, found bool) 74 75 // Contains checks whether `key` exists in the tree. 76 Contains(key interface{}) bool 77 78 // Size returns number of nodes in the tree. 79 Size() int 80 81 // IsEmpty returns true if tree does not contain any nodes. 82 IsEmpty() bool 83 84 // Remove removes the node from the tree by key. 85 // Key should adhere to the comparator's type assertion, otherwise method panics. 86 Remove(key interface{}) (value interface{}) 87 88 // Removes batch deletes values of the tree by `keys`. 89 Removes(keys []interface{}) 90 91 // Clear removes all nodes from the tree. 92 Clear() 93 94 // Keys returns all keys in asc order. 95 Keys() []interface{} 96 97 // Values returns all values in asc order based on the key. 98 Values() []interface{} 99 100 // Replace the data of the tree with given `data`. 101 Replace(data map[interface{}]interface{}) 102 103 // Print prints the tree to stdout. 104 Print() 105 106 // String returns a string representation of container 107 String() string 108 109 // MarshalJSON implements the interface MarshalJSON for json.Marshal. 110 MarshalJSON() (jsonBytes []byte, err error) 111 112 Map() map[interface{}]interface{} 113 MapStrAny() map[string]interface{} 114 115 // Iterator is alias of IteratorAsc. 116 Iterator(f func(key, value interface{}) bool) 117 118 // IteratorFrom is alias of IteratorAscFrom. 119 IteratorFrom(key interface{}, match bool, f func(key, value interface{}) bool) 120 121 // IteratorAsc iterates the tree readonly in ascending order with given callback function `f`. 122 // If `f` returns true, then it continues iterating; or false to stop. 123 IteratorAsc(f func(key, value interface{}) bool) 124 125 // IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`. 126 // The parameter `key` specifies the start entry for iterating. The `match` specifies whether 127 // starting iterating if the `key` is fully matched, or else using index searching iterating. 128 // If `f` returns true, then it continues iterating; or false to stop. 129 IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) 130 131 // IteratorDesc iterates the tree readonly in descending order with given callback function `f`. 132 // If `f` returns true, then it continues iterating; or false to stop. 133 IteratorDesc(f func(key, value interface{}) bool) 134 135 // IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`. 136 // The parameter `key` specifies the start entry for iterating. The `match` specifies whether 137 // starting iterating if the `key` is fully matched, or else using index searching iterating. 138 // If `f` returns true, then it continues iterating; or false to stop. 139 IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) 140 }