const MaxKeys
MaxKeys bounds the map so gas stays predictable.
Package orderedmap is a map that remembers the order its keys were inserted, as a pure, reusable package.
gno.land/p/moul/x/daily/orderedmap/v0Map that remembers insertion order — New, Set, Get, Has, Delete,
Keys, Values, Iterate, At, Clone, Len, MaxKeys.
1import "gno.land/p/moul/x/daily/orderedmap/v0"
2
3o := orderedmap.New()
4o.Set("delta", "4"); o.Set("alpha", "1")
5o.Keys() // ["delta" "alpha"] — insertion order, not sorted
6o.Set("delta", "99") // updates in place, keeps position
This matters more on chain than off it. gno map iteration order is
unspecified, so a realm that ranges over a built-in map to build its Render
can emit a different page on every call — a consensus bug, not a cosmetic
one. This type gives back a deterministic order without requiring the keys to be
sortable.
Semantics worth knowing, each with a test:
Keys returns a copy, so a caller cannot reorder the map through it.Backed by a built-in map for O(1) Get plus a slice holding the order. Delete
is O(n): closing the gap in that slice is what preserves order, and that
trade is stated rather than hidden. MaxKeys (4096) bounds growth; a full map
refuses new keys but still accepts updates to existing ones.
Live demo: r/moul/x/daily/orderedmapdemo
· render it at /r/moul/x/daily/orderedmapdemo/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 orderedmap is a map that remembers the order its keys were inserted, as a pure, reusable package.
This matters more on chain than off it. gno map iteration order is unspecified, so a realm that ranges over a built-in map to build its Render can emit a different page on every call — which is a consensus bug, not a cosmetic one. This type gives back a deterministic order without needing the keys to be sortable.
Backed by a built-in map for O(1) lookup plus a slice holding insertion order. Delete is O(n) in the number of keys, because it has to close the gap in that slice — an honest trade for O(1) Get and an allocation-free walk. Re-Setting an existing key updates the value and KEEPS its original position: insertion order means first insertion, not last write.
A live demo of this package is at r/moul/x/daily/orderedmapdemo(/r/moul/x/daily/orderedmapdemo/v0).
OrderedMap is a string-keyed map with deterministic iteration.
At returns the i-th entry in insertion order.
Clone returns an independent copy preserving order.
Delete removes k and reports whether it was present. O(n): the key's slot in the order slice has to be closed up, and the remaining keys shifted, so that order is preserved.
Get returns the value for k.
Has reports whether k is present.
Iterate calls fn for each entry in insertion order, stopping early if fn returns true. The map must not be mutated from inside fn — the walk is over a live slice.
Keys returns the keys in insertion order, as an independent copy.
Len returns the number of entries.
Set inserts or updates k. Updating an existing key keeps its original position — insertion order means FIRST insertion. Returns false when the map is full and k is new.
Values returns the values in key-insertion order.