Files
tasks/common/collection/priority_queue.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07: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 interface{}) {
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() interface{} {
pqItem := pq.items[pq.Len()-1]
pq.items = pq.items[0 : pq.Len()-1]
return pqItem
}