-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheap.go
68 lines (56 loc) · 1.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
package zkafka
import (
"container/heap"
"errors"
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
)
type offsetHeap struct {
data _offsetHeap
}
// Push adds an offset to the heap
func (h *offsetHeap) Push(offset kafka.TopicPartition) {
heap.Push(&h.data, offset)
}
// Pop returns the minimum offset from the heap and rearranges the heap to put the new minimum at the root
func (h *offsetHeap) Pop() kafka.TopicPartition {
if len(h.data) == 0 {
panic("popped empty heap")
}
return heap.Pop(&h.data).(kafka.TopicPartition)
}
// Peek returns the minimum offset from the heap without any side effects.
func (h *offsetHeap) Peek() (kafka.TopicPartition, error) {
if len(h.data) == 0 {
return kafka.TopicPartition{}, errors.New("peeked empty heap")
}
return (h.data)[0], nil
}
// SeekPop linearly searches the heap looking for a match, and removes and returns it.
// If nothing is found, nil is returned and the heap isn't mutated.
// It is an O(n) and therefore is not as efficient as Peek or Pop, but is necessary
// for removing arbitrary items from the data structure
func (h *offsetHeap) SeekPop(partition kafka.TopicPartition) *kafka.TopicPartition {
for i, d := range h.data {
if d == partition {
h.data = append(h.data[:i], h.data[i+1:]...)
return &d
}
}
return nil
}
// An _offsetHeap is a min-heap of topicPartitions where offset is used to determine order
type _offsetHeap []kafka.TopicPartition
var _ heap.Interface = (*_offsetHeap)(nil)
func (h _offsetHeap) Len() int { return len(h) }
func (h _offsetHeap) Less(i, j int) bool { return h[i].Offset < h[j].Offset }
func (h _offsetHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *_offsetHeap) Push(x any) {
*h = append(*h, x.(kafka.TopicPartition))
}
func (h *_offsetHeap) Pop() any {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}