-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.go
177 lines (157 loc) · 4.39 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
// Package heap implements a generic heap data structure.
package heap
import (
"cmp"
"iter"
"slices"
)
// A Heap is a heap of Ts.
type Heap[T any] struct {
lessFunc func(T, T) bool
values []T
}
// NewHeap returns a new heap that uses lessFunc to compare elements.
func NewHeap[T any](lessFunc func(T, T) bool) *Heap[T] {
return &Heap[T]{
lessFunc: lessFunc,
}
}
// NewOrderedHeap returns a new heap that operates on [cmp.Ordered] elements.
func NewOrderedHeap[T cmp.Ordered]() *Heap[T] {
return NewHeap(cmp.Less[T])
}
// NewReverseOrderedHeap returns a new heap that operates on [cmp.Ordered]
// elements in reverse order.
func NewReverseOrderedHeap[T cmp.Ordered]() *Heap[T] {
return NewHeap(func(a, b T) bool {
return cmp.Less(b, a)
})
}
// Cap returns the underlying capacity of h.
func (h *Heap[T]) Cap() int {
return cap(h.values)
}
// Clip removes unused capacity from h.
func (h *Heap[T]) Clip() *Heap[T] {
h.values = slices.Clip(h.values)
return h
}
// Empty returns whether h is empty in O(1) time and memory.
func (h *Heap[T]) Empty() bool {
return len(h.values) == 0
}
// Grow increases h's capacity by at least n.
func (h *Heap[T]) Grow(n int) *Heap[T] {
h.values = slices.Grow(h.values, n)
return h
}
// Len returns the size of h in O(1) time and memory.
func (h *Heap[T]) Len() int {
return len(h.values)
}
// MustPop returns the lowest value in h. It panics if h is empty.
func (h *Heap[T]) MustPop() T {
value, ok := h.Pop()
if !ok {
panic("empty heap")
}
return value
}
// Peek returns the lowest value in h in O(1) time and memory, without removing
// it, and whether it exists.
func (h *Heap[T]) Peek() (T, bool) {
if h.Empty() {
var zero T
return zero, false
}
return h.values[0], true
}
// Pop returns the lowest value in h, removing it, and whether it exists in O(N)
// time and O(1) memory.
func (h *Heap[T]) Pop() (T, bool) {
switch n := len(h.values); n {
case 0:
var zero T
return zero, false
case 1:
value := h.values[0]
h.values = h.values[:0] // Truncate values instead of setting values to nil to reduce GC pressure.
return value, true
default:
value := h.values[0]
h.values[0] = h.values[n-1]
h.values = h.values[:n-1]
h.siftDown(0)
return value, true
}
}
// PopAll returns an iterator that pops all values.
func (h *Heap[T]) PopAll() iter.Seq[T] {
return func(yield func(T) bool) {
for value, ok := h.Pop(); ok && yield(value); value, ok = h.Pop() { //nolint:revive
}
}
}
// Push adds value to h in amortized O(N) time.
func (h *Heap[T]) Push(value T) *Heap[T] {
h.values = append(h.values, value)
h.siftUp(len(h.values) - 1)
return h
}
// PushMany pushes multiple values onto the heap.
func (h *Heap[T]) PushMany(values ...T) *Heap[T] {
n := len(h.values)
h.values = append(h.values, values...)
for i := range values {
h.siftUp(n + i)
}
return h
}
// PushPop pushes value onto the heap and then pops the lowest value off the
// heap and returns it in O(N) time. It is slightly more efficient than separate
// calls to [Heap.Push] and [Heap.Pop].
func (h *Heap[T]) PushPop(value T) T {
if len(h.values) == 0 || h.lessFunc(value, h.values[0]) {
return value
}
value, h.values[0] = h.values[0], value
h.siftDown(0)
return value
}
// Set sets the values on h to be values in amortized O(N) time. h takes
// ownership of values.
func (h *Heap[T]) Set(values []T) *Heap[T] {
h.values = values
for index := len(values) / 2; index >= 0; index-- {
h.siftDown(index)
}
return h
}
// siftDown implements the sift down operation, moving the element at index
// towards the leaves.
func (h *Heap[T]) siftDown(index int) {
for n := len(h.values); index < n/2; {
leftChildIndex, rightChildIndex := 2*index+1, 2*index+2
smallestChildIndex := leftChildIndex
if rightChildIndex < n && h.lessFunc(h.values[rightChildIndex], h.values[leftChildIndex]) {
smallestChildIndex = rightChildIndex
}
if h.lessFunc(h.values[index], h.values[smallestChildIndex]) {
return
}
h.values[index], h.values[smallestChildIndex] = h.values[smallestChildIndex], h.values[index]
index = smallestChildIndex
}
}
// siftUp implements the sift up operation, moving the element at index towards
// the root.
func (h *Heap[T]) siftUp(index int) {
for index > 0 {
parentIndex := (index - 1) / 2
if h.lessFunc(h.values[parentIndex], h.values[index]) {
return
}
h.values[index], h.values[parentIndex] = h.values[parentIndex], h.values[index]
index = parentIndex
}
}