github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/utils/ring/ring.go (about)

     1  package ring
     2  
     3  import "container/ring"
     4  
     5  type Ring[T any] struct {
     6  	r *ring.Ring
     7  }
     8  
     9  func NewRing[T any](n int, initValue func() T) *Ring[T] {
    10  	r := ring.New(n)
    11  
    12  	r.Value = initValue()
    13  	for p := r.Next(); p != r; p = p.Next() {
    14  		p.Value = initValue()
    15  	}
    16  
    17  	return &Ring[T]{r: r}
    18  }
    19  
    20  func (r *Ring[T]) Next() *Ring[T] {
    21  	rr := r.r.Next()
    22  
    23  	return &Ring[T]{
    24  		r: rr,
    25  	}
    26  }
    27  
    28  func (r *Ring[T]) Value() T {
    29  	v := r.r.Value
    30  	return v.(T)
    31  }