Documentation
¶
Overview ¶
Package heap implements a generic heap using the standard library's container/heap.
Index ¶
- type Comparable
- type Heap
- func (h *Heap[T]) Fix(i int)
- func (h *Heap[T]) Init()
- func (h *Heap[T]) Len() int
- func (h *Heap[T]) Less(i int, j int) bool
- func (h *Heap[T]) MustPeekElement() T
- func (h *Heap[T]) MustPopElement() T
- func (h *Heap[T]) PeekElement() (T, bool)
- func (h *Heap[T]) Pop() any
- func (h *Heap[T]) PopElement() (T, bool)
- func (h *Heap[T]) Push(v any)
- func (h *Heap[T]) PushElement(e T)
- func (h *Heap[T]) RemoveElement(i int) T
- func (h *Heap[T]) Swap(i int, j int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Comparable ¶
type Comparable[T any] interface { // Less reports whether the receiver value must sort before the argument value. Less(v T) bool }
A Comparable type can be compared with a method to other values of the same type.
type Heap ¶
type Heap[T Comparable[T]] []T
A Heap is a min-heap implemented as a slice with generic methods.
func (*Heap[T]) Fix ¶
Fix re-establishes the heap ordering after the element at index i has changed its value.
func (*Heap[T]) Init ¶
func (h *Heap[T]) Init()
Init establishes the heap invariants required by the other routines in this package.
func (*Heap[T]) Len ¶
Len implements container/heap.Interface.Len and sort.Interface.Len.
Since the Heap is defined to be a slice, using the built-in len is equivalent.
func (*Heap[T]) MustPeekElement ¶
func (h *Heap[T]) MustPeekElement() T
MustPeekElement returns the min element in the heap. It panics if no elements are in the heap.
func (*Heap[T]) MustPopElement ¶
func (h *Heap[T]) MustPopElement() T
MustPopElement removes and returns the min element in the heap. It panics if no elements are in the heap.
func (*Heap[T]) PeekElement ¶
PeekElement returns the min element in the heap.
func (*Heap[T]) PopElement ¶
PopElement removes and returns the min element in the heap.
func (*Heap[T]) PushElement ¶
func (h *Heap[T]) PushElement(e T)
PushElement adds an element to the heap.
func (*Heap[T]) RemoveElement ¶
RemoveElement removes and returns the element at index i from the heap.