Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v0 source pure

Package heap is a binary min-heap / priority queue over string items with integer priorities, as a pure, reusable pac...

Readme View source

gno.land/p/moul/x/daily/heap/v0

Binary heap / priority queueNew, NewMax, Push, Pop, Peek, Drain, Clone, Len, IsEmpty, IsMax, MaxItems.

1import "gno.land/p/moul/x/daily/heap/v0"
2
3h := heap.New()          // min-heap; NewMax() for max
4h.Push("pay invoice", 1)
5h.Push("clear cache", 9)
6h.Peek()                 // "pay invoice", 1, true — does not remove
7h.Drain()                // ["pay invoice" "clear cache"]

Go's container/heap makes you implement five methods and hands back an interface. This is the concrete structure instead: an implicit binary heap in a slice, Push/Pop in O(log n), Peek in O(1).

The ordering is total. Equal priorities pop oldest-first, and that tiebreak does not invert in a max-heap — only the priority comparison does. Without it, ties would fall back on whatever order the backing slice happened to hold, and two nodes could pop the same queue differently: a consensus bug, not a cosmetic one.

MaxItems (4096) bounds growth; a full heap refuses new items rather than growing without limit.

Live demo: r/moul/x/daily/heapdemo · render it at /r/moul/x/daily/heapdemo/v0.


Part of moul/gno-contracts — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage.

🧪 Highly experimental — potentially vibe-coded. Not audited; may break, change, or be removed at any time. Do not use with anything of value. Full disclaimer: DISCLAIMER.

Overview

Package heap is a binary min-heap / priority queue over string items with integer priorities, as a pure, reusable package.

Go's container/heap makes the caller implement five methods and hands back an interface; that indirection buys generality this chain does not need and costs gas it does. This is the concrete data structure instead: an implicit binary heap in a slice, Push/Pop in O(log n), Peek in O(1).

Ordering is TOTAL and deterministic. Equal priorities are broken by insertion sequence, so two heaps fed the same items in the same order always pop the same sequence — a heap that reordered ties by allocation address would make a Render vary between nodes, which is a consensus bug rather than a cosmetic one.

A live demo of this package is at r/moul/x/daily/heapdemo(/r/moul/x/daily/heapdemo/v0).

Constants 1

const MaxItems

1const MaxItems = 4096
source

MaxItems bounds the heap so gas stays predictable.

Functions 2

func New

1func New() *Heap
source

New returns an empty min-heap (lowest priority pops first).

func NewMax

1func NewMax() *Heap
source

NewMax returns an empty max-heap (highest priority pops first). Ties are still broken by insertion order, oldest first.

Types 1

type Heap

struct
1type Heap struct {
2	items []item
3	next  int // monotonic insertion counter
4	max   bool
5}
source

Heap is a binary min-heap: the lowest priority pops first.

Methods on Heap

func Clone

method on Heap
1func (h *Heap) Clone() *Heap
source

Clone returns an independent copy.

func Drain

method on Heap
1func (h *Heap) Drain() []string
source

Drain pops everything, returning values in pop order. The heap ends empty.

func IsEmpty

method on Heap
1func (h *Heap) IsEmpty() bool
source

IsEmpty reports whether the heap holds nothing.

func IsMax

method on Heap
1func (h *Heap) IsMax() bool
source

IsMax reports whether this is a max-heap.

func Len

method on Heap
1func (h *Heap) Len() int
source

Len returns the number of items.

func Peek

method on Heap
1func (h *Heap) Peek() (value string, priority int, ok bool)
source

Peek returns the item that would pop next, without removing it.

func Pop

method on Heap
1func (h *Heap) Pop() (value string, priority int, ok bool)
source

Pop removes and returns the next item.

func Push

method on Heap
1func (h *Heap) Push(value string, priority int) bool
source

Push adds value with the given priority. Returns false when the heap is full.

Source Files 3