rcolyer.net
RC Lib  Version 202403231100
Classes | Public Member Functions | Friends | List of all members
RC::Bitfield Class Reference

A bounds-safe one-dimensional vector-like structure of efficiently packed bits. More...

#include <Bitfield.h>

Classes

class  BitfieldBool
 A temporary return-type that serves as an interface to specific bit values. More...
 
class  BitfieldBoolConst
 A temporary return-type that serves as a const interface to specific bit values. More...
 

Public Member Functions

 Bitfield ()
 Default constructor which initializes to size 0.
 
 Bitfield (size_t b_size)
 Constructor which sets the initial size to b_size bits. More...
 
void Delete ()
 Deletes the stored data, resets the size to 0, and releases the memory.
 
bool IsEmpty () const
 True if the size is 0.
 
BitfieldBool operator[] (size_t x)
 Bounds-checked access of the element at index x. More...
 
BitfieldBool operator() (size_t x)
 Identical to operator[].
 
BitfieldBoolConst operator[] (size_t x) const
 Const version of operator[].
 
BitfieldBoolConst operator() (size_t x) const
 Const version of operator[].
 
BitfieldBool At (size_t x)
 Const version of At()
 
BitfieldBoolConst At (size_t x) const
 Identical to operator[].
 
size_t size () const
 Return the current number of bits.
 
size_t reserved () const
 Return the number of bits for which space is reserved.
 
void Zero ()
 Set all bits to zero.
 
void ZeroRange (const size_t start, const size_t end)
 Set all bits to zero between index start and end, inclusive.
 
Data1D< u32 > & Raw ()
 Provides raw access to the Data1D<u32> in which the bits are packed. More...
 
void Reserve (const size_t reserve_size)
 Reserve storage without resizing the Bitfield. More...
 
void Resize (const size_t resize_size)
 Resize the array, reallocating if necessary. More...
 
void Crop ()
 Reduce memory consumption to only that necessary for size() bits.
 
void Clear ()
 Identical to Delete()
 
void Append (const bool new_bit)
 Add a bit to the end, expanding if necessary. More...
 
void Append (const Bitfield &other)
 Append bits from another Bitfield to this Bitfield. More...
 
void ExpandSet (size_t pos, const bool new_bit)
 Assign new_bit to index pos, expanding if necessary to reach that index.
 
Bitfieldoperator+= (const bool new_bit)
 Appends new_bit, expanding as necessary. More...
 
Bitfieldoperator+= (const Bitfield &other)
 Appends the bits from other, expanding as necessary. More...
 
bool Check (const size_t x) const
 True if index x is within the Bitfield.
 
void Assert (const size_t x) const
 Throws ErrorMsgBounds if index x is out of bounds for this Bitfield.
 
size_t CountOnes () const
 Efficiently determines the total number of true / 1 values in the Bitfield.
 
size_t CountZeroes () const
 Efficiently determines the total number of false / 0 values in the Bitfield.
 

Friends

class Bitfield2D
 
class Bitfield3D
 
void swap (Bitfield &a, Bitfield &b)
 Swap the data in Bitfield a and b.
 

Detailed Description

A bounds-safe one-dimensional vector-like structure of efficiently packed bits.

It provides efficient resizing and append operations, and bounds-safe array access.

See also
Bitfield2D
Bitfield3D

Constructor & Destructor Documentation

◆ Bitfield()

RC::Bitfield::Bitfield ( size_t  b_size)
inlineexplicit

Constructor which sets the initial size to b_size bits.

Parameters
b_sizeThe initial number of bits.

Member Function Documentation

◆ Append() [1/2]

void RC::Bitfield::Append ( const Bitfield other)
inline

Append bits from another Bitfield to this Bitfield.

Parameters
otherThe Bitfield from which bits should be copied over.

◆ Append() [2/2]

void RC::Bitfield::Append ( const bool  new_bit)
inline

Add a bit to the end, expanding if necessary.

Efficiency note: Expands by doubling for linear efficiency.

Parameters
new_bitThe new bit to append to the end.
See also
Reserve

◆ operator+=() [1/2]

Bitfield& RC::Bitfield::operator+= ( const Bitfield other)
inline

Appends the bits from other, expanding as necessary.

See also
Append

◆ operator+=() [2/2]

Bitfield& RC::Bitfield::operator+= ( const bool  new_bit)
inline

Appends new_bit, expanding as necessary.

See also
Append

◆ operator[]()

BitfieldBool RC::Bitfield::operator[] ( size_t  x)
inline

Bounds-checked access of the element at index x.

Throws an ErrorMsgBounds exception if x is out of the bounds.

Parameters
xIndex of the bit to access.
Returns
A BitfieldBool structure that casts to a bool or can be assigned a bool value.

◆ Raw()

Data1D<u32>& RC::Bitfield::Raw ( )
inline

Provides raw access to the Data1D<u32> in which the bits are packed.

The bits are packed in order, least significant bit first.

◆ Reserve()

void RC::Bitfield::Reserve ( const size_t  reserve_size)
inline

Reserve storage without resizing the Bitfield.

Parameters
reserve_sizeThe number of bits for which storage should be reserved.

◆ Resize()

void RC::Bitfield::Resize ( const size_t  resize_size)
inline

Resize the array, reallocating if necessary.

See Data1D::Resize for efficiency details.


The documentation for this class was generated from the following file:
email address
— (c) 2015