-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] TRK-2258 Add FIFO queue (#29)
- Loading branch information
Showing
4 changed files
with
198 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/msales/gox | ||
|
||
go 1.16 | ||
go 1.21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package queuex | ||
|
||
import "sync" | ||
|
||
// FIFO is simple implementation of FIFO (first-in-first-out) queue with locks. | ||
type FIFO[T any] struct { | ||
m sync.Mutex | ||
|
||
vals []T | ||
} | ||
|
||
// NewFIFO initializes queue with values. | ||
func NewFIFO[T any](vals ...T) *FIFO[T] { | ||
return &FIFO[T]{vals: vals} | ||
} | ||
|
||
// Push adds elements at the end of the queue. | ||
func (f *FIFO[T]) Push(v T) *FIFO[T] { | ||
f.m.Lock() | ||
defer f.m.Unlock() | ||
|
||
f.vals = append(f.vals, v) | ||
return f | ||
} | ||
|
||
// Pop returns first element from the queue. | ||
// Bool is also returned to indicate if queue has run out of the values. | ||
func (f *FIFO[T]) Pop() (T, bool) { | ||
f.m.Lock() | ||
defer f.m.Unlock() | ||
|
||
if len(f.vals) == 0 { | ||
var t T | ||
return t, false | ||
} | ||
|
||
v := f.vals[0] | ||
f.vals = f.vals[1:] | ||
return v, true | ||
} | ||
|
||
// Len returns length of queue. | ||
func (f *FIFO[T]) Len() int { | ||
f.m.Lock() | ||
defer f.m.Unlock() | ||
|
||
return len(f.vals) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package queuex_test | ||
|
||
import ( | ||
"github.com/msales/gox/queuex" | ||
"testing" | ||
) | ||
|
||
func TestFIFO(t *testing.T) { | ||
queue := queuex.NewFIFO([]int{ | ||
1, 2, | ||
}...) | ||
|
||
queue.Push(3) | ||
|
||
got, ok := queue.Pop() | ||
wantVal := 1 | ||
wantOK := true | ||
if wantVal != got { | ||
t.Errorf("Got %+v, want %+v", got, wantVal) | ||
} | ||
|
||
if ok != wantOK { | ||
t.Errorf("Got %+v, want %+v", ok, wantOK) | ||
} | ||
|
||
got, ok = queue.Pop() | ||
wantVal = 2 | ||
wantOK = true | ||
if wantVal != got { | ||
t.Errorf("Got %+v, want %+v", got, wantVal) | ||
} | ||
|
||
if ok != wantOK { | ||
t.Errorf("Got %+v, want %+v", ok, wantOK) | ||
} | ||
|
||
got, ok = queue.Pop() | ||
wantVal = 3 | ||
wantOK = true | ||
if wantVal != got { | ||
t.Errorf("Got %+v, want %+v", got, wantVal) | ||
} | ||
|
||
if ok != wantOK { | ||
t.Errorf("Got %+v, want %+v", ok, wantOK) | ||
} | ||
|
||
got, ok = queue.Pop() | ||
wantVal = 0 | ||
wantOK = false | ||
if wantVal != got { | ||
t.Errorf("Got %+v, want %+v", got, wantVal) | ||
} | ||
|
||
if ok != wantOK { | ||
t.Errorf("Got %+v, want %+v", ok, wantOK) | ||
} | ||
} | ||
|
||
func TestFIFO_EmptyQueue(t *testing.T) { | ||
queue := queuex.NewFIFO[int]() | ||
|
||
got, ok := queue.Pop() | ||
wantVal := 0 | ||
wantOK := false | ||
if wantVal != got { | ||
t.Errorf("Got %+v, want %+v", got, wantVal) | ||
} | ||
|
||
if ok != wantOK { | ||
t.Errorf("Got %+v, want %+v", ok, wantOK) | ||
} | ||
|
||
got, ok = queue.Pop() | ||
if wantVal != got { | ||
t.Errorf("Got %+v, want %+v", got, wantVal) | ||
} | ||
|
||
if ok != wantOK { | ||
t.Errorf("Got %+v, want %+v", ok, wantOK) | ||
} | ||
} | ||
|
||
func BenchmarkFIFO_Pop(b *testing.B) { | ||
vals := make([]int, 1000) | ||
for i := 0; i < 1000; i++ { | ||
vals[i] = i | ||
} | ||
|
||
queue := queuex.NewFIFO(vals...) | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
queue.Pop() | ||
} | ||
} | ||
|
||
func BenchmarkFIFO_Push(b *testing.B) { | ||
vals := make([]int, 1000) | ||
for i := 0; i < 1000; i++ { | ||
vals[i] = i | ||
} | ||
|
||
queue := queuex.NewFIFO(vals...) | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
queue.Push(i) | ||
} | ||
} | ||
|
||
func BenchmarkFIFO_EmptyPush(b *testing.B) { | ||
queue := queuex.NewFIFO[int]() | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
queue.Push(i) | ||
} | ||
} | ||
|
||
func BenchmarkFIFO_RandomPushPop(b *testing.B) { | ||
vals := make([]int, 1000) | ||
for i := 0; i < 1000; i++ { | ||
vals[i] = i | ||
} | ||
|
||
queue := queuex.NewFIFO(vals...) | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
if i%2 == 0 { | ||
queue.Pop() | ||
continue | ||
} else { | ||
queue.Push(i) | ||
} | ||
} | ||
} |