-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdequeue.go
200 lines (154 loc) · 4.39 KB
/
dequeue.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
package blocking_dequeue
import (
"sync"
)
// Blocking dequeue, implemented with a circular buffer.
// The dequeue is thread safe. And must not be copied.
type BlockingDequeue[T any] struct {
buffer []T
lock *sync.Mutex
notEmpty, notFull *sync.Cond
first, last int
isEmpty bool
}
// Creates a new blocking dequeue with the provided buffer.
// The dequeue MUST only be created using this method.
func NewBlockingDequeue[T any](buffer []T) *BlockingDequeue[T] {
d := new(BlockingDequeue[T])
d.buffer = buffer
d.first = 0
d.last = 0
d.isEmpty = true
d.lock = &sync.Mutex{}
d.notEmpty = sync.NewCond(d.lock)
d.notFull = sync.NewCond(d.lock)
return d
}
// =================================[Buffer helpers]=================================
func (d BlockingDequeue[T]) nextIndex(i int) int {
return (i + 1) % len(d.buffer)
}
func (d BlockingDequeue[T]) prevIndex(i int) int {
return (i - 1 + len(d.buffer)) % len(d.buffer)
}
// =================================[Push/Pop/Peek]=================================
// Add an item into the front (top) of the dequeue. Blocks if dequeue is full.
func (d *BlockingDequeue[T]) PushFront(item T) {
d.lock.Lock()
defer d.lock.Unlock()
defer d.notEmpty.Signal()
// If the dequeue is full, wait until an item is removed
for d.isFull_unsafe() {
d.notFull.Wait()
}
if !d.isEmpty {
d.first = d.prevIndex(d.first)
}
d.buffer[d.first] = item
d.isEmpty = false
}
// Add an item to the back (bottom) of the dequeue. Blocks if dequeue is full.
func (d *BlockingDequeue[T]) PushBack(item T) {
d.lock.Lock()
defer d.lock.Unlock()
defer d.notEmpty.Signal()
// If the dequeue is full, wait until an item is removed
for d.isFull_unsafe() {
d.notFull.Wait()
}
if !d.isEmpty {
d.last = d.nextIndex(d.last)
}
d.buffer[d.last] = item
d.isEmpty = false
}
// Read the first item (on the top/front) of the dequeue and remove it. Blocks if the dequeue is empty.
func (d *BlockingDequeue[T]) PopFront() T {
d.lock.Lock()
defer d.lock.Unlock()
defer d.notFull.Signal()
// If the dequeue is empty, wait until an item is added
for d.isEmpty_unsafe() {
d.notEmpty.Wait()
}
item := d.buffer[d.first]
if d.first == d.last {
d.isEmpty = true
} else {
d.first = d.nextIndex(d.first)
}
return item
}
// Read the last item (at the end/back) of the dequeue and remove it. Blocks if the dequeue is empty.
func (d *BlockingDequeue[T]) PopBack() T {
d.lock.Lock()
defer d.lock.Unlock()
defer d.notFull.Signal()
// If the dequeue is empty, wait until an item is added
for d.isEmpty_unsafe() {
d.notEmpty.Wait()
}
item := d.buffer[d.last]
if d.first == d.last {
d.isEmpty = true
} else {
d.last = d.prevIndex(d.last)
}
return item
}
// Read the first item of the dequeue without removing it. Blocks if the dequeue is empty.
func (d *BlockingDequeue[T]) PeekFront() T {
d.lock.Lock()
defer d.lock.Unlock()
// If the dequeue is empty, wait until an item is added
for d.isEmpty_unsafe() {
d.notEmpty.Wait()
}
return d.buffer[d.first]
}
// Read the last item of the dequeue without removing it. Blocks if the dequeue is empty.
func (d *BlockingDequeue[T]) PeekBack() T {
d.lock.Lock()
defer d.lock.Unlock()
// If the dequeue is empty, wait until an item is added
for d.isEmpty_unsafe() {
d.notEmpty.Wait()
}
return d.buffer[d.last]
}
// ================================[Size related]================================
// Return the number of elements in the dequeue.
func (d *BlockingDequeue[T]) Size() int {
d.lock.Lock()
defer d.lock.Unlock()
if d.isEmpty {
return 0
}
if d.first <= d.last {
return d.last - d.first + 1
} else {
return (len(d.buffer) - d.first) + (d.last + 1)
}
}
// Return true if the dequeue is empty, without acquiring any locks.
// Dequeue is empty if the first and last indices are the same.
func (d *BlockingDequeue[T]) isEmpty_unsafe() bool {
return d.isEmpty
}
// Return true if the dequeue is empty.
func (d *BlockingDequeue[T]) IsEmpty() bool {
d.lock.Lock()
defer d.lock.Unlock()
return d.isEmpty_unsafe()
}
// Return true if the dequeue is full, without acquiring any locks.
// Dequeue is full if the next item to be added will be the first item in the dequeue.
func (d *BlockingDequeue[T]) isFull_unsafe() bool {
return d.nextIndex(d.last) == d.first
}
// Return true if the dequeue is full.
func (d *BlockingDequeue[T]) IsFull() bool {
d.lock.Lock()
defer d.lock.Unlock()
return d.isFull_unsafe()
}