scantools  1.0.4
Graphics manipulation with a view towards scanned documents
Classes | Public Member Functions | Static Public Member Functions | List of all members
bitVector Class Reference

The bitVector class provides an array of bits, similar to QBitArray. More...

#include <bitVector.h>

Classes

class  miniBitVector
 Simple array of bits, useful for static data. More...
 

Public Member Functions

 bitVector ()
 Constructs an empty bit array.
 
 bitVector (int numBits)
 Constructs an array of size bits. More...
 
 bitVector (QByteArray data)
 Constructs a bitVector from a QByteArray. More...
 
quint8 getBit (int index) const
 Returns the bit at position index. More...
 
void setBit (int index, quint8 bit)
 Sets the bit at position index. More...
 
int size () const
 Size of the bitVector in bits. More...
 
void resize (int newSize)
 Sets the size of the bitVector array to newSize bits. More...
 
bool startsWith (const miniBitVector &mbv, int index=0) const
 Check if the content of the given miniBitVector is found at a given position. More...
 
void append (const miniBitVector &mbv)
 Appends a miniBitVector to the end of the bitVector. More...
 
 operator QByteArray ()
 Converts the bitVector to a QByteArray. More...
 
 operator QString () const
 Convert bitVector to human-readable string. More...
 

Static Public Member Functions

static quint8 getBit (const quint8 *ptr, int index)
 Convenience method for read access to a bit in a C-array of bytes. More...
 
static void setBit (quint8 *ptr, int index, quint8 bit)
 Convenience method for write access to a bit in a C-array of bytes. More...
 
static void fill (quint8 *ptr, int startIndex, int numBits, quint8 bit)
 Convenience method for write access to a continuous block of bits in a C-array of bytes. More...
 
static int findEndOfRun (const quint8 *ptr, int startIndex, int endIndex, quint8 runValue)
 Searches for end of a run of consecutive bits with same value. More...
 

Detailed Description

The bitVector class provides an array of bits, similar to QBitArray.

A bitVector is an array that gives access to individual bits. In this respect, the class is very similar to QBitArray. Unlike QBitArray, there are convenient and well-defined conversions from bitVectors to QByteArrays with guraranteed behaviour.

The bitVector class is based on QByteArray, and therefore uses implicit sharing (copy-on-write) to reduce memory usage and to avoid the needless copying of data. The methods of this class are reentrant, but not thread safe.

Warning
The implementation has been tested for correctness, but has not been optimized for speed.

Definition at line 42 of file bitVector.h.

Constructor & Destructor Documentation

◆ bitVector() [1/2]

bitVector::bitVector ( int  numBits)
explicit

Constructs an array of size bits.

Parameters
numBitsSize of the newly created bit vector

The bit are initialized to the value '0'.

◆ bitVector() [2/2]

bitVector::bitVector ( QByteArray  data)
explicit

Constructs a bitVector from a QByteArray.

Constructs an array of 8*data.size() bits. Bit number 0 in the resulting bitVector corresponds to the most significant bit in byte 0 of the QByteArray. Bit number 1 corresponds to the second most significant bit in byte 0 of the QByteArray, etc.

Parameters
dataQByteArray whose data is copied

This operation uses implicit sharing and therefore takes constant time.

Member Function Documentation

◆ append()

void bitVector::append ( const miniBitVector mbv)
inline

Appends a miniBitVector to the end of the bitVector.

The bitVector will be resized if necessary.

Parameters
mbvminiBitVector whose content is appended

Definition at line 168 of file bitVector.h.

◆ fill()

static void bitVector::fill ( quint8 *  ptr,
int  startIndex,
int  numBits,
quint8  bit 
)
inlinestatic

Convenience method for write access to a continuous block of bits in a C-array of bytes.

Parameters
ptrThis is a pointer to an array by bytes (a.k.a. quint8).
startIndexThis must be a valid index position in the bit array. Bit number 0 is the most significant bit in byte 0 of the array. Bit number 1 is the second most significant bit in byte 0 of the array, etc. Indices that point out of the array boundes will likely lead to segfault.
numBitsNumber of bits to be written. The array must be at least of size startIndex+numBits, or else a segfault will likely occur.
bitEither 0 or 1

Definition at line 251 of file bitVector.h.

◆ findEndOfRun()

static int bitVector::findEndOfRun ( const quint8 *  ptr,
int  startIndex,
int  endIndex,
quint8  runValue 
)
inlinestatic

Searches for end of a run of consecutive bits with same value.

Parameters
ptrPointer to the data whose content is searched
startIndexIndex of the first bit to be looked at
endIndexIndex of the last bit to be looked at
runValueValue of the bits in the run
Returns
Index of the first bit in the interval [startIndex : endIndex-1] that is not of value 'runValue'. If no such bit has been found, 'endIndex' is returned.

Definition at line 304 of file bitVector.h.

◆ getBit() [1/2]

static quint8 bitVector::getBit ( const quint8 *  ptr,
int  index 
)
inlinestatic

Convenience method for read access to a bit in a C-array of bytes.

Parameters
ptrThis is a pointer to an array by bytes (a.k.a. quint8).
indexNumber of the bit that is retrieved from the array. Bit number 0 is the most significant bit in byte 0 of the array. Bit number 1 is the second most significant bit in byte 0 of the array, etc. Indices that point out of the array boundes will likely lead to segfault.
Returns
Either 0 or 1

Definition at line 212 of file bitVector.h.

◆ getBit() [2/2]

quint8 bitVector::getBit ( int  index) const
inline

Returns the bit at position index.

Parameters
indexThis must be a valid index position in the bit array. In other words, index < size(). Invalid values will likely lead to segfault
Returns
Either 0 or 1

Definition at line 77 of file bitVector.h.

◆ operator QByteArray()

bitVector::operator QByteArray ( )

Converts the bitVector to a QByteArray.

Stores the bitVector in an array of (size()+7)/8 bytes. Bit number 0 in the bitVector corresponds to the most significant bit in byte 0 of the QByteArray. Bit number 1 corresponds to the second most significant bit in byte 0 of the QByteArray, etc. Unused bits in the last byte of the resulting array are set to zero.

Returns
QByteArray containing the bitvector data

◆ operator QString()

bitVector::operator QString ( ) const

Convert bitVector to human-readable string.

Returns
QString describing the bitVector

◆ resize()

void bitVector::resize ( int  newSize)

Sets the size of the bitVector array to newSize bits.

If size is greater than the current size, the bitVector is extended by adding the extra bits to the end. The new bits are uninitialized. If newSize is less than the current size, bits are removed from the end.

Parameters
newSizeNew size of the vector, in bits

◆ setBit() [1/2]

void bitVector::setBit ( int  index,
quint8  bit 
)
inline

Sets the bit at position index.

Parameters
indexThis must be a valid index position in the bit array. In other words, index < size(). Invalid values for index will likely lead to segfault
bitBit value, either 0 or 1.

Definition at line 89 of file bitVector.h.

◆ setBit() [2/2]

static void bitVector::setBit ( quint8 *  ptr,
int  index,
quint8  bit 
)
inlinestatic

Convenience method for write access to a bit in a C-array of bytes.

Parameters
ptrThis is a pointer to an array by bytes (a.k.a. quint8).
indexThis must be a valid index position in the bit array. Bit number 0 is the most significant bit in byte 0 of the array. Bit number 1 is the second most significant bit in byte 0 of the array, etc. Indices that point out of the array boundes will likely lead to segfault.
bitEither 0 or 1

Definition at line 227 of file bitVector.h.

◆ size()

int bitVector::size ( ) const
inline

Size of the bitVector in bits.

Returns
Size of the bitVector, in number of bits

Definition at line 101 of file bitVector.h.

◆ startsWith()

bool bitVector::startsWith ( const miniBitVector mbv,
int  index = 0 
) const

Check if the content of the given miniBitVector is found at a given position.

Parameters
mbvminiBitVector whose content is expected to by found at position index
indexPosition where the content of mbv is expected
Returns
True if the content of the given miniBitVector is found a position index; otherwise returns false

The documentation for this class was generated from the following file: