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

README.md

1.82 Kb · 41 lines

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.