Merge pull request #4 from eapache/cleanup
Cleanup
Evan Huus
8 years ago
16 | 16 | |
17 | 17 | // New constructs and returns a new Queue. |
18 | 18 | func New() *Queue { |
19 | return &Queue{buf: make([]interface{}, minQueueLen)} | |
19 | return &Queue{ | |
20 | buf: make([]interface{}, minQueueLen), | |
21 | } | |
20 | 22 | } |
21 | 23 | |
22 | 24 | // Length returns the number of elements currently stored in the queue. |
24 | 26 | return q.count |
25 | 27 | } |
26 | 28 | |
29 | // resizes the queue to fit exactly twice its current contents | |
30 | // this can result in shrinking if the queue is less than half-full | |
27 | 31 | func (q *Queue) resize() { |
28 | 32 | newBuf := make([]interface{}, q.count*2) |
29 | 33 | |
62 | 66 | // Get returns the element at index i in the queue. If the index is |
63 | 67 | // invalid, the call will panic. |
64 | 68 | func (q *Queue) Get(i int) interface{} { |
65 | if i >= q.count || i < 0 { | |
69 | if i < 0 || i >= q.count { | |
66 | 70 | panic("queue: Get() called with index out of range") |
67 | 71 | } |
68 | 72 | return q.buf[(q.head+i)%len(q.buf)] |
77 | 81 | q.buf[q.head] = nil |
78 | 82 | q.head = (q.head + 1) % len(q.buf) |
79 | 83 | q.count-- |
80 | if len(q.buf) > minQueueLen && q.count*4 <= len(q.buf) { | |
84 | if len(q.buf) > minQueueLen && q.count*4 == len(q.buf) { | |
81 | 85 | q.resize() |
82 | 86 | } |
83 | 87 | } |