version: 1.10
package ring
import "container/ring"
Overview
Package ring implements operations on circular lists.
Index
Examples
Package files
type Ring ¶
- type Ring struct {
- Value interface{} // for use by client; untouched by this library
- // contains filtered or unexported fields
- }
A Ring is an element of a circular list, or ring. Rings do not have a beginning or end; a pointer to any ring element serves as reference to the entire ring. Empty rings are represented as nil Ring pointers. The zero value for a Ring is a one-element ring with a nil Value.
func New ¶
New creates a ring of n elements.
func (*Ring) Do ¶
- func (r *Ring) Do(f func(interface{}))
Do calls function f on each element of the ring, in forward order. The behavior of Do is undefined if f changes *r.
// Create a new ring of size 5r := ring.New(5)// Get the length of the ringn := r.Len()// Initialize the ring with some integer valuesfor i := 0; i < n; i++ {r.Value = ir = r.Next()}// Iterate through the ring and print its contentsr.Do(func(p interface{}) {fmt.Println(p.(int))})// Output:// 0// 1// 2// 3// 4
func (*Ring) Len ¶
Len computes the number of elements in ring r. It executes in time proportional to the number of elements.
// Create a new ring of size 4r := ring.New(4)// Print out its lengthfmt.Println(r.Len())// Output:// 4
func (*Ring) Link ¶
Link connects ring r with ring s such that r.Next() becomes s and returns the original value for r.Next(). r must not be empty.
If r and s point to the same ring, linking them removes the elements between r and s from the ring. The removed elements form a subring and the result is a reference to that subring (if no elements were removed, the result is still the original value for r.Next(), and not nil).
If r and s point to different rings, linking them creates a single ring with the elements of s inserted after r. The result points to the element following the last element of s after insertion.
// Create two rings, r and s, of size 2r := ring.New(2)s := ring.New(2)// Get the length of the ringlr := r.Len()ls := s.Len()// Initialize r with 0sfor i := 0; i < lr; i++ {r.Value = 0r = r.Next()}// Initialize s with 1sfor j := 0; j < ls; j++ {s.Value = 1s = s.Next()}// Link ring r and ring srs := r.Link(s)// Iterate through the combined ring and print its contentsrs.Do(func(p interface{}) {fmt.Println(p.(int))})// Output:// 0// 0// 1// 1
func (*Ring) Move ¶
Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) in the ring and returns that ring element. r must not be empty.
// Create a new ring of size 5r := ring.New(5)// Get the length of the ringn := r.Len()// Initialize the ring with some integer valuesfor i := 0; i < n; i++ {r.Value = ir = r.Next()}// Move the pointer forward by three stepsr = r.Move(3)// Iterate through the ring and print its contentsr.Do(func(p interface{}) {fmt.Println(p.(int))})// Output:// 3// 4// 0// 1// 2
func (*Ring) Next ¶
Next returns the next ring element. r must not be empty.
// Create a new ring of size 5r := ring.New(5)// Get the length of the ringn := r.Len()// Initialize the ring with some integer valuesfor i := 0; i < n; i++ {r.Value = ir = r.Next()}// Iterate through the ring and print its contentsfor j := 0; j < n; j++ {fmt.Println(r.Value)r = r.Next()}// Output:// 0// 1// 2// 3// 4
func (*Ring) Prev ¶
Prev returns the previous ring element. r must not be empty.
// Create a new ring of size 5r := ring.New(5)// Get the length of the ringn := r.Len()// Initialize the ring with some integer valuesfor i := 0; i < n; i++ {r.Value = ir = r.Next()}// Iterate through the ring backwards and print its contentsfor j := 0; j < n; j++ {r = r.Prev()fmt.Println(r.Value)}// Output:// 4// 3// 2// 1// 0
func (*Ring) Unlink ¶
Unlink removes n % r.Len() elements from the ring r, starting at r.Next(). If n % r.Len() == 0, r remains unchanged. The result is the removed subring. r must not be empty.
// Create a new ring of size 6r := ring.New(6)// Get the length of the ringn := r.Len()// Initialize the ring with some integer valuesfor i := 0; i < n; i++ {r.Value = ir = r.Next()}// Unlink three elements from r, starting from r.Next()r.Unlink(3)// Iterate through the remaining ring and print its contentsr.Do(func(p interface{}) {fmt.Println(p.(int))})// Output:// 0// 4// 5
