Package ring implements operations on circular lists.

Package files

ring.go

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

  1. func New(n ) *Ring

New creates a ring of n elements.

  1. func (r *) 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.

  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()

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


Example:

  1. func (r *) Link(s *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. lr := r.Len()
  5. ls := s.Len()
  6. // Initialize r with 0s
  7. for i := 0; i < lr; i++ {
  8. r.Value = 0
  9. r = r.Next()
  10. }
  11. // Initialize s with 1s
  12. for j := 0; j < ls; j++ {
  13. s.Value = 1
  14. s = s.Next()
  15. }
  16. // Link ring r and ring s
  17. rs := r.Link(s)
  18. // Iterate through the combined ring and print its contents
  19. rs.Do(func(p interface{}) {
  20. fmt.Println(p.(int))
  21. })
  22. // Output:
  23. // 0
  24. // 0
  25. // 1
  26. // 1

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


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. // 0
  19. // 1
  20. // 2

func (*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. n := r.Len()
  4. // Initialize the ring with some integer values
  5. for i := 0; i < n; i++ {
  6. r.Value = i
  7. r = r.Next()
  8. }
  9. // Iterate through the ring and print its contents
  10. for j := 0; j < n; j++ {
  11. fmt.Println(r.Value)
  12. r = r.Next()
  13. }
  14. // Output:
  15. // 0
  16. // 1
  17. // 2
  18. // 3
  19. // 4

func (*Ring) Prev

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

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

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

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.