Skip to content
Merged
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
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: go
sudo: false

go:
- 1.2
- 1.3
- 1.4
- 1.2
- 1.3
- 1.4
10 changes: 7 additions & 3 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ type Queue struct {

// New constructs and returns a new Queue.
func New() *Queue {
return &Queue{buf: make([]interface{}, minQueueLen)}
return &Queue{
buf: make([]interface{}, minQueueLen),
}
}

// Length returns the number of elements currently stored in the queue.
func (q *Queue) Length() int {
return q.count
}

// resizes the queue to fit exactly twice its current contents
// this can result in shrinking if the queue is less than half-full
func (q *Queue) resize() {
newBuf := make([]interface{}, q.count*2)

Expand Down Expand Up @@ -63,7 +67,7 @@ func (q *Queue) Peek() interface{} {
// Get returns the element at index i in the queue. If the index is
// invalid, the call will panic.
func (q *Queue) Get(i int) interface{} {
if i >= q.count || i < 0 {
if i < 0 || i >= q.count {
panic("queue: Get() called with index out of range")
}
return q.buf[(q.head+i)%len(q.buf)]
Expand All @@ -78,7 +82,7 @@ func (q *Queue) Remove() {
q.buf[q.head] = nil
q.head = (q.head + 1) % len(q.buf)
q.count--
if len(q.buf) > minQueueLen && q.count*4 <= len(q.buf) {
if len(q.buf) > minQueueLen && q.count*4 == len(q.buf) {
q.resize()
}
}
2 changes: 0 additions & 2 deletions queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ func assertPanics(t *testing.T, name string, f func()) {
defer func() {
if r := recover(); r == nil {
t.Errorf("%s: didn't panic as expected", name)
} else {
t.Logf("%s: got panic as expected: %v", name, r)
}
}()

Expand Down