@@ -18,7 +18,7 @@ limitations under the License.
18
18
// as cache.heap, however, this heap does not perform synchronization. It leaves
19
19
// synchronization to the SchedulingQueue.
20
20
21
- package util
21
+ package heap
22
22
23
23
import (
24
24
"container/heap"
@@ -41,9 +41,9 @@ type itemKeyValue struct {
41
41
obj interface {}
42
42
}
43
43
44
- // heapData is an internal struct that implements the standard heap interface
44
+ // data is an internal struct that implements the standard heap interface
45
45
// and keeps the data stored in the heap.
46
- type heapData struct {
46
+ type data struct {
47
47
// items is a map from key of the objects to the objects and their index.
48
48
// We depend on the property that items in the map are in the queue and vice versa.
49
49
items map [string ]* heapItem
@@ -56,16 +56,16 @@ type heapData struct {
56
56
// should be deterministic.
57
57
keyFunc KeyFunc
58
58
// lessFunc is used to compare two objects in the heap.
59
- lessFunc LessFunc
59
+ lessFunc lessFunc
60
60
}
61
61
62
62
var (
63
- _ = heap .Interface (& heapData {}) // heapData is a standard heap
63
+ _ = heap .Interface (& data {}) // heapData is a standard heap
64
64
)
65
65
66
66
// Less compares two objects and returns true if the first one should go
67
67
// in front of the second one in the heap.
68
- func (h * heapData ) Less (i , j int ) bool {
68
+ func (h * data ) Less (i , j int ) bool {
69
69
if i > len (h .queue ) || j > len (h .queue ) {
70
70
return false
71
71
}
@@ -81,11 +81,11 @@ func (h *heapData) Less(i, j int) bool {
81
81
}
82
82
83
83
// Len returns the number of items in the Heap.
84
- func (h * heapData ) Len () int { return len (h .queue ) }
84
+ func (h * data ) Len () int { return len (h .queue ) }
85
85
86
86
// Swap implements swapping of two elements in the heap. This is a part of standard
87
87
// heap interface and should never be called directly.
88
- func (h * heapData ) Swap (i , j int ) {
88
+ func (h * data ) Swap (i , j int ) {
89
89
h .queue [i ], h .queue [j ] = h .queue [j ], h .queue [i ]
90
90
item := h.items [h.queue [i ]]
91
91
item .index = i
@@ -94,15 +94,15 @@ func (h *heapData) Swap(i, j int) {
94
94
}
95
95
96
96
// Push is supposed to be called by heap.Push only.
97
- func (h * heapData ) Push (kv interface {}) {
97
+ func (h * data ) Push (kv interface {}) {
98
98
keyValue := kv .(* itemKeyValue )
99
99
n := len (h .queue )
100
100
h .items [keyValue .key ] = & heapItem {keyValue .obj , n }
101
101
h .queue = append (h .queue , keyValue .key )
102
102
}
103
103
104
104
// Pop is supposed to be called by heap.Pop only.
105
- func (h * heapData ) Pop () interface {} {
105
+ func (h * data ) Pop () interface {} {
106
106
key := h .queue [len (h .queue )- 1 ]
107
107
h .queue = h .queue [0 : len (h .queue )- 1 ]
108
108
item , ok := h .items [key ]
@@ -115,7 +115,7 @@ func (h *heapData) Pop() interface{} {
115
115
}
116
116
117
117
// Peek is supposed to be called by heap.Peek only.
118
- func (h * heapData ) Peek () interface {} {
118
+ func (h * data ) Peek () interface {} {
119
119
if len (h .queue ) > 0 {
120
120
return h .items [h .queue [0 ]].obj
121
121
}
@@ -127,7 +127,7 @@ func (h *heapData) Peek() interface{} {
127
127
type Heap struct {
128
128
// data stores objects and has a queue that keeps their ordering according
129
129
// to the heap invariant.
130
- data * heapData
130
+ data * data
131
131
// metricRecorder updates the counter when elements of a heap get added or
132
132
// removed, and it does nothing if it's nil
133
133
metricRecorder metrics.MetricRecorder
@@ -239,15 +239,15 @@ func (h *Heap) Len() int {
239
239
return len (h .data .queue )
240
240
}
241
241
242
- // NewHeap returns a Heap which can be used to queue up items to process.
243
- func NewHeap (keyFn KeyFunc , lessFn LessFunc ) * Heap {
244
- return NewHeapWithRecorder (keyFn , lessFn , nil )
242
+ // New returns a Heap which can be used to queue up items to process.
243
+ func New (keyFn KeyFunc , lessFn lessFunc ) * Heap {
244
+ return NewWithRecorder (keyFn , lessFn , nil )
245
245
}
246
246
247
- // NewHeapWithRecorder wraps an optional metricRecorder to compose a Heap object.
248
- func NewHeapWithRecorder (keyFn KeyFunc , lessFn LessFunc , metricRecorder metrics.MetricRecorder ) * Heap {
247
+ // NewWithRecorder wraps an optional metricRecorder to compose a Heap object.
248
+ func NewWithRecorder (keyFn KeyFunc , lessFn lessFunc , metricRecorder metrics.MetricRecorder ) * Heap {
249
249
return & Heap {
250
- data : & heapData {
250
+ data : & data {
251
251
items : map [string ]* heapItem {},
252
252
queue : []string {},
253
253
keyFunc : keyFn ,
@@ -256,3 +256,7 @@ func NewHeapWithRecorder(keyFn KeyFunc, lessFn LessFunc, metricRecorder metrics.
256
256
metricRecorder : metricRecorder ,
257
257
}
258
258
}
259
+
260
+ // lessFunc is a function that receives two items and returns true if the first
261
+ // item should be placed before the second one when the list is sorted.
262
+ type lessFunc = func (item1 , item2 interface {}) bool
0 commit comments