github.com/wangyougui/gf/v2@v2.6.5/container/gtree/gtree_z_example_btree_test.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 gm file, 5 // You can obtain one at https://github.com/Agogf/gf. 6 7 package gtree_test 8 9 import ( 10 "fmt" 11 12 "github.com/wangyougui/gf/v2/container/gtree" 13 "github.com/wangyougui/gf/v2/internal/json" 14 "github.com/wangyougui/gf/v2/util/gconv" 15 "github.com/wangyougui/gf/v2/util/gutil" 16 ) 17 18 func ExampleBTree_Clone() { 19 b := gtree.NewBTree(3, gutil.ComparatorString) 20 for i := 0; i < 6; i++ { 21 b.Set("key"+gconv.String(i), "val"+gconv.String(i)) 22 } 23 24 tree := b.Clone() 25 26 fmt.Println(tree.Map()) 27 fmt.Println(tree.Size()) 28 29 // Output: 30 // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 31 // 6 32 } 33 34 func ExampleBTree_Set() { 35 tree := gtree.NewBTree(3, gutil.ComparatorString) 36 for i := 0; i < 6; i++ { 37 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 38 } 39 40 fmt.Println(tree.Map()) 41 fmt.Println(tree.Size()) 42 43 // Output: 44 // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 45 // 6 46 } 47 48 func ExampleBTree_Sets() { 49 tree := gtree.NewBTree(3, gutil.ComparatorString) 50 51 tree.Sets(map[interface{}]interface{}{ 52 "key1": "val1", 53 "key2": "val2", 54 }) 55 56 fmt.Println(tree.Map()) 57 fmt.Println(tree.Size()) 58 59 // Output: 60 // map[key1:val1 key2:val2] 61 // 2 62 } 63 64 func ExampleBTree_Get() { 65 tree := gtree.NewBTree(3, gutil.ComparatorString) 66 for i := 0; i < 6; i++ { 67 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 68 } 69 70 fmt.Println(tree.Get("key1")) 71 fmt.Println(tree.Get("key10")) 72 73 // Output: 74 // val1 75 // <nil> 76 } 77 78 func ExampleBTree_GetOrSet() { 79 tree := gtree.NewBTree(3, gutil.ComparatorString) 80 for i := 0; i < 6; i++ { 81 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 82 } 83 84 fmt.Println(tree.GetOrSet("key1", "newVal1")) 85 fmt.Println(tree.GetOrSet("key6", "val6")) 86 87 // Output: 88 // val1 89 // val6 90 } 91 92 func ExampleBTree_GetOrSetFunc() { 93 tree := gtree.NewBTree(3, gutil.ComparatorString) 94 for i := 0; i < 6; i++ { 95 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 96 } 97 98 fmt.Println(tree.GetOrSetFunc("key1", func() interface{} { 99 return "newVal1" 100 })) 101 fmt.Println(tree.GetOrSetFunc("key6", func() interface{} { 102 return "val6" 103 })) 104 105 // Output: 106 // val1 107 // val6 108 } 109 110 func ExampleBTree_GetOrSetFuncLock() { 111 tree := gtree.NewBTree(3, gutil.ComparatorString) 112 for i := 0; i < 6; i++ { 113 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 114 } 115 116 fmt.Println(tree.GetOrSetFuncLock("key1", func() interface{} { 117 return "newVal1" 118 })) 119 fmt.Println(tree.GetOrSetFuncLock("key6", func() interface{} { 120 return "val6" 121 })) 122 123 // Output: 124 // val1 125 // val6 126 } 127 128 func ExampleBTree_GetVar() { 129 tree := gtree.NewBTree(3, gutil.ComparatorString) 130 for i := 0; i < 6; i++ { 131 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 132 } 133 134 fmt.Println(tree.GetVar("key1").String()) 135 136 // Output: 137 // val1 138 } 139 140 func ExampleBTree_GetVarOrSet() { 141 tree := gtree.NewBTree(3, gutil.ComparatorString) 142 for i := 0; i < 6; i++ { 143 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 144 } 145 146 fmt.Println(tree.GetVarOrSet("key1", "newVal1")) 147 fmt.Println(tree.GetVarOrSet("key6", "val6")) 148 149 // Output: 150 // val1 151 // val6 152 } 153 154 func ExampleBTree_GetVarOrSetFunc() { 155 tree := gtree.NewBTree(3, gutil.ComparatorString) 156 for i := 0; i < 6; i++ { 157 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 158 } 159 160 fmt.Println(tree.GetVarOrSetFunc("key1", func() interface{} { 161 return "newVal1" 162 })) 163 fmt.Println(tree.GetVarOrSetFunc("key6", func() interface{} { 164 return "val6" 165 })) 166 167 // Output: 168 // val1 169 // val6 170 } 171 172 func ExampleBTree_GetVarOrSetFuncLock() { 173 tree := gtree.NewBTree(3, gutil.ComparatorString) 174 for i := 0; i < 6; i++ { 175 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 176 } 177 178 fmt.Println(tree.GetVarOrSetFuncLock("key1", func() interface{} { 179 return "newVal1" 180 })) 181 fmt.Println(tree.GetVarOrSetFuncLock("key6", func() interface{} { 182 return "val6" 183 })) 184 185 // Output: 186 // val1 187 // val6 188 } 189 190 func ExampleBTree_SetIfNotExist() { 191 tree := gtree.NewBTree(3, gutil.ComparatorString) 192 for i := 0; i < 6; i++ { 193 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 194 } 195 196 fmt.Println(tree.SetIfNotExist("key1", "newVal1")) 197 fmt.Println(tree.SetIfNotExist("key6", "val6")) 198 199 // Output: 200 // false 201 // true 202 } 203 204 func ExampleBTree_SetIfNotExistFunc() { 205 tree := gtree.NewBTree(3, gutil.ComparatorString) 206 for i := 0; i < 6; i++ { 207 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 208 } 209 210 fmt.Println(tree.SetIfNotExistFunc("key1", func() interface{} { 211 return "newVal1" 212 })) 213 fmt.Println(tree.SetIfNotExistFunc("key6", func() interface{} { 214 return "val6" 215 })) 216 217 // Output: 218 // false 219 // true 220 } 221 222 func ExampleBTree_SetIfNotExistFuncLock() { 223 tree := gtree.NewBTree(3, gutil.ComparatorString) 224 for i := 0; i < 6; i++ { 225 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 226 } 227 228 fmt.Println(tree.SetIfNotExistFuncLock("key1", func() interface{} { 229 return "newVal1" 230 })) 231 fmt.Println(tree.SetIfNotExistFuncLock("key6", func() interface{} { 232 return "val6" 233 })) 234 235 // Output: 236 // false 237 // true 238 } 239 240 func ExampleBTree_Contains() { 241 tree := gtree.NewBTree(3, gutil.ComparatorString) 242 for i := 0; i < 6; i++ { 243 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 244 } 245 246 fmt.Println(tree.Contains("key1")) 247 fmt.Println(tree.Contains("key6")) 248 249 // Output: 250 // true 251 // false 252 } 253 254 func ExampleBTree_Remove() { 255 tree := gtree.NewBTree(3, gutil.ComparatorString) 256 for i := 0; i < 6; i++ { 257 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 258 } 259 260 fmt.Println(tree.Remove("key1")) 261 fmt.Println(tree.Remove("key6")) 262 fmt.Println(tree.Map()) 263 264 // Output: 265 // val1 266 // <nil> 267 // map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5] 268 } 269 270 func ExampleBTree_Removes() { 271 tree := gtree.NewBTree(3, gutil.ComparatorString) 272 for i := 0; i < 6; i++ { 273 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 274 } 275 276 removeKeys := make([]interface{}, 2) 277 removeKeys = append(removeKeys, "key1") 278 removeKeys = append(removeKeys, "key6") 279 280 tree.Removes(removeKeys) 281 282 fmt.Println(tree.Map()) 283 284 // Output: 285 // map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5] 286 } 287 288 func ExampleBTree_IsEmpty() { 289 tree := gtree.NewBTree(3, gutil.ComparatorString) 290 291 fmt.Println(tree.IsEmpty()) 292 293 for i := 0; i < 6; i++ { 294 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 295 } 296 297 fmt.Println(tree.IsEmpty()) 298 299 // Output: 300 // true 301 // false 302 } 303 304 func ExampleBTree_Size() { 305 tree := gtree.NewBTree(3, gutil.ComparatorString) 306 307 fmt.Println(tree.Size()) 308 309 for i := 0; i < 6; i++ { 310 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 311 } 312 313 fmt.Println(tree.Size()) 314 315 // Output: 316 // 0 317 // 6 318 } 319 320 func ExampleBTree_Keys() { 321 tree := gtree.NewBTree(3, gutil.ComparatorString) 322 for i := 6; i > 0; i-- { 323 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 324 } 325 326 fmt.Println(tree.Keys()) 327 328 // Output: 329 // [key1 key2 key3 key4 key5 key6] 330 } 331 332 func ExampleBTree_Values() { 333 tree := gtree.NewBTree(3, gutil.ComparatorString) 334 for i := 6; i > 0; i-- { 335 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 336 } 337 338 fmt.Println(tree.Values()) 339 340 // Output: 341 // [val1 val2 val3 val4 val5 val6] 342 } 343 344 func ExampleBTree_Map() { 345 tree := gtree.NewBTree(3, gutil.ComparatorString) 346 for i := 0; i < 6; i++ { 347 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 348 } 349 350 fmt.Println(tree.Map()) 351 352 // Output: 353 // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 354 } 355 356 func ExampleBTree_MapStrAny() { 357 tree := gtree.NewBTree(3, gutil.ComparatorString) 358 for i := 0; i < 6; i++ { 359 tree.Set(1000+i, "val"+gconv.String(i)) 360 } 361 362 fmt.Println(tree.MapStrAny()) 363 364 // Output: 365 // map[1000:val0 1001:val1 1002:val2 1003:val3 1004:val4 1005:val5] 366 } 367 368 func ExampleBTree_Clear() { 369 tree := gtree.NewBTree(3, gutil.ComparatorString) 370 for i := 0; i < 6; i++ { 371 tree.Set(1000+i, "val"+gconv.String(i)) 372 } 373 fmt.Println(tree.Size()) 374 375 tree.Clear() 376 fmt.Println(tree.Size()) 377 378 // Output: 379 // 6 380 // 0 381 } 382 383 func ExampleBTree_Replace() { 384 tree := gtree.NewBTree(3, gutil.ComparatorString) 385 for i := 0; i < 6; i++ { 386 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 387 } 388 389 fmt.Println(tree.Map()) 390 391 data := map[interface{}]interface{}{ 392 "newKey0": "newVal0", 393 "newKey1": "newVal1", 394 "newKey2": "newVal2", 395 } 396 397 tree.Replace(data) 398 399 fmt.Println(tree.Map()) 400 401 // Output: 402 // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 403 // map[newKey0:newVal0 newKey1:newVal1 newKey2:newVal2] 404 } 405 406 func ExampleBTree_Height() { 407 tree := gtree.NewBTree(3, gutil.ComparatorInt) 408 for i := 0; i < 100; i++ { 409 tree.Set(i, i) 410 } 411 fmt.Println(tree.Height()) 412 413 // Output: 414 // 6 415 } 416 417 func ExampleBTree_Left() { 418 tree := gtree.NewBTree(3, gutil.ComparatorInt) 419 for i := 1; i < 100; i++ { 420 tree.Set(i, i) 421 } 422 fmt.Println(tree.Left().Key, tree.Left().Value) 423 424 emptyTree := gtree.NewBTree(3, gutil.ComparatorInt) 425 fmt.Println(emptyTree.Left()) 426 427 // Output: 428 // 1 1 429 // <nil> 430 } 431 432 func ExampleBTree_Right() { 433 tree := gtree.NewBTree(3, gutil.ComparatorInt) 434 for i := 1; i < 100; i++ { 435 tree.Set(i, i) 436 } 437 fmt.Println(tree.Right().Key, tree.Right().Value) 438 439 emptyTree := gtree.NewBTree(3, gutil.ComparatorInt) 440 fmt.Println(emptyTree.Left()) 441 442 // Output: 443 // 99 99 444 // <nil> 445 } 446 447 func ExampleBTree_String() { 448 tree := gtree.NewBTree(3, gutil.ComparatorString) 449 for i := 0; i < 6; i++ { 450 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 451 } 452 453 fmt.Println(tree.String()) 454 455 // Output: 456 // key0 457 // key1 458 // key2 459 // key3 460 // key4 461 // key5 462 } 463 464 func ExampleBTree_Search() { 465 tree := gtree.NewBTree(3, gutil.ComparatorString) 466 for i := 0; i < 6; i++ { 467 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 468 } 469 470 fmt.Println(tree.Search("key0")) 471 fmt.Println(tree.Search("key6")) 472 473 // Output: 474 // val0 true 475 // <nil> false 476 } 477 478 func ExampleBTree_Print() { 479 tree := gtree.NewBTree(3, gutil.ComparatorString) 480 for i := 0; i < 6; i++ { 481 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 482 } 483 484 tree.Print() 485 486 // Output: 487 // key0 488 // key1 489 // key2 490 // key3 491 // key4 492 // key5 493 } 494 495 func ExampleBTree_Iterator() { 496 tree := gtree.NewBTree(3, gutil.ComparatorString) 497 for i := 0; i < 10; i++ { 498 tree.Set(i, 10-i) 499 } 500 501 var totalKey, totalValue int 502 tree.Iterator(func(key, value interface{}) bool { 503 totalKey += key.(int) 504 totalValue += value.(int) 505 506 return totalValue < 20 507 }) 508 509 fmt.Println("totalKey:", totalKey) 510 fmt.Println("totalValue:", totalValue) 511 512 // Output: 513 // totalKey: 3 514 // totalValue: 27 515 } 516 517 func ExampleBTree_IteratorFrom() { 518 m := make(map[interface{}]interface{}) 519 for i := 1; i <= 5; i++ { 520 m[i] = i * 10 521 } 522 tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m) 523 524 tree.IteratorFrom(1, true, func(key, value interface{}) bool { 525 fmt.Println("key:", key, ", value:", value) 526 return true 527 }) 528 529 // Output: 530 // key: 1 , value: 10 531 // key: 2 , value: 20 532 // key: 3 , value: 30 533 // key: 4 , value: 40 534 // key: 5 , value: 50 535 } 536 537 func ExampleBTree_IteratorAsc() { 538 tree := gtree.NewBTree(3, gutil.ComparatorString) 539 for i := 0; i < 10; i++ { 540 tree.Set(i, 10-i) 541 } 542 543 tree.IteratorAsc(func(key, value interface{}) bool { 544 fmt.Println("key:", key, ", value:", value) 545 return true 546 }) 547 548 // Output: 549 // key: 0 , value: 10 550 // key: 1 , value: 9 551 // key: 2 , value: 8 552 // key: 3 , value: 7 553 // key: 4 , value: 6 554 // key: 5 , value: 5 555 // key: 6 , value: 4 556 // key: 7 , value: 3 557 // key: 8 , value: 2 558 // key: 9 , value: 1 559 } 560 561 func ExampleBTree_IteratorAscFrom_Normal() { 562 m := make(map[interface{}]interface{}) 563 for i := 1; i <= 5; i++ { 564 m[i] = i * 10 565 } 566 tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m) 567 568 tree.IteratorAscFrom(1, true, func(key, value interface{}) bool { 569 fmt.Println("key:", key, ", value:", value) 570 return true 571 }) 572 573 // Output: 574 // key: 1 , value: 10 575 // key: 2 , value: 20 576 // key: 3 , value: 30 577 // key: 4 , value: 40 578 // key: 5 , value: 50 579 } 580 581 func ExampleBTree_IteratorAscFrom_NoExistKey() { 582 m := make(map[interface{}]interface{}) 583 for i := 1; i <= 5; i++ { 584 m[i] = i * 10 585 } 586 tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m) 587 588 tree.IteratorAscFrom(0, true, func(key, value interface{}) bool { 589 fmt.Println("key:", key, ", value:", value) 590 return true 591 }) 592 593 // Output: 594 } 595 596 func ExampleBTree_IteratorAscFrom_NoExistKeyAndMatchFalse() { 597 m := make(map[interface{}]interface{}) 598 for i := 1; i <= 5; i++ { 599 m[i] = i * 10 600 } 601 tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m) 602 603 tree.IteratorAscFrom(0, false, func(key, value interface{}) bool { 604 fmt.Println("key:", key, ", value:", value) 605 return true 606 }) 607 608 // Output: 609 // key: 1 , value: 10 610 // key: 2 , value: 20 611 // key: 3 , value: 30 612 // key: 4 , value: 40 613 // key: 5 , value: 50 614 } 615 616 func ExampleBTree_IteratorDesc() { 617 tree := gtree.NewBTree(3, gutil.ComparatorString) 618 for i := 0; i < 10; i++ { 619 tree.Set(i, 10-i) 620 } 621 622 tree.IteratorDesc(func(key, value interface{}) bool { 623 fmt.Println("key:", key, ", value:", value) 624 return true 625 }) 626 627 // Output: 628 // key: 9 , value: 1 629 // key: 8 , value: 2 630 // key: 7 , value: 3 631 // key: 6 , value: 4 632 // key: 5 , value: 5 633 // key: 4 , value: 6 634 // key: 3 , value: 7 635 // key: 2 , value: 8 636 // key: 1 , value: 9 637 // key: 0 , value: 10 638 } 639 640 func ExampleBTree_IteratorDescFrom() { 641 m := make(map[interface{}]interface{}) 642 for i := 1; i <= 5; i++ { 643 m[i] = i * 10 644 } 645 tree := gtree.NewBTreeFrom(3, gutil.ComparatorInt, m) 646 647 tree.IteratorDescFrom(5, true, func(key, value interface{}) bool { 648 fmt.Println("key:", key, ", value:", value) 649 return true 650 }) 651 652 // Output: 653 // key: 5 , value: 50 654 // key: 4 , value: 40 655 // key: 3 , value: 30 656 // key: 2 , value: 20 657 // key: 1 , value: 10 658 } 659 660 func ExampleBTree_MarshalJSON() { 661 tree := gtree.NewBTree(3, gutil.ComparatorString) 662 for i := 0; i < 6; i++ { 663 tree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 664 } 665 666 bytes, err := json.Marshal(tree) 667 if err == nil { 668 fmt.Println(gconv.String(bytes)) 669 } 670 671 // Output: 672 // {"key0":"val0","key1":"val1","key2":"val2","key3":"val3","key4":"val4","key5":"val5"} 673 }