ring.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package util
  2. // A Ring is an element of a circular list, or ring.
  3. // Rings do not have a beginning or end; a pointer to any ring element
  4. // serves as reference to the entire ring. Empty rings are represented
  5. // as nil Ring pointers. The zero value for a Ring is a one-element
  6. // ring with a nil Value.
  7. //
  8. type Ring[T any] struct {
  9. next, prev *Ring[T]
  10. Value T // for use by client; untouched by this library
  11. }
  12. func (r *Ring[T]) init() *Ring[T] {
  13. r.next = r
  14. r.prev = r
  15. return r
  16. }
  17. // Next returns the next ring element. r must not be empty.
  18. func (r *Ring[T]) Next() *Ring[T] {
  19. if r.next == nil {
  20. return r.init()
  21. }
  22. return r.next
  23. }
  24. // Prev returns the previous ring element. r must not be empty.
  25. func (r *Ring[T]) Prev() *Ring[T] {
  26. if r.next == nil {
  27. return r.init()
  28. }
  29. return r.prev
  30. }
  31. // Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0)
  32. // in the ring and returns that ring element. r must not be empty.
  33. //
  34. func (r *Ring[T]) Move(n int) *Ring[T] {
  35. if r.next == nil {
  36. return r.init()
  37. }
  38. switch {
  39. case n < 0:
  40. for ; n < 0; n++ {
  41. r = r.prev
  42. }
  43. case n > 0:
  44. for ; n > 0; n-- {
  45. r = r.next
  46. }
  47. }
  48. return r
  49. }
  50. // New creates a ring of n elements.
  51. func NewRing[T any](n int) *Ring[T] {
  52. if n <= 0 {
  53. return nil
  54. }
  55. r := new(Ring[T])
  56. p := r
  57. for i := 1; i < n; i++ {
  58. p.next = &Ring[T]{prev: p}
  59. p = p.next
  60. }
  61. p.next = r
  62. r.prev = p
  63. return r
  64. }
  65. // Link connects ring r with ring s such that r.Next()
  66. // becomes s and returns the original value for r.Next().
  67. // r must not be empty.
  68. //
  69. // If r and s point to the same ring, linking
  70. // them removes the elements between r and s from the ring.
  71. // The removed elements form a subring and the result is a
  72. // reference to that subring (if no elements were removed,
  73. // the result is still the original value for r.Next(),
  74. // and not nil).
  75. //
  76. // If r and s point to different rings, linking
  77. // them creates a single ring with the elements of s inserted
  78. // after r. The result points to the element following the
  79. // last element of s after insertion.
  80. //
  81. func (r *Ring[T]) Link(s *Ring[T]) *Ring[T] {
  82. n := r.Next()
  83. if s != nil {
  84. p := s.Prev()
  85. // Note: Cannot use multiple assignment because
  86. // evaluation order of LHS is not specified.
  87. r.next = s
  88. s.prev = r
  89. n.prev = p
  90. p.next = n
  91. }
  92. return n
  93. }
  94. // Unlink removes n % r.Len() elements from the ring r, starting
  95. // at r.Next(). If n % r.Len() == 0, r remains unchanged.
  96. // The result is the removed subring. r must not be empty.
  97. //
  98. func (r *Ring[T]) Unlink(n int) *Ring[T] {
  99. if n <= 0 {
  100. return nil
  101. }
  102. return r.Link(r.Move(n + 1))
  103. }
  104. // Len computes the number of elements in ring r.
  105. // It executes in time proportional to the number of elements.
  106. //
  107. func (r *Ring[T]) Len() int {
  108. n := 0
  109. if r != nil {
  110. n = 1
  111. for p := r.Next(); p != r; p = p.next {
  112. n++
  113. }
  114. }
  115. return n
  116. }
  117. // Do calls function f on each element of the ring, in forward order.
  118. // The behavior of Do is undefined if f changes *r.
  119. func (r *Ring[T]) Do(f func(T)) {
  120. if r != nil {
  121. f(r.Value)
  122. for p := r.Next(); p != r; p = p.next {
  123. f(p.Value)
  124. }
  125. }
  126. }