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 orderedmap is a map that remembers the order its keys were inserted, as a pure, reusable package.

Readme View source

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

Map that remembers insertion orderNew, 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:

  • Updating keeps position. Insertion order means first insertion, not last write.
  • Re-inserting after a delete goes last — it is a new insertion.
  • 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.

Overview

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).

Constants 1

const MaxKeys

1const MaxKeys = 4096
source

MaxKeys bounds the map so gas stays predictable.

Functions 1

func New

1func New() *OrderedMap
source

New returns an empty OrderedMap.

Types 1

type OrderedMap

struct
1type OrderedMap struct {
2	m    map[string]string
3	keys []string
4}
source

OrderedMap is a string-keyed map with deterministic iteration.

Methods on OrderedMap

func At

method on OrderedMap
1func (o *OrderedMap) At(i int) (k, v string, ok bool)
source

At returns the i-th entry in insertion order.

func Clone

method on OrderedMap
1func (o *OrderedMap) Clone() *OrderedMap
source

Clone returns an independent copy preserving order.

func Delete

method on OrderedMap
1func (o *OrderedMap) Delete(k string) bool
source

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.

func Get

method on OrderedMap
1func (o *OrderedMap) Get(k string) (string, bool)
source

Get returns the value for k.

func Has

method on OrderedMap
1func (o *OrderedMap) Has(k string) bool
source

Has reports whether k is present.

func Iterate

method on OrderedMap
1func (o *OrderedMap) Iterate(fn func(k, v string) bool)
source

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.

func Keys

method on OrderedMap
1func (o *OrderedMap) Keys() []string
source

Keys returns the keys in insertion order, as an independent copy.

func Len

method on OrderedMap
1func (o *OrderedMap) Len() int
source

Len returns the number of entries.

func Set

method on OrderedMap
1func (o *OrderedMap) Set(k, v string) bool
source

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.

func Values

method on OrderedMap
1func (o *OrderedMap) Values() []string
source

Values returns the values in key-insertion order.

Source Files 3