-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
164 lines (145 loc) · 3.69 KB
/
queue.go
File metadata and controls
164 lines (145 loc) · 3.69 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package bfq
import (
"fmt"
"strings"
"unsafe"
)
// Queue represents a double-ended queue using a circular buffer.
type Queue[T any] struct {
buf []T
front int
back int
length int
}
const (
minCapacity = 8
)
// NewQueue creates an empty queue with an initial capacity.
func NewQueue[T any]() *Queue[T] {
return &Queue[T]{buf: make([]T, minCapacity)}
}
// nextPowerOfTwo returns the smallest power of two greater than or equal to n.
func nextPowerOfTwo(n int) int {
if n < minCapacity {
return minCapacity
}
n--
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n |= n >> 32 // For 64-bit integers
n++
return n
}
// FromSlice creates a queue from a given slice, ensuring the buffer size is a power of two.
func FromSlice[T any](slice []T) *Queue[T] {
size := nextPowerOfTwo(len(slice))
q := &Queue[T]{buf: make([]T, size), front: 0, back: len(slice), length: len(slice)}
copy(q.buf, slice)
return q
}
// Len returns the number of elements in the queue.
func (q *Queue[T]) Len() int { return q.length }
// IsEmpty checks if the queue is empty.
func (q *Queue[T]) IsEmpty() bool { return q.length == 0 }
// resize resizes the queue when needed.
func (q *Queue[T]) resize(size int) {
newBuf := make([]T, size)
if q.front < q.back {
copy(newBuf, q.buf[q.front:q.back])
} else {
n := copy(newBuf, q.buf[q.front:])
copy(newBuf[n:], q.buf[:q.back])
}
q.buf = newBuf
q.front = 0
q.back = q.length
}
// grow expands the queue when full.
func (q *Queue[T]) grow() {
if q.length == len(q.buf) {
q.resize(len(q.buf) << 1)
}
}
// shrink reduces memory usage when necessary.
func (q *Queue[T]) shrink() {
if q.length > minCapacity && q.length == len(q.buf) >> 2 {
q.resize(len(q.buf) >> 1)
}
}
// indexUnsafe gets the pointer to an element without bounds checks.
func (q *Queue[T]) indexUnsafe(index int) *T {
base := unsafe.Pointer(&q.buf[0]) // Base address of buffer
size := unsafe.Sizeof(q.buf[0]) // Size of one element
return (*T)(unsafe.Pointer(uintptr(base) + uintptr(index)*size))
}
// PushFront inserts an element at the front.
func (q *Queue[T]) PushFront(v T) {
q.grow()
q.front = (q.front - 1 + len(q.buf)) & (len(q.buf) - 1)
*(*T)(unsafe.Pointer(q.indexUnsafe(q.front))) = v
q.length++
}
// PushBack inserts an element at the back.
func (q *Queue[T]) PushBack(v T) {
q.grow()
*(*T)(unsafe.Pointer(q.indexUnsafe(q.back))) = v
q.back = (q.back + 1) & (len(q.buf) - 1)
q.length++
}
// PopFront removes and returns the front element.
func (q *Queue[T]) PopFront() (T, bool) {
if q.IsEmpty() {
var zero T
return zero, false
}
v := *q.indexUnsafe(q.front)
q.front = (q.front + 1) & (len(q.buf) - 1)
q.length--
q.shrink()
return v, true
}
// PopBack removes and returns the back element.
func (q *Queue[T]) PopBack() (T, bool) {
if q.IsEmpty() {
var zero T
return zero, false
}
q.back = (q.back - 1 + len(q.buf)) & (len(q.buf) - 1)
v := *q.indexUnsafe(q.back)
q.length--
q.shrink()
return v, true
}
// Front returns the first element without removing it.
func (q *Queue[T]) Front() (T, bool) {
if q.IsEmpty() {
var zero T
return zero, false
}
return *q.indexUnsafe(q.front), true
}
// Back returns the last element without removing it.
func (q *Queue[T]) Back() (T, bool) {
if q.IsEmpty() {
var zero T
return zero, false
}
return *q.indexUnsafe((q.back - 1 + len(q.buf)) & (len(q.buf) - 1)), true
}
// String returns a string representation of the queue.
func (q *Queue[T]) String() string {
var sb strings.Builder
sb.WriteByte('[')
for i, idx := 0, q.front; i < q.length; i++ {
if i > 0 {
sb.WriteByte(' ')
}
sb.WriteString(fmt.Sprintf("%v", *q.indexUnsafe(idx)))
idx = (idx + 1) & (len(q.buf) - 1)
}
sb.WriteByte(']')
return sb.String()
}