Files
tasks/common/collection/priority_queue.go
Stephan BehnkeandGitHub c9e360a3fb go fix (#9337)
## What changed?

Integrate Go 1.26's new `go fix` into workflow.

NOTE that the changes caused our linter to fire; a [separate
commit](https://github.com/temporalio/temporal/pull/9337/commits/1b23f787aef370098b35829c4d17dcc06230b4c1)
addresses those.

## Why?

Ensure Go code is standardized/modernized.

## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [x] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)
2026-02-18 09:12:19 -08:00

91 lines
2.2 KiB
Go

package collection
import (
"container/heap"
)
type (
priorityQueueImpl[T any] struct {
compareLess func(this T, other T) bool
items []T
}
)
// NewPriorityQueue create a new priority queue
func NewPriorityQueue[T any](
compareLess func(this T, other T) bool,
) Queue[T] {
return &priorityQueueImpl[T]{
compareLess: compareLess,
}
}
// NewPriorityQueueWithItems creats a new priority queue
// with the provided list of items.
// PriorityQueue will take ownership of the passed in items,
// so caller should stop modifying it.
// The complexity is O(n) where n is the number of items
func NewPriorityQueueWithItems[T any](
compareLess func(this T, other T) bool,
items []T,
) Queue[T] {
pq := &priorityQueueImpl[T]{
compareLess: compareLess,
items: items,
}
heap.Init(pq)
return pq
}
// Peek returns the top item of the priority queue
func (pq *priorityQueueImpl[T]) Peek() T {
if pq.IsEmpty() {
panic("Cannot peek item because priority queue is empty")
}
return pq.items[0]
}
// Add push an item to priority queue
func (pq *priorityQueueImpl[T]) Add(item T) {
heap.Push(pq, item)
}
// Remove pop an item from priority queue
func (pq *priorityQueueImpl[T]) Remove() T {
return heap.Pop(pq).(T)
}
// IsEmpty indicate if the priority queue is empty
func (pq *priorityQueueImpl[T]) IsEmpty() bool {
return pq.Len() == 0
}
// below are the functions used by heap.Interface and go internal heap implementation
// Len implements sort.Interface
func (pq *priorityQueueImpl[T]) Len() int {
return len(pq.items)
}
// Less implements sort.Interface
func (pq *priorityQueueImpl[T]) Less(i, j int) bool {
return pq.compareLess(pq.items[i], pq.items[j])
}
// Swap implements sort.Interface
func (pq *priorityQueueImpl[T]) Swap(i, j int) {
pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
}
// Push push an item to priority queue, used by go internal heap implementation
func (pq *priorityQueueImpl[T]) Push(item any) {
pq.items = append(pq.items, item.(T))
}
// Pop pop an item from priority queue, used by go internal heap implementation
func (pq *priorityQueueImpl[T]) Pop() any {
pqItem := pq.items[pq.Len()-1]
pq.items = pq.items[0 : pq.Len()-1]
return pqItem
}