heap

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 4 Imported by: 0

README

go-heap

PkgGoDev

Package heap implements a generic heap data structure.

License

MIT

Documentation

Overview

Package heaps implements a generic heap data structure.

Index

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

func PriorityChannel[T any](ctx context.Context, inCh <-chan T, lessFunc func(T, T) bool) <-chan T

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 NewHeap

func NewHeap[T any](lessFunc func(T, T) bool) *Heap[T]

NewHeap returns a new heap that uses lessFunc to compare elements.

func NewOrderedHeap

func NewOrderedHeap[T cmp.Ordered]() *Heap[T]

NewOrderedHeap returns a new heap that operates on cmp.Ordered elements.

func NewReverseOrderedHeap

func NewReverseOrderedHeap[T cmp.Ordered]() *Heap[T]

NewReverseOrderedHeap returns a new heap that operates on cmp.Ordered elements in reverse order.

func (*Heap[T]) Cap

func (h *Heap[T]) Cap() int

Cap returns the underlying capacity of h.

func (*Heap[T]) Clip

func (h *Heap[T]) Clip() *Heap[T]

Clip removes unused capacity from h.

func (*Heap[T]) Empty

func (h *Heap[T]) Empty() bool

Empty returns whether h is empty in O(1) time and memory.

func (*Heap[T]) Grow

func (h *Heap[T]) Grow(n int) *Heap[T]

Grow increases h's capacity by at least n.

func (*Heap[T]) Len

func (h *Heap[T]) Len() int

Len returns the size of h in O(1) time and memory.

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

func (h *Heap[T]) Peek() (T, bool)

Peek returns the lowest value in h in O(1) time and memory, without removing it, and whether it exists.

func (*Heap[T]) Pop

func (h *Heap[T]) Pop() (T, bool)

Pop returns the lowest value in h, removing it, and whether it exists in O(N) time and O(1) memory.

func (*Heap[T]) PopAll

func (h *Heap[T]) PopAll() iter.Seq[T]

PopAll returns an iterator that pops all values.

func (*Heap[T]) Push

func (h *Heap[T]) Push(value T) *Heap[T]

Push adds value to h in amortized O(N) time.

func (*Heap[T]) PushMany

func (h *Heap[T]) PushMany(values ...T) *Heap[T]

PushMany pushes multiple values onto the heap.

func (*Heap[T]) PushPop

func (h *Heap[T]) PushPop(value T) T

PushPop pushes value onto the heap and then pops the lowest value off the heap and returns it in O(N) time. It is slightly more efficient than separate calls to Heap.Push and Heap.Pop.

func (*Heap[T]) Set

func (h *Heap[T]) Set(values []T) *Heap[T]

Set sets the values on h to be values in amortized O(N) time. h takes ownership of values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL