-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq.go
217 lines (183 loc) · 4.28 KB
/
q.go
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// q.go - Fixed size circular queue
//
// (c) 2024 Sudhi Herle <sw-at-herle.net>
//
// Placed in the Public Domain
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
package utils
import (
"fmt"
"sync"
)
// Notes:
// - read from 'rd', write to 'wr+1'.
// - queue size always a power-of-2
// - for a queue of capacity N, it will store N-1 usable elements
// - queue-empty: rd == wr
// - queue-full: wr+1 == rd
// Q[T] is a generic fixed-size queue. This queue always has a
// power-of-2 size. For a queue with capacity 'N', it will store
// N-1 queue elements.
type Q[T any] struct {
wr, rd uint64
mask uint64 // size-1 (qhen size is a power-of-2
q []T
}
// Make a new Queue instance to hold (at least) 'n' slots. If 'n' is
// NOT a power-of-2, this function will pick the next closest
// power-of-2.
func NewQ[T any](n int) *Q[T] {
q := &Q[T]{}
q.init(n)
return q
}
// NewQFrom makes a new queue with contents from the initial list
func NewQFrom[T any](v []T) *Q[T] {
q := &Q[T]{}
q.init2(v)
return q
}
func (q *Q[T]) init(n int) {
z := nextpow2(uint64(n))
q.rd, q.wr = 0, 0
q.mask = z - 1
q.q = make([]T, z)
}
func (q *Q[T]) init2(v []T) {
q.init(len(v))
n := copy(q.q[1:], v)
q.wr = uint64(n)
}
// Empty the queue
func (q *Q[T]) Flush() {
q.wr = 0
q.rd = 0
}
// Insert new element; return false if queue full
func (q *Q[T]) Enq(x T) bool {
wr := (1 + q.wr) & q.mask
if wr == q.rd {
return false
}
q.q[wr] = x
q.wr = wr
return true
}
// Remove oldest element; return false if queue empty
func (q *Q[T]) Deq() (T, bool) {
rd := q.rd
if rd == q.wr {
var z T
return z, false
}
rd = (rd + 1) & q.mask
q.rd = rd
return q.q[rd], true
}
// Return true if queue is empty
func (q *Q[T]) IsEmpty() bool {
return qempty(q.rd, q.wr, q.mask)
}
// Return true if queue is full
func (q *Q[T]) IsFull() bool {
return qfull(q.rd, q.wr, q.mask)
}
// Return number of valid/usable elements
func (q *Q[T]) Len() int {
return qlen(q.rd, q.wr, q.mask)
}
// Return total capacity of the queue
func (q *Q[T]) Size() int {
// Due to the q-full and q-empty conditions, we will
// always have one unused slot.
return len(q.q) - 1
}
// Dump queue in human readable form
func (q *Q[T]) String() string {
return q.repr("Q")
}
func (q *Q[T]) repr(nm string) string {
suff := qrepr(q.rd, q.wr, q.mask)
return fmt.Sprintf("<%s %T %s>", nm, q, suff)
}
// SyncQ[T] is a generic, thread-safe, fixed-size queue. This queue
// always has a power-of-2 size. For a queue with capacity 'N', it will
// store N-1 queue elements.
type SyncQ[T any] struct {
Q[T]
sync.Mutex
}
// Make a new thread-safe queue instance to hold (at least) 'n' slots.
// If 'n' is NOT a power-of-2, this function will pick the next closest
// power-of-2.
func NewSyncQ[T any](n int) *SyncQ[T] {
q := &SyncQ[T]{}
q.init(n)
return q
}
// NewSyncQFrom makes a new queue with contents from the initial list
func NewSyncQFrom[T any](v []T) *SyncQ[T] {
q := &SyncQ[T]{}
q.init2(v)
return q
}
// Flush empties the queue
func (q *SyncQ[T]) Flush() {
q.Lock()
q.Q.Flush()
q.Unlock()
}
// Enq enqueues a new element to the queue, return false if the queue is full
// and true otherwise.
func (q *SyncQ[T]) Enq(x T) bool {
q.Lock()
r := q.Q.Enq(x)
q.Unlock()
return r
}
// Deq dequeues an element from the queue and returns it. The bool retval is false
// if the queue is empty and true otherwise.
func (q *SyncQ[T]) Deq() (T, bool) {
q.Lock()
a, b := q.Q.Deq()
q.Unlock()
return a, b
}
// IsEmpty returns true if the queue is empty and false otherwise
func (q *SyncQ[T]) IsEmpty() bool {
q.Lock()
r := q.Q.IsEmpty()
q.Unlock()
return r
}
// IsFull returns true if the queue is full and false otherwise
func (q *SyncQ[T]) IsFull() bool {
q.Lock()
r := q.Q.IsFull()
q.Unlock()
return r
}
// Len returns the number of elements in the queue
func (q *SyncQ[T]) Len() int {
q.Lock()
r := q.Q.Len()
q.Unlock()
return r
}
// Size returns the capacity of the queue
func (q *SyncQ[T]) Size() int {
q.Lock()
r := q.Q.Size()
q.Unlock()
return r
}
// String prints a string representation of the queue
func (q *SyncQ[T]) String() string {
q.Lock()
s := q.Q.repr("SyncQ")
q.Unlock()
return s
}
// vim: ft=go:sw=8:ts=8:noexpandtab:tw=98: