- generics + comparator-func
- time complexity O(n*log(n)) as any other priority queue
- examples
import "github.com/turneps403/prqueue"
pqMin := prqueue.New(func(a, b int) bool { return a < b })
pqMax := prqueue.New(func(a, b int) bool { return a > b })
type custom struct {
w int
}
pqMin := prqueue.New(func(a, b custom) bool { return a.w < b.w })
pqMax := prqueue.New(func(a, b custom) bool { return a.w > b.w })
pq.Add(el)
el, err := pq.Poll() // Retrieves and removes
el, err := pq.Peek() // Retrieves, but does not remove
pq.IsEmpty() // bool
pq.Len() // bool
recieves a comparator-func and optional capacity.
func New[T any]( func(a, b T) bool, ...int ) *pq[T]
e.g.
pq := prqueue.New(1000, func(a, b int) bool { return a < b })
pq := prqueue.New(func(a, b int) bool { return a < b })
pqmin := prqueue.New(func(a, b int) bool { return a < b })
pqmax := prqueue.New(func(a, b int) bool { return a > b })
inserts the specified element into this priority queue. Thread safe.
func (*pq[T]) Add(el T)
retrieves and removes the head of this queue, or return error ErrEmptyQueue. Thread safe.
func (*pq[T]) Poll() (el T, er error)
retrieves, but does not remove, the head of this queue, or return error ErrEmptyQueue. Thread safe.
func (*pq[T]) Peek() (el T, er error)
returns true if this collection contains no elements.
func (*pq[T]) IsEmpty() bool
current length of queue
func (*pq[T]) Len() int
indicates about empty queue. contains string "priority queue is empty"