Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement the version with generics #1

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kevwan/stream

go 1.17
go 1.18

require github.com/stretchr/testify v1.7.0

Expand Down
16 changes: 8 additions & 8 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ package stream
import "sync"

// A Ring can be used as fixed size ring.
type Ring struct {
elements []interface{}
type Ring[T any] struct {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
expected ']', found any

elements []T
index int
lock sync.Mutex
}

// NewRing returns a Ring object with the given size n.
func NewRing(n int) *Ring {
func NewRing[T any](n int) *Ring[T] {
if n < 1 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
expected declaration, found 'if'

panic("n should be greater than 0")
}

return &Ring{
elements: make([]interface{}, n),
return &Ring[T]{
elements: make([]T, n),
}
}

// Add adds v into r.
func (r *Ring) Add(v interface{}) {
func (r *Ring[T]) Add(v T) {
r.lock.Lock()
defer r.lock.Unlock()

Expand All @@ -30,7 +30,7 @@ func (r *Ring) Add(v interface{}) {
}

// Take takes all items from r.
func (r *Ring) Take() []interface{} {
func (r *Ring[T]) Take() []T {
r.lock.Lock()
defer r.lock.Unlock()

Expand All @@ -43,7 +43,7 @@ func (r *Ring) Take() []interface{} {
size = r.index
}

elements := make([]interface{}, size)
elements := make([]T, size)
for i := 0; i < size; i++ {
elements[i] = r.elements[(start+i)%len(r.elements)]
}
Expand Down
10 changes: 5 additions & 5 deletions ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

func TestNewRing(t *testing.T) {
assert.Panics(t, func() {
NewRing(0)
NewRing[int](0)
})
}

func TestRingLess(t *testing.T) {
ring := NewRing(5)
ring := NewRing[int](5)
for i := 0; i < 3; i++ {
ring.Add(i)
}
Expand All @@ -23,7 +23,7 @@ func TestRingLess(t *testing.T) {
}

func TestRingMore(t *testing.T) {
ring := NewRing(5)
ring := NewRing[int](5)
for i := 0; i < 11; i++ {
ring.Add(i)
}
Expand All @@ -32,7 +32,7 @@ func TestRingMore(t *testing.T) {
}

func TestRingAdd(t *testing.T) {
ring := NewRing(5051)
ring := NewRing[int](5051)
wg := sync.WaitGroup{}
for i := 1; i <= 100; i++ {
wg.Add(1)
Expand All @@ -48,7 +48,7 @@ func TestRingAdd(t *testing.T) {
}

func BenchmarkRingAdd(b *testing.B) {
ring := NewRing(500)
ring := NewRing[int](500)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
for i := 0; i < b.N; i++ {
Expand Down
Loading