-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.go
59 lines (48 loc) · 1.13 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
package gwrap
import (
"container/heap"
)
type innerHeap[T any] struct {
slice []T
less func(T, T) bool
}
// Heap is implemented with container/heap and is not thread-safe
type Heap[T any] struct {
i innerHeap[T]
}
// NewHeap creates a Heap from a comparison function.
func NewHeap[T any](less func(a T, b T) bool) *Heap[T] {
i := innerHeap[T]{
slice: make([]T, 0, 100),
less: less,
}
heap.Init(&i)
return &Heap[T]{
i: i,
}
}
// Code below originated with the container/heap documentation
func (h *innerHeap[T]) Len() int { return len(h.slice) }
func (h *innerHeap[T]) Less(i, j int) bool { return h.less(h.slice[i], h.slice[j]) }
func (h *innerHeap[T]) Swap(i, j int) { h.slice[i], h.slice[j] = h.slice[j], h.slice[i] }
// Push is O(log n)
func (h *Heap[T]) Push(x T) {
heap.Push(&h.i, x)
}
func (h *innerHeap[T]) Push(x any) {
h.slice = append(h.slice, x.(T))
}
// Pop is O(log n)
func (h *Heap[T]) Pop() T {
return heap.Pop(&h.i).(T)
}
func (h *innerHeap[T]) Pop() any {
old := h.slice
n := len(old)
x := old[n-1]
h.slice = old[0 : n-1]
return x
}
func (h Heap[T]) Len() int {
return len(h.i.slice)
}