1
2
3
4
5 package nettest
6
7 type queue[T any] struct {
8 headPos int
9 head []T
10 tail []T
11 }
12
13 func (q *queue[T]) len() int {
14 return len(q.head[q.headPos:]) + len(q.tail)
15 }
16
17 func (q *queue[T]) push(v T) {
18 q.tail = append(q.tail, v)
19 }
20
21 func (q *queue[T]) pop() T {
22 var zero T
23 if q.headPos >= len(q.head) {
24 if len(q.tail) == 0 {
25 return zero
26 }
27 q.head, q.headPos, q.tail = q.tail, 0, q.head[:0]
28 }
29 v := q.head[q.headPos]
30 q.head[q.headPos] = zero
31 q.headPos++
32 return v
33 }
34
View as plain text