bitset

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package bitset provides a BitSet data structure backed by *big.Int, along with helpers for reading and writing bitsets in variable-length (self-delimiting) and fixed-length formats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BitSet

type BitSet struct {
	// contains filtered or unexported fields
}

BitSet implements a growable set of bits, backed by a *big.Int. It provides methods to get, set, and unset individual bits. The zero value of a BitSet is not ready for use; always use NewBitSet.

func NewBitSet

func NewBitSet() *BitSet

NewBitSet creates and initializes a new BitSet to zero.

func (*BitSet) Bytes

func (bs *BitSet) Bytes() []byte

Bytes returns the raw byte representation of the BitSet's underlying *big.Int. The bytes are in big-endian order. Returns nil if the BitSet or its internal value is nil.

func (*BitSet) GetBit

func (bs *BitSet) GetBit(index int) bool

GetBit returns the boolean value of the bit at the given index. It panics if the index is negative.

func (*BitSet) SetBit

func (bs *BitSet) SetBit(index int) *BitSet

SetBit sets the bit at the given index to 1 (true). It panics if the index is negative. Returns the BitSet pointer for chaining.

func (*BitSet) UnsetBit

func (bs *BitSet) UnsetBit(index int) *BitSet

UnsetBit sets the bit at the given index to 0 (false). It panics if the index is negative. Returns the BitSet pointer for chaining.

type FixedBitSet

type FixedBitSet struct{}

FixedBitSet provides methods for reading and writing fixed-length, undelimited bitsets. In this format, all 8 bits of each byte are used for data. The total length of the bitset in bytes must be known beforehand for reading.

func (*FixedBitSet) BytesNeededForNumBits

func (f *FixedBitSet) BytesNeededForNumBits(numBits int) int

BytesNeededForNumBits calculates the minimum number of bytes required to store a fixed-length bitset containing 'numBits' of information. This is equivalent to ceil(numBits / 8).

func (*FixedBitSet) Read

func (f *FixedBitSet) Read(bytes []byte, pos int, length int) (*BitSet, error)

Read reconstructs a BitSet from a fixed-length, undelimited sequence of bytes within a larger byte slice. 'pos' specifies the starting position in 'bytes', and 'length' specifies how many bytes to read to form the bitset. All 8 bits of each read byte contribute to the BitSet.

func (*FixedBitSet) Write

func (f *FixedBitSet) Write(bs *BitSet, padToLength int) ([]byte, error)

Write encodes a BitSet into a fixed-length, undelimited byte slice. All 8 bits of each byte in the output are used for data from the BitSet. The 'padToLength' argument ensures the output byte slice has at least that many bytes, padding with zero bytes if necessary. The least significant bytes of the BitSet appear first. Returns an error if the BitSet contains negative values.

type VarBitSet

type VarBitSet struct{}

VarBitSet provides methods for reading and writing variable-length, self-delimiting bitsets. In this format, each byte encodes 7 bits of data, with the least significant bit (LSB) acting as a continuation flag (1 means more bytes follow, 0 means this is the last byte).

func (*VarBitSet) Read

func (v *VarBitSet) Read(buf buf.Read) (int, *BitSet, error)

Read reads a variable-length, self-delimiting bitset from a buf.Read. It reconstructs the BitSet from bytes where each byte contributes 7 data bits and 1 continuation bit. It returns the number of bytes read, the resulting BitSet, and any error encountered during reading.

func (*VarBitSet) Write

func (v *VarBitSet) Write(bs *BitSet, padToLength int) ([]byte, error)

Write encodes a BitSet into a variable-length, self-delimiting byte slice. Each byte in the output slice contains 7 bits of data from the BitSet and one continuation bit (LSB). If the LSB is 1, more bytes follow; if 0, it's the last byte. The 'padToLength' argument ensures the output byte slice has at least that many bytes, padding with zero bytes (0x00, which represents zero value and no continuation) if necessary. Returns an error if the BitSet contains negative values (which is not typical for bitsets).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL