-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.go
More file actions
137 lines (121 loc) · 2.67 KB
/
heap.go
File metadata and controls
137 lines (121 loc) · 2.67 KB
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
package golds
// Heap data structure.
type Heap[T any] struct {
data []T
cmp func(a, b T) bool
}
// NewHeap instantiates a new heap with a given size.
func NewHeap[T any](size int, cmp func(a, b T) bool) *Heap[T] {
h := &Heap[T]{
data: make([]T, 0, size),
cmp: cmp,
}
return h
}
// NewHeapFrom instantiates a new heap with a given size.
func NewHeapFrom[T any](v []T, cmp func(a, b T) bool) *Heap[T] {
h := &Heap[T]{
data: make([]T, len(v)),
cmp: cmp,
}
h.Build(v)
return h
}
// Size of the heap.
func (h *Heap[T]) Size() int {
return len(h.data)
}
// Reset the structure. Remove all the elements.
func (h *Heap[T]) Reset() {
h.data = h.data[:0]
}
// Build pushes all items from values to the heap
func (h *Heap[T]) Build(values []T) {
size := len(values)
if len(h.data) < size {
h.data = make([]T, size)
}
copy(h.data, values)
for i := size/2 - 1; i >= 0; i-- {
h.siftDown(i)
}
}
// Push element to the heap.
func (h *Heap[T]) Push(value T) {
h.data = append(h.data, value)
h.siftUpLast()
}
// PushMany elements to the heap.
func (h *Heap[T]) PushMany(values ...T) {
for _, v := range values {
h.Push(v)
}
}
// Pop removes and returns top element of the heap
func (h *Heap[T]) Pop() (value T, ok bool) {
if h.Size() == 0 {
return zeroOf[T](), false
}
size := len(h.data) - 1
h.data[0], h.data[size] = h.data[size], h.data[0]
h.siftDown(0)
value, h.data = h.data[size], h.data[:size]
return value, true
}
// PopMany removes and returns top k elements of the heap
func (h *Heap[T]) PopMany(k int) (values []T, ok bool) {
if h.Size() == 0 {
return nil, false
}
k = min(k, len(h.data))
values = make([]T, k)
for i := 0; i < k; i++ {
value, _ := h.Pop()
values[i] = value
}
return values, true
}
// Top returns top element of the heap
func (h *Heap[T]) Top() (value T, ok bool) {
if h.Size() == 0 {
return zeroOf[T](), false
}
return h.data[0], true
}
// Values that are in the heap.
func (h *Heap[T]) Values() []T {
vals := make([]T, len(h.data))
copy(vals, h.data)
return vals
}
// down pushes element down in the heap-tree
func (h *Heap[T]) siftDown(i int) {
data := h.data
for i >= 0 && i < len(data) {
j := 2*i + 1
if j <= 0 || j >= len(data)-1 {
break
}
if j2 := j + 1; j2 < len(data)-1 && j2 >= 0 && h.cmp(data[j2], data[j]) {
j = j2
}
if h.cmp(data[i], data[j]) {
break
}
data[i], data[j] = data[j], data[i]
i = j
}
}
// siftUpLast pushes element up in the heap-tree.
func (h *Heap[T]) siftUpLast() {
data := h.data
i := uint(len(data)) - 1
j := (uint(len(data)) - 2) >> 1
for i > j && i < uint(len(data)) {
if !h.cmp(data[i], data[j]) {
break
}
data[i], data[j] = data[j], data[i]
i, j = j, (j-1)>>1
}
}