Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

bitsetdemo.gno

// Package bitsetdemo is a small gnoweb demo of the dense bit vector provided by
// the [p/moul/x/daily/bitset](/p/nym-moultest123/x/daily/bitset/v0) library: it sieves the
// primes under 100 into a bit set and shows the set algebra on two small sets.
//
// It contains no bit-twiddling of its own — storage, popcount and the set
// operations all come from the library. Stateless, so Render is deterministic.
package bitsetdemo

import (
	"strconv"
	"strings"

	"gno.land/p/nym-moultest123/x/daily/bitset/v0"
)

// limit bounds the sieve shown on the page.
const limit = 100

// primes returns a bit set of the primes below limit, sieved with the bit set
// itself as the composite marker.
func primes() *bitset.BitSet {
	composite := bitset.New(limit)
	out := bitset.New(limit)
	for p := 2; p < limit; p++ {
		if composite.Has(p) {
			continue
		}
		out.Set(p)
		for m := p * p; m < limit; m += p {
			composite.Set(m)
		}
	}
	return out
}

// Render renders the demo for gnoweb.
func Render(path string) string {
	var b strings.Builder
	b.WriteString("# BitSet\n\n")
	b.WriteString("A dense bit vector, demoing the ")
	b.WriteString("[`p/moul/x/daily/bitset`](/p/nym-moultest123/x/daily/bitset/v0) library.\n\n")

	p := primes()
	b.WriteString("## Primes under ")
	b.WriteString(strconv.Itoa(limit))
	b.WriteString("\n\n**")
	b.WriteString(strconv.Itoa(p.Count()))
	b.WriteString("** set bits in ")
	b.WriteString(strconv.Itoa(p.Cap()))
	b.WriteString(" bits of capacity:\n\n")
	b.WriteString(list(p.Slice()))

	small := bitset.FromSlice(16, []int{1, 2, 3, 5, 8})
	even := bitset.FromSlice(16, []int{2, 4, 6, 8, 10})
	b.WriteString("\n## Set algebra\n\n")
	b.WriteString("| set | bits |\n|---|---|\n")
	row(&b, "a", small)
	row(&b, "b", even)
	row(&b, "a ∪ b", bitset.Union(small, even))
	row(&b, "a ∩ b", bitset.Intersect(small, even))
	row(&b, "a \\ b", bitset.Difference(small, even))
	row(&b, "a △ b", bitset.SymmetricDifference(small, even))
	return b.String()
}

func row(b *strings.Builder, name string, s *bitset.BitSet) {
	b.WriteString("| ")
	b.WriteString(name)
	b.WriteString(" | `")
	b.WriteString(list1(s.Slice()))
	b.WriteString("` |\n")
}

func list(xs []int) string {
	if len(xs) == 0 {
		return "_none_\n"
	}
	return "`" + list1(xs) + "`\n"
}

func list1(xs []int) string {
	parts := []string{}
	for _, x := range xs {
		parts = append(parts, strconv.Itoa(x))
	}
	return strings.Join(parts, " ")
}

gnomod.toml

module = "gno.land/r/nym-moultest123/x/daily/bitsetdemo/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186410
  max_deposit = "5000000ugnot"

render_example_test.gno

package bitsetdemo

// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
	print(Render(""))
	// Output:
	// # BitSet
	//
	// A dense bit vector, demoing the [`p/moul/x/daily/bitset`](/p/nym-moultest123/x/daily/bitset/v0) library.
	//
	// ## Primes under 100
	//
	// **25** set bits in 100 bits of capacity:
	//
	// `2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97`
	//
	// ## Set algebra
	//
	// | set | bits |
	// |---|---|
	// | a | `1 2 3 5 8` |
	// | b | `2 4 6 8 10` |
	// | a ∪ b | `1 2 3 4 5 6 8 10` |
	// | a ∩ b | `2 8` |
	// | a \ b | `1 3 5` |
	// | a △ b | `1 3 4 5 6 10` |
}