heap

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package heap provides a typed min-heap with addressable items, so the scheduler can re-key or remove an entry in O(log n) without searching.

Index

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 is a min-heap keyed by int64 (the scheduler uses UnixNano). The zero value is usable. It is not safe for concurrent use.

func New

func New[T any]() *Heap[T]

New returns an empty heap.

func (*Heap[T]) Fix

func (h *Heap[T]) Fix(it *Item[T], newKey int64)

Fix changes it's key and restores heap order. Items not stored in h (never pushed, or already popped or removed) are ignored.

func (*Heap[T]) Len

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

Len reports the number of items.

func (*Heap[T]) Peek

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

Peek returns the minimum item without removing it; ok is false when the heap is empty.

func (*Heap[T]) Pop

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

Pop removes and returns the minimum item; ok is false when the heap is empty. The returned item's Index becomes -1.

func (*Heap[T]) Push

func (h *Heap[T]) Push(key int64, v T) *Item[T]

Push inserts v under key and returns its item handle.

func (*Heap[T]) Remove

func (h *Heap[T]) Remove(it *Item[T])

Remove deletes it from h and sets its Index to -1. Items not stored in h are ignored.

type Item

type Item[T any] struct {
	Key   int64
	Value T
	// contains filtered or unexported fields
}

Item is a heap entry returned by Push and accepted by Fix and Remove. Key and Value may be read at any time; change Key only through Fix.

func (*Item[T]) Index

func (it *Item[T]) Index() int

Index reports the item's heap position, or -1 after removal.

Jump to

Keyboard shortcuts

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