bitutil

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package bitutil provides bit manipulation utilities for barcode processing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BitArray

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

BitArray is a simple, fast array of bits represented compactly by an array of uint32 values internally.

func NewBitArray

func NewBitArray(size int) *BitArray

NewBitArray creates a new BitArray with the given size.

func NewBitArrayFromBits

func NewBitArrayFromBits(b []uint32, size int) *BitArray

NewBitArrayFromBits creates a BitArray from existing data (for testing).

func (*BitArray) AppendBit

func (ba *BitArray) AppendBit(bit bool)

AppendBit appends a single bit.

func (*BitArray) AppendBitArray

func (ba *BitArray) AppendBitArray(other *BitArray)

AppendBitArray appends another BitArray to this one.

func (*BitArray) AppendBits

func (ba *BitArray) AppendBits(value uint32, numBits int)

AppendBits appends the least-significant numBits bits of value, from most significant to least significant.

func (*BitArray) BitData

func (ba *BitArray) BitData() []uint32

BitData returns the underlying uint32 slice.

func (*BitArray) Clear

func (ba *BitArray) Clear()

Clear clears all bits.

func (*BitArray) Clone

func (ba *BitArray) Clone() *BitArray

Clone returns a copy of this BitArray.

func (*BitArray) Flip

func (ba *BitArray) Flip(i int)

Flip flips bit i.

func (*BitArray) Get

func (ba *BitArray) Get(i int) bool

Get returns true if bit i is set.

func (*BitArray) GetNextSet

func (ba *BitArray) GetNextSet(from int) int

GetNextSet returns the index of the first set bit starting from the given index, or size if none are set.

func (*BitArray) GetNextUnset

func (ba *BitArray) GetNextUnset(from int) int

GetNextUnset returns the index of the first unset bit starting from the given index, or size if none are unset.

func (*BitArray) IsRange

func (ba *BitArray) IsRange(start, end int, value bool) bool

IsRange checks if all bits in [start, end) have the given value.

func (*BitArray) Reverse

func (ba *BitArray) Reverse()

Reverse reverses all bits in the array.

func (*BitArray) Set

func (ba *BitArray) Set(i int)

Set sets bit i.

func (*BitArray) SetBulk

func (ba *BitArray) SetBulk(i int, newBits uint32)

SetBulk sets a block of 32 bits starting at bit i.

func (*BitArray) SetRange

func (ba *BitArray) SetRange(start, end int)

SetRange sets a range of bits [start, end).

func (*BitArray) Size

func (ba *BitArray) Size() int

Size returns the number of bits in the array.

func (*BitArray) SizeInBytes

func (ba *BitArray) SizeInBytes() int

SizeInBytes returns the number of bytes needed to hold the bits.

func (*BitArray) String

func (ba *BitArray) String() string

String returns a string representation using 'X' for set and '.' for unset.

func (*BitArray) ToBytes

func (ba *BitArray) ToBytes(bitOffset int, array []byte, offset, numBytes int)

ToBytes writes bits to a byte slice (most-significant byte first within each byte).

func (*BitArray) Xor

func (ba *BitArray) Xor(other *BitArray)

Xor performs XOR with another BitArray.

type BitMatrix

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

BitMatrix represents a 2D matrix of bits. x is the column position, y is the row position. The origin is at the top-left.

func NewBitMatrix

func NewBitMatrix(dimension int) *BitMatrix

NewBitMatrix creates a new square BitMatrix with the given dimension.

func NewBitMatrixWithSize

func NewBitMatrixWithSize(width, height int) *BitMatrix

NewBitMatrixWithSize creates a new BitMatrix with the given width and height.

func ParseBoolMatrix

func ParseBoolMatrix(image [][]bool) *BitMatrix

ParseBoolMatrix creates a BitMatrix from a 2D boolean array.

func ParseStringMatrix

func ParseStringMatrix(repr, setStr, unsetStr string) *BitMatrix

ParseStringMatrix creates a BitMatrix from a string representation.

func (*BitMatrix) BottomRightOnBit

func (bm *BitMatrix) BottomRightOnBit() []int

BottomRightOnBit returns the [x, y] of the bottom-right set bit, or nil if none are set.

func (*BitMatrix) Clear

func (bm *BitMatrix) Clear()

Clear clears all bits.

func (*BitMatrix) Clone

func (bm *BitMatrix) Clone() *BitMatrix

Clone returns a deep copy of the BitMatrix.

func (*BitMatrix) EnclosingRectangle

func (bm *BitMatrix) EnclosingRectangle() []int

EnclosingRectangle returns [left, top, width, height] of the enclosing rectangle of all set bits, or nil if all bits are unset.

func (*BitMatrix) Equals

func (bm *BitMatrix) Equals(other *BitMatrix) bool

Equals returns true if two BitMatrices are equal.

func (*BitMatrix) Flip

func (bm *BitMatrix) Flip(x, y int)

Flip flips the bit at (x, y).

func (*BitMatrix) FlipAll

func (bm *BitMatrix) FlipAll()

FlipAll flips every bit in the matrix.

func (*BitMatrix) Get

func (bm *BitMatrix) Get(x, y int) bool

Get returns true if the bit at (x, y) is set.

func (*BitMatrix) Height

func (bm *BitMatrix) Height() int

Height returns the height.

func (*BitMatrix) Rotate

func (bm *BitMatrix) Rotate(degrees int)

Rotate rotates the matrix by the given degrees (0, 90, 180, 270).

func (*BitMatrix) Rotate90

func (bm *BitMatrix) Rotate90()

Rotate90 rotates the matrix 90 degrees counterclockwise.

func (*BitMatrix) Rotate180

func (bm *BitMatrix) Rotate180()

Rotate180 rotates the matrix 180 degrees.

func (*BitMatrix) Row

func (bm *BitMatrix) Row(y int, row *BitArray) *BitArray

Row returns a row as a BitArray. If row is nil or too small, a new one is allocated.

func (*BitMatrix) RowSize

func (bm *BitMatrix) RowSize() int

RowSize returns the row size in uint32 units.

func (*BitMatrix) Set

func (bm *BitMatrix) Set(x, y int)

Set sets the bit at (x, y).

func (*BitMatrix) SetRegion

func (bm *BitMatrix) SetRegion(left, top, width, height int)

SetRegion sets a rectangular region of bits.

func (*BitMatrix) SetRow

func (bm *BitMatrix) SetRow(y int, row *BitArray)

SetRow sets the row at y from the given BitArray.

func (*BitMatrix) String

func (bm *BitMatrix) String() string

String returns a string representation using "X " for set and " " for unset.

func (*BitMatrix) StringWithChars

func (bm *BitMatrix) StringWithChars(setString, unsetString string) string

StringWithChars returns a string representation using the given set/unset strings.

func (*BitMatrix) TopLeftOnBit

func (bm *BitMatrix) TopLeftOnBit() []int

TopLeftOnBit returns the [x, y] of the top-left set bit, or nil if none are set.

func (*BitMatrix) Unset

func (bm *BitMatrix) Unset(x, y int)

Unset clears the bit at (x, y).

func (*BitMatrix) Width

func (bm *BitMatrix) Width() int

Width returns the width.

func (*BitMatrix) Xor

func (bm *BitMatrix) Xor(mask *BitMatrix)

Xor flips bits in this matrix where the mask has bits set.

type BitSource

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

BitSource reads bits from a byte sequence where the number of bits read is not necessarily a multiple of 8.

func NewBitSource

func NewBitSource(bytes []byte) *BitSource

NewBitSource creates a new BitSource from a byte slice. Bits are read from the first byte first, from most-significant to least-significant.

func (*BitSource) Available

func (bs *BitSource) Available() int

Available returns the number of bits that can still be read.

func (*BitSource) BitOffset

func (bs *BitSource) BitOffset() int

BitOffset returns the index of the next bit within the current byte.

func (*BitSource) ByteOffset

func (bs *BitSource) ByteOffset() int

ByteOffset returns the index of the next byte to be read.

func (*BitSource) ReadBits

func (bs *BitSource) ReadBits(numBits int) (int, error)

ReadBits reads numBits bits and returns them as the least-significant bits of an int.

type BitSourceError

type BitSourceError struct {
	NumBits int
}

BitSourceError is returned when an invalid number of bits is requested.

func (*BitSourceError) Error

func (e *BitSourceError) Error() string

Jump to

Keyboard shortcuts

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