-
Notifications
You must be signed in to change notification settings - Fork 1
/
heap.go
179 lines (150 loc) · 2.56 KB
/
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
package ttl
import "fmt"
type Heap[T any] struct {
data []T
m map[string]int
less func(x, y T) bool
key func(T) string
}
func NewHeap[T any](data []T, less func(x, y T) bool, key func(T) string) *Heap[T] {
h := &Heap[T]{data: data, less: less, key: key, m: map[string]int{}}
n := h.Len()
if key != nil {
for i, t := range h.data {
h.m[key(t)] = i
}
}
for i := n/2 - 1; i >= 0; i-- {
h.down(i, n)
}
return h
}
func (h *Heap[T]) Push(x T) {
h.data = append(h.data, x)
if h.key != nil {
h.m[h.key(x)] = h.Len() - 1
}
h.up(h.Len() - 1)
}
func (h *Heap[T]) Pop() (T, bool) {
var t T
if h.Empty() {
return t, false
}
n := h.Len() - 1
h.Swap(0, n)
h.down(0, n)
return h.pop(), true
}
func (h *Heap[T]) pop() T {
var t T
item := h.data[h.Len()-1]
h.data[h.Len()-1] = t // set zero value
if h.key != nil {
delete(h.m, h.key(item))
}
h.data = h.data[0 : h.Len()-1]
return item
}
func (h *Heap[T]) Peek() (T, bool) {
if h.Empty() {
var t T
return t, false
}
return h.data[0], true
}
func (h *Heap[T]) remove(i int) T {
if i >= h.Len() {
var t T
return t
}
n := h.Len() - 1
if n != i {
h.Swap(i, n)
if !h.down(i, n) {
h.up(i)
}
}
return h.pop()
}
func (h *Heap[T]) Remove(key string) (t T) {
if h.key == nil {
return
}
return h.remove(h.m[key])
}
func (h *Heap[T]) Update(t T) {
if h.key == nil {
return
}
i := h.m[h.key(t)]
h.data[i] = t
h.Fix(i)
}
func (h *Heap[T]) Fix(i int) {
if h.Empty() {
return
}
if !h.down(i, h.Len()) {
h.up(i)
}
}
func (h *Heap[T]) Len() int {
return len(h.data)
}
func (h *Heap[T]) Empty() bool {
return h.Len() == 0
}
func (h *Heap[T]) Less(i, j int) bool {
return h.less(h.data[i], h.data[j])
}
func (h *Heap[T]) Swap(i, j int) {
h.data[i], h.data[j] = h.data[j], h.data[i]
if h.key != nil {
h.m[h.key(h.data[i])] = i
h.m[h.key(h.data[j])] = j
}
}
func (h *Heap[T]) Get(key string) (t T) {
index, ok := h.m[key]
if ok {
return h.data[index]
}
return
}
func (h *Heap[T]) Scan() []T {
items := make([]T, len(h.data))
copy(items, h.data)
fmt.Println(h.data)
fmt.Println(h.m)
return items
}
func (h *Heap[T]) up(j int) {
for {
i := (j - 1) / 2 // parent
if i == j || !h.Less(j, i) {
break
}
h.Swap(i, j)
j = i
}
}
func (h *Heap[T]) down(i0, n int) bool {
i := i0
for {
j1 := 2*i + 1
if j1 >= n || j1 < 0 { // j1 < 0 after int overflow
break
}
j := j1 // left child
if j2 := j1 + 1; j2 < n && h.Less(j2, j1) {
j = j2 // = 2*i + 2 // right child
}
if !h.Less(j, i) {
break
}
h.Swap(i, j)
i = j
}
return i > i0
}