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
5package ring_test
6
7import (
8	"container/ring"
9	"fmt"
10)
11
12func ExampleRing_Len() {
13	// Create a new ring of size 4
14	r := ring.New(4)
15
16	// Print out its length
17	fmt.Println(r.Len())
18
19	// Output:
20	// 4
21}
22
23func ExampleRing_Next() {
24	// Create a new ring of size 5
25	r := ring.New(5)
26
27	// Get the length of the ring
28	n := r.Len()
29
30	// Initialize the ring with some integer values
31	for i := 0; i < n; i++ {
32		r.Value = i
33		r = r.Next()
34	}
35
36	// Iterate through the ring and print its contents
37	for j := 0; j < n; j++ {
38		fmt.Println(r.Value)
39		r = r.Next()
40	}
41
42	// Output:
43	// 0
44	// 1
45	// 2
46	// 3
47	// 4
48}
49
50func ExampleRing_Prev() {
51	// Create a new ring of size 5
52	r := ring.New(5)
53
54	// Get the length of the ring
55	n := r.Len()
56
57	// Initialize the ring with some integer values
58	for i := 0; i < n; i++ {
59		r.Value = i
60		r = r.Next()
61	}
62
63	// Iterate through the ring backwards and print its contents
64	for j := 0; j < n; j++ {
65		r = r.Prev()
66		fmt.Println(r.Value)
67	}
68
69	// Output:
70	// 4
71	// 3
72	// 2
73	// 1
74	// 0
75}
76
77func ExampleRing_Do() {
78	// Create a new ring of size 5
79	r := ring.New(5)
80
81	// Get the length of the ring
82	n := r.Len()
83
84	// Initialize the ring with some integer values
85	for i := 0; i < n; i++ {
86		r.Value = i
87		r = r.Next()
88	}
89
90	// Iterate through the ring and print its contents
91	r.Do(func(p any) {
92		fmt.Println(p.(int))
93	})
94
95	// Output:
96	// 0
97	// 1
98	// 2
99	// 3
100	// 4
101}
102
103func ExampleRing_Move() {
104	// Create a new ring of size 5
105	r := ring.New(5)
106
107	// Get the length of the ring
108	n := r.Len()
109
110	// Initialize the ring with some integer values
111	for i := 0; i < n; i++ {
112		r.Value = i
113		r = r.Next()
114	}
115
116	// Move the pointer forward by three steps
117	r = r.Move(3)
118
119	// Iterate through the ring and print its contents
120	r.Do(func(p any) {
121		fmt.Println(p.(int))
122	})
123
124	// Output:
125	// 3
126	// 4
127	// 0
128	// 1
129	// 2
130}
131
132func ExampleRing_Link() {
133	// Create two rings, r and s, of size 2
134	r := ring.New(2)
135	s := ring.New(2)
136
137	// Get the length of the ring
138	lr := r.Len()
139	ls := s.Len()
140
141	// Initialize r with 0s
142	for i := 0; i < lr; i++ {
143		r.Value = 0
144		r = r.Next()
145	}
146
147	// Initialize s with 1s
148	for j := 0; j < ls; j++ {
149		s.Value = 1
150		s = s.Next()
151	}
152
153	// Link ring r and ring s
154	rs := r.Link(s)
155
156	// Iterate through the combined ring and print its contents
157	rs.Do(func(p any) {
158		fmt.Println(p.(int))
159	})
160
161	// Output:
162	// 0
163	// 0
164	// 1
165	// 1
166}
167
168func ExampleRing_Unlink() {
169	// Create a new ring of size 6
170	r := ring.New(6)
171
172	// Get the length of the ring
173	n := r.Len()
174
175	// Initialize the ring with some integer values
176	for i := 0; i < n; i++ {
177		r.Value = i
178		r = r.Next()
179	}
180
181	// Unlink three elements from r, starting from r.Next()
182	r.Unlink(3)
183
184	// Iterate through the remaining ring and print its contents
185	r.Do(func(p any) {
186		fmt.Println(p.(int))
187	})
188
189	// Output:
190	// 0
191	// 4
192	// 5
193}
194