Documentation ¶
Overview ¶
The package provides typical bitmap storage and manipulation functions. BitSet is a data structure that holds a set of bits. Each bit is represented by a single bit in the byte slice. The first bit is stored in the left bit of the first byte, and the last bit is stored in the least significant bit of the last byte. Examples:
1000 0010 where bits 0,6 is set in the string representation will look like hex string "82" 1000 0011 where bits 0, 6, 7 is set in the string representation will look like hes string "83" 1100 0001 1100 0011 where bits 0,1,7, 8,9, 14,15 us set in the string representation will look like "c1c3"
Index ¶
- Variables
- func AreSet(buf []byte, rule CompareRule, bitpos ...uint) (bool, error)
- func Validate(buf []byte) error
- type BitSet
- type ByteBitSet
- func (bs *ByteBitSet) AreSet(rule CompareRule, bitpos ...uint) bool
- func (bs *ByteBitSet) BinaryString() string
- func (bs *ByteBitSet) Bytes() []byte
- func (bs *ByteBitSet) Empty() bool
- func (bs *ByteBitSet) IsAllocated(bitpos uint) bool
- func (bs *ByteBitSet) IsSet(bitpos uint) bool
- func (bs *ByteBitSet) Len() uint
- func (bs *ByteBitSet) Set(val bool, bitpos ...uint) BitSet
- func (bs *ByteBitSet) String() string
- type CompareRule
Constants ¶
This section is empty.
Variables ¶
var ( ErrParseFailed = errors.New("invalid character found") ErrInvalidSourceString = errors.New("invalid source string") )
ErrParseFailed is returned by the Parse function when an invalid character is found in the input string. Valid characters are 0-9, a-f, A-F.
Functions ¶
Types ¶
type BitSet ¶
type BitSet interface { IsSet(pos uint) bool AreSet(rule CompareRule, bitpos ...uint) bool Set(val bool, bitpos ...uint) BitSet Empty() bool String() string Bytes() []byte }
func Clone ¶ added in v0.0.2
Clone returns a deep copy of the given BitSet. It creates a new BitSet and copies the mask from the source BitSet.
func NewFromBinaryString ¶ added in v1.0.0
type ByteBitSet ¶ added in v1.0.0
type ByteBitSet struct {
// contains filtered or unexported fields
}
ByteBitSet holds a set of bits using a slice of bytes. Each bit in the bitset is represented by one bit in the byte slice.
func New ¶
func New(bitSize int) *ByteBitSet
New creates a BitSet with allocated space for the specified number of bits. It takes the bit size as input, calculates how many bytes are needed to store that many bits, and returns a pointer to a new BitSet.
func NewFromBytes ¶ added in v1.0.0
func NewFromBytes(buf []byte) (ByteBitSet, error)
NewFromBytes converts a hexadecimal byte slice into a BitSet. It returns an error if any of the input characters are not valid hexadecimal digits.
func NewFromString ¶ added in v1.0.0
func NewFromString(src string) (ByteBitSet, error)
func (*ByteBitSet) AreSet ¶ added in v1.0.0
func (bs *ByteBitSet) AreSet(rule CompareRule, bitpos ...uint) bool
AreSet checks if all or any of the specified bits are set to 1. If any of the bits is not set, or if the bitset is empty, it returns false.
func (*ByteBitSet) BinaryString ¶ added in v1.0.0
func (bs *ByteBitSet) BinaryString() string
BinaryString returns binary representation of the bitset. Each byte in the bitset is converted to 8 characters (0 or 1). Zero bit of the bitset will be at the end of the string.
func (*ByteBitSet) Bytes ¶ added in v1.0.0
func (bs *ByteBitSet) Bytes() []byte
Bytes returns the internal representation of the bitset as a byte slice.
func (*ByteBitSet) Empty ¶ added in v1.0.0
func (bs *ByteBitSet) Empty() bool
Empty returns true if BitSet is not initialized.
func (*ByteBitSet) IsAllocated ¶ added in v1.0.0
func (bs *ByteBitSet) IsAllocated(bitpos uint) bool
IsAllocated returns true if the space for the specified bit is already allocated. It checks whether the internal storage has enough space for the given bit position.
func (*ByteBitSet) IsSet ¶ added in v1.0.0
func (bs *ByteBitSet) IsSet(bitpos uint) bool
IsSet returns true if the bit at the specified position is set (i.e., 1). If the position is beyond the current length of the bitset, it returns false.
func (*ByteBitSet) Len ¶ added in v1.0.0
func (bs *ByteBitSet) Len() uint
Len returns the length of the allocated bitset in bits. The length is calculated by multiplying the number of bytes in the mask by 8.
func (*ByteBitSet) Set ¶ added in v1.0.0
func (bs *ByteBitSet) Set(val bool, bitpos ...uint) BitSet
Set sets the specified bits to 1. If the bit position is out of range, the internal byte slice is automatically extended to accommodate the bit.
func (*ByteBitSet) String ¶ added in v1.0.0
func (bs *ByteBitSet) String() string
String returns a hexadecimal representation of the bitset. Each byte in the bitset is converted to two hexadecimal characters.