-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathiheap.go
186 lines (161 loc) · 3.66 KB
/
iheap.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
package ron
import "sort"
//import "github.com/gritzko/RON"
// FrameHeap is an iterator heap - gives the minimum available element
// at every step. Useful for merge sort like algorithms.
type FrameHeap struct {
// Most of the time, a heap has 2 elements, optimize for that.
// Sometimes, it can get millions of elements, ensure that is O(NlogN)
iters []*Frame
primary, secondary Comparator
}
func MakeFrameHeap(primary, secondary Comparator, size int) (ret FrameHeap) {
ret.iters = make([]*Frame, 1, size+1)
ret.primary = primary
ret.secondary = secondary
return
}
func (heap FrameHeap) less(i, j int) bool {
c := heap.primary(heap.iters[i], heap.iters[j])
if c == 0 {
if heap.secondary != nil {
c = heap.secondary(heap.iters[i], heap.iters[j])
} else {
c = int64(j) - int64(i)
}
}
//fmt.Printf("CMP %s %s GOT %d\n", heap.iters[i].OpString(), heap.iters[j].OpString(), c)
return c < 0
}
func (heap *FrameHeap) sink(i int) {
to := i
j := i << 1
if j < len(heap.iters) && heap.less(j, i) {
to = j
}
j++
if j < len(heap.iters) && heap.less(j, to) {
to = j
}
if to != i {
heap.swap(i, to)
heap.sink(to)
}
}
func (heap *FrameHeap) raise(i int) {
j := i >> 1
if j > 0 && heap.less(i, j) {
heap.swap(i, j)
if j > 1 {
heap.raise(j)
}
}
}
func (heap FrameHeap) Len() int { return len(heap.iters) - 1 }
func (heap FrameHeap) swap(i, j int) {
//fmt.Printf("SWAP %d %d\n", i, j)
heap.iters[i], heap.iters[j] = heap.iters[j], heap.iters[i]
}
func (heap *FrameHeap) PutAll(b Batch) {
for i := 0; i < len(b); i++ {
heap.Put(&b[i])
}
}
func (heap *FrameHeap) Put(i *Frame) {
for {
if !i.EOF() && (i.IsHeader() || i.IsQuery()) {
i.Next()
} else {
break
}
}
if !i.EOF() && !i.IsHeader() {
at := len(heap.iters)
heap.iters = append(heap.iters, i)
heap.raise(at)
}
}
func (heap *FrameHeap) Current() (frame *Frame) {
if len(heap.iters) > 1 {
return heap.iters[1]
} else {
return nil
}
}
func (heap *FrameHeap) remove(i int) {
heap.iters[i] = heap.iters[len(heap.iters)-1]
heap.iters = heap.iters[:len(heap.iters)-1]
heap.sink(i)
}
func (heap *FrameHeap) next(i int) {
heap.iters[i].Next()
if heap.iters[i].EOF() || heap.iters[i].IsHeader() {
heap.remove(i)
} else {
heap.sink(i)
}
}
func (heap *FrameHeap) Next() (frame *Frame) {
heap.next(1)
for len(heap.iters) > 1 && heap.iters[1].Type() == COMMENT_UUID { // skip comments
heap.next(1)
}
return heap.Current()
}
func (heap FrameHeap) listEqs(at int, eqs *[]int) {
*eqs = append(*eqs, at)
l := at << 1
if l < len(heap.iters) {
if 0 == heap.primary(heap.iters[1], heap.iters[l]) {
heap.listEqs(l, eqs)
}
r := l | 1
if r < len(heap.iters) {
if 0 == heap.primary(heap.iters[1], heap.iters[r]) {
heap.listEqs(r, eqs)
}
}
}
}
func (heap *FrameHeap) NextPrim() (frame *Frame) {
var _eqs [16]int
eqs := _eqs[0:0:16]
heap.listEqs(1, &eqs)
if len(eqs) > 1 {
sort.Ints(eqs)
}
for i := len(eqs) - 1; i >= 0; i-- {
heap.next(eqs[i])
heap.sink(eqs[i])
}
return heap.Current()
}
func (heap *FrameHeap) PutFrame(frame Frame) {
heap.Put(&frame)
}
func (heap *FrameHeap) EOF() bool {
return len(heap.iters) == 1
}
func (heap *FrameHeap) Frame() Frame {
cur := MakeFrame(128)
for !heap.EOF() {
cur.Append(*heap.Current())
heap.Next()
}
return cur.Close()
}
func (heap *FrameHeap) Clear() {
heap.iters = heap.iters[:1]
}
func EventComparator(a, b *Frame) int64 {
return a.Event().Compare(b.Event())
}
func EventComparatorDesc(a, b *Frame) int64 {
return b.Event().Compare(a.Event())
}
func RefComparator(a, b *Frame) int64 {
return a.Ref().Compare(b.Ref())
}
func RefComparatorDesc(a, b *Frame) int64 {
return b.Ref().Compare(a.Ref())
}