-
Notifications
You must be signed in to change notification settings - Fork 451
/
heap.go
192 lines (164 loc) · 4.84 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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package utils
import (
"container/heap"
)
// ValueIndexPair is a pair of float value and index at which it exists
type ValueIndexPair struct {
Val float64
Index int
}
type lessFn func(ValueIndexPair, ValueIndexPair) bool
func maxHeapLess(i, j ValueIndexPair) bool {
if i.Val == j.Val {
return i.Index > j.Index
}
return i.Val < j.Val
}
func minHeapLess(i, j ValueIndexPair) bool {
if i.Val == j.Val {
return i.Index > j.Index
}
return i.Val > j.Val
}
// FloatHeap is a heap that can be given a maximum size
type FloatHeap struct {
isMaxHeap bool
capacity int
floatHeap *floatHeap
}
// NewFloatHeap builds a new FloatHeap based on first parameter
// and a capacity given by second parameter. Zero and negative
// values for maxSize provide an unbounded FloatHeap
func NewFloatHeap(isMaxHeap bool, capacity int) FloatHeap {
var less lessFn
if isMaxHeap {
less = maxHeapLess
} else {
less = minHeapLess
}
if capacity < 1 {
capacity = 0
}
floatHeap := &floatHeap{
heap: make([]ValueIndexPair, 0, capacity),
less: less,
}
heap.Init(floatHeap)
return FloatHeap{
isMaxHeap: isMaxHeap,
capacity: capacity,
floatHeap: floatHeap,
}
}
// Push pushes a value and index pair to the heap
func (fh FloatHeap) Push(value float64, index int) {
h := fh.floatHeap
// If capacity is zero or negative, allow infinite size heap
if fh.capacity > 0 {
// At max size, drop or replace incoming value.
// Otherwise, continue as normal
if len(h.heap) >= fh.capacity {
peek := h.heap[0]
// Compare incoming value with current top of heap.
// Decide if to replace the current top, or to drop incoming
// value as appropriate for this min/max heap
//
// If values are equal, do not add incoming value regardless
//
// NB(arnikola): unfortunately, can't just replace first
// element as it may not respect internal order. Need to
// run heap.Fix() to rectify this
if fh.isMaxHeap && value > peek.Val ||
(!fh.isMaxHeap && value < peek.Val) {
h.heap[0] = ValueIndexPair{
Val: value,
Index: index,
}
heap.Fix(h, 0)
}
return
}
// Otherwise, fallthrough
}
heap.Push(h, ValueIndexPair{
Val: value,
Index: index,
})
}
// Len returns the current length of the heap
func (fh FloatHeap) Len() int {
return fh.floatHeap.Len()
}
// Cap returns the capacity of the heap
func (fh FloatHeap) Cap() int {
return fh.capacity
}
// Reset resets the heap
func (fh *FloatHeap) Reset() {
fh.floatHeap.heap = fh.floatHeap.heap[:0]
}
// Flush flushes the float heap and resets it. Does not guarantee order.
func (fh FloatHeap) Flush() []ValueIndexPair {
values := fh.floatHeap.heap
fh.Reset()
return values
}
// Peek reveals the top value of the heap without mutating the heap.
func (fh FloatHeap) Peek() (ValueIndexPair, bool) {
h := fh.floatHeap.heap
if len(h) == 0 {
return ValueIndexPair{}, false
}
return h[0], true
}
// floatHeap is a heap that can be given a maximum size
type floatHeap struct {
less lessFn
heap []ValueIndexPair
}
// Assert that floatHeap is a heap.Interface
var _ heap.Interface = (*floatHeap)(nil)
// Len gives the length of items in the heap
func (h *floatHeap) Len() int {
return len(h.heap)
}
// Less is true if value of i is less than value of j
func (h *floatHeap) Less(i, j int) bool {
return h.less(h.heap[i], h.heap[j])
}
// Swap swaps values at these indices
func (h *floatHeap) Swap(i, j int) {
h.heap[i], h.heap[j] = h.heap[j], h.heap[i]
}
// Push pushes a ValueIndexPair to the FloatMaxHeap
func (h *floatHeap) Push(x interface{}) {
pair := x.(ValueIndexPair)
h.heap = append(h.heap, pair)
}
// Pop pops a ValueIndexPair from the FloatMaxHeap
func (h *floatHeap) Pop() interface{} {
old := h.heap
n := len(old)
tail := old[n-1]
h.heap = old[:n-1]
return tail
}