Playground
flatmapdemo.gno
// Package flatmapdemo is a small gnoweb demo of the sorted-vector map provided
// by the [p/moul/x/daily/flatmap](/p/nym-moultest123/x/daily/flatmap/v0) library: sorted
// storage, indexed access and range queries.
//
// It contains no map logic of its own. Stateless, so Render is deterministic —
// which is precisely what the library is for.
package flatmapdemo
import (
"strconv"
"strings"
"gno.land/p/nym-moultest123/x/daily/flatmap/v0"
)
// Render renders the demo for gnoweb.
func Render(path string) string {
var b strings.Builder
b.WriteString("# Flat Map\n\n")
b.WriteString("A map backed by sorted slices, demoing the ")
b.WriteString("[`p/moul/x/daily/flatmap`](/p/nym-moultest123/x/daily/flatmap/v0) library.\n\n")
f := flatmap.New()
inserted := []string{"delta", "alpha", "echo", "bravo", "charlie"}
for i, k := range inserted {
f.Set(k, strconv.Itoa(i+1))
}
b.WriteString("## Sorted by construction\n\n")
b.WriteString("Inserted as `" + strings.Join(inserted, ", ") + "` — stored sorted:\n\n")
b.WriteString(table(f))
b.WriteString("\nNo sort on read, and no dependence on map iteration order — which ")
b.WriteString("gno leaves unspecified, and which would let two nodes render different ")
b.WriteString("pages from the same state.\n\n")
b.WriteString("## Indexed access\n\n")
b.WriteString("Sorted storage gives positional lookup that a hash map cannot:\n\n")
for _, i := range []int{0, 2, 4} {
k, v, _ := f.At(i)
b.WriteString("- `At(" + strconv.Itoa(i) + ")` → `" + k + "` = `" + v + "`\n")
}
b.WriteString("\n## Range queries\n\n")
b.WriteString("Two binary searches and a walk — `lo` inclusive, `hi` exclusive:\n\n")
b.WriteString("| range | keys |\n|---|---|\n")
b.WriteString("| `[\"bravo\", \"delta\")` | " + rng(f, "bravo", "delta") + " |\n")
b.WriteString("| `[\"charlie\", ∞)` | " + rng(f, "charlie", "") + " |\n")
b.WriteString("| `[\"b\", \"d\")` | " + rng(f, "b", "d") + " |\n")
b.WriteString("\nThe last one asks for bounds that are not keys at all — the search ")
b.WriteString("still lands in the right place.\n\n")
b.WriteString("## The trade\n\n")
b.WriteString("| operation | cost |\n|---|---|\n")
b.WriteString("| `Get` | O(log n) binary search over contiguous memory |\n")
b.WriteString("| iteration | O(n), already ordered, nothing to sort |\n")
b.WriteString("| `Set` in the middle | **O(n)** — the tail shifts |\n")
b.WriteString("| `Set` at the end | O(1) amortised — the fast path |\n")
b.WriteString("\nCheap reads and cheap ordered iteration, paid for at write time.\n")
return b.String()
}
func table(f *flatmap.FlatMap) string {
var b strings.Builder
b.WriteString("| # | key | value |\n|---|---|---|\n")
i := 0
f.Iterate(func(k, v string) bool {
b.WriteString("| " + strconv.Itoa(i) + " | `" + k + "` | `" + v + "` |\n")
i++
return false
})
return b.String()
}
func rng(f *flatmap.FlatMap, lo, hi string) string {
var out []string
f.Range(lo, hi, func(k, v string) bool {
out = append(out, "`"+k+"`")
return false
})
if len(out) == 0 {
return "_none_"
}
return strings.Join(out, ", ")
}
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/flatmapdemo/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186411 max_deposit = "5000000ugnot"
render_example_test.gno
package flatmapdemo
// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # Flat Map
//
// A map backed by sorted slices, demoing the [`p/moul/x/daily/flatmap`](/p/nym-moultest123/x/daily/flatmap/v0) library.
//
// ## Sorted by construction
//
// Inserted as `delta, alpha, echo, bravo, charlie` — stored sorted:
//
// | # | key | value |
// |---|---|---|
// | 0 | `alpha` | `2` |
// | 1 | `bravo` | `4` |
// | 2 | `charlie` | `5` |
// | 3 | `delta` | `1` |
// | 4 | `echo` | `3` |
//
// No sort on read, and no dependence on map iteration order — which gno leaves unspecified, and which would let two nodes render different pages from the same state.
//
// ## Indexed access
//
// Sorted storage gives positional lookup that a hash map cannot:
//
// - `At(0)` → `alpha` = `2`
// - `At(2)` → `charlie` = `5`
// - `At(4)` → `echo` = `3`
//
// ## Range queries
//
// Two binary searches and a walk — `lo` inclusive, `hi` exclusive:
//
// | range | keys |
// |---|---|
// | `["bravo", "delta")` | `bravo`, `charlie` |
// | `["charlie", ∞)` | `charlie`, `delta`, `echo` |
// | `["b", "d")` | `bravo`, `charlie` |
//
// The last one asks for bounds that are not keys at all — the search still lands in the right place.
//
// ## The trade
//
// | operation | cost |
// |---|---|
// | `Get` | O(log n) binary search over contiguous memory |
// | iteration | O(n), already ordered, nothing to sort |
// | `Set` in the middle | **O(n)** — the tail shifts |
// | `Set` at the end | O(1) amortised — the fast path |
//
// Cheap reads and cheap ordered iteration, paid for at write time.
}