heap

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

type Heap[T any] struct {
	Data []T
	Less func(i, j int) bool
}

Heap keeps data sorted with O(log n) for Push and Pop.

func NewMax

func NewMax[T Ordered]() *Heap[T]

NewMax creates a max heap. Pop will return the highest value.

func NewMin

func NewMin[T Ordered]() *Heap[T]

NewMin creates a min heap. Pop will return the lowest value.

func (*Heap[T]) Pop

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

Pop a value from the heap. This will return the "Least" value according to the Less func.

func (*Heap[T]) Push

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

Push a value onto the heap.

func (*Heap[T]) SetLess

func (h *Heap[T]) SetLess(less func(i, j T) bool)

SetLess using a function to compare the types directly instead of comparing based in indice.

func (*Heap[T]) Sort

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

Sort a heap using Less. Useful when initilizing a heap.

type Ordered

type Ordered interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 |
		~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string
}

Ordered data types can be used with NewMin and NewMax without the need to define the Less func on the Heap.

Jump to

Keyboard shortcuts

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