Developer documentation
Version 3.0.3-105-gd3941f44
MR::BitSet Class Reference

a class for storing bitwise information More...

#include "misc/bitset.h"

Inheritance diagram for MR::BitSet:
MR::DWI::Directions::Mask

Classes

class  ConstValue
 
class  Value
 

Public Member Functions

 BitSet (const size_t, const bool allocator=false)
 create a new bitset with the desired size. More...
 
 BitSet (const BitSet &)
 copy-construct a bitset, with explicit copying of data into the new instance More...
 
 ~BitSet ()
 
void resize (const size_t, const bool allocator=false)
 resize the bitset, retaining existing data More...
 
void clear (const bool allocator=false)
 clear the data More...
 
ConstValue operator[] (const size_t i) const
 access boolean value at a given index More...
 
Value operator[] (const size_t i)
 
size_t size () const
 the number of boolean elements in the set More...
 
bool full () const
 whether or not the bitset is 'full' i.e. all elements are true More...
 
bool empty () const
 whether or not the bitset is 'empty' i.e. all elements are false More...
 
size_t count () const
 count the number of true entries in the set More...
 
BitSetoperator= (const BitSet &)
 convenience functions for performing boolean operations More...
 
bool operator== (const BitSet &) const
 
bool operator!= (const BitSet &) const
 
BitSetoperator|= (const BitSet &)
 
BitSetoperator&= (const BitSet &)
 
BitSetoperator^= (const BitSet &)
 
BitSet operator| (const BitSet &) const
 
BitSet operator& (const BitSet &) const
 
BitSet operator^ (const BitSet &) const
 
BitSet operator~ () const
 
const uint8_t * get_data_ptr () const
 

Protected Member Functions

bool have_excess_bits () const
 
size_t excess_bits () const
 
uint8_t excess_bit_mask () const
 
bool test (const size_t index) const
 
void set (const size_t index)
 
void reset (const size_t index)
 

Protected Attributes

size_t bits
 
size_t bytes
 

Friends

std::ostream & operator<< (std::ostream &, const BitSet &)
 

Detailed Description

a class for storing bitwise information

The BitSet class stores information in a bitwise fashion. Only a single bit of memory is used for each bit of information. Unlike the std::bitset class, the size of the BitSet can be specified (and modified) at runtime.

This class is useful for storing a single boolean value (true or false) for each element of some vector. It may also be useful for two-dimensional data, though the programmer is responsible for performing the conversion from a two-dimensional position to an array index. If a boolean value for each voxel is required for three- or four-dimensional data, use of an Image::BufferScratch<bool> is recommended.

Definition at line 43 of file bitset.h.

Constructor & Destructor Documentation

◆ BitSet() [1/2]

MR::BitSet::BitSet ( const  size_t,
const bool  allocator = false 
)

create a new bitset with the desired size.

If allocator is unspecified or set to false, the initial value for each element will be false. If it is specified as true, then all data will be initialised as true.

◆ BitSet() [2/2]

MR::BitSet::BitSet ( const BitSet )

copy-construct a bitset, with explicit copying of data into the new instance

The BitSet copy-constructor explicitly copies all of the data from the constructing instance into the new instance. Therefore, their sizes and data will be identical, but subsequent modifications to the data in one instance will not affect the other.

◆ ~BitSet()

MR::BitSet::~BitSet ( )

Member Function Documentation

◆ clear()

void MR::BitSet::clear ( const bool  allocator = false)

clear the data

Clears all existing data in the BitSet. By default all values will be set to false; if allocator is explicitly set to true, then all values will be set to true.

◆ count()

size_t MR::BitSet::count ( ) const

count the number of true entries in the set

Convenience function for counting the number of true entries in the set. This can be useful if the programmer chooses not to manually keep track of the number of entries set or not set. The number of entries in the data that are set to false can be calculated as:

BitSet data (1000);
// ...
const size_t false_count = data.size() - data.count();
BitSet(const size_t, const bool allocator=false)
create a new bitset with the desired size.
Returns
the number of elements in the array set to true.

◆ empty()

bool MR::BitSet::empty ( ) const

whether or not the bitset is 'empty' i.e. all elements are false

Convenience function for testing whether or not the BitSet is 'empty', i.e. all elements in the array are set to false. This can be useful if the programmer chooses not to manually keep track of the number of entries set or not set. Because it processes the data in bytes rather than bits, it is faster than the programmer manually performing this calculation.

Returns
true if all elements are set to false, false otherwise.

◆ excess_bit_mask()

uint8_t MR::BitSet::excess_bit_mask ( ) const
inlineprotected

Definition at line 201 of file bitset.h.

◆ excess_bits()

size_t MR::BitSet::excess_bits ( ) const
inlineprotected

Definition at line 200 of file bitset.h.

◆ full()

bool MR::BitSet::full ( ) const

whether or not the bitset is 'full' i.e. all elements are true

Convenience function for testing whether or not the BitSet is 'full', i.e. all elements in the array are set to true. This can be useful if the programmer chooses not to manually keep track of the number of entries set or not set. Because it processes the data in bytes rather than bits, it is faster than the programmer manually performing this calculation.

Returns
true if all elements are set to true, false otherwise.

◆ get_data_ptr()

const uint8_t * MR::BitSet::get_data_ptr ( ) const
inline

Definition at line 190 of file bitset.h.

◆ have_excess_bits()

bool MR::BitSet::have_excess_bits ( ) const
inlineprotected

Definition at line 199 of file bitset.h.

◆ operator!=()

bool MR::BitSet::operator!= ( const BitSet ) const

◆ operator&()

BitSet MR::BitSet::operator& ( const BitSet ) const

◆ operator&=()

BitSet & MR::BitSet::operator&= ( const BitSet )

◆ operator=()

BitSet & MR::BitSet::operator= ( const BitSet )

convenience functions for performing boolean operations

Convenience function for performing boolean operations using BitSet data. Each of these functions performs a particular boolean operation, but for all of the data in the array. Because they process the data in bytes rather than bits, they are faster than if the programmer manually performed these operations on a per-bit basis.

Particular notes of interest:

  • The '=' (assignment) operator will copy both the size of the passed BitSet, and the data itself.
  • The '==' (comparison) operator will return false if the two BitSets differ in their number of bits. If the programmer wishes to compare two BitSets of different sizes, where only the length of the smaller BitSet is considered, this can be achieved as follows:
    BitSet A (1000), B (2000);
    // ...
    BitSet B_small (B);
    B_small.resize (A.size());
    if (A == B_small) {
    // Do something
    }

◆ operator==()

bool MR::BitSet::operator== ( const BitSet ) const

◆ operator[]() [1/2]

Value MR::BitSet::operator[] ( const size_t  i)
inline

Definition at line 114 of file bitset.h.

◆ operator[]() [2/2]

ConstValue MR::BitSet::operator[] ( const size_t  i) const
inline

access boolean value at a given index

These functions provide access to the raw boolean data using the [] (square-bracket) operator. Both const and non-const versions are provided. Although internally the BitSet class stores eight boolean values in each byte of memory (to minimise memory usage), these operators can be used to access and manipulate the bit wise data without corrupting the surrounding data.

Returns
a Value or ConstValue class used to manipulate the bit data at the specified index

Definition at line 113 of file bitset.h.

◆ operator^()

BitSet MR::BitSet::operator^ ( const BitSet ) const

◆ operator^=()

BitSet & MR::BitSet::operator^= ( const BitSet )

◆ operator|()

BitSet MR::BitSet::operator| ( const BitSet ) const

◆ operator|=()

BitSet & MR::BitSet::operator|= ( const BitSet )

◆ operator~()

BitSet MR::BitSet::operator~ ( ) const

◆ reset()

void MR::BitSet::reset ( const size_t  index)
inlineprotected

Definition at line 217 of file bitset.h.

◆ resize()

void MR::BitSet::resize ( const  size_t,
const bool  allocator = false 
)

resize the bitset, retaining existing data

Modify the size of the BitSet. Existing data information will be retained by the resizing process. If the new size is smaller than the existing size, then all excess data will be truncated. If the new size is larger than the existing size, then all existing data will be retained, and additional bits beyond the previous size will be set to false; unless allocator is explicitly provided as true, in which case all additional bits will be set to true.

◆ set()

void MR::BitSet::set ( const size_t  index)
inlineprotected

Definition at line 209 of file bitset.h.

◆ size()

size_t MR::BitSet::size ( ) const
inline

the number of boolean elements in the set

The size of the BitSet. Note that this is the number of boolean values stored in the array; NOT the memory consumption of the class.

Returns
the number of boolean elements in the BitSet.

Definition at line 120 of file bitset.h.

◆ test()

bool MR::BitSet::test ( const size_t  index) const
inlineprotected

Definition at line 203 of file bitset.h.

Friends And Related Function Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  ,
const BitSet  
)
friend

Member Data Documentation

◆ bits

size_t MR::BitSet::bits
protected

Definition at line 196 of file bitset.h.

◆ bytes

size_t MR::BitSet::bytes
protected

Definition at line 197 of file bitset.h.


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