planner
This commit is contained in:
36
cmd/planner/prio-queue.go
Normal file
36
cmd/planner/prio-queue.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import "time"
|
||||
|
||||
type pqItem struct {
|
||||
Stop string
|
||||
Cost time.Time
|
||||
index int
|
||||
}
|
||||
|
||||
type priorityQueue []*pqItem
|
||||
|
||||
func (pq priorityQueue) Len() int { return len(pq) }
|
||||
|
||||
func (pq priorityQueue) Less(i, j int) bool { return pq[i].Cost.Before(pq[j].Cost) }
|
||||
|
||||
func (pq priorityQueue) Swap(i, j int) {
|
||||
pq[i], pq[j] = pq[j], pq[i]
|
||||
pq[i].index = i
|
||||
pq[j].index = j
|
||||
}
|
||||
|
||||
func (pq *priorityQueue) Push(x any) {
|
||||
item := x.(*pqItem)
|
||||
item.index = len(*pq)
|
||||
*pq = append(*pq, item)
|
||||
}
|
||||
|
||||
func (pq *priorityQueue) Pop() any {
|
||||
old := *pq
|
||||
n := len(old)
|
||||
item := old[n-1]
|
||||
item.index = -1
|
||||
*pq = old[:n-1]
|
||||
return item
|
||||
}
|
||||
Reference in New Issue
Block a user