sliceheap

package
v0.0.0-...-0ff3251 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package sliceheap implements a generic heap based on a slice. - https://github.com/golang/go/issues/47632 - https://gotipplay.golang.org/p/d4M0QBkfmIr

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

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

A Heap is a min-heap backed by a slice.

func New

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

New constructs a new Heap with a comparison function.

func (*Heap[E]) Fix

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

Fix re-establishes the heap ordering after the element at index i has changed its value. Changing the value of the element at index i and then calling Fix is equivalent to, but less expensive than, calling h.Remove(i) followed by a Push of the new value. The complexity is O(log n) where n = h.Len(). The index for use with Fix is recorded using the function passed to SetIndex.

func (*Heap[E]) Len

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

Len returns the number of elements in the heap.

func (*Heap[E]) Peek

func (h *Heap[E]) Peek() E

Peek returns the minimum element (according to the less function) in the heap. Peek panics if the heap is empty. The complexity is O(1).

func (*Heap[E]) Pop

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

Pop removes and returns the minimum element (according to the less function) from the heap. Pop panics if the heap is empty. The complexity is O(log n) where n = h.Len().

func (*Heap[E]) Push

func (h *Heap[E]) Push(elem E)

Push pushes an element onto the heap. The complexity is O(log n) where n = h.Len().

func (*Heap[E]) Remove

func (h *Heap[E]) Remove(i int) E

Remove removes and returns the element at index i from the heap. The complexity is O(log n) where n = h.Len(). The index for use with Remove is recorded using the function passed to SetIndex.

func (*Heap[E]) Slice

func (h *Heap[E]) Slice() []E

Slice returns the underlying slice. The slice is in heap order; the minimum value is at index 0. The heap retains the returned slice, so altering the slice may break the invariants and invalidate the heap.

Jump to

Keyboard shortcuts

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