mirror of https://github.com/v2ray/v2ray-core
parent
1a10f7c4da
commit
4624de091b
@ -1,24 +0,0 @@
|
||||
package collect
|
||||
|
||||
type SizedQueue struct {
|
||||
elements []interface{}
|
||||
nextPos int
|
||||
}
|
||||
|
||||
func NewSizedQueue(size int) *SizedQueue {
|
||||
return &SizedQueue{
|
||||
elements: make([]interface{}, size),
|
||||
nextPos: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// Put puts a new element into the queue and pop out the first element if queue is full.
|
||||
func (this *SizedQueue) Put(element interface{}) interface{} {
|
||||
res := this.elements[this.nextPos]
|
||||
this.elements[this.nextPos] = element
|
||||
this.nextPos++
|
||||
if this.nextPos == len(this.elements) {
|
||||
this.nextPos = 0
|
||||
}
|
||||
return res
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package collect_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/collect"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
|
||||
func TestSizedQueue(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
queue := collect.NewSizedQueue(2)
|
||||
assert.Pointer(queue.Put(1)).IsNil()
|
||||
assert.Pointer(queue.Put(2)).IsNil()
|
||||
assert.Int(queue.Put(3).(int)).Equals(1)
|
||||
}
|
Loading…
Reference in new issue