2024-03-28 12:53:26 +00:00
|
|
|
package pilorama
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type heapInfo struct {
|
|
|
|
id Node
|
|
|
|
filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
type filenameHeap []heapInfo
|
|
|
|
|
|
|
|
func (h filenameHeap) Len() int { return len(h) }
|
|
|
|
func (h filenameHeap) Less(i, j int) bool { return h[i].filename < h[j].filename }
|
|
|
|
func (h filenameHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
|
|
|
|
func (h *filenameHeap) Push(x any) {
|
|
|
|
*h = append(*h, x.(heapInfo))
|
|
|
|
}
|
2024-04-03 08:44:02 +00:00
|
|
|
|
2024-03-28 12:53:26 +00:00
|
|
|
func (h *filenameHeap) Pop() any {
|
|
|
|
old := *h
|
|
|
|
n := len(old)
|
|
|
|
x := old[n-1]
|
|
|
|
*h = old[0 : n-1]
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
// fixedHeap maintains a fixed number of smallest elements started at some point.
|
|
|
|
type fixedHeap struct {
|
|
|
|
start string
|
|
|
|
max string
|
|
|
|
count int
|
|
|
|
h *filenameHeap
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHeap(start string, count int) *fixedHeap {
|
|
|
|
h := new(filenameHeap)
|
|
|
|
heap.Init(h)
|
|
|
|
|
|
|
|
return &fixedHeap{
|
|
|
|
start: start,
|
|
|
|
max: "",
|
|
|
|
count: count,
|
|
|
|
h: h,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *fixedHeap) push(id Node, filename string) bool {
|
|
|
|
if filename == "" || filename <= h.start {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
heap.Push(h.h, heapInfo{id: id, filename: filename})
|
|
|
|
if h.h.Len() > h.count {
|
|
|
|
heap.Remove(h.h, h.h.Len()-1)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *fixedHeap) pop() (heapInfo, bool) {
|
|
|
|
if h.h.Len() != 0 {
|
|
|
|
return heap.Pop(h.h).(heapInfo), true
|
|
|
|
}
|
|
|
|
return heapInfo{}, false
|
|
|
|
}
|