|
|
@@ -3,9 +3,7 @@ Package queue provides a fast, ring-buffer queue based on the version suggested
|
|
|
Using this instead of other, simpler, queue implementations (slice+append or linked list) provides
|
|
|
substantial memory and time benefits, and fewer GC pauses.
|
|
|
|
|
|
-The queue implemented here is as fast as it is for two additional reasons: it is *not* thread-safe, and it
|
|
|
-intentionally does not follow go best-practices regarding errors - if you make a mistake with this
|
|
|
-queue (such as trying to remove an element from an empty queue) then who knows what will happen.
|
|
|
+The queue implemented here is as fast as it is for an additional reason: it is *not* thread-safe.
|
|
|
*/
|
|
|
package queue
|
|
|
|
|
|
@@ -53,8 +51,8 @@ func (q *Queue) Add(elem interface{}) {
|
|
|
q.count++
|
|
|
}
|
|
|
|
|
|
-// Peek returns the element at the head of the queue. If the queue is empty (Length == 0),
|
|
|
-// Peek does not panic, it simply returns garbage.
|
|
|
+// Peek returns the element at the head of the queue. This call panics
|
|
|
+// if the queue is empty.
|
|
|
func (q *Queue) Peek() interface{} {
|
|
|
if q.Length() <= 0 {
|
|
|
panic("queue: empty queue")
|
|
|
@@ -62,8 +60,8 @@ func (q *Queue) Peek() interface{} {
|
|
|
return q.buf[q.head]
|
|
|
}
|
|
|
|
|
|
-// Get returns the element at index i in the queue. If the index is invalid, the
|
|
|
-// call will panic.
|
|
|
+// 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.Length() || i < 0 {
|
|
|
panic("queue: index out of range")
|
|
|
@@ -72,9 +70,8 @@ func (q *Queue) Get(i int) interface{} {
|
|
|
return q.buf[modi]
|
|
|
}
|
|
|
|
|
|
-// Remove removes the element from the front of the queue. If you actually want the element,
|
|
|
-// call Peek first. If the queue is empty (Length == 0), Remove will put the queue in a bad
|
|
|
-// state and all further operations will be undefined.
|
|
|
+// 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() {
|
|
|
if q.Length() <= 0 {
|
|
|
panic("queue: empty queue")
|