-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.go
47 lines (40 loc) · 940 Bytes
/
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
package heap
import "container/heap"
// Heap is a binary min heap, generic wrapper of container/heap
type Heap[T any] struct {
base *base[T]
}
// New return a binary min heap with corresponding type
func New[T any](lessFunc func(T, T) bool) *Heap[T] {
b := &base[T]{
lessFunc: lessFunc,
}
heap.Init(b)
return &Heap[T]{base: b}
}
// Push add an element to the heap
func (h *Heap[T]) Push(t T) {
heap.Push(h.base, t)
}
// Pop removes and returns the minimum element (according to Less) from the heap.
// The complexity is O(log n) where n = h.Len().
func (h *Heap[T]) Pop() T {
if h.Len() == 0 {
var t T
return t
}
return heap.Pop(h.base).(T)
}
// Len return heap size
func (h *Heap[T]) Len() int {
return h.base.Len()
}
// Peek returns the minimum element (according to Less) from the heap.
// The complexity is O(1).
func (h *Heap[T]) Peek() T {
if h.Len() == 0 {
var t T
return t
}
return h.base.Peek().(T)
}