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 multiset is a bag / frequency counter — a set that allows duplicates and remembers how many — as a pure, reus...

Readme View source

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

Bag / frequency counterNew, 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:

  • 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 · 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.

Overview

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

Constants 1

const MaxDistinct

1const MaxDistinct = 4096
source

MaxDistinct bounds the number of DISTINCT elements so gas stays predictable. Counts themselves are unbounded.

Functions 2

func FromSlice

1func FromSlice(elems []string) *MultiSet
source

FromSlice builds a MultiSet from elements, counting duplicates.

func New

1func New() *MultiSet
source

New returns an empty MultiSet.

Types 2

type Entry

struct
1type Entry struct {
2	Elem  string
3	Count int
4}
source

Entry pairs an element with its count.

type MultiSet

struct
1type MultiSet struct {
2	counts map[string]int
3	total  int
4}
source

MultiSet counts occurrences of string elements.

Methods on MultiSet

func Add

method on MultiSet
1func (m *MultiSet) Add(e string) bool
source

Add records one occurrence of e. Returns false when e is new and the set already holds MaxDistinct distinct elements.

func AddN

method on MultiSet
1func (m *MultiSet) AddN(e string, n int) bool
source

AddN records n occurrences of e. A non-positive n is a no-op returning true.

func Clone

method on MultiSet
1func (m *MultiSet) Clone() *MultiSet
source

Clone returns an independent copy.

func Count

method on MultiSet
1func (m *MultiSet) Count(e string) int
source

Count returns how many times e occurs; zero when absent.

func Distinct

method on MultiSet
1func (m *MultiSet) Distinct() int
source

Distinct returns the number of distinct elements.

func Elements

method on MultiSet
1func (m *MultiSet) Elements() []string
source

Elements returns the distinct elements, sorted.

func Expand

method on MultiSet
1func (m *MultiSet) Expand() []string
source

Expand returns every occurrence, sorted — a multiset flattened back to a slice. Length equals Total.

func Has

method on MultiSet
1func (m *MultiSet) Has(e string) bool
source

Has reports whether e occurs at least once.

func Intersect

method on MultiSet
1func (m *MultiSet) Intersect(other *MultiSet) *MultiSet
source

Intersect returns a set where each element's count is the MINIMUM of the two, keeping only elements present in both.

func IsEmpty

method on MultiSet
1func (m *MultiSet) IsEmpty() bool
source

IsEmpty reports whether the set holds nothing.

func MostCommon

method on MultiSet
1func (m *MultiSet) MostCommon(n int) []Entry
source

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 Remove

method on MultiSet
1func (m *MultiSet) Remove(e string) bool
source

Remove drops one occurrence of e, deleting it entirely when the count hits zero. Returns false when e was not present.

func RemoveAll

method on MultiSet
1func (m *MultiSet) RemoveAll(e string) bool
source

RemoveAll drops every occurrence of e. Returns false when e was absent.

func RemoveN

method on MultiSet
1func (m *MultiSet) RemoveN(e string, n int) bool
source

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 Sum

method on MultiSet
1func (m *MultiSet) Sum(other *MultiSet) *MultiSet
source

Sum returns a set where each element's count is the SUM of the two.

func Total

method on MultiSet
1func (m *MultiSet) Total() int
source

Total returns the sum of every count.

func Union

method on MultiSet
1func (m *MultiSet) Union(other *MultiSet) *MultiSet
source

Union returns a set where each element's count is the MAXIMUM of the two — the standard multiset union.

Imports 1

  • sort stdlib

Source Files 3