-
Notifications
You must be signed in to change notification settings - Fork 26
/
queue.go
226 lines (185 loc) · 3.79 KB
/
queue.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
218
219
220
221
222
223
224
225
226
package queue
import (
"math/rand"
"sync"
)
const minQueueLen = 32
type Queue struct {
items map[int64]interface{}
ids map[interface{}]int64
buf []int64
head, tail, count int
mutex *sync.Mutex
notEmpty *sync.Cond
// You can subscribe to this channel to know whether queue is not empty
NotEmpty chan struct{}
}
func New() *Queue {
q := &Queue{
items: make(map[int64]interface{}),
ids: make(map[interface{}]int64),
buf: make([]int64, minQueueLen),
mutex: &sync.Mutex{},
NotEmpty: make(chan struct{}, 1),
}
q.notEmpty = sync.NewCond(q.mutex)
return q
}
// Removes all elements from queue
func (q *Queue) Clean() {
q.mutex.Lock()
defer q.mutex.Unlock()
q.items = make(map[int64]interface{})
q.ids = make(map[interface{}]int64)
q.buf = make([]int64, minQueueLen)
q.tail = 0
q.head = 0
q.count = 0
}
// Returns the number of elements in queue
func (q *Queue) Length() int {
q.mutex.Lock()
defer q.mutex.Unlock()
return len(q.items)
}
// resizes the queue to fit exactly twice its current contents
// this can result in shrinking if the queue is less than half-full
func (q *Queue) resize() {
newCount := q.count << 1
if q.count < 2<<18 {
newCount = newCount << 2
}
newBuf := make([]int64, newCount)
if q.tail > q.head {
copy(newBuf, q.buf[q.head:q.tail])
} else {
n := copy(newBuf, q.buf[q.head:])
copy(newBuf[n:], q.buf[:q.tail])
}
q.head = 0
q.tail = q.count
q.buf = newBuf
}
func (q *Queue) notify() {
if len(q.items) > 0 {
select {
case q.NotEmpty <- struct{}{}:
default:
}
}
}
// Adds one element at the back of the queue
func (q *Queue) Append(elem interface{}) {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.count == len(q.buf) {
q.resize()
}
id := q.newId()
q.items[id] = elem
q.ids[elem] = id
q.buf[q.tail] = id
// bitwise modulus
q.tail = (q.tail + 1) & (len(q.buf) - 1)
q.count++
q.notify()
if q.count == 1 {
q.notEmpty.Broadcast()
}
}
func (q *Queue) newId() int64 {
for {
id := rand.Int63()
_, ok := q.items[id]
if id != 0 && !ok {
return id
}
}
}
// Adds one element at the front of queue
func (q *Queue) Prepend(elem interface{}) {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.count == len(q.buf) {
q.resize()
}
q.head = (q.head - 1) & (len(q.buf) - 1)
id := q.newId()
q.items[id] = elem
q.ids[elem] = id
q.buf[q.head] = id
// bitwise modulus
q.count++
q.notify()
if q.count == 1 {
q.notEmpty.Broadcast()
}
}
// Previews element at the front of queue
func (q *Queue) Front() interface{} {
q.mutex.Lock()
defer q.mutex.Unlock()
id := q.buf[q.head]
if id != 0 {
return q.items[id]
}
return nil
}
// Previews element at the back of queue
func (q *Queue) Back() interface{} {
q.mutex.Lock()
defer q.mutex.Unlock()
id := q.buf[(q.tail-1)&(len(q.buf)-1)]
if id != 0 {
return q.items[id]
}
return nil
}
func (q *Queue) pop() int64 {
for {
if q.count <= 0 {
q.notEmpty.Wait()
}
// I have no idea why, but sometimes it's less than 0
if q.count > 0 {
break
}
}
id := q.buf[q.head]
q.buf[q.head] = 0
// bitwise modulus
q.head = (q.head + 1) & (len(q.buf) - 1)
q.count--
if len(q.buf) > minQueueLen && (q.count<<1) == len(q.buf) {
q.resize()
}
return id
}
// Pop removes and returns the element from the front of the queue.
// If the queue is empty, it will block
func (q *Queue) Pop() interface{} {
q.mutex.Lock()
defer q.mutex.Unlock()
for {
id := q.pop()
item, ok := q.items[id]
if ok {
delete(q.ids, item)
delete(q.items, id)
q.notify()
return item
}
}
}
// Removes one element from the queue
func (q *Queue) Remove(elem interface{}) bool {
q.mutex.Lock()
defer q.mutex.Unlock()
id, ok := q.ids[elem]
if !ok {
return false
}
delete(q.ids, elem)
delete(q.items, id)
return true
}