const MaxItems
MaxItems bounds the heap so gas stays predictable.
Package heap is a binary min-heap / priority queue over string items with integer priorities, as a pure, reusable pac...
gno.land/p/moul/x/daily/heap/v0Binary heap / priority queue — New, 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.
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).
Heap is a binary min-heap: the lowest priority pops first.
Clone returns an independent copy.
Drain pops everything, returning values in pop order. The heap ends empty.
IsEmpty reports whether the heap holds nothing.
IsMax reports whether this is a max-heap.
Len returns the number of items.
Peek returns the item that would pop next, without removing it.
Pop removes and returns the next item.
Push adds value with the given priority. Returns false when the heap is full.