[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batcher: use abstract Queue type for blocks state #12180

Merged
merged 12 commits into from
Oct 2, 2024
Prev Previous commit
Next Next commit
add godoc
  • Loading branch information
geoknee committed Sep 30, 2024
commit 4690bd63c57d9795a67d5a65bf491f8a8f231302
17 changes: 17 additions & 0 deletions op-service/queue/queue.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package queue
sebastianst marked this conversation as resolved.
Show resolved Hide resolved

// Queue implements a FIFO queue.
type Queue[T any] []T
geoknee marked this conversation as resolved.
Show resolved Hide resolved

// Enqueue adds the elements to the back of the queue.
func (q *Queue[T]) Enqueue(t ...T) {
*q = append(*q, t...)
}

// Dequeue removes a single element from the front of the queue
// (if there is one) and returns it. Returns a zero value and false
// if there is no element to dequeue.
func (q *Queue[T]) Dequeue() (T, bool) {
if len(*q) == 0 {
var zeroValue T
Expand All @@ -14,15 +20,26 @@ func (q *Queue[T]) Dequeue() (T, bool) {
*q = (*q)[1:]
return t, true
}

// Prepend inserts the elements at the front of the queue,
// preserving their order.
func (q *Queue[T]) Prepend(t ...T) {
*q = append(t, *q...)
}

// Clear removes all elements from the queue.
func (q *Queue[T]) Clear() {
*q = (*q)[:0]
}

// Len returns the number of elements in the queue.
func (q *Queue[T]) Len() int {
return len(*q)
}

// Peek returns the single element at the front of the queue
// (if there is one) without removing it Returns a zero value and
// false if there is no element to peek at.
func (q *Queue[T]) Peek() (T, bool) {
if len(*q) > 0 {
return (*q)[0], true
Expand Down