heaps

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2025 License: MIT Imports: 1 Imported by: 1

Documentation

Overview

Package heaps provides generic heaps.

This package provides better run speeds than the standard [heap] package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

type Heap[T any] struct {
	// contains filtered or unexported fields
}

Heap is a generic heap.

func Max

func Max[T constraints.Ordered]() *Heap[T]

Max returns a new max-heap of an ordered type by its natural order.

func Min

func Min[T constraints.Ordered]() *Heap[T]

Min returns a new min-heap of an ordered type by its natural order.

func New

func New[T any](less func(T, T) bool) *Heap[T]

New returns a new heap that uses the given comparator function.

func (*Heap[T]) Clip added in v0.6.0

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

Clip removes unused capacity from the heap.

func (*Heap[T]) Fix added in v0.5.0

func (h *Heap[T]) Fix(i int)

Fix fixes the heap after a single value had been modified. i is the index of the modified value.

func (*Heap[T]) Head

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

Head returns the minimal element in h.

func (*Heap[T]) Len

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

Len returns the number of elements in h.

func (*Heap[T]) Pop

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

Pop removes and returns the minimal element in h.

func (*Heap[T]) Push

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

Push adds x to h while maintaining its heap invariants.

func (*Heap[T]) PushSlice added in v0.1.13

func (h *Heap[T]) PushSlice(s []T)

PushSlice adds the elements of s to h while maintaining its heap invariants. The complexity is O(new n), so it should be typically used to initialize a new heap.

func (*Heap[T]) View

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

View returns the underlying slice of h, containing all of its elements. Modifying the slice may invalidate the heap.

Jump to

Keyboard shortcuts

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