-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.go
194 lines (179 loc) · 3.65 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package heap
import (
goheap "container/heap"
"errors"
"fmt"
)
var ErrUnsupportedHeap = errors.New("dynamic heap support not found for type:")
func Init(h interface{}) error {
// heapify
switch h := h.(type) {
case goheap.Interface:
goheap.Init(h)
default:
if !IsSupported(h) {
return fmt.Errorf("%w: %T", ErrUnsupportedHeap, h)
}
n := Len(h)
for i := n/2 - 1; i >= 0; i-- {
down(h, i, n)
}
}
return nil
}
// Push pushes the element x onto the heap.
// The complexity is O(log n) where n = h.Len().
func Push(h interface{}, x any) interface{} {
switch h := h.(type) {
case goheap.Interface:
goheap.Push(h, x)
default:
Append(h, x)
up(h, Len(h)-1)
}
return nil
}
func Append(h interface{}, x any) {
switch h := h.(type) {
case *[]int:
*h = append(*h, x.(int))
case *[]string:
*h = append(*h, x.(string))
default:
panic(fmt.Sprintf("unsupported heap type: %T", h))
}
}
// Pop removes and returns the minimum element (according to Less) from the heap.
// The complexity is O(log n) where n = h.Len().
// Pop is equivalent to Remove(h, 0).
func Pop(h interface{}) any {
switch h := h.(type) {
case goheap.Interface:
return goheap.Pop(h)
default:
n := Len(h) - 1
Swap(h, 0, n)
down(h, 0, n)
return PopLast(h)
}
}
func PopLast(h interface{}) any {
switch h := h.(type) {
case *[]int:
arr := *h
ret := arr[len(arr)-1]
*h = arr[:len(arr)-1]
return ret
case *[]string:
arr := *h
ret := arr[len(arr)-1]
*h = arr[:len(arr)-1]
return ret
default:
panic(fmt.Sprintf("unsupported type: %T", h))
}
}
func Len(h interface{}) int {
switch h := h.(type) {
case *[]int:
return len(*h)
case *[]string:
return len(*h)
default:
panic(fmt.Sprintf("unsupported type: %T", h))
}
}
// Remove removes and returns the element at index i from the heap.
// The complexity is O(log n) where n = h.Len().
func Remove(h interface{}, i int) any {
switch h := h.(type) {
case goheap.Interface:
return goheap.Remove(h, i)
default:
n := Len(h) - 1
if n != i {
Swap(h, i, n)
if !down(h, i, n) {
up(h, i)
}
}
return PopLast(h)
}
}
// Fix re-establishes the heap ordering after the element at index i has changed its value.
// Changing the value of the element at index i and then calling Fix is equivalent to,
// but less expensive than, calling Remove(h, i) followed by a Push of the new value.
// The complexity is O(log n) where n = h.Len().
func Fix(h interface{}, i int) {
switch h := h.(type) {
case goheap.Interface:
goheap.Fix(h, i)
default:
if !down(h, i, Len(h)) {
up(h, i)
}
}
}
func Swap(h interface{}, i, j int) {
switch h := h.(type) {
case *[]int:
arr := *h
arr[i], arr[j] = arr[j], arr[i]
case *[]string:
arr := *h
arr[i], arr[j] = arr[j], arr[i]
default:
panic(fmt.Sprintf("unsupported type: %T", h))
}
}
func up(h interface{}, j int) {
for {
i := (j - 1) / 2 // parent
if i == j || !Less(h, j, i) {
break
}
Swap(h, i, j)
j = i
}
}
func down(h interface{}, 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 && Less(h, j2, j1) {
j = j2 // = 2*i + 2 // right child
}
if !Less(h, j, i) {
break
}
Swap(h, i, j)
i = j
}
return i > i0
}
func IsSupported(h interface{}) bool {
switch h.(type) {
case *[]int:
return true
default:
return false
}
}
func Less(h interface{}, i, j int) bool {
switch h := h.(type) {
case goheap.Interface:
return h.Less(i, j)
case *[]int:
arr := *h
return arr[i] < arr[j]
case *[]string:
arr := *h
return arr[i] < arr[j]
default:
panic(fmt.Sprintf("unsupported heap element type: %T", h))
}
}