version: 1.10

package ring

import "container/ring"

Overview

Package ring implements operations on circular lists.

Index

Examples

Package files

ring.go

type Ring

  1. type Ring struct {
  2. Value interface{} // for use by client; untouched by this library
  3. // contains filtered or unexported fields
  4. }

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

  1. func New(n int) *Ring

New creates a ring of n elements.

func (*Ring) Do

  1. 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.


Example:

  1. // Create a new ring of size 5
  2. r := ring.New(5)
  3. // Get the length of the ring
  4. n := r.Len()
  5. // Initialize the ring with some integer values
  6. for i := 0; i < n; i++ {
  7. r.Value = i
  8. r = r.Next()
  9. }
  10. // Iterate through the ring and print its contents
  11. r.Do(func(p interface{}) {
  12. fmt.Println(p.(int))
  13. })
  14. // Output:
  15. // 0
  16. // 1
  17. // 2
  18. // 3
  19. // 4

func (*Ring) Len

  1. func (r *Ring) Len() int

Len computes the number of elements in ring r. It executes in time proportional
to the number of elements.


Example:

  1. // Create a new ring of size 4
  2. r := ring.New(4)
  3. // Print out its length
  4. fmt.Println(r.Len())
  5. // Output:
  6. // 4

  1. func (r *Ring) Link(s *Ring) *Ring

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.


Example:

  1. // Create two rings, r and s, of size 2
  2. r := ring.New(2)
  3. s := ring.New(2)
  4. // Get the length of the ring
  5. lr := r.Len()
  6. ls := s.Len()
  7. // Initialize r with 0s
  8. for i := 0; i < lr; i++ {
  9. r.Value = 0
  10. r = r.Next()
  11. }
  12. // Initialize s with 1s
  13. for j := 0; j < ls; j++ {
  14. s.Value = 1
  15. s = s.Next()
  16. }
  17. // Link ring r and ring s
  18. rs := r.Link(s)
  19. // Iterate through the combined ring and print its contents
  20. rs.Do(func(p interface{}) {
  21. fmt.Println(p.(int))
  22. })
  23. // Output:
  24. // 0
  25. // 0
  26. // 1
  27. // 1

func (*Ring) Move

  1. func (r *Ring) Move(n int) *Ring

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.


Example:

  1. // Create a new ring of size 5
  2. r := ring.New(5)
  3. // Get the length of the ring
  4. n := r.Len()
  5. // Initialize the ring with some integer values
  6. for i := 0; i < n; i++ {
  7. r.Value = i
  8. r = r.Next()
  9. }
  10. // Move the pointer forward by three steps
  11. r = r.Move(3)
  12. // Iterate through the ring and print its contents
  13. r.Do(func(p interface{}) {
  14. fmt.Println(p.(int))
  15. })
  16. // Output:
  17. // 3
  18. // 4
  19. // 0
  20. // 1
  21. // 2

func (*Ring) Next

  1. func (r *Ring) Next() *Ring

Next returns the next ring element. r must not be empty.


Example:

  1. // Create a new ring of size 5
  2. r := ring.New(5)
  3. // Get the length of the ring
  4. n := r.Len()
  5. // Initialize the ring with some integer values
  6. for i := 0; i < n; i++ {
  7. r.Value = i
  8. r = r.Next()
  9. }
  10. // Iterate through the ring and print its contents
  11. for j := 0; j < n; j++ {
  12. fmt.Println(r.Value)
  13. r = r.Next()
  14. }
  15. // Output:
  16. // 0
  17. // 1
  18. // 2
  19. // 3
  20. // 4

func (*Ring) Prev

  1. func (r *Ring) Prev() *Ring

Prev returns the previous ring element. r must not be empty.


Example:

  1. // Create a new ring of size 5
  2. r := ring.New(5)
  3. // Get the length of the ring
  4. n := r.Len()
  5. // Initialize the ring with some integer values
  6. for i := 0; i < n; i++ {
  7. r.Value = i
  8. r = r.Next()
  9. }
  10. // Iterate through the ring backwards and print its contents
  11. for j := 0; j < n; j++ {
  12. r = r.Prev()
  13. fmt.Println(r.Value)
  14. }
  15. // Output:
  16. // 4
  17. // 3
  18. // 2
  19. // 1
  20. // 0

  1. func (r *Ring) Unlink(n int) *Ring

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.


Example:

  1. // Create a new ring of size 6
  2. r := ring.New(6)
  3. // Get the length of the ring
  4. n := r.Len()
  5. // Initialize the ring with some integer values
  6. for i := 0; i < n; i++ {
  7. r.Value = i
  8. r = r.Next()
  9. }
  10. // Unlink three elements from r, starting from r.Next()
  11. r.Unlink(3)
  12. // Iterate through the remaining ring and print its contents
  13. r.Do(func(p interface{}) {
  14. fmt.Println(p.(int))
  15. })
  16. // Output:
  17. // 0
  18. // 4
  19. // 5