heap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2022 License: MIT Imports: 0 Imported by: 0

README

Generic Heap

This library provides a generic implementation of binary heap in Go.

Documentation

Overview

Package heap provides the implementation of a generic binary heap.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompareFunc

type CompareFunc[T any] func(a, b T) int

CompareFunc compares a and b and returns 1, 0 or -1 if a is greater than b, a is equal to b, or a is smaller than b, respectively.

type Heap

type Heap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Heap is a binary heap that stores items in an ascending order.

func New

func New[K comparable, V any](cmp CompareFunc[V]) *Heap[K, V]

New instantiates a new heap.

func (*Heap[K, V]) Clear

func (h *Heap[K, V]) Clear()

Clear removes all items in the heap.

func (*Heap[K, V]) Get

func (h *Heap[K, V]) Get(key K) (value V, ok bool)

Get returns a value given its key.

func (*Heap[K, V]) Has

func (h *Heap[K, V]) Has(key K) bool

Has returns true if a key exists in the heap.

func (*Heap[K, V]) Len

func (h *Heap[K, V]) Len() int

Len returns the number of items in the heap.

func (*Heap[K, V]) Peek

func (h *Heap[K, V]) Peek() (key K, value V, ok bool)

Peek returns the minimum key-value pair.

func (*Heap[K, V]) Pop

func (h *Heap[K, V]) Pop() (key K, value V, ok bool)

Pop removes the minimum key-value pair and returns it.

func (*Heap[K, V]) Push

func (h *Heap[K, V]) Push(key K, value V)

Push adds a new key-value pair to the heap.

func (*Heap[K, V]) Remove

func (h *Heap[K, V]) Remove(key K) (value V, ok bool)

Remove deletes a value given its key.

Jump to

Keyboard shortcuts

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