Debian Codegraph @

queue.go

raw
1 // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
2 // SPDX-License-Identifier: MIT
3
4 package sctp
5
6 type queue[T any] struct {
7 buf []T
8 head int
9 tail int
10 count int
11 }
12
13 const minCap = 16
14
15 func newQueue[T any](capacity int) *queue[T] {
16 queueCap := minCap
17 for queueCap < capacity {
18 queueCap <<= 1
19 }
20
21 return &queue[T]{
22 buf: make([]T, queueCap),
23 }
24 }
25
26 func (q *queue[T]) Len() int {
27 return q.count
28 }
29
30 func (q *queue[T]) PushBack(ele T) {
31 q.growIfFull()
32 q.buf[q.tail] = ele
33 q.tail = (q.tail + 1) % len(q.buf)
34 q.count++
35 }
36
37 func (q *queue[T]) PopFront() T {
38 ele := q.buf[q.head]
39 var zeroVal T
40 q.buf[q.head] = zeroVal
41 q.head = (q.head + 1) % len(q.buf)
42 q.count--
43
44 return ele
45 }
46
47 func (q *queue[T]) Front() T {
48 return q.buf[q.head]
49 }
50
51 func (q *queue[T]) Back() T {
52 return q.buf[(q.tail-1+len(q.buf))%len(q.buf)]
53 }
54
55 func (q *queue[T]) At(i int) T {
56 return q.buf[(q.head+i)%(len(q.buf))]
57 }
58
59 func (q *queue[T]) growIfFull() {
60 if q.count < len(q.buf) {
61 return
62 }
63
64 newBuf := make([]T, q.count<<1)
65 if q.tail > q.head {
66 copy(newBuf, q.buf[q.head:q.tail])
67 } else {
68 n := copy(newBuf, q.buf[q.head:])
69 copy(newBuf[n:], q.buf[:q.tail])
70 }
71
72 q.head = 0
73 q.tail = q.count
74 q.buf = newBuf
75 }