Transaction
5EC92E1DDE8578…0491BBEE3CA8
Block 25,943 · index 0 · indexed
Summary
- Hash
- 5EC92E1DDE8578BA0A2493796DC042CA96CA29C0F6E91394EEEF0491BBEE3CA8
- Block
- 25,943
- Size
- 8607 bytes
- Gas used
- 13,439,338 / 16,127,265
- Fee
- 1000000ugnot
- Memo
- gnopublish
- Status
- success
Messages
Arguments · 9
- #1multiset
- #2README.md
- #3# `gno.land/p/moul/x/daily/multiset/v0` **Bag / frequency counter** — `New`, `FromSlice`, `Add`, `AddN`, `Remove`, `RemoveN`, `RemoveAll`, `Count`, `MostCommon`, `Union`, `Intersect`, `Sum`, `Elements`, `Expand`, `Clone`, `MaxDistinct`. ```go import "gno.land/p/moul/x/daily/multiset/v0" m := multiset.FromSlice([]string{"a", "a", "a", "b", "b", "c"}) m.Count("a") // 3 m.MostCommon(2) // [{a 3} {b 2}] m.Total() // 6 occurrences m.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: - **A count reaching zero removes the element**, rather than leaving a zero-count ghost that `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`](https://github.com/moul/gno-contracts/tree/main/r/moul/x/daily/multisetdemo/v0) · render it at [`/r/moul/x/daily/multisetdemo/v0`](https://gno.land/r/moul/x/daily/multisetdemo/v0). <!-- BEGIN GNOCONTRACTS FOOTER (generated by `make readmes`; do not edit below) --> --- Part of **[moul/gno-contracts](https://github.com/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](https://github.com/moul/gno-contracts/blob/main/DISCLAIMER.md). <!-- END GNOCONTRACTS FOOTER -->
- #4gnomod.toml
- #5module = "gno.land/p/moul/x/daily/multiset/v0" gno = "0.9"
- #6multiset.gno
- #7// 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). package multiset import "sort" // MaxDistinct bounds the number of DISTINCT elements so gas stays predictable. // Counts themselves are unbounded. const MaxDistinct = 4096 // MultiSet counts occurrences of string elements. type MultiSet struct { counts map[string]int total int } // New returns an empty MultiSet. func New() *MultiSet { return &MultiSet{counts: map[string]int{}} } // FromSlice builds a MultiSet from elements, counting duplicates. func FromSlice(elems []string) *MultiSet { m := New() for _, e := range elems { m.Add(e) } return m } // Add records one occurrence of e. Returns false when e is new and the set // already holds MaxDistinct distinct elements. func (m *MultiSet) Add(e string) bool { return m.AddN(e, 1) } // AddN records n occurrences of e. A non-positive n is a no-op returning true. func (m *MultiSet) AddN(e string, n int) bool { if n <= 0 { return true } if _, seen := m.counts[e]; !seen && len(m.counts) >= MaxDistinct { return false } m.counts[e] += n m.total += n return true } // Count returns how many times e occurs; zero when absent. func (m *MultiSet) Count(e string) int { return m.counts[e] } // Has reports whether e occurs at least once. func (m *MultiSet) Has(e string) bool { return m.counts[e] > 0 } // Remove drops one occurrence of e, deleting it entirely when the count hits // zero. Returns false when e was not present. func (m *MultiSet) Remove(e string) bool { return m.RemoveN(e, 1) } // 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. func (m *MultiSet) RemoveN(e string, n int) bool { have, ok := m.counts[e] if !ok || n <= 0 { return false } if n >= have { delete(m.counts, e) m.total -= have return true } m.counts[e] = have - n m.total -= n return true } // RemoveAll drops every occurrence of e. Returns false when e was absent. func (m *MultiSet) RemoveAll(e string) bool { have, ok := m.counts[e] if !ok { return false } delete(m.counts, e) m.total -= have return true } // Distinct returns the number of distinct elements. func (m *MultiSet) Distinct() int { return len(m.counts) } // Total returns the sum of every count. func (m *MultiSet) Total() int { return m.total } // IsEmpty reports whether the set holds nothing. func (m *MultiSet) IsEmpty() bool { return len(m.counts) == 0 } // Elements returns the distinct elements, sorted. func (m *MultiSet) Elements() []string { out := make([]string, 0, len(m.counts)) for e := range m.counts { out = append(out, e) } sort.Strings(out) return out } // Expand returns every occurrence, sorted — a multiset flattened back to a // slice. Length equals Total. func (m *MultiSet) Expand() []string { out := make([]string, 0, m.total) for _, e := range m.Elements() { for i := 0; i < m.counts[e]; i++ { out = append(out, e) } } return out } // Entry pairs an element with its count. type Entry struct { Elem string Count int } // byRank orders entries by count descending, then element ascending — a TOTAL // order, so ranking never depends on map iteration. type byRank []Entry func (r byRank) Len() int { return len(r) } func (r byRank) Swap(i, j int) { r[i], r[j] = r[j], r[i] } func (r byRank) Less(i, j int) bool { if r[i].Count != r[j].Count { return r[i].Count > r[j].Count } return r[i].Elem < r[j].Elem } // 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. func (m *MultiSet) MostCommon(n int) []Entry { all := make([]Entry, 0, len(m.counts)) for e, c := range m.counts { all = append(all, Entry{Elem: e, Count: c}) } sort.Sort(byRank(all)) if n <= 0 || n > len(all) { return all } return all[:n] } // Union returns a set where each element's count is the MAXIMUM of the two — // the standard multiset union. func (m *MultiSet) Union(other *MultiSet) *MultiSet { out := m.Clone() for e, c := range other.counts { if c > out.counts[e] { out.setCount(e, c) } } return out } // Intersect returns a set where each element's count is the MINIMUM of the two, // keeping only elements present in both. func (m *MultiSet) Intersect(other *MultiSet) *MultiSet { out := New() for e, c := range m.counts { if oc, ok := other.counts[e]; ok { if oc < c { c = oc } out.AddN(e, c) } } return out } // Sum returns a set where each element's count is the SUM of the two. func (m *MultiSet) Sum(other *MultiSet) *MultiSet { out := m.Clone() for e, c := range other.counts { out.AddN(e, c) } return out } // Clone returns an independent copy. func (m *MultiSet) Clone() *MultiSet { out := New() for e, c := range m.counts { out.counts[e] = c } out.total = m.total return out } // setCount overwrites an element's count, keeping total in step. func (m *MultiSet) setCount(e string, c int) { m.total += c - m.counts[e] m.counts[e] = c }
- #8/gno.MemPackageType
- #9 MPUserProd
Result log
msg:0,success:true,log:,events:[]