Skip to content

Commit

Permalink
Ring2 impl Drop, At
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon-mint committed Jan 8, 2023
1 parent 3b3d594 commit 6be7dd2
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
17 changes: 17 additions & 0 deletions container2/ring2/ring2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ type Ring2[T any] struct {
r, w uint64
}

// NewRing2 creates a new ring with the given capacity.
func NewRing2[T any](size uint64) *Ring2[T] {
return &Ring2[T]{data: make([]T, size)}
}

// Len returns the number of elements in the ring.
func (r *Ring2[T]) Len() uint64 {
return r.w - r.r
}

// Write writes data to the ring. If the ring is full, ok is false.
func (r *Ring2[T]) Write(data T) (ok bool) {
if r.w-r.r >= uint64(len(r.data)) {
return false
Expand All @@ -22,6 +25,7 @@ func (r *Ring2[T]) Write(data T) (ok bool) {
return true
}

// Read reads the next element from the ring. If the ring is empty, ok is false.
func (r *Ring2[T]) Read() (data T, ok bool) {
if r.w == r.r {
return
Expand All @@ -31,14 +35,27 @@ func (r *Ring2[T]) Read() (data T, ok bool) {
return data, true
}

// Reset resets the ring to empty.
func (r *Ring2[T]) Reset() {
r.r, r.w = 0, 0
}

// Cap returns the capacity of the ring.
func (r *Ring2[T]) Cap() uint64 {
return uint64(len(r.data))
}

// Free returns the number of elements that can be written to the ring.
func (r *Ring2[T]) Free() uint64 {
return r.Cap() - r.Len()
}

// At returns the element at index i, where i is relative to the read index.
func (r *Ring2[T]) At(i uint64) T {
return r.data[(r.r+i)%r.Cap()]
}

// Drop drops the first n elements from the ring.
func (r *Ring2[T]) Drop(n uint64) {
r.r += n
}
78 changes: 78 additions & 0 deletions container2/ring2/ring2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,81 @@ func TestRing2(t *testing.T) {
t.Errorf("expected read to fail")
}
}

func TestRing2At(t *testing.T) {
r := ring2.NewRing2[uint64](3)
r.Write(1)
r.Write(2)
r.Write(3)
r.Read()
r.Read()
r.Write(4)
r.Write(5)

if r.At(0) != 3 {
t.Errorf("expected data 3, got %d", r.At(0))
}

if r.At(1) != 4 {
t.Errorf("expected data 4, got %d", r.At(1))
}

if r.At(2) != 5 {
t.Errorf("expected data 5, got %d", r.At(2))
}
}

func TestRing2Drop(t *testing.T) {
r := ring2.NewRing2[uint64](3)
r.Write(1)
r.Write(2)
r.Write(3)
r.Read()
r.Read()
r.Write(4)
r.Write(5)
r.Drop(2)

if r.Len() != 1 {
t.Errorf("expected len 1, got %d", r.Len())
}

if r.Free() != 2 {
t.Errorf("expected free 2, got %d", r.Free())
}

if r.At(0) != 5 {
t.Errorf("expected data 5, got %d", r.At(0))
}

r.Reset()

if r.Len() != 0 {
t.Errorf("expected len 0, got %d", r.Len())
}

if r.Free() != 3 {
t.Errorf("expected free 3, got %d", r.Free())
}

r.Write(100)
r.Write(200)
r.Write(300)
r.Drop(1)

if r.Len() != 2 {
t.Errorf("expected len 2, got %d", r.Len())
}

if r.Free() != 1 {
t.Errorf("expected free 1, got %d", r.Free())
}

if r.At(0) != 200 {
t.Errorf("expected data 200, got %d", r.At(0))
}

if r.At(1) != 300 {
t.Errorf("expected data 300, got %d", r.At(1))
}
}

0 comments on commit 6be7dd2

Please sign in to comment.