aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/eapache/queue/queue.go
diff options
context:
space:
mode:
authorGravatar Yong Tang <yong.tang.github@outlook.com> 2017-12-18 11:50:56 -0600
committerGravatar GitHub <noreply@github.com> 2017-12-18 11:50:56 -0600
commit4dd40a292c773aad5eee761940840de64ba9090a (patch)
treee7b872bed228fdf06b4ea91db60bf9b7cc8fec2a /vendor/github.com/eapache/queue/queue.go
parentba4e77672c9f7b53a8c2f6e197d3f12ede1e46cd (diff)
downloadcoredns-4dd40a292c773aad5eee761940840de64ba9090a.tar.gz
coredns-4dd40a292c773aad5eee761940840de64ba9090a.tar.zst
coredns-4dd40a292c773aad5eee761940840de64ba9090a.zip
Update apache/thrift to 0.11.0 and remove pinning (#1317)
The `apache/thrift` recently released a new version of `0.11.0` several days ago. This release is compatible with other packages and as such, there is no need to pinning the `apache/thrift` to `master` anymore in Gopkg.toml. This fix removes the pinning of `apache/thrift` in Gopkg.toml, and updates all dependencies of coredns. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
Diffstat (limited to 'vendor/github.com/eapache/queue/queue.go')
-rw-r--r--vendor/github.com/eapache/queue/queue.go32
1 files changed, 23 insertions, 9 deletions
diff --git a/vendor/github.com/eapache/queue/queue.go b/vendor/github.com/eapache/queue/queue.go
index 2dc8d9395..71d1acdf2 100644
--- a/vendor/github.com/eapache/queue/queue.go
+++ b/vendor/github.com/eapache/queue/queue.go
@@ -7,6 +7,8 @@ The queue implemented here is as fast as it is for an additional reason: it is *
*/
package queue
+// minQueueLen is smallest capacity that queue may have.
+// Must be power of 2 for bitwise modulus: x % n == x & (n - 1).
const minQueueLen = 16
// Queue represents a single instance of the queue data structure.
@@ -30,7 +32,7 @@ func (q *Queue) Length() int {
// 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)
+ newBuf := make([]interface{}, q.count<<1)
if q.tail > q.head {
copy(newBuf, q.buf[q.head:q.tail])
@@ -51,7 +53,8 @@ func (q *Queue) Add(elem interface{}) {
}
q.buf[q.tail] = elem
- q.tail = (q.tail + 1) % len(q.buf)
+ // bitwise modulus
+ q.tail = (q.tail + 1) & (len(q.buf) - 1)
q.count++
}
@@ -65,24 +68,35 @@ func (q *Queue) Peek() interface{} {
}
// Get returns the element at index i in the queue. If the index is
-// invalid, the call will panic.
+// invalid, the call will panic. This method accepts both positive and
+// negative index values. Index 0 refers to the first element, and
+// index -1 refers to the last.
func (q *Queue) Get(i int) interface{} {
+ // If indexing backwards, convert to positive index.
+ if i < 0 {
+ i += q.count
+ }
if i < 0 || i >= q.count {
panic("queue: Get() called with index out of range")
}
- return q.buf[(q.head+i)%len(q.buf)]
+ // bitwise modulus
+ return q.buf[(q.head+i)&(len(q.buf)-1)]
}
-// Remove removes the element from the front of the queue. If you actually
-// want the element, call Peek first. This call panics if the queue is empty.
-func (q *Queue) Remove() {
+// Remove removes and returns the element from the front of the queue. If the
+// queue is empty, the call will panic.
+func (q *Queue) Remove() interface{} {
if q.count <= 0 {
panic("queue: Remove() called on empty queue")
}
+ ret := q.buf[q.head]
q.buf[q.head] = nil
- q.head = (q.head + 1) % len(q.buf)
+ // bitwise modulus
+ q.head = (q.head + 1) & (len(q.buf) - 1)
q.count--
- if len(q.buf) > minQueueLen && q.count*4 == len(q.buf) {
+ // Resize down if buffer 1/4 full.
+ if len(q.buf) > minQueueLen && (q.count<<2) == len(q.buf) {
q.resize()
}
+ return ret
}