1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
}