example_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ring_test
  5. import (
  6. "container/ring"
  7. "fmt"
  8. )
  9. func ExampleRing_Len() {
  10. // Create a new ring of size 4
  11. r := ring.New(4)
  12. // Print out its length
  13. fmt.Println(r.Len())
  14. // Output:
  15. // 4
  16. }
  17. func ExampleRing_Next() {
  18. // Create a new ring of size 5
  19. r := ring.New(5)
  20. // Get the length of the ring
  21. n := r.Len()
  22. // Initialize the ring with some integer values
  23. for i := 0; i < n; i++ {
  24. r.Value = i
  25. r = r.Next()
  26. }
  27. // Iterate through the ring and print its contents
  28. for j := 0; j < n; j++ {
  29. fmt.Println(r.Value)
  30. r = r.Next()
  31. }
  32. // Output:
  33. // 0
  34. // 1
  35. // 2
  36. // 3
  37. // 4
  38. }
  39. func ExampleRing_Prev() {
  40. // Create a new ring of size 5
  41. r := ring.New(5)
  42. // Get the length of the ring
  43. n := r.Len()
  44. // Initialize the ring with some integer values
  45. for i := 0; i < n; i++ {
  46. r.Value = i
  47. r = r.Next()
  48. }
  49. // Iterate through the ring backwards and print its contents
  50. for j := 0; j < n; j++ {
  51. r = r.Prev()
  52. fmt.Println(r.Value)
  53. }
  54. // Output:
  55. // 4
  56. // 3
  57. // 2
  58. // 1
  59. // 0
  60. }
  61. func ExampleRing_Do() {
  62. // Create a new ring of size 5
  63. r := ring.New(5)
  64. // Get the length of the ring
  65. n := r.Len()
  66. // Initialize the ring with some integer values
  67. for i := 0; i < n; i++ {
  68. r.Value = i
  69. r = r.Next()
  70. }
  71. // Iterate through the ring and print its contents
  72. r.Do(func(p any) {
  73. fmt.Println(p.(int))
  74. })
  75. // Output:
  76. // 0
  77. // 1
  78. // 2
  79. // 3
  80. // 4
  81. }
  82. func ExampleRing_Move() {
  83. // Create a new ring of size 5
  84. r := ring.New(5)
  85. // Get the length of the ring
  86. n := r.Len()
  87. // Initialize the ring with some integer values
  88. for i := 0; i < n; i++ {
  89. r.Value = i
  90. r = r.Next()
  91. }
  92. // Move the pointer forward by three steps
  93. r = r.Move(3)
  94. // Iterate through the ring and print its contents
  95. r.Do(func(p any) {
  96. fmt.Println(p.(int))
  97. })
  98. // Output:
  99. // 3
  100. // 4
  101. // 0
  102. // 1
  103. // 2
  104. }
  105. func ExampleRing_Link() {
  106. // Create two rings, r and s, of size 2
  107. r := ring.New(2)
  108. s := ring.New(2)
  109. // Get the length of the ring
  110. lr := r.Len()
  111. ls := s.Len()
  112. // Initialize r with 0s
  113. for i := 0; i < lr; i++ {
  114. r.Value = 0
  115. r = r.Next()
  116. }
  117. // Initialize s with 1s
  118. for j := 0; j < ls; j++ {
  119. s.Value = 1
  120. s = s.Next()
  121. }
  122. // Link ring r and ring s
  123. rs := r.Link(s)
  124. // Iterate through the combined ring and print its contents
  125. rs.Do(func(p any) {
  126. fmt.Println(p.(int))
  127. })
  128. // Output:
  129. // 0
  130. // 0
  131. // 1
  132. // 1
  133. }
  134. func ExampleRing_Unlink() {
  135. // Create a new ring of size 6
  136. r := ring.New(6)
  137. // Get the length of the ring
  138. n := r.Len()
  139. // Initialize the ring with some integer values
  140. for i := 0; i < n; i++ {
  141. r.Value = i
  142. r = r.Next()
  143. }
  144. // Unlink three elements from r, starting from r.Next()
  145. r.Unlink(3)
  146. // Iterate through the remaining ring and print its contents
  147. r.Do(func(p any) {
  148. fmt.Println(p.(int))
  149. })
  150. // Output:
  151. // 0
  152. // 4
  153. // 5
  154. }