erigon-pulse/common/etl/heap.go

41 lines
655 B
Go

package etl
import "bytes"
type HeapElem struct {
key []byte
timeIdx int
value []byte
}
type Heap []HeapElem
func (h Heap) Len() int {
return len(h)
}
func (h Heap) Less(i, j int) bool {
if c := bytes.Compare(h[i].key, h[j].key); c != 0 {
return c < 0
}
return h[i].timeIdx < h[j].timeIdx
}
func (h Heap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *Heap) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*h = append(*h, x.(HeapElem))
}
func (h *Heap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}