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 bitset is a dense, fixed-capacity bit vector as a pure, reusable package: a compact set of small non-negative...

Readme View source

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

Dense fixed-capacity bit vectorNew, FromSlice, Set, Clear, Flip, Has, Count, Slice, Clone, Union, Intersect, Difference, SymmetricDifference, Equal, MaxBits.

A compact set of small non-negative integers. Storage is []uint64 of ceil(n/64) words, so 1024 bits cost 16 words instead of 1024 booleans — the reason to reach for this on chain, where every byte is paid for.

1import "gno.land/p/moul/x/daily/bitset/v0"
2
3b := bitset.FromSlice(20, []int{1, 2, 3})
4c := bitset.FromSlice(20, []int{3, 4})
5bitset.Union(b, c).Slice()       // [1 2 3 4]
6bitset.Intersect(b, c).Slice()   // [3]
7b.Count()                        // 3

Capacity is fixed at construction: operations are bounds-checked and return false out of range rather than growing, because silent growth would make gas unpredictable. Has on an out-of-range index is simply false — something that cannot be a member is not a member.

Combining two sets of different capacities returns nil rather than padding one silently; a size mismatch is a caller error worth surfacing.

MaxBits (65536) caps allocation. Note String() prints bit 0 first, so it reads in index order — the reverse of binary notation.

Live demo: r/moul/x/daily/bitsetdemo · render it at /r/moul/x/daily/bitsetdemo/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 bitset is a dense, fixed-capacity bit vector as a pure, reusable package: a compact set of small non-negative integers with the usual set algebra (union, intersection, difference).

Storage is a []uint64 of ceil(n/64) words, so 1024 bits cost 16 words rather than 1024 booleans — the reason to reach for this on chain, where every byte is paid for.

Capacity is fixed at construction and every operation is bounds-checked rather than growing: an out-of-range index is a caller bug, and silently growing would make gas unpredictable.

A live demo of this package is at r/moul/x/daily/bitsetdemo(/r/moul/x/daily/bitsetdemo/v0).

Constants 1

const MaxBits

1const MaxBits = 1 << 16 // 65536 bits = 1024 words = 8 KiB
source

MaxBits bounds a BitSet so allocation and iteration stay predictable.

Functions 7

func Equal

1func Equal(a, b *BitSet) bool
source

Equal reports whether two sets have the same capacity and the same bits.

func Difference

1func Difference(a, b *BitSet) *BitSet
source

Difference returns a \ b, or nil when the capacities differ.

func FromSlice

1func FromSlice(n int, idx []int) *BitSet
source

FromSlice builds a BitSet of capacity n containing the given indices. Out-of-range indices are ignored.

func Intersect

1func Intersect(a, b *BitSet) *BitSet
source

Intersect returns a ∩ b, or nil when the capacities differ.

func New

1func New(n int) *BitSet
source

New returns a BitSet holding bits [0, n). n is clamped to [0, MaxBits].

func SymmetricDifference

1func SymmetricDifference(a, b *BitSet) *BitSet
source

SymmetricDifference returns a △ b, or nil when the capacities differ.

func Union

1func Union(a, b *BitSet) *BitSet
source

Union returns a ∪ b, or nil when the capacities differ. Mismatched capacities are a caller error rather than something to silently pad.

Types 1

type BitSet

struct
1type BitSet struct {
2	n     int
3	words []uint64
4}
source

BitSet is a fixed-capacity set of integers in [0, n).

Methods on BitSet

func Cap

method on BitSet
1func (b *BitSet) Cap() int
source

Cap returns the capacity in bits.

func Clear

method on BitSet
1func (b *BitSet) Clear(i int) bool
source

Clear turns bit i off and reports whether i was in range.

func Clone

method on BitSet
1func (b *BitSet) Clone() *BitSet
source

Clone returns an independent copy.

func Count

method on BitSet
1func (b *BitSet) Count() int
source

Count returns the number of set bits (popcount).

func Flip

method on BitSet
1func (b *BitSet) Flip(i int) bool
source

Flip inverts bit i and reports whether i was in range.

func Has

method on BitSet
1func (b *BitSet) Has(i int) bool
source

Has reports whether bit i is set. Out of range is false, never a panic: membership of something that cannot be a member is simply false.

func InRange

method on BitSet
1func (b *BitSet) InRange(i int) bool
source

InRange reports whether i is a valid index.

func Set

method on BitSet
1func (b *BitSet) Set(i int) bool
source

Set turns bit i on and reports whether i was in range.

func Slice

method on BitSet
1func (b *BitSet) Slice() []int
source

Slice returns the set bits in ascending order.

func String

method on BitSet
1func (b *BitSet) String() string
source

String renders the set as '0'/'1' from bit 0 upward, which reads left-to-right in index order (note this is the reverse of binary notation).

Imports 1

  • strings stdlib

Source Files 3