@@ -54,30 +54,35 @@ func (tree *AVLTree) lazyInit() {
5454
5555// Clone clones and returns a new tree from current tree.
5656func (tree * AVLTree ) Clone () * AVLTree {
57+ tree .lazyInit ()
5758 return & AVLTree {
5859 AVLKVTree : tree .AVLKVTree .Clone (),
5960 }
6061}
6162
6263// Set sets key-value pair into the tree.
6364func (tree * AVLTree ) Set (key any , value any ) {
65+ tree .lazyInit ()
6466 tree .AVLKVTree .Set (key , value )
6567}
6668
6769// Sets batch sets key-values to the tree.
6870func (tree * AVLTree ) Sets (data map [any ]any ) {
71+ tree .lazyInit ()
6972 tree .AVLKVTree .Sets (data )
7073}
7174
7275// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
7376// It returns false if `key` exists, and such setting key-value pair operation would be ignored.
7477func (tree * AVLTree ) SetIfNotExist (key any , value any ) bool {
78+ tree .lazyInit ()
7579 return tree .AVLKVTree .SetIfNotExist (key , value )
7680}
7781
7882// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
7983// It returns false if `key` exists, and such setting key-value pair operation would be ignored.
8084func (tree * AVLTree ) SetIfNotExistFunc (key any , f func () any ) bool {
85+ tree .lazyInit ()
8186 return tree .AVLKVTree .SetIfNotExistFunc (key , f )
8287}
8388
@@ -87,6 +92,7 @@ func (tree *AVLTree) SetIfNotExistFunc(key any, f func() any) bool {
8792// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
8893// it executes function `f` within mutex lock.
8994func (tree * AVLTree ) SetIfNotExistFuncLock (key any , f func () any ) bool {
95+ tree .lazyInit ()
9096 return tree .AVLKVTree .SetIfNotExistFuncLock (key , f )
9197}
9298
@@ -95,18 +101,21 @@ func (tree *AVLTree) SetIfNotExistFuncLock(key any, f func() any) bool {
95101// Note that, the `nil` value from Get function cannot be used to determine key existence, please use Contains function
96102// to do so.
97103func (tree * AVLTree ) Get (key any ) (value any ) {
104+ tree .lazyInit ()
98105 return tree .AVLKVTree .Get (key )
99106}
100107
101108// GetOrSet returns its `value` of `key`, or sets value with given `value` if it does not exist and then returns
102109// this value.
103110func (tree * AVLTree ) GetOrSet (key any , value any ) any {
111+ tree .lazyInit ()
104112 return tree .AVLKVTree .GetOrSet (key , value )
105113}
106114
107115// GetOrSetFunc returns its `value` of `key`, or sets value with returned value of callback function `f` if it does not
108116// exist and then returns this value.
109117func (tree * AVLTree ) GetOrSetFunc (key any , f func () any ) any {
118+ tree .lazyInit ()
110119 return tree .AVLKVTree .GetOrSetFunc (key , f )
111120}
112121
@@ -115,6 +124,7 @@ func (tree *AVLTree) GetOrSetFunc(key any, f func() any) any {
115124//
116125// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` within mutex lock.
117126func (tree * AVLTree ) GetOrSetFuncLock (key any , f func () any ) any {
127+ tree .lazyInit ()
118128 return tree .AVLKVTree .GetOrSetFuncLock (key , f )
119129}
120130
@@ -123,6 +133,7 @@ func (tree *AVLTree) GetOrSetFuncLock(key any, f func() any) any {
123133//
124134// Also see function Get.
125135func (tree * AVLTree ) GetVar (key any ) * gvar.Var {
136+ tree .lazyInit ()
126137 return tree .AVLKVTree .GetVar (key )
127138}
128139
@@ -131,6 +142,7 @@ func (tree *AVLTree) GetVar(key any) *gvar.Var {
131142//
132143// Also see function GetOrSet.
133144func (tree * AVLTree ) GetVarOrSet (key any , value any ) * gvar.Var {
145+ tree .lazyInit ()
134146 return tree .AVLKVTree .GetVarOrSet (key , value )
135147}
136148
@@ -139,6 +151,7 @@ func (tree *AVLTree) GetVarOrSet(key any, value any) *gvar.Var {
139151//
140152// Also see function GetOrSetFunc.
141153func (tree * AVLTree ) GetVarOrSetFunc (key any , f func () any ) * gvar.Var {
154+ tree .lazyInit ()
142155 return tree .AVLKVTree .GetVarOrSetFunc (key , f )
143156}
144157
@@ -147,83 +160,99 @@ func (tree *AVLTree) GetVarOrSetFunc(key any, f func() any) *gvar.Var {
147160//
148161// Also see function GetOrSetFuncLock.
149162func (tree * AVLTree ) GetVarOrSetFuncLock (key any , f func () any ) * gvar.Var {
163+ tree .lazyInit ()
150164 return tree .AVLKVTree .GetVarOrSetFuncLock (key , f )
151165}
152166
153167// Search searches the tree with given `key`.
154168// Second return parameter `found` is true if key was found, otherwise false.
155169func (tree * AVLTree ) Search (key any ) (value any , found bool ) {
170+ tree .lazyInit ()
156171 return tree .AVLKVTree .Search (key )
157172}
158173
159174// Contains checks and returns whether given `key` exists in the tree.
160175func (tree * AVLTree ) Contains (key any ) bool {
176+ tree .lazyInit ()
161177 return tree .AVLKVTree .Contains (key )
162178}
163179
164180// Size returns number of nodes in the tree.
165181func (tree * AVLTree ) Size () int {
182+ tree .lazyInit ()
166183 return tree .AVLKVTree .Size ()
167184}
168185
169186// IsEmpty returns true if the tree does not contain any nodes.
170187func (tree * AVLTree ) IsEmpty () bool {
188+ tree .lazyInit ()
171189 return tree .AVLKVTree .IsEmpty ()
172190}
173191
174192// Remove removes the node from the tree by `key`, and returns its associated value of `key`.
175193// The given `key` should adhere to the comparator's type assertion, otherwise method panics.
176194func (tree * AVLTree ) Remove (key any ) (value any ) {
195+ tree .lazyInit ()
177196 return tree .AVLKVTree .Remove (key )
178197}
179198
180199// Removes batch deletes key-value pairs from the tree by `keys`.
181200func (tree * AVLTree ) Removes (keys []any ) {
201+ tree .lazyInit ()
182202 tree .AVLKVTree .Removes (keys )
183203}
184204
185205// Clear removes all nodes from the tree.
186206func (tree * AVLTree ) Clear () {
207+ tree .lazyInit ()
187208 tree .AVLKVTree .Clear ()
188209}
189210
190211// Keys returns all keys from the tree in order by its comparator.
191212func (tree * AVLTree ) Keys () []any {
213+ tree .lazyInit ()
192214 return tree .AVLKVTree .Keys ()
193215}
194216
195217// Values returns all values from the true in order by its comparator based on the key.
196218func (tree * AVLTree ) Values () []any {
219+ tree .lazyInit ()
197220 return tree .AVLKVTree .Values ()
198221}
199222
200223// Replace clears the data of the tree and sets the nodes by given `data`.
201224func (tree * AVLTree ) Replace (data map [any ]any ) {
225+ tree .lazyInit ()
202226 tree .AVLKVTree .Replace (data )
203227}
204228
205229// Print prints the tree to stdout.
206230func (tree * AVLTree ) Print () {
231+ tree .lazyInit ()
207232 tree .AVLKVTree .Print ()
208233}
209234
210235// String returns a string representation of container.
211236func (tree * AVLTree ) String () string {
237+ tree .lazyInit ()
212238 return tree .AVLKVTree .String ()
213239}
214240
215241// MarshalJSON implements the interface MarshalJSON for json.Marshal.
216242func (tree * AVLTree ) MarshalJSON () (jsonBytes []byte , err error ) {
243+ tree .lazyInit ()
217244 return tree .AVLKVTree .MarshalJSON ()
218245}
219246
220247// Map returns all key-value pairs as map.
221248func (tree * AVLTree ) Map () map [any ]any {
249+ tree .lazyInit ()
222250 return tree .AVLKVTree .Map ()
223251}
224252
225253// MapStrAny returns all key-value items as map[string]any.
226254func (tree * AVLTree ) MapStrAny () map [string ]any {
255+ tree .lazyInit ()
227256 return tree .AVLKVTree .MapStrAny ()
228257}
229258
@@ -244,6 +273,7 @@ func (tree *AVLTree) IteratorFrom(key any, match bool, f func(key, value any) bo
244273// IteratorAsc iterates the tree readonly in ascending order with given callback function `f`.
245274// If callback function `f` returns true, then it continues iterating; or false to stop.
246275func (tree * AVLTree ) IteratorAsc (f func (key , value any ) bool ) {
276+ tree .lazyInit ()
247277 tree .AVLKVTree .IteratorAsc (f )
248278}
249279
@@ -254,13 +284,15 @@ func (tree *AVLTree) IteratorAsc(f func(key, value any) bool) {
254284// searching iterating.
255285// If callback function `f` returns true, then it continues iterating; or false to stop.
256286func (tree * AVLTree ) IteratorAscFrom (key any , match bool , f func (key , value any ) bool ) {
287+ tree .lazyInit ()
257288 tree .AVLKVTree .IteratorAscFrom (key , match , f )
258289}
259290
260291// IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
261292//
262293// If callback function `f` returns true, then it continues iterating; or false to stop.
263294func (tree * AVLTree ) IteratorDesc (f func (key , value any ) bool ) {
295+ tree .lazyInit ()
264296 tree .AVLKVTree .IteratorDesc (f )
265297}
266298
@@ -271,16 +303,19 @@ func (tree *AVLTree) IteratorDesc(f func(key, value any) bool) {
271303// searching iterating.
272304// If callback function `f` returns true, then it continues iterating; or false to stop.
273305func (tree * AVLTree ) IteratorDescFrom (key any , match bool , f func (key , value any ) bool ) {
306+ tree .lazyInit ()
274307 tree .AVLKVTree .IteratorDescFrom (key , match , f )
275308}
276309
277310// Left returns the minimum element corresponding to the comparator of the tree or nil if the tree is empty.
278311func (tree * AVLTree ) Left () * AVLTreeNode {
312+ tree .lazyInit ()
279313 return tree .AVLKVTree .Left ()
280314}
281315
282316// Right returns the maximum element corresponding to the comparator of the tree or nil if the tree is empty.
283317func (tree * AVLTree ) Right () * AVLTreeNode {
318+ tree .lazyInit ()
284319 return tree .AVLKVTree .Right ()
285320}
286321
@@ -293,6 +328,7 @@ func (tree *AVLTree) Right() *AVLTreeNode {
293328//
294329// Key should adhere to the comparator's type assertion, otherwise method panics.
295330func (tree * AVLTree ) Floor (key any ) (floor * AVLTreeNode , found bool ) {
331+ tree .lazyInit ()
296332 return tree .AVLKVTree .Floor (key )
297333}
298334
@@ -305,6 +341,7 @@ func (tree *AVLTree) Floor(key any) (floor *AVLTreeNode, found bool) {
305341//
306342// Key should adhere to the comparator's type assertion, otherwise method panics.
307343func (tree * AVLTree ) Ceiling (key any ) (ceiling * AVLTreeNode , found bool ) {
344+ tree .lazyInit ()
308345 return tree .AVLKVTree .Ceiling (key )
309346}
310347
@@ -314,6 +351,8 @@ func (tree *AVLTree) Ceiling(key any) (ceiling *AVLTreeNode, found bool) {
314351//
315352// If the type of value is different with key, you pass the new `comparator`.
316353func (tree * AVLTree ) Flip (comparator ... func (v1 , v2 any ) int ) {
354+ tree .lazyInit ()
355+
317356 var t = new (AVLTree )
318357 if len (comparator ) > 0 {
319358 t = NewAVLTree (comparator [0 ], tree .mu .IsSafe ())
0 commit comments