forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
batcher: use abstract Queue type for blocks state (ethereum-optimism#…
…12180) * op-service: add queue package * batcher: use Queue type for blocks * revert changes to errors.As/Is * implement and use Peek operation * queue: add unit tests * add godoc * add more test cases permute expected / got * ensure enqueue and prepend are noops when args is empty * use queue.PeekN and queue.DequeueN * typo * queue: simplify method implementations * revert to old dequeue impl
- Loading branch information
Showing
4 changed files
with
225 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package queue | ||
|
||
// Queue implements a FIFO queue. | ||
type Queue[T any] []T | ||
|
||
// Enqueue adds the elements to the back of the queue. | ||
func (q *Queue[T]) Enqueue(t ...T) { | ||
if len(t) == 0 { | ||
return | ||
} | ||
*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 | ||
return zeroValue, false | ||
} | ||
t := (*q)[0] | ||
*q = (*q)[1:] | ||
return t, true | ||
} | ||
|
||
// DequeueN removes N elements from the front of the queue | ||
// (if there are enough) and returns a slice of those elements. Returns | ||
// a nil slice and false if there are insufficient elements to dequeue. | ||
func (q *Queue[T]) DequeueN(N int) ([]T, bool) { | ||
if len(*q) < N { | ||
return nil, false | ||
} | ||
t := (*q)[0:N] | ||
*q = (*q)[N:] | ||
return t, true | ||
} | ||
|
||
// Prepend inserts the elements at the front of the queue, | ||
// preserving their order. A noop if t is empty. | ||
func (q *Queue[T]) Prepend(t ...T) { | ||
if len(t) == 0 { | ||
return | ||
} | ||
*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) { | ||
return q.PeekN(0) | ||
} | ||
|
||
// PeekN returns the element in Nth position in the queue | ||
// Returns a zero value and false if there are insufficient elements | ||
// in the queue. | ||
func (q *Queue[T]) PeekN(N int) (T, bool) { | ||
if len(*q) <= N { | ||
var zeroValue T | ||
return zeroValue, false | ||
} | ||
t := (*q)[N] | ||
return t, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package queue | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestQueue(t *testing.T) { | ||
t.Run("enqueue amd dequeue", func(t *testing.T) { | ||
q := Queue[int]{} | ||
q.Enqueue(1, 2, 3, 4) | ||
|
||
p, peekOk := q.Peek() | ||
require.True(t, peekOk) | ||
require.Equal(t, 1, p) | ||
|
||
d, dequeueOk := q.Dequeue() | ||
require.Equal(t, 1, d) | ||
require.True(t, dequeueOk) | ||
require.Equal(t, 3, q.Len()) | ||
p, peekOk = q.Peek() | ||
require.True(t, peekOk) | ||
require.Equal(t, 2, p) | ||
|
||
d, dequeueOk = q.Dequeue() | ||
require.Equal(t, 2, d) | ||
require.True(t, dequeueOk) | ||
require.Equal(t, 2, q.Len()) | ||
p, peekOk = q.Peek() | ||
require.True(t, peekOk) | ||
require.Equal(t, 3, p) | ||
|
||
d, dequeueOk = q.Dequeue() | ||
require.Equal(t, 3, d) | ||
require.True(t, dequeueOk) | ||
require.Equal(t, 1, q.Len()) | ||
p, peekOk = q.Peek() | ||
require.True(t, peekOk) | ||
require.Equal(t, 4, p) | ||
|
||
d, dequeueOk = q.Dequeue() | ||
require.Equal(t, 4, d) | ||
require.True(t, dequeueOk) | ||
require.Equal(t, 0, q.Len()) | ||
p, peekOk = q.Peek() | ||
require.False(t, peekOk) | ||
require.Equal(t, 0, p) | ||
|
||
d, dequeueOk = q.Dequeue() | ||
require.Equal(t, 0, d) | ||
require.False(t, dequeueOk) | ||
require.Equal(t, 0, q.Len()) | ||
p, peekOk = q.Peek() | ||
require.False(t, peekOk) | ||
require.Equal(t, 0, p) | ||
p, peekOk = q.Peek() | ||
require.False(t, peekOk) | ||
require.Equal(t, 0, p) | ||
}) | ||
|
||
t.Run("peekN and deqeueueN", func(t *testing.T) { | ||
q := Queue[int]{} | ||
q.Enqueue(1, 2, 3, 4) | ||
|
||
p, peekOk := q.PeekN(1) | ||
require.True(t, peekOk) | ||
require.Equal(t, 2, p) | ||
|
||
p, peekOk = q.PeekN(2) | ||
require.Equal(t, 3, p) | ||
require.True(t, peekOk) | ||
require.Equal(t, 4, q.Len()) | ||
|
||
p, peekOk = q.PeekN(4) | ||
require.Equal(t, 0, p) | ||
require.False(t, peekOk) | ||
|
||
d, dequeueOk := q.DequeueN(1) | ||
require.Equal(t, []int{1}, d) | ||
require.True(t, dequeueOk) | ||
require.Equal(t, 3, q.Len()) | ||
|
||
d, dequeueOk = q.DequeueN(3) | ||
require.Equal(t, []int{2, 3, 4}, d) | ||
require.True(t, dequeueOk) | ||
require.Equal(t, 0, q.Len()) | ||
}) | ||
|
||
t.Run("enqueue and clear", func(t *testing.T) { | ||
q := Queue[int]{} | ||
q.Enqueue(5, 6, 7) | ||
|
||
q.Clear() | ||
require.Equal(t, 0, q.Len()) | ||
|
||
d, ok := q.Dequeue() | ||
require.Equal(t, 0, d) | ||
require.False(t, ok) | ||
}) | ||
|
||
t.Run("prepend", func(t *testing.T) { | ||
var q, r Queue[int] | ||
q.Enqueue(5, 6, 7) | ||
r.Enqueue(8, 9) | ||
|
||
q.Prepend(r...) | ||
require.Equal(t, 5, q.Len()) | ||
|
||
d, ok := q.Dequeue() | ||
require.Equal(t, 8, d) | ||
require.True(t, ok) | ||
require.Equal(t, 4, q.Len()) | ||
|
||
q.Prepend() | ||
require.Equal(t, 4, q.Len()) | ||
|
||
d, ok = q.Dequeue() | ||
require.Equal(t, 9, d) | ||
require.True(t, ok) | ||
}) | ||
} |