github.com/wangyougui/gf/v2@v2.6.5/container/gring/gring_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 gring_test
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/wangyougui/gf/v2/container/gring"
    13  )
    14  
    15  func ExampleNew() {
    16  	// Non concurrent safety
    17  	gring.New(10)
    18  
    19  	// Concurrent safety
    20  	gring.New(10, true)
    21  
    22  	// Output:
    23  }
    24  
    25  func ExampleRing_Val() {
    26  	r := gring.New(10)
    27  	r.Set(1)
    28  	fmt.Println("Val:", r.Val())
    29  
    30  	r.Next().Set("GoFrame")
    31  	fmt.Println("Val:", r.Val())
    32  
    33  	// Output:
    34  	// Val: 1
    35  	// Val: GoFrame
    36  }
    37  
    38  func ExampleRing_Len() {
    39  	r1 := gring.New(10)
    40  	for i := 0; i < 5; i++ {
    41  		r1.Set(i).Next()
    42  	}
    43  	fmt.Println("Len:", r1.Len())
    44  
    45  	r2 := gring.New(10, true)
    46  	for i := 0; i < 10; i++ {
    47  		r2.Set(i).Next()
    48  	}
    49  	fmt.Println("Len:", r2.Len())
    50  
    51  	// Output:
    52  	// Len: 5
    53  	// Len: 10
    54  }
    55  
    56  func ExampleRing_Cap() {
    57  	r1 := gring.New(10)
    58  	for i := 0; i < 5; i++ {
    59  		r1.Set(i).Next()
    60  	}
    61  	fmt.Println("Cap:", r1.Cap())
    62  
    63  	r2 := gring.New(10, true)
    64  	for i := 0; i < 10; i++ {
    65  		r2.Set(i).Next()
    66  	}
    67  	fmt.Println("Cap:", r2.Cap())
    68  
    69  	// Output:
    70  	// Cap: 10
    71  	// Cap: 10
    72  }
    73  
    74  func ExampleRing_Set() {
    75  	r := gring.New(10)
    76  	r.Set(1)
    77  	fmt.Println("Val:", r.Val())
    78  
    79  	r.Next().Set("GoFrame")
    80  	fmt.Println("Val:", r.Val())
    81  
    82  	// Output:
    83  	// Val: 1
    84  	// Val: GoFrame
    85  }
    86  
    87  func ExampleRing_Put() {
    88  	r := gring.New(10)
    89  	r.Put(1)
    90  	fmt.Println("Val:", r.Val())
    91  	fmt.Println("Val:", r.Prev().Val())
    92  
    93  	// Output:
    94  	// Val: <nil>
    95  	// Val: 1
    96  }
    97  
    98  func ExampleRing_Move() {
    99  	r := gring.New(10)
   100  	for i := 0; i < 10; i++ {
   101  		r.Set(i).Next()
   102  	}
   103  	// ring at Pos 0
   104  	fmt.Println("CurVal:", r.Val())
   105  
   106  	r.Move(5)
   107  
   108  	// ring at Pos 5
   109  	fmt.Println("CurVal:", r.Val())
   110  
   111  	// Output:
   112  	// CurVal: 0
   113  	// CurVal: 5
   114  }
   115  
   116  func ExampleRing_Prev() {
   117  	r := gring.New(10)
   118  	for i := 0; i < 5; i++ {
   119  		r.Set(i).Next()
   120  	}
   121  
   122  	fmt.Println("Prev:", r.Prev().Val())
   123  	fmt.Println("Prev:", r.Prev().Val())
   124  
   125  	// Output:
   126  	// Prev: 4
   127  	// Prev: 3
   128  }
   129  
   130  func ExampleRing_Next() {
   131  	r := gring.New(10)
   132  	for i := 5; i > 0; i-- {
   133  		r.Set(i).Prev()
   134  	}
   135  
   136  	fmt.Println("Prev:", r.Next().Val())
   137  	fmt.Println("Prev:", r.Next().Val())
   138  
   139  	// Output:
   140  	// Prev: 1
   141  	// Prev: 2
   142  }
   143  
   144  func ExampleRing_Link_Common() {
   145  	r := gring.New(10)
   146  	for i := 0; i < 5; i++ {
   147  		r.Set(i).Next()
   148  	}
   149  
   150  	s := gring.New(10)
   151  	for i := 0; i < 10; i++ {
   152  		val := i + 5
   153  		s.Set(val).Next()
   154  	}
   155  
   156  	r.Link(s) // Link Ring s to Ring r
   157  
   158  	fmt.Println("Len:", r.Len())
   159  	fmt.Println("Cap:", r.Cap())
   160  	fmt.Println(r.SlicePrev())
   161  	fmt.Println(r.SliceNext())
   162  
   163  	// Output:
   164  	// Len: 15
   165  	// Cap: 20
   166  	// [4 3 2 1 0]
   167  	// [5 6 7 8 9 10 11 12 13 14]
   168  }
   169  
   170  func ExampleRing_Link_SameRing() {
   171  	r := gring.New(10)
   172  	for i := 0; i < 5; i++ {
   173  		r.Set(i).Next()
   174  	}
   175  
   176  	same_r := r.Link(r.Prev())
   177  
   178  	fmt.Println("Len:", same_r.Len())
   179  	fmt.Println("Cap:", same_r.Cap())
   180  	fmt.Println(same_r.SlicePrev())
   181  	fmt.Println(same_r.SliceNext())
   182  
   183  	// Output:
   184  	// Len: 1
   185  	// Cap: 1
   186  	// [4]
   187  	// [4]
   188  }
   189  
   190  func ExampleRing_Unlink() {
   191  	r := gring.New(10)
   192  	for i := 0; i < 10; i++ {
   193  		r.Set(i).Next()
   194  	}
   195  
   196  	fmt.Println("Before Unlink, Len:", r.Len())
   197  	fmt.Println("Before Unlink, Cap:", r.Cap())
   198  	fmt.Println("Before Unlink, ", r.SlicePrev())
   199  	fmt.Println("Before Unlink, ", r.SliceNext())
   200  
   201  	r.Unlink(7)
   202  
   203  	fmt.Println("After Unlink, Len:", r.Len())
   204  	fmt.Println("After Unlink, Cap:", r.Cap())
   205  	fmt.Println("After Unlink, ", r.SlicePrev())
   206  	fmt.Println("After Unlink, ", r.SliceNext())
   207  
   208  	// Output:
   209  	// Before Unlink, Len: 10
   210  	// Before Unlink, Cap: 10
   211  	// Before Unlink,  [0 9 8 7 6 5 4 3 2 1]
   212  	// Before Unlink,  [0 1 2 3 4 5 6 7 8 9]
   213  	// After Unlink, Len: 3
   214  	// After Unlink, Cap: 3
   215  	// After Unlink,  [0 9 8]
   216  	// After Unlink,  [0 8 9]
   217  }
   218  
   219  func ExampleRing_RLockIteratorNext() {
   220  	r := gring.New(10)
   221  	for i := 0; i < 10; i++ {
   222  		r.Set(i).Next()
   223  	}
   224  
   225  	r.RLockIteratorNext(func(value interface{}) bool {
   226  		if value.(int) < 5 {
   227  			fmt.Println("IteratorNext Success, Value:", value)
   228  			return true
   229  		}
   230  
   231  		return false
   232  	})
   233  
   234  	// Output:
   235  	// IteratorNext Success, Value: 0
   236  	// IteratorNext Success, Value: 1
   237  	// IteratorNext Success, Value: 2
   238  	// IteratorNext Success, Value: 3
   239  	// IteratorNext Success, Value: 4
   240  }
   241  
   242  func ExampleRing_RLockIteratorPrev() {
   243  	r := gring.New(10)
   244  	for i := 0; i < 10; i++ {
   245  		r.Set(i).Next()
   246  	}
   247  
   248  	// move r to pos 9
   249  	r.Prev()
   250  
   251  	r.RLockIteratorPrev(func(value interface{}) bool {
   252  		if value.(int) >= 5 {
   253  			fmt.Println("IteratorPrev Success, Value:", value)
   254  			return true
   255  		}
   256  
   257  		return false
   258  	})
   259  
   260  	// Output:
   261  	// IteratorPrev Success, Value: 9
   262  	// IteratorPrev Success, Value: 8
   263  	// IteratorPrev Success, Value: 7
   264  	// IteratorPrev Success, Value: 6
   265  	// IteratorPrev Success, Value: 5
   266  }
   267  
   268  func ExampleRing_SliceNext() {
   269  	r := gring.New(10)
   270  	for i := 0; i < 10; i++ {
   271  		r.Set(i).Next()
   272  	}
   273  
   274  	fmt.Println(r.SliceNext())
   275  
   276  	// Output:
   277  	// [0 1 2 3 4 5 6 7 8 9]
   278  }
   279  
   280  func ExampleRing_SlicePrev() {
   281  	r := gring.New(10)
   282  	for i := 0; i < 10; i++ {
   283  		r.Set(i).Next()
   284  	}
   285  
   286  	fmt.Println(r.SlicePrev())
   287  
   288  	// Output:
   289  	// [0 9 8 7 6 5 4 3 2 1]
   290  }