Documentation
¶
Overview ¶
Package heaps implements a generic heap data structure.
Index ¶
- func BufferedPriorityChannel[T any](ctx context.Context, inCh <-chan T, size int, lessFunc func(T, T) bool) <-chan T
- func PriorityChannel[T any](ctx context.Context, inCh <-chan T, lessFunc func(T, T) bool) <-chan T
- type Heap
- func (h *Heap[T]) Cap() int
- func (h *Heap[T]) Clip() *Heap[T]
- func (h *Heap[T]) Empty() bool
- func (h *Heap[T]) Grow(n int) *Heap[T]
- func (h *Heap[T]) Len() int
- func (h *Heap[T]) MustPop() T
- func (h *Heap[T]) Peek() (T, bool)
- func (h *Heap[T]) Pop() (T, bool)
- func (h *Heap[T]) PopAll() iter.Seq[T]
- func (h *Heap[T]) Push(value T) *Heap[T]
- func (h *Heap[T]) PushMany(values ...T) *Heap[T]
- func (h *Heap[T]) PushPop(value T) T
- func (h *Heap[T]) Set(values []T) *Heap[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BufferedPriorityChannel ¶ added in v0.0.2
func BufferedPriorityChannel[T any](ctx context.Context, inCh <-chan T, size int, lessFunc func(T, T) bool) <-chan T
BufferedPriorityChannel reads values from inCh and returns a channel that returns the same values prioritized according to lessFunc, with lesser values returned first.
It maintains a buffer of size size, reading from inCh until the buffer is full, and then returning the values in priority over the returned channel, and reading more values from inCh when required. When inCh is closed, all remaining values are written to the returned channel, and then the returned channel is closed.
If ctx is canceled then the returned channel is closed immediately.
func PriorityChannel ¶
PriorityChannel greedily reads values from inCh and returns a channel that returns the same values prioritized according to lessFunc, with lesser values returned first.
When inCh is closed, all remaining values are written to the returned channel, and then the returned channel is closed.
If ctx is canceled then the returned channel is closed immediately.
Types ¶
type Heap ¶
type Heap[T any] struct { // contains filtered or unexported fields }
A Heap is a heap of Ts.
func NewOrderedHeap ¶
NewOrderedHeap returns a new heap that operates on cmp.Ordered elements.
func NewReverseOrderedHeap ¶
NewReverseOrderedHeap returns a new heap that operates on cmp.Ordered elements in reverse order.
func (*Heap[T]) MustPop ¶
func (h *Heap[T]) MustPop() T
MustPop returns the lowest value in h. It panics if h is empty.
func (*Heap[T]) Peek ¶
Peek returns the lowest value in h in O(1) time and memory, without removing it, and whether it exists.
func (*Heap[T]) Pop ¶
Pop returns the lowest value in h, removing it, and whether it exists in O(N) time and O(1) memory.