heap

package module
v0.0.0-...-2ba483f Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2024 License: BSD-3-Clause Imports: 1 Imported by: 0

README

Generic heap for Go

Use the Go standard library's container/heap to implement a fully generic and type safe slice-based min-heap.

Documentation

Overview

Package heap implements a generic heap using the standard library's container/heap.

Index

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

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

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

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

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]) Less

func (h *Heap[T]) Less(i int, j int) bool

Less implements container/heap.Interface.Less and sort.Interface.Less.

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

func (h *Heap[T]) PeekElement() (T, bool)

PeekElement returns the min element in the heap.

func (*Heap[T]) Pop

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

Pop implements container/heap.Interface.Pop.

Prefer PopElement over Pop.

func (*Heap[T]) PopElement

func (h *Heap[T]) PopElement() (T, bool)

PopElement removes and returns the min element in the heap.

func (*Heap[T]) Push

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

Push implements container/heap.Interface.Push.

Prefer PushElement over Push.

func (*Heap[T]) PushElement

func (h *Heap[T]) PushElement(e T)

PushElement adds an element to the heap.

func (*Heap[T]) RemoveElement

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

RemoveElement removes and returns the element at index i from the heap.

func (*Heap[T]) Swap

func (h *Heap[T]) Swap(i int, j int)

Swap implements container/heap.Interface.Swap.

Jump to

Keyboard shortcuts

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