Skip to content

Commit

Permalink
container: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Aug 2, 2024
1 parent c414335 commit 61ea8b7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
25 changes: 12 additions & 13 deletions container/ringbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ type RingBuffer[T any] struct {
full bool
}

// NewRingBuffer initializes the new instance of ring buffer. size must be
// greater or equal to zero.
// NewRingBuffer initializes the new instance of ring buffer.
func NewRingBuffer[T any](size uint) (rb *RingBuffer[T]) {
return &RingBuffer[T]{
buf: make([]T, size),
}
}

// Append appends an element to the buffer and sets the current position to the
// Push appends an element to the buffer and sets the current position to the
// next element.
func (rb *RingBuffer[T]) Append(e T) {
func (rb *RingBuffer[T]) Push(e T) {
if len(rb.buf) == 0 {
return
}
Expand All @@ -39,36 +38,36 @@ func (rb *RingBuffer[T]) Current() (e T) {
return rb.buf[rb.cur]
}

// Range calls cb for each element of the buffer. If cb returns false it stops.
func (rb *RingBuffer[T]) Range(cb func(T) (cont bool)) {
// Range calls f for each element of the buffer. If f returns false it stops.
func (rb *RingBuffer[T]) Range(f func(T) (cont bool)) {
before, after := rb.splitCur()

for _, e := range before {
if !cb(e) {
if !f(e) {
return
}
}

for _, e := range after {
if !cb(e) {
if !f(e) {
return
}
}
}

// ReverseRange calls cb for each element of the buffer in reverse order. If
// cb returns false it stops.
func (rb *RingBuffer[T]) ReverseRange(cb func(T) (cont bool)) {
// ReverseRange calls f for each element of the buffer in reverse order. If f
// returns false it stops.
func (rb *RingBuffer[T]) ReverseRange(f func(T) (cont bool)) {
before, after := rb.splitCur()

for i := len(after) - 1; i >= 0; i-- {
if !cb(after[i]) {
if !f(after[i]) {
return
}
}

for i := len(before) - 1; i >= 0; i-- {
if !cb(before[i]) {
if !f(before[i]) {
return
}
}
Expand Down
6 changes: 3 additions & 3 deletions container/ringbuffer_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ func ExampleRingBuffer_Current() {
rb = container.NewRingBuffer[int](size)
fmt.Printf("empty: %#v\n", rb.Current())

rb.Append(x)
rb.Push(x)
fmt.Printf("append %d: %#v\n", x, rb.Current())

rb.Append(y)
rb.Push(y)
fmt.Printf("append %d: %#v\n", y, rb.Current())

rb.Append(z)
rb.Push(z)
fmt.Printf("append %d: %#v\n", z, rb.Current())
fmt.Printf("current: %#v\n", rb.Current())

Expand Down
10 changes: 5 additions & 5 deletions container/ringbuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestNewRingBuffer(t *testing.T) {
t.Run("success_and_clear", func(t *testing.T) {
b := container.NewRingBuffer[int](5)
for i := range 10 {
b.Append(i)
b.Push(i)
}
assert.Equal(t, []int{5, 6, 7, 8, 9}, elements(b, b.Len(), false))

Expand All @@ -45,7 +45,7 @@ func TestNewRingBuffer(t *testing.T) {
t.Run("zero", func(t *testing.T) {
b := container.NewRingBuffer[int](0)
for i := range 10 {
b.Append(i)
b.Push(i)
bufLen := b.Len()
assert.EqualValues(t, 0, bufLen)
assert.Empty(t, elements(b, bufLen, false))
Expand All @@ -56,7 +56,7 @@ func TestNewRingBuffer(t *testing.T) {
t.Run("single", func(t *testing.T) {
b := container.NewRingBuffer[int](1)
for i := range 10 {
b.Append(i)
b.Push(i)
bufLen := b.Len()
assert.EqualValues(t, 1, bufLen)
assert.Equal(t, []int{i}, elements(b, bufLen, false))
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestRingBuffer_Range(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i := range tc.count {
b.Append(i)
b.Push(i)
}

bufLen := b.Len()
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestRingBuffer_Range_increment(t *testing.T) {

for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b.Append(i)
b.Push(i)
bufLen := b.Len()
assert.Equal(t, tc.want, elements(b, bufLen, false))

Expand Down

0 comments on commit 61ea8b7

Please sign in to comment.