-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.go
More file actions
226 lines (194 loc) · 5.01 KB
/
queue.go
File metadata and controls
226 lines (194 loc) · 5.01 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
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 calendarqueue
import "fmt"
type CalendarQueue[T any] struct {
buckets []*event[T]
numBuckets int
lastBucket int
bucketWidth float64
bucketTop float64
lastPrio float64
size int
topResizeThreshold int
botResizeThreshold int
resizeEnabled bool
}
func New[T any]() *CalendarQueue[T] {
q := CalendarQueue[T]{}
q.localInit(2, 1.0, 0.0)
q.resizeEnabled = true
return &q
}
func (q *CalendarQueue[T]) Enqueue(entry *event[T]) {
i := int(entry.priority/q.bucketWidth) % q.numBuckets
if q.buckets[i] == nil || q.buckets[i].priority > entry.priority {
entry.next = q.buckets[i]
q.buckets[i] = entry
} else {
current := q.buckets[i]
for current.next != nil {
if current.next.priority < entry.priority {
current = current.next
} else {
break
}
}
entry.next = current.next
current.next = entry
}
q.size++
if q.size > q.topResizeThreshold {
q.resize(2 * q.numBuckets)
}
}
func (q *CalendarQueue[T]) Dequeue() *event[T] {
if q.size == 0 {
return nil
}
i := q.lastBucket
for {
curr := q.buckets[i]
if curr != nil && curr.priority < q.bucketTop {
q.buckets[i] = curr.next
q.lastBucket = i
q.lastPrio = curr.priority
q.size--
if q.size < q.botResizeThreshold {
q.resize(q.numBuckets / 2)
}
return curr
} else {
i++
if i == q.numBuckets {
i = 0
}
q.bucketTop += q.bucketWidth
if i == q.lastBucket {
break
}
}
}
var minPrio float64
for i, bucket := range q.buckets {
if bucket != nil {
q.lastBucket = i
q.lastPrio = bucket.priority
minPrio = bucket.priority
break
}
}
for i := q.lastBucket; i < q.numBuckets; i++ {
bucket := q.buckets[i]
if bucket != nil && bucket.priority < minPrio {
q.lastBucket = i
q.lastPrio = bucket.priority
minPrio = bucket.priority
}
}
n := q.lastPrio / q.bucketWidth
q.bucketTop = (n + 1.5) * q.bucketWidth
return q.Dequeue()
}
func (q *CalendarQueue[T]) String() string {
out := fmt.Sprintf("NumBuckets: %d\nBucketWidth: %f\nResizeEnabled: %t\nBottomResizeThreshold: %d\nTopResizeThreshold: %d\nSize: %d\nLastBucket: %d\nBucketTop: %f\nLastPrio: %f\nBuckets:\n", q.numBuckets, q.bucketWidth, q.resizeEnabled, q.botResizeThreshold, q.topResizeThreshold, q.size, q.lastBucket, q.bucketTop, q.lastPrio)
for i := 0; i < len(q.buckets); i++ {
out += fmt.Sprintf("Bucket %d: \n", i)
curr := q.buckets[i]
for curr != nil {
out += fmt.Sprintf("\tPrio: %f, Data: %v\n", curr.priority, curr.Data)
curr = curr.next
}
}
return out
}
func (q *CalendarQueue[T]) localInit(numBuckets int, bucketWidth, startPrio float64) {
q.buckets = make([]*event[T], numBuckets)
q.bucketWidth = bucketWidth
q.numBuckets = numBuckets
q.size = 0
q.lastPrio = startPrio
n := startPrio / bucketWidth
q.lastBucket = int(n) % numBuckets
q.bucketTop = (n + 1.5) * bucketWidth
q.botResizeThreshold = numBuckets/2 - 2
q.topResizeThreshold = numBuckets * 2
}
func (q *CalendarQueue[T]) resize(numBuckets int) {
if !q.resizeEnabled {
return
}
bucketWidth := q.newWidth()
oldBuckets := q.buckets
oldNumBuckets := q.numBuckets
q.localInit(numBuckets, bucketWidth, q.lastPrio)
for i := oldNumBuckets - 1; i >= 0; i-- {
curr := oldBuckets[i]
for curr != nil {
e := NewEvent(curr.Data, curr.priority)
q.Enqueue(e)
curr = curr.next
}
}
}
func (q *CalendarQueue[T]) newWidth() float64 {
var numSamples int
if q.size < 2.0 {
return 1.0
}
if q.size <= 5 {
numSamples = q.size
} else {
numSamples = 5 + q.size/10
}
if numSamples > 25 {
numSamples = 25
}
cachedLastPrio := q.lastPrio
cachedLastBucket := q.lastBucket
cachedBucketTop := q.bucketTop
sampledEvents := make([]*event[T], numSamples)
sampledPrios := make([]float64, numSamples)
q.resizeEnabled = false
for i := 0; i < numSamples; i++ {
e := q.Dequeue()
sampledEvents[i] = e
sampledPrios[i] = e.priority
}
for i := numSamples - 1; i >= 0; i-- {
q.Enqueue(sampledEvents[i])
}
q.lastPrio = cachedLastPrio
q.lastBucket = cachedLastBucket
q.bucketTop = cachedBucketTop
q.resizeEnabled = true
separationsSum := 0.0
for i := 1; i < numSamples; i++ {
separationsSum += sampledPrios[i] - sampledPrios[i-1]
}
avgSeparation := separationsSum / float64(numSamples-1)
separationsSum = 0
count := 0
for i := 1; i < numSamples; i++ {
separation := sampledPrios[i] - sampledPrios[i-1]
if separation < avgSeparation*2 {
separationsSum += separation
count++
}
}
recalculatedAverage := separationsSum / float64(count)
return recalculatedAverage * 3
}
type event[T any] struct {
Data T
priority float64
next *event[T]
}
func NewEvent[T any](data T, priority float64) *event[T] {
return &event[T]{
Data: data,
priority: priority,
next: nil,
}
}
func (e event[T]) String() string {
return fmt.Sprintf("Priority: %f\nData: %v", e.priority, e.Data)
}