github.com/wangyougui/gf/v2@v2.6.5/container/glist/glist_z_example_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 this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package glist_test 8 9 import ( 10 "container/list" 11 "fmt" 12 13 "github.com/wangyougui/gf/v2/container/garray" 14 "github.com/wangyougui/gf/v2/container/glist" 15 "github.com/wangyougui/gf/v2/frame/g" 16 ) 17 18 func ExampleNew() { 19 n := 10 20 l := glist.New() 21 for i := 0; i < n; i++ { 22 l.PushBack(i) 23 } 24 25 fmt.Println(l.Len()) 26 fmt.Println(l) 27 fmt.Println(l.FrontAll()) 28 fmt.Println(l.BackAll()) 29 30 for i := 0; i < n; i++ { 31 fmt.Print(l.PopFront()) 32 } 33 34 fmt.Println() 35 fmt.Println(l.Len()) 36 37 // Output: 38 // 10 39 // [0,1,2,3,4,5,6,7,8,9] 40 // [0 1 2 3 4 5 6 7 8 9] 41 // [9 8 7 6 5 4 3 2 1 0] 42 // 0123456789 43 // 0 44 } 45 46 func ExampleNewFrom() { 47 n := 10 48 l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice()) 49 50 fmt.Println(l.Len()) 51 fmt.Println(l) 52 fmt.Println(l.FrontAll()) 53 fmt.Println(l.BackAll()) 54 55 for i := 0; i < n; i++ { 56 fmt.Print(l.PopFront()) 57 } 58 59 fmt.Println() 60 fmt.Println(l.Len()) 61 62 // Output: 63 // 10 64 // [1,2,3,4,5,6,7,8,9,10] 65 // [1 2 3 4 5 6 7 8 9 10] 66 // [10 9 8 7 6 5 4 3 2 1] 67 // 12345678910 68 // 0 69 } 70 71 func ExampleList_PushFront() { 72 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 73 74 fmt.Println(l.Len()) 75 fmt.Println(l) 76 77 l.PushFront(0) 78 79 fmt.Println(l.Len()) 80 fmt.Println(l) 81 82 // Output: 83 // 5 84 // [1,2,3,4,5] 85 // 6 86 // [0,1,2,3,4,5] 87 } 88 89 func ExampleList_PushBack() { 90 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 91 92 fmt.Println(l.Len()) 93 fmt.Println(l) 94 95 l.PushBack(6) 96 97 fmt.Println(l.Len()) 98 fmt.Println(l) 99 100 // Output: 101 // 5 102 // [1,2,3,4,5] 103 // 6 104 // [1,2,3,4,5,6] 105 } 106 107 func ExampleList_PushFronts() { 108 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 109 110 fmt.Println(l.Len()) 111 fmt.Println(l) 112 113 l.PushFronts(g.Slice{0, -1, -2, -3, -4}) 114 115 fmt.Println(l.Len()) 116 fmt.Println(l) 117 118 // Output: 119 // 5 120 // [1,2,3,4,5] 121 // 10 122 // [-4,-3,-2,-1,0,1,2,3,4,5] 123 } 124 125 func ExampleList_PushBacks() { 126 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 127 128 fmt.Println(l.Len()) 129 fmt.Println(l) 130 131 l.PushBacks(g.Slice{6, 7, 8, 9, 10}) 132 133 fmt.Println(l.Len()) 134 fmt.Println(l) 135 136 // Output: 137 // 5 138 // [1,2,3,4,5] 139 // 10 140 // [1,2,3,4,5,6,7,8,9,10] 141 } 142 143 func ExampleList_PopBack() { 144 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 145 146 fmt.Println(l.Len()) 147 fmt.Println(l) 148 fmt.Println(l.PopBack()) 149 fmt.Println(l.Len()) 150 fmt.Println(l) 151 152 // Output: 153 // 5 154 // [1,2,3,4,5] 155 // 5 156 // 4 157 // [1,2,3,4] 158 } 159 160 func ExampleList_PopFront() { 161 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 162 163 fmt.Println(l.Len()) 164 fmt.Println(l) 165 fmt.Println(l.PopFront()) 166 fmt.Println(l.Len()) 167 fmt.Println(l) 168 169 // Output: 170 // 5 171 // [1,2,3,4,5] 172 // 1 173 // 4 174 // [2,3,4,5] 175 } 176 177 func ExampleList_PopBacks() { 178 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 179 180 fmt.Println(l.Len()) 181 fmt.Println(l) 182 fmt.Println(l.PopBacks(2)) 183 fmt.Println(l.Len()) 184 fmt.Println(l) 185 186 // Output: 187 // 5 188 // [1,2,3,4,5] 189 // [5 4] 190 // 3 191 // [1,2,3] 192 } 193 194 func ExampleList_PopFronts() { 195 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 196 197 fmt.Println(l.Len()) 198 fmt.Println(l) 199 fmt.Println(l.PopFronts(2)) 200 fmt.Println(l.Len()) 201 fmt.Println(l) 202 203 // Output: 204 // 5 205 // [1,2,3,4,5] 206 // [1 2] 207 // 3 208 // [3,4,5] 209 } 210 211 func ExampleList_PopBackAll() { 212 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 213 214 fmt.Println(l.Len()) 215 fmt.Println(l) 216 fmt.Println(l.PopBackAll()) 217 fmt.Println(l.Len()) 218 219 // Output: 220 // 5 221 // [1,2,3,4,5] 222 // [5 4 3 2 1] 223 // 0 224 } 225 226 func ExampleList_PopFrontAll() { 227 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 228 229 fmt.Println(l.Len()) 230 fmt.Println(l) 231 fmt.Println(l.PopFrontAll()) 232 fmt.Println(l.Len()) 233 234 // Output: 235 // 5 236 // [1,2,3,4,5] 237 // [1 2 3 4 5] 238 // 0 239 } 240 241 func ExampleList_FrontAll() { 242 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 243 244 fmt.Println(l) 245 fmt.Println(l.FrontAll()) 246 247 // Output: 248 // [1,2,3,4,5] 249 // [1 2 3 4 5] 250 } 251 252 func ExampleList_BackAll() { 253 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 254 255 fmt.Println(l) 256 fmt.Println(l.BackAll()) 257 258 // Output: 259 // [1,2,3,4,5] 260 // [5 4 3 2 1] 261 } 262 263 func ExampleList_FrontValue() { 264 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 265 266 fmt.Println(l) 267 fmt.Println(l.FrontValue()) 268 269 // Output: 270 // [1,2,3,4,5] 271 // 1 272 } 273 274 func ExampleList_BackValue() { 275 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 276 277 fmt.Println(l) 278 fmt.Println(l.BackValue()) 279 280 // Output: 281 // [1,2,3,4,5] 282 // 5 283 } 284 285 func ExampleList_Front() { 286 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 287 288 fmt.Println(l.Front().Value) 289 fmt.Println(l) 290 291 e := l.Front() 292 l.InsertBefore(e, 0) 293 l.InsertAfter(e, "a") 294 295 fmt.Println(l) 296 297 // Output: 298 // 1 299 // [1,2,3,4,5] 300 // [0,1,a,2,3,4,5] 301 } 302 303 func ExampleList_Back() { 304 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 305 306 fmt.Println(l.Back().Value) 307 fmt.Println(l) 308 309 e := l.Back() 310 l.InsertBefore(e, "a") 311 l.InsertAfter(e, 6) 312 313 fmt.Println(l) 314 315 // Output: 316 // 5 317 // [1,2,3,4,5] 318 // [1,2,3,4,a,5,6] 319 } 320 321 func ExampleList_Len() { 322 l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5}) 323 324 fmt.Println(l.Len()) 325 326 // Output: 327 // 5 328 } 329 330 func ExampleList_Size() { 331 l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5}) 332 333 fmt.Println(l.Size()) 334 335 // Output: 336 // 5 337 } 338 339 func ExampleList_MoveBefore() { 340 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 341 342 fmt.Println(l.Size()) 343 fmt.Println(l) 344 345 // element of `l` 346 e := l.PushBack(6) 347 fmt.Println(l.Size()) 348 fmt.Println(l) 349 350 l.MoveBefore(e, l.Front()) 351 352 fmt.Println(l.Size()) 353 fmt.Println(l) 354 355 // not element of `l` 356 e = &glist.Element{Value: 7} 357 l.MoveBefore(e, l.Front()) 358 359 fmt.Println(l.Size()) 360 fmt.Println(l) 361 362 // Output: 363 // 5 364 // [1,2,3,4,5] 365 // 6 366 // [1,2,3,4,5,6] 367 // 6 368 // [6,1,2,3,4,5] 369 // 6 370 // [6,1,2,3,4,5] 371 } 372 373 func ExampleList_MoveAfter() { 374 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 375 376 fmt.Println(l.Size()) 377 fmt.Println(l) 378 379 // element of `l` 380 e := l.PushFront(0) 381 fmt.Println(l.Size()) 382 fmt.Println(l) 383 384 l.MoveAfter(e, l.Back()) 385 386 fmt.Println(l.Size()) 387 fmt.Println(l) 388 389 // not element of `l` 390 e = &glist.Element{Value: -1} 391 l.MoveAfter(e, l.Back()) 392 393 fmt.Println(l.Size()) 394 fmt.Println(l) 395 396 // Output: 397 // 5 398 // [1,2,3,4,5] 399 // 6 400 // [0,1,2,3,4,5] 401 // 6 402 // [1,2,3,4,5,0] 403 // 6 404 // [1,2,3,4,5,0] 405 } 406 407 func ExampleList_MoveToFront() { 408 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 409 410 fmt.Println(l.Size()) 411 fmt.Println(l) 412 413 // element of `l` 414 l.MoveToFront(l.Back()) 415 416 fmt.Println(l.Size()) 417 fmt.Println(l) 418 419 // not element of `l` 420 e := &glist.Element{Value: 6} 421 l.MoveToFront(e) 422 423 fmt.Println(l.Size()) 424 fmt.Println(l) 425 426 // Output: 427 // 5 428 // [1,2,3,4,5] 429 // 5 430 // [5,1,2,3,4] 431 // 5 432 // [5,1,2,3,4] 433 } 434 435 func ExampleList_MoveToBack() { 436 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 437 438 fmt.Println(l.Size()) 439 fmt.Println(l) 440 441 // element of `l` 442 l.MoveToBack(l.Front()) 443 444 fmt.Println(l.Size()) 445 fmt.Println(l) 446 447 // not element of `l` 448 e := &glist.Element{Value: 0} 449 l.MoveToBack(e) 450 451 fmt.Println(l.Size()) 452 fmt.Println(l) 453 454 // Output: 455 // 5 456 // [1,2,3,4,5] 457 // 5 458 // [2,3,4,5,1] 459 // 5 460 // [2,3,4,5,1] 461 } 462 463 func ExampleList_PushBackList() { 464 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 465 466 fmt.Println(l.Size()) 467 fmt.Println(l) 468 469 other := glist.NewFrom(g.Slice{6, 7, 8, 9, 10}) 470 471 fmt.Println(other.Size()) 472 fmt.Println(other) 473 474 l.PushBackList(other) 475 476 fmt.Println(l.Size()) 477 fmt.Println(l) 478 479 // Output: 480 // 5 481 // [1,2,3,4,5] 482 // 5 483 // [6,7,8,9,10] 484 // 10 485 // [1,2,3,4,5,6,7,8,9,10] 486 } 487 488 func ExampleList_PushFrontList() { 489 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 490 491 fmt.Println(l.Size()) 492 fmt.Println(l) 493 494 other := glist.NewFrom(g.Slice{-4, -3, -2, -1, 0}) 495 496 fmt.Println(other.Size()) 497 fmt.Println(other) 498 499 l.PushFrontList(other) 500 501 fmt.Println(l.Size()) 502 fmt.Println(l) 503 504 // Output: 505 // 5 506 // [1,2,3,4,5] 507 // 5 508 // [-4,-3,-2,-1,0] 509 // 10 510 // [-4,-3,-2,-1,0,1,2,3,4,5] 511 } 512 513 func ExampleList_InsertAfter() { 514 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 515 516 fmt.Println(l.Len()) 517 fmt.Println(l) 518 519 l.InsertAfter(l.Front(), "a") 520 l.InsertAfter(l.Back(), "b") 521 522 fmt.Println(l.Len()) 523 fmt.Println(l) 524 525 // Output: 526 // 5 527 // [1,2,3,4,5] 528 // 7 529 // [1,a,2,3,4,5,b] 530 } 531 532 func ExampleList_InsertBefore() { 533 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 534 535 fmt.Println(l.Len()) 536 fmt.Println(l) 537 538 l.InsertBefore(l.Front(), "a") 539 l.InsertBefore(l.Back(), "b") 540 541 fmt.Println(l.Len()) 542 fmt.Println(l) 543 544 // Output: 545 // 5 546 // [1,2,3,4,5] 547 // 7 548 // [a,1,2,3,4,b,5] 549 } 550 551 func ExampleList_Remove() { 552 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 553 554 fmt.Println(l.Len()) 555 fmt.Println(l) 556 557 fmt.Println(l.Remove(l.Front())) 558 fmt.Println(l.Remove(l.Back())) 559 560 fmt.Println(l.Len()) 561 fmt.Println(l) 562 563 // Output: 564 // 5 565 // [1,2,3,4,5] 566 // 1 567 // 5 568 // 3 569 // [2,3,4] 570 } 571 572 func ExampleList_Removes() { 573 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 574 575 fmt.Println(l.Len()) 576 fmt.Println(l) 577 578 l.Removes([]*glist.Element{l.Front(), l.Back()}) 579 580 fmt.Println(l.Len()) 581 fmt.Println(l) 582 583 // Output: 584 // 5 585 // [1,2,3,4,5] 586 // 3 587 // [2,3,4] 588 } 589 590 func ExampleList_RemoveAll() { 591 l := glist.NewFrom(garray.NewArrayRange(1, 5, 1).Slice()) 592 593 fmt.Println(l.Len()) 594 fmt.Println(l) 595 596 l.RemoveAll() 597 598 fmt.Println(l.Len()) 599 600 // Output: 601 // 5 602 // [1,2,3,4,5] 603 // 0 604 } 605 606 func ExampleList_RLockFunc() { 607 // concurrent-safe list. 608 l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) 609 // iterate reading from head. 610 l.RLockFunc(func(list *list.List) { 611 length := list.Len() 612 if length > 0 { 613 for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() { 614 fmt.Print(e.Value) 615 } 616 } 617 }) 618 fmt.Println() 619 // iterate reading from tail. 620 l.RLockFunc(func(list *list.List) { 621 length := list.Len() 622 if length > 0 { 623 for i, e := 0, list.Back(); i < length; i, e = i+1, e.Prev() { 624 fmt.Print(e.Value) 625 } 626 } 627 }) 628 629 fmt.Println() 630 // Output: 631 // 12345678910 632 // 10987654321 633 } 634 635 func ExampleList_IteratorAsc() { 636 // concurrent-safe list. 637 l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) 638 // iterate reading from head using IteratorAsc. 639 l.IteratorAsc(func(e *glist.Element) bool { 640 fmt.Print(e.Value) 641 return true 642 }) 643 644 // Output: 645 // 12345678910 646 } 647 648 func ExampleList_IteratorDesc() { 649 // concurrent-safe list. 650 l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) 651 // iterate reading from tail using IteratorDesc. 652 l.IteratorDesc(func(e *glist.Element) bool { 653 fmt.Print(e.Value) 654 return true 655 }) 656 // Output: 657 // 10987654321 658 } 659 660 func ExampleList_LockFunc() { 661 // concurrent-safe list. 662 l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) 663 // iterate writing from head. 664 l.LockFunc(func(list *list.List) { 665 length := list.Len() 666 if length > 0 { 667 for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() { 668 if e.Value == 6 { 669 e.Value = "M" 670 break 671 } 672 } 673 } 674 }) 675 fmt.Println(l) 676 677 // Output: 678 // [1,2,3,4,5,M,7,8,9,10] 679 } 680 681 func ExampleList_Join() { 682 var l glist.List 683 l.PushBacks(g.Slice{"a", "b", "c", "d"}) 684 685 fmt.Println(l.Join(",")) 686 687 // Output: 688 // a,b,c,d 689 }