github.com/gogf/gf@v1.16.9/container/glist/glist_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/gogf/gf.
     6  
     7  package glist_test
     8  
     9  import (
    10  	"container/list"
    11  	"fmt"
    12  	"github.com/gogf/gf/container/garray"
    13  	"github.com/gogf/gf/frame/g"
    14  
    15  	"github.com/gogf/gf/container/glist"
    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  	fmt.Println(l.Len())
    25  	fmt.Println(l.FrontAll())
    26  	fmt.Println(l.BackAll())
    27  	for i := 0; i < n; i++ {
    28  		fmt.Print(l.PopFront())
    29  	}
    30  	l.Clear()
    31  	fmt.Println()
    32  	fmt.Println(l.Len())
    33  
    34  	// Output:
    35  	// 10
    36  	// [0 1 2 3 4 5 6 7 8 9]
    37  	// [9 8 7 6 5 4 3 2 1 0]
    38  	// 0123456789
    39  	// 0
    40  }
    41  
    42  func ExampleList_RLockFunc() {
    43  	// concurrent-safe list.
    44  	l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true)
    45  	// iterate reading from head.
    46  	l.RLockFunc(func(list *list.List) {
    47  		length := list.Len()
    48  		if length > 0 {
    49  			for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() {
    50  				fmt.Print(e.Value)
    51  			}
    52  		}
    53  	})
    54  	fmt.Println()
    55  	// iterate reading from tail.
    56  	l.RLockFunc(func(list *list.List) {
    57  		length := list.Len()
    58  		if length > 0 {
    59  			for i, e := 0, list.Back(); i < length; i, e = i+1, e.Prev() {
    60  				fmt.Print(e.Value)
    61  			}
    62  		}
    63  	})
    64  
    65  	fmt.Println()
    66  	// Output:
    67  	// 12345678910
    68  	// 10987654321
    69  }
    70  
    71  func ExampleList_IteratorAsc() {
    72  	// concurrent-safe list.
    73  	l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true)
    74  	// iterate reading from head using IteratorAsc.
    75  	l.IteratorAsc(func(e *glist.Element) bool {
    76  		fmt.Print(e.Value)
    77  		return true
    78  	})
    79  
    80  	// Output:
    81  	// 12345678910
    82  }
    83  
    84  func ExampleList_IteratorDesc() {
    85  	// concurrent-safe list.
    86  	l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true)
    87  	// iterate reading from tail using IteratorDesc.
    88  	l.IteratorDesc(func(e *glist.Element) bool {
    89  		fmt.Print(e.Value)
    90  		return true
    91  	})
    92  	// Output:
    93  	// 10987654321
    94  }
    95  
    96  func ExampleList_LockFunc() {
    97  	// concurrent-safe list.
    98  	l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true)
    99  	// iterate writing from head.
   100  	l.LockFunc(func(list *list.List) {
   101  		length := list.Len()
   102  		if length > 0 {
   103  			for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() {
   104  				if e.Value == 6 {
   105  					e.Value = "M"
   106  					break
   107  				}
   108  			}
   109  		}
   110  	})
   111  	fmt.Println(l)
   112  
   113  	// Output:
   114  	// [1,2,3,4,5,M,7,8,9,10]
   115  }
   116  
   117  func ExampleList_PopBack() {
   118  	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
   119  
   120  	fmt.Println(l.PopBack())
   121  
   122  	// Output:
   123  	// 9
   124  }
   125  func ExampleList_PopBacks() {
   126  	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
   127  
   128  	fmt.Println(l.PopBacks(2))
   129  
   130  	// Output:
   131  	// [9 8]
   132  }
   133  
   134  func ExampleList_PopFront() {
   135  	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
   136  
   137  	fmt.Println(l.PopFront())
   138  
   139  	// Output:
   140  	// 1
   141  }
   142  
   143  func ExampleList_PopFronts() {
   144  	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
   145  
   146  	fmt.Println(l.PopFronts(2))
   147  
   148  	// Output:
   149  	// [1 2]
   150  }
   151  
   152  func ExampleList_Join() {
   153  	var l glist.List
   154  	l.PushBacks(g.Slice{"a", "b", "c", "d"})
   155  
   156  	fmt.Println(l.Join(","))
   157  
   158  	// Output:
   159  	// a,b,c,d
   160  }