const MaxDistinct
MaxDistinct bounds the number of DISTINCT elements so gas stays predictable. Counts themselves are unbounded.
Package multiset is a bag / frequency counter — a set that allows duplicates and remembers how many — as a pure, reus...
gno.land/p/moul/x/daily/multiset/v0Bag / frequency counter — New, FromSlice, Add, AddN, Remove,
RemoveN, RemoveAll, Count, MostCommon, Union, Intersect, Sum,
Elements, Expand, Clone, MaxDistinct.
1import "gno.land/p/moul/x/daily/multiset/v0"
2
3m := multiset.FromSlice([]string{"a", "a", "a", "b", "b", "c"})
4m.Count("a") // 3
5m.MostCommon(2) // [{a 3} {b 2}]
6m.Total() // 6 occurrences
7m.Distinct() // 3 elements
The STL multiset and Python's collections.Counter in one type.
MostCommon has a total order: count descending, then element ascending.
Sorting by count alone would leave ties in whatever order the backing map
yielded — unspecified in gno, and enough to make two nodes render different
tables from identical state. The tiebreak is not decoration.
Semantics worth knowing, each with a test:
Distinct would still count.RemoveN clamps: removing more than are present clears the element
instead of going negative.Union takes the max of each count, Intersect the min (common
elements only), Sum adds them.MaxDistinct (4096) bounds the number of distinct elements; counts themselves
are unbounded, so a full set still accepts more occurrences of what it holds.
Live demo: r/moul/x/daily/multisetdemo
· render it at /r/moul/x/daily/multisetdemo/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 multiset is a bag / frequency counter — a set that allows duplicates and remembers how many — as a pure, reusable package.
It is the STL multiset and Python's collections.Counter in one type: Add an element several times and the count rises; the distinct elements stay sorted so iteration and rendering are deterministic.
The interesting operation is MostCommon(n), and the interesting problem with it is ties. Sorting by count alone leaves elements with equal counts in whatever order the underlying storage happened to yield — which, if that is a built-in map, is unspecified in gno and can differ between nodes. Here the order is total: count descending, then element ascending. Two multisets built from the same elements always produce the same ranking.
A live demo of this package is at r/moul/x/daily/multisetdemo(/r/moul/x/daily/multisetdemo/v0).
MaxDistinct bounds the number of DISTINCT elements so gas stays predictable. Counts themselves are unbounded.
Entry pairs an element with its count.
MultiSet counts occurrences of string elements.
Add records one occurrence of e. Returns false when e is new and the set already holds MaxDistinct distinct elements.
AddN records n occurrences of e. A non-positive n is a no-op returning true.
Clone returns an independent copy.
Count returns how many times e occurs; zero when absent.
Distinct returns the number of distinct elements.
Elements returns the distinct elements, sorted.
Expand returns every occurrence, sorted — a multiset flattened back to a slice. Length equals Total.
Has reports whether e occurs at least once.
Intersect returns a set where each element's count is the MINIMUM of the two, keeping only elements present in both.
IsEmpty reports whether the set holds nothing.
MostCommon returns the n most frequent entries, ranked by count descending then element ascending. n <= 0, or larger than the number of distinct elements, returns them all.
Remove drops one occurrence of e, deleting it entirely when the count hits zero. Returns false when e was not present.
RemoveAll drops every occurrence of e. Returns false when e was absent.
RemoveN drops up to n occurrences of e. Returns false when e was absent. Removing more than are present clears the element rather than going negative.
Sum returns a set where each element's count is the SUM of the two.
Total returns the sum of every count.
Union returns a set where each element's count is the MAXIMUM of the two — the standard multiset union.