-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
128 additions
and
7 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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package sctp | ||
|
||
import "fmt" | ||
|
||
type queue[T any] struct { | ||
buf []T | ||
head int | ||
tail int | ||
count int | ||
} | ||
|
||
const minCap = 16 | ||
|
||
func newQueue[T any](capacity int) *queue[T] { | ||
queueCap := minCap | ||
for queueCap < capacity { | ||
queueCap <<= 1 | ||
} | ||
|
||
return &queue[T]{ | ||
buf: make([]T, queueCap), | ||
} | ||
} | ||
|
||
func (q *queue[T]) Len() int { | ||
return q.count | ||
} | ||
|
||
func (q *queue[T]) PushBack(ele T) { | ||
q.growIfFull() | ||
q.buf[q.tail] = ele | ||
q.tail = (q.tail + 1) % len(q.buf) | ||
q.count++ | ||
} | ||
|
||
func (q *queue[T]) PopFront() T { | ||
if q.count <= 0 { | ||
panic("PopFront() called on empty queue") | ||
} | ||
ele := q.buf[q.head] | ||
var zeroVal T | ||
q.buf[q.head] = zeroVal | ||
q.head = (q.head + 1) % len(q.buf) | ||
q.count-- | ||
return ele | ||
} | ||
|
||
func (q *queue[T]) Front() T { | ||
if q.count <= 0 { | ||
panic("Front() called on empty queue") | ||
} | ||
return q.buf[q.head] | ||
} | ||
|
||
func (q *queue[T]) At(i int) T { | ||
if i < 0 || i >= q.count { | ||
panic(fmt.Sprintf("index %d out of range %d", i, q.count)) | ||
} | ||
return q.buf[(q.head+i)%(len(q.buf))] | ||
} | ||
|
||
func (q *queue[T]) growIfFull() { | ||
if q.count < len(q.buf) { | ||
return | ||
} | ||
|
||
newBuf := make([]T, q.count<<1) | ||
if q.tail > q.head { | ||
copy(newBuf, q.buf[q.head:q.tail]) | ||
} else { | ||
n := copy(newBuf, q.buf[q.head:]) | ||
copy(newBuf[n:], q.buf[:q.tail]) | ||
} | ||
|
||
q.head = 0 | ||
q.tail = q.count | ||
q.buf = newBuf | ||
} |
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 sctp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestQueue(t *testing.T) { | ||
q := newQueue[int](32) | ||
assert.Panics(t, func() { q.PopFront() }) | ||
assert.Panics(t, func() { q.Front() }) | ||
assert.Zero(t, q.Len()) | ||
|
||
// test push & pop | ||
for i := 1; i < 33; i++ { | ||
q.PushBack(i) | ||
} | ||
assert.Equal(t, 32, q.Len()) | ||
assert.Equal(t, 5, q.At(4)) | ||
for i := 1; i < 33; i++ { | ||
assert.Equal(t, i, q.Front()) | ||
assert.Equal(t, i, q.PopFront()) | ||
} | ||
assert.Zero(t, q.Len()) | ||
|
||
q.PushBack(10) | ||
q.PushBack(11) | ||
assert.Equal(t, 2, q.Len()) | ||
assert.Equal(t, 11, q.At(1)) | ||
assert.Equal(t, 10, q.Front()) | ||
assert.Panics(t, func() { | ||
q.At(33) | ||
}) | ||
assert.Equal(t, 10, q.PopFront()) | ||
assert.Equal(t, 11, q.PopFront()) | ||
|
||
// test grow capacity | ||
for i := 0; i < 64; i++ { | ||
q.PushBack(i) | ||
} | ||
assert.Equal(t, 64, q.Len()) | ||
assert.Equal(t, 2, q.At(2)) | ||
for i := 0; i < 64; i++ { | ||
assert.Equal(t, i, q.Front()) | ||
assert.Equal(t, i, q.PopFront()) | ||
} | ||
} |