heaps

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2022 License: MIT Imports: 2 Imported by: 1

Documentation

Overview

Package heaps provides generic heaps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comparator

type Comparator[T any] interface {
	Less(a, b T) bool
}

Comparator provides the comparison function for a heap. Less should determine whether a should be popped before b.

type Heap

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

Heap is a generic heap.

func New

func New[C Comparator[T], T any]() *Heap[C, T]

New returns a new heap that uses the given comparator.

func NewMax

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

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

func NewMin

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

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

func (*Heap[C, T]) Head

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

Head returns the minimal element in h.

func (*Heap[C, T]) Len

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

Len returns the number of elements in h.

func (*Heap[C, T]) Pop

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

Pop removes and returns the minimal element in h.

func (*Heap[C, T]) Push

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

Push adds x to h while maintaining its heap invariants.

func (*Heap[C, T]) View

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

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

type Max

type Max[T constraints.Ordered] struct{}

Max is a comparator that places the maximal value at the top of the heap.

func (Max[T]) Less

func (_ Max[T]) Less(a, b T) bool

type Min

type Min[T constraints.Ordered] struct{}

Min is a comparator that places the minimal value at the top of the heap.

func (Min[T]) Less

func (_ Min[T]) Less(a, b T) bool

Jump to

Keyboard shortcuts

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