minheap

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package minheap provides a binary heap whose elements live off the Go heap.

Example
package main

import (
	"fmt"

	memory "github.com/stubbedev/memory-go"
	"github.com/stubbedev/memory-go/minheap"
)

func main() {
	var a memory.Local
	defer a.Close()

	type job struct {
		Priority int
		ID       uint32
	}

	h, err := minheap.New(&a, func(x, y job) bool { return x.Priority < y.Priority })
	if err != nil {
		panic(err)
	}
	defer h.Free()
	for i, p := range []int{5, 1, 3} {
		if err := h.Push(job{Priority: p, ID: uint32(i)}); err != nil {
			panic(err)
		}
	}
	for h.Len() > 0 {
		j, _ := h.Pop()
		fmt.Print(j.ID, " ")
	}
	fmt.Println()
}
Output:
1 2 0

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

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

Heap pops the element for which less reports true against every other first; pass a greater-than func for a max-heap. It is not safe for concurrent use.

func New

func New[T any](a memory.Allocator, less func(a, b T) bool) (*Heap[T], error)

New returns an empty Heap ordered by less.

func (*Heap[T]) All

func (h *Heap[T]) All() iter.Seq2[int, T]

All yields every element in heap order, which is not sorted order.

func (*Heap[T]) Clip

func (h *Heap[T]) Clip() error

Clip shrinks the capacity of h to its length.

func (*Heap[T]) DeleteFunc

func (h *Heap[T]) DeleteFunc(del func(T) bool) int

DeleteFunc removes every element for which del returns true and returns how many it removed.

func (*Heap[T]) Free

func (h *Heap[T]) Free() error

Free releases the elements and leaves h empty.

func (*Heap[T]) Len

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

Len returns the number of elements.

func (*Heap[T]) Peek

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

Peek returns the least element without removing it, or reports false if h is empty.

func (*Heap[T]) Pop

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

Pop removes and returns the least element, or reports false if h is empty.

func (*Heap[T]) Push

func (h *Heap[T]) Push(value T) error

Push adds value.

Jump to

Keyboard shortcuts

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