-
Notifications
You must be signed in to change notification settings - Fork 2
/
wg_heap_gen.go
126 lines (106 loc) · 2.03 KB
/
wg_heap_gen.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
// Code generated by running "make generics". DO NOT EDIT.
package xsync
type wgHeap struct {
data []*wg
}
var wgHeapZeroItem *wg
func (h *wgHeap) Push(x *wg) {
i := len(h.data)
h.data = append(h.data, x)
x.pos = i
h.siftUp(i)
}
func (h *wgHeap) Pop() *wg {
return h.remove(0)
}
func (h *wgHeap) Max() *wg {
if len(h.data) > 0 {
return h.data[0]
}
return wgHeapZeroItem
}
func (h *wgHeap) Remove(x *wg) bool {
if h.data[x.pos] == x {
h.remove(x.pos)
return true
}
return false
}
func (h *wgHeap) Size() int {
return len(h.data)
}
func (h *wgHeap) Reserve(n int) {
m := len(h.data)
if m < n {
d := make([]*wg, m, n)
copy(d, h.data)
h.data = d
}
}
func (h *wgHeap) IsFull() bool {
return len(h.data) == cap(h.data)
}
func (h *wgHeap) IsEmpty() bool {
return len(h.data) == 0
}
func (h *wgHeap) remove(i int) *wg {
n := h.Size()
if n == 0 {
return wgHeapZeroItem
}
x := h.data[i]
h.swap(i, n-1)
x.pos = -1
h.data[n-1] = wgHeapZeroItem
h.data = h.data[:n-1]
if i == n-1 {
return x
}
if p := h.parent(i); p < len(h.data) && h.data[p].less(h.data[i]) {
h.siftUp(i)
} else {
h.siftDown(i)
}
return x
}
func (h *wgHeap) swap(i, j int) {
//log.Printf("swap #%d[%v]@%d <-> #%d[%v]@%d", i, h.data[i].t, h.data[i].pos, j, h.data[j].t, h.data[j].pos)
h.data[i], h.data[j] = h.data[j], h.data[i]
h.data[i].pos = i
h.data[j].pos = j
//log.Printf("now: #%d[%v]@%d", i, h.data[i].t, h.data[i].pos)
//log.Printf("now: #%d[%v]@%d", j, h.data[j].t, h.data[j].pos)
}
func (h *wgHeap) siftUp(i int) {
for i > 0 {
p := h.parent(i)
if !h.data[p].less(h.data[i]) {
return
}
h.swap(p, i)
i = p
}
}
func (h *wgHeap) siftDown(i int) {
for {
max := i
i1, i2 := h.children(i)
if i1 < len(h.data) && h.data[max].less(h.data[i1]) {
max = i1
}
if i2 < len(h.data) && h.data[max].less(h.data[i2]) {
max = i2
}
if max == i {
break
}
h.swap(i, max)
i = max
}
}
func (h *wgHeap) parent(x int) int {
return (x - 1) / 2
}
func (h *wgHeap) children(x int) (int, int) {
return 2*x + 1, 2*x + 2
}