PathrockNetwork Gno Explorer
HomeBlocksTransactionsTokensRealmsPackagesValidatorsAnalytics

PathrockNetwork Gno Explorer — an independent explorer for Gno.land Mainnet (gnoland-1), operated by PathrockNetwork. Not an official Gno.land service.

gnowebarchive RPC

gno.land/r/moul/x/daily/flatmapdemo/v0

Realm
Open in gnoweb ↗

Overview

Kind
Realm (renderable)
Name
v0
Namespace
moul / x / daily / flatmapdemo
Files
3 (README)(gnomod.toml)
Exported functions
1
Module
gno.land/r/moul/x/daily/flatmapdemo/v0
gno
0.9

Files (3)

  • README.mdmarkdown
  • gnomod.tomltoml
  • flatmapdemo.gnogno
flatmapdemo.gnogno
1// Package flatmapdemo is a small gnoweb demo of the sorted-vector map provided2// by the [p/moul/x/daily/flatmap](/p/moul/x/daily/flatmap/v0) library: sorted3// storage, indexed access and range queries.4//5// It contains no map logic of its own. Stateless, so Render is deterministic —6// which is precisely what the library is for.7package flatmapdemo89import (10	"strconv"11	"strings"1213	"gno.land/p/moul/x/daily/flatmap/v0"14)1516// Render renders the demo for gnoweb.17func Render(path string) string {18	var b strings.Builder19	b.WriteString("# Flat Map\n\n")20	b.WriteString("A map backed by sorted slices, demoing the ")21	b.WriteString("[`p/moul/x/daily/flatmap`](/p/moul/x/daily/flatmap/v0) library.\n\n")2223	f := flatmap.New()24	inserted := []string{"delta", "alpha", "echo", "bravo", "charlie"}25	for i, k := range inserted {26		f.Set(k, strconv.Itoa(i+1))27	}2829	b.WriteString("## Sorted by construction\n\n")30	b.WriteString("Inserted as `" + strings.Join(inserted, ", ") + "` — stored sorted:\n\n")31	b.WriteString(table(f))32	b.WriteString("\nNo sort on read, and no dependence on map iteration order — which ")33	b.WriteString("gno leaves unspecified, and which would let two nodes render different ")34	b.WriteString("pages from the same state.\n\n")3536	b.WriteString("## Indexed access\n\n")37	b.WriteString("Sorted storage gives positional lookup that a hash map cannot:\n\n")38	for _, i := range []int{0, 2, 4} {39		k, v, _ := f.At(i)40		b.WriteString("- `At(" + strconv.Itoa(i) + ")` → `" + k + "` = `" + v + "`\n")41	}4243	b.WriteString("\n## Range queries\n\n")44	b.WriteString("Two binary searches and a walk — `lo` inclusive, `hi` exclusive:\n\n")45	b.WriteString("| range | keys |\n|---|---|\n")46	b.WriteString("| `[\"bravo\", \"delta\")` | " + rng(f, "bravo", "delta") + " |\n")47	b.WriteString("| `[\"charlie\", ∞)` | " + rng(f, "charlie", "") + " |\n")48	b.WriteString("| `[\"b\", \"d\")` | " + rng(f, "b", "d") + " |\n")49	b.WriteString("\nThe last one asks for bounds that are not keys at all — the search ")50	b.WriteString("still lands in the right place.\n\n")5152	b.WriteString("## The trade\n\n")53	b.WriteString("| operation | cost |\n|---|---|\n")54	b.WriteString("| `Get` | O(log n) binary search over contiguous memory |\n")55	b.WriteString("| iteration | O(n), already ordered, nothing to sort |\n")56	b.WriteString("| `Set` in the middle | **O(n)** — the tail shifts |\n")57	b.WriteString("| `Set` at the end | O(1) amortised — the fast path |\n")58	b.WriteString("\nCheap reads and cheap ordered iteration, paid for at write time.\n")59	return b.String()60}6162func table(f *flatmap.FlatMap) string {63	var b strings.Builder64	b.WriteString("| # | key | value |\n|---|---|---|\n")65	i := 066	f.Iterate(func(k, v string) bool {67		b.WriteString("| " + strconv.Itoa(i) + " | `" + k + "` | `" + v + "` |\n")68		i++69		return false70	})71	return b.String()72}7374func rng(f *flatmap.FlatMap, lo, hi string) string {75	var out []string76	f.Range(lo, hi, func(k, v string) bool {77		out = append(out, "`"+k+"`")78		return false79	})80	if len(out) == 0 {81		return "_none_"82	}83	return strings.Join(out, ", ")84}85

Functions

  • Render(path string) string

Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.

Rendered

RenderedRawgnoweb ↗

vm/qrender output, sanitized (docs/render-security.md) and displayed in an empty-sandbox iframe — scripts, forms and popups cannot run. Links stay inert in-preview; right-click to open.