Files
engine/common/ring.go
2023-04-15 08:37:22 +08:00

42 lines
749 B
Go

package common
import (
"m7s.live/engine/v4/util"
)
type RingBuffer[T any] struct {
*util.Ring[T] `json:"-" yaml:"-"`
Size int
MoveCount uint32
LastValue *T
}
func (rb *RingBuffer[T]) Init(n int) *RingBuffer[T] {
if rb == nil {
rb = new(RingBuffer[T])
}
rb.Ring = util.NewRing[T](n)
rb.Size = n
rb.LastValue = &rb.Value
return rb
}
func (rb *RingBuffer[T]) MoveNext() *T {
rb.LastValue = &rb.Value
rb.Ring = rb.Next()
rb.MoveCount++
return &rb.Value
}
func (rb *RingBuffer[T]) Glow(size int) (newItem *util.Ring[T]) {
newItem = rb.Link(util.NewRing[T](size))
rb.Size += size
return
}
func (rb *RingBuffer[T]) Reduce(size int) (newItem *util.Ring[T]) {
newItem = rb.Unlink(size)
rb.Size -= size
return
}