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

bitset source pure

Readme View source

Bitset Package

Package implements an arbitrary-size bit set, also known as bit array.

Bit sets are useful when you need a compact way to track large sets of boolean flags, such as permissions, feature toggles, or membership sets, using significantly less memory than a slice of booleans while also supporting fast bulk operations across entire sets.

Repository can be found at jeronimoalbi/gnome, as part of jeronimoalbi's Gno smart contracts monorepo.

Usage

 1package main
 2
 3import "gno.land/p/g17khqpukees4237dtn3astzapmp462vjhsz6st4/bitset"
 4
 5const (
 6	PermRead   = 0
 7	PermWrite  = 1
 8	PermDelete = 2
 9	PermAdmin  = 3
10)
11
12func main() {
13	var perms bitset.BitSet
14	perms.Set(PermRead)
15	perms.Set(PermWrite)
16
17	println("Can read:", perms.IsSet(PermRead))
18	println("Can write:", perms.IsSet(PermWrite))
19	println("Can delete:", perms.IsSet(PermDelete))
20	println("Is admin:", perms.IsSet(PermAdmin))
21	println("Permissions set:", perms.Len())
22}
23
24// Output:
25// Can read: true
26// Can write: true
27// Can delete: false
28// Is admin: false
29// Permissions set: 2

Functions 1

func New

1func New(size uint64) BitSet
source

New creates a new BitSet pre-allocated to hold at least size bits.

Types 1

type BitSet

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

BitSet implements an arbitrary-size bit array.

Methods on BitSet

func And

method on BitSet
1func (b *BitSet) And(other BitSet)
source

And performs an in-place AND with other bitset. Bits beyond the other bitset are cleared.

func Clear

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

Clear turns off the bit at a given position.

func ClearAll

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

ClearAll turns off all bits. The size of the bitset remains unchanged.

func Compact

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

Compact reclaims memory by removing trailing zero words.

func Equal

method on BitSet
1func (b BitSet) Equal(other BitSet) bool
source

Equal checks whether other bitset have exactly the same bits set.

func IsSet

method on BitSet
1func (b BitSet) IsSet(i uint64) bool
source

IsSet checks whether the bit at a given position is set.

func Len

method on BitSet
1func (b BitSet) Len() int
source

Len returns the number of set bits.

func Or

method on BitSet
1func (b *BitSet) Or(other BitSet)
source

Or performs an in-place OR (union) with other bitset. Current bitset grows if the other bitset is bigger.

func PaddedString

method on BitSet
1func (b BitSet) PaddedString() string
source

PaddedString returns a binary representation of the bitset with zero padding. Representation uses MSB-first binary representation.

func Set

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

Set turns on the bit at a given position.

func Size

method on BitSet
1func (b BitSet) Size() int
source

Size returns the total number of bits (including unset) currently stored. It's the total space allocated within the set in bits.

func String

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

String returns a binary representation of the bitset. Representation uses MSB-first binary representation.

func Xor

method on BitSet
1func (b *BitSet) Xor(other BitSet)
source

Xor performs an in-place XOR with other. Current bitset grows if the other bitset is bigger.

Imports 1

  • strings stdlib

Source Files 3