-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecompute_heap.go
234 lines (207 loc) · 5.27 KB
/
recompute_heap.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
227
228
229
230
231
232
233
234
package incr
import (
"fmt"
"sync"
)
func newRecomputeHeap(maxHeight int) *recomputeHeap {
return &recomputeHeap{
heights: make([]*recomputeHeapList, maxHeight),
}
}
type recomputeHeap struct {
mu sync.Mutex
minHeight int
maxHeight int
heights []*recomputeHeapList
numItems int
}
func (rh *recomputeHeap) clear() (aborted []INode) {
rh.mu.Lock()
defer rh.mu.Unlock()
var next INode
aborted = make([]INode, 0, rh.numItems)
for rh.numItems > 0 {
next, _ = rh.removeMinUnsafe()
next.Node().heightInRecomputeHeap = HeightUnset
aborted = append(aborted, next)
}
rh.heights = make([]*recomputeHeapList, len(rh.heights))
rh.minHeight = 0
rh.maxHeight = 0
rh.numItems = 0
return
}
func (rh *recomputeHeap) len() int {
rh.mu.Lock()
defer rh.mu.Unlock()
return rh.numItems
}
func (rh *recomputeHeap) add(nodes ...INode) {
rh.mu.Lock()
defer rh.mu.Unlock()
for _, n := range nodes {
rh.addNodeUnsafe(n)
}
}
func (rh *recomputeHeap) addIfNotPresent(n INode) {
rh.mu.Lock()
defer rh.mu.Unlock()
if n.Node().heightInRecomputeHeap == HeightUnset {
rh.addNodeUnsafe(n)
}
}
func (rh *recomputeHeap) fix(n INode) {
rh.mu.Lock()
defer rh.mu.Unlock()
rh.fixUnsafe(n)
}
func (rh *recomputeHeap) has(s INode) (ok bool) {
rh.mu.Lock()
defer rh.mu.Unlock()
nodeID := s.Node().id
for x := rh.minHeight; x <= rh.maxHeight; x++ {
if rh.heights[x].has(nodeID) {
ok = true
return
}
}
return
}
type recomputeHeapListIter struct {
cursor INode
}
func (i *recomputeHeapListIter) Next() (INode, bool) {
if i.cursor == nil {
return nil, false
}
prev := i.cursor
i.cursor = i.cursor.Node().nextInRecomputeHeap
prev.Node().nextInRecomputeHeap = nil
prev.Node().previousInRecomputeHeap = nil
prev.Node().heightInRecomputeHeap = HeightUnset
return prev, true
}
func (rh *recomputeHeap) setIterToMinHeight(iter *recomputeHeapListIter) {
rh.mu.Lock()
defer rh.mu.Unlock()
var heightBlock *recomputeHeapList
for x := 0; x < len(rh.heights); x++ {
heightBlock = rh.heights[x]
if heightBlock != nil && heightBlock.len() > 0 {
break
}
}
iter.cursor = heightBlock.head
heightBlock.head = nil
heightBlock.tail = nil
rh.numItems = rh.numItems - heightBlock.len()
heightBlock.count = 0
rh.minHeight = rh.nextMinHeightUnsafe()
}
func (rh *recomputeHeap) remove(node INode) {
rh.mu.Lock()
defer rh.mu.Unlock()
rh.removeNodeUnsafe(node)
}
//
// utils
//
func (rh *recomputeHeap) removeMinUnsafe() (node INode, ok bool) {
for x := rh.minHeight; x <= rh.maxHeight; x++ {
if rh.heights[x] != nil && rh.heights[x].len() > 0 {
_, node, ok = rh.heights[x].pop()
rh.numItems--
node.Node().heightInRecomputeHeap = HeightUnset
if rh.heights[x].len() > 0 {
rh.minHeight = x
} else {
rh.minHeight = rh.nextMinHeightUnsafe()
}
return
}
}
return
}
func (rh *recomputeHeap) addNodeUnsafe(s INode) {
sn := s.Node()
height := sn.height
s.Node().heightInRecomputeHeap = height
rh.maybeUpdateMinMaxHeightsUnsafe(height)
rh.maybeAddNewHeightsUnsafe(height)
if rh.heights[height] == nil {
rh.heights[height] = new(recomputeHeapList)
}
rh.heights[height].push(s)
rh.numItems++
}
func (rh *recomputeHeap) removeNodeUnsafe(item INode) {
rh.numItems--
id := item.Node().id
height := item.Node().heightInRecomputeHeap
rh.heights[height].remove(id)
isLastAtHeight := rh.heights[height].len() == 0
if height == rh.minHeight && isLastAtHeight {
rh.minHeight = rh.nextMinHeightUnsafe()
}
item.Node().heightInRecomputeHeap = HeightUnset
}
func (rh *recomputeHeap) maybeUpdateMinMaxHeightsUnsafe(newHeight int) {
if rh.numItems == 0 {
rh.minHeight = newHeight
rh.maxHeight = newHeight
return
}
if rh.minHeight > newHeight {
rh.minHeight = newHeight
}
if rh.maxHeight < newHeight {
rh.maxHeight = newHeight
}
}
func (rh *recomputeHeap) maybeAddNewHeightsUnsafe(newHeight int) {
if len(rh.heights) <= newHeight {
required := (newHeight - len(rh.heights)) + 1
for x := 0; x < required; x++ {
rh.heights = append(rh.heights, nil)
}
}
}
func (rh *recomputeHeap) nextMinHeightUnsafe() (next int) {
if rh.numItems == 0 {
return
}
for x := 0; x < len(rh.heights); x++ {
if rh.heights[x] != nil && rh.heights[x].len() > 0 {
next = x
break
}
}
return
}
func (rh *recomputeHeap) fixUnsafe(n INode) {
rh.removeNodeUnsafe(n)
rh.addNodeUnsafe(n)
}
// sanityCheck loops through each item in each height block
// and checks that all the height values match.
func (rh *recomputeHeap) sanityCheck() error {
if rh.numItems > 0 && (rh.heights[rh.minHeight] == nil || rh.heights[rh.minHeight].len() == 0) {
return fmt.Errorf("recompute heap; sanity check; lookup has items but min height block is empty")
}
for heightIndex, height := range rh.heights {
if height == nil {
continue
}
cursor := height.head
for cursor != nil {
if cursor.Node().heightInRecomputeHeap != heightIndex {
return fmt.Errorf("recompute heap; sanity check; at height %d item has height %d", heightIndex, cursor.Node().heightInRecomputeHeap)
}
if cursor.Node().heightInRecomputeHeap != cursor.Node().height {
return fmt.Errorf("recompute heap; sanity check; at height %d item has height %d and node has height %d", heightIndex, cursor.Node().heightInRecomputeHeap, cursor.Node().height)
}
cursor = cursor.Node().nextInRecomputeHeap
}
}
return nil
}