Run of fresh-snapshots for golang-gopkg-eapache-queue.v1

Try this locally (using silver-platter):

debian-svp new-upstream --snapshot golang-gopkg-eapache-queue.v1 

Merge these changes:

Summary

Merged new upstream version: 1.1.0 (was: 1.0.2).

Diff

diff --git a/debian/changelog b/debian/changelog
index 03fb169..90b4cbf 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,8 +1,12 @@
-golang-gopkg-eapache-queue.v1 (1.0.2-5) UNRELEASED; urgency=medium
+golang-gopkg-eapache-queue.v1 (1.1.0-1) UNRELEASED; urgency=medium
 
+  [ Jelmer Vernooij ]
   * Use secure copyright file specification URI.
 
- -- Jelmer Vernooij <jelmer@debian.org>  Thu, 18 Oct 2018 21:29:07 +0000
+  [ Debian Janitor ]
+  * New upstream release.
+
+ -- Debian Janitor <janitor@jelmer.uk>  Sun, 23 Jun 2019 06:15:16 +0000
 
 golang-gopkg-eapache-queue.v1 (1.0.2-4) unstable; urgency=medium
 
diff --git a/queue.go b/queue.go
index 2dc8d93..71d1acd 100644
--- a/queue.go
+++ b/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
 }
diff --git a/queue_test.go b/queue_test.go
index f2765c1..a875848 100644
--- a/queue_test.go
+++ b/queue_test.go
@@ -12,7 +12,10 @@ func TestQueueSimple(t *testing.T) {
 		if q.Peek().(int) != i {
 			t.Error("peek", i, "had value", q.Peek())
 		}
-		q.Remove()
+		x := q.Remove()
+		if x != i {
+			t.Error("remove", i, "had value", x)
+		}
 	}
 }
 
@@ -69,6 +72,19 @@ func TestQueueGet(t *testing.T) {
 	}
 }
 
+func TestQueueGetNegative(t *testing.T) {
+	q := New()
+
+	for i := 0; i < 1000; i++ {
+		q.Add(i)
+		for j := 1; j <= q.Length(); j++ {
+			if q.Get(-j).(int) != q.Length()-j {
+				t.Errorf("index %d doesn't contain %d", -j, q.Length()-j)
+			}
+		}
+	}
+}
+
 func TestQueueGetOutOfRangePanics(t *testing.T) {
 	q := New()
 
@@ -77,7 +93,7 @@ func TestQueueGetOutOfRangePanics(t *testing.T) {
 	q.Add(3)
 
 	assertPanics(t, "should panic when negative index", func() {
-		q.Get(-1)
+		q.Get(-4)
 	})
 
 	assertPanics(t, "should panic when index greater than length", func() {

Full worker log Full build log