Evo C++ Library v0.5.1
Public Types | Public Member Functions | List of all members
BitArraySubsetT< TParent > Class Template Reference

#include <evo/bit_array.h>

Detailed Description

template<class TParent>
class evo::BitArraySubsetT< TParent >

A subset of a BitArray.

C++11:

Note that this is not a full EvoContainer and doesn't have any iterators.

Constructors
Read Access
Modifiers
Example
#include <evo/bit_array.h>
#include <evo/io.h>
using namespace evo;
static Console& c = con();
int main() {
// Create 10-bit array
BitArray array(10);
// Set first and last bits
array.setbit(0);
array.setbit(9);
// Create subset excluding first and last bits
BitArraySubset subset(array, 1, 8);
// Set all bits 2-6 in subset, setting bits 3-7 in parent bit array
subset.setbits(2, 5);
// Extract and print integer in hex from all 8 bits in subset: 00111110
uint16 val = subset.extractr<uint16>(0, 8);
c.out << FmtUInt16(val, fHEX) << NL;
// Extract and print integer in hex from all 10 bits: 1001111101
val = array.extractr<uint16>(0, 10);
c.out << FmtUInt16(val, fHEX) << NL;
return 0;
}

Output:

3E
27D

Public Types

typedef TParent Parent
 Parent BitArray type. More...
 
typedef TParent::Size Size
 Size integer type. More...
 
typedef BitArraySubsetT< TParent > ThisType
 This BitArraySubset type. More...
 
typedef TParent::Value Value
 Chunk value type. More...
 

Public Member Functions

 BitArraySubsetT ()
 Constructor sets as null. More...
 
 BitArraySubsetT (const Parent &parent)
 Constructor to reference a parent BitArray as read-only. More...
 
 BitArraySubsetT (Parent &parent)
 Constructor to reference a parent BitArray. More...
 
 BitArraySubsetT (const Parent &parent, Size pos, Size count=ALL)
 Constructor to reference a subset of parent BitArray as read-only. More...
 
 BitArraySubsetT (Parent &parent, Size pos, Size count=ALL)
 Constructor to reference a subset of parent BitArray. More...
 
 BitArraySubsetT (const ThisType &src)
 Copy constructor to reference the same parent and subset. More...
 
 BitArraySubsetT (ThisType &&src)
 Move constructor (C++11). More...
 
Size checkall () const
 Check if all bits are set in subset (const). More...
 
bool checkany () const
 Check if any bits are set in subset (const). More...
 
bool clearbit (Size pos)
 Clear bit at position in subset. More...
 
Size clearbits (Size pos=0, Size count=ALL) const
 Clear count bits at position in subset. More...
 
Size countbits (bool value=true) const
 Count number of bits set or cleared in subset (const). More...
 
bool empty () const
 Get whether empty. More...
 
template<class U = ulong>
extractl (Size pos=0, Size count=ALL) const
 Extract bits from subset. More...
 
template<class U = ulong>
extractr (Size pos=0, Size count=ALL) const
 Extract bits from subset. More...
 
template<class U >
bool format (U &out, int base=fBIN)
 Format bits from subset to stream or string using base. More...
 
bool getbit (Size pos) const
 Get bit at position in subset (const). More...
 
bool null () const
 Get whether null. More...
 
Size offset () const
 Get subset offset position in parent. More...
 
bool operator!= (const ThisType &data) const
 Inequality operator. More...
 
ThisTypeoperator= (ThisType &&src)
 Move assignment operator (C++11). More...
 
ThisTypeoperator= (const ThisType &src)
 Assignment operator to copy a subset, referencing the same parent. More...
 
ThisTypeoperator= (const Parent &parent)
 Assignment operator to reference a new parent as read-only. More...
 
ThisTypeoperator= (Parent &parent)
 Assignment operator to reference a new parent. More...
 
bool operator== (const ThisType &data) const
 Equality operator. More...
 
bool operator[] (Size pos) const
 Get bit at position in subset (const). More...
 
const Parentparent () const
 Get pointer to parent BitArray. More...
 
Parentparent_nonconst ()
 Get non-const pointer to parent BitArray. More...
 
bool readonly () const
 Get whether subset is read-only, meaning writes will fail. More...
 
ThisTypeset ()
 Set as null. More...
 
ThisTypeset (const ThisType &src, Size pos=0, Size count=ALL)
 Set as subset of another subset. More...
 
ThisTypeset (const Parent &parent, Size pos=0, Size count=ALL)
 Set as read-only subset of BitArray. More...
 
ThisTypeset (Parent &parent, Size pos=0, Size count=ALL)
 Set as subset of BitArray. More...
 
bool setbit (Size pos, bool value=true)
 Set or clear bit at position in subset. More...
 
Size setbits (Size pos=0, Size count=ALL, bool value=true)
 Set or clear count bits at position in subset. More...
 
Size size () const
 Get size as number of bits in subset. More...
 
template<class U >
bool store (Size pos, Size count, U value)
 Stores bits from value in subset. More...
 
bool togglebit (Size pos)
 Toggle bit at position in subset. More...
 
Size togglebits (Size pos=0, uint count=ALL)
 Toggle count bits at position in subset. More...
 

Member Typedef Documentation

◆ Parent

typedef TParent Parent

Parent BitArray type.

◆ Size

typedef TParent::Size Size

Size integer type.

◆ ThisType

typedef BitArraySubsetT<TParent> ThisType

This BitArraySubset type.

◆ Value

typedef TParent::Value Value

Chunk value type.

Constructor & Destructor Documentation

◆ BitArraySubsetT() [1/7]

BitArraySubsetT ( )
inline

Constructor sets as null.

◆ BitArraySubsetT() [2/7]

BitArraySubsetT ( const Parent parent)
inline

Constructor to reference a parent BitArray as read-only.

Parameters
parentParent to reference (const)

◆ BitArraySubsetT() [3/7]

BitArraySubsetT ( Parent parent)
inline

Constructor to reference a parent BitArray.

Parameters
parentParent to reference

◆ BitArraySubsetT() [4/7]

BitArraySubsetT ( const Parent parent,
Size  pos,
Size  count = ALL 
)
inline

Constructor to reference a subset of parent BitArray as read-only.

Parameters
parentParent to reference (const)
posBit posiiton in parent for subset
countNumber of bits in parent for subset

◆ BitArraySubsetT() [5/7]

BitArraySubsetT ( Parent parent,
Size  pos,
Size  count = ALL 
)
inline

Constructor to reference a subset of parent BitArray.

Parameters
parentParent to reference
posBit posiiton in parent for subset
countNumber of bits in parent for subset

◆ BitArraySubsetT() [6/7]

BitArraySubsetT ( const ThisType src)
inline

Copy constructor to reference the same parent and subset.

Parameters
srcSubset to copy from

◆ BitArraySubsetT() [7/7]

BitArraySubsetT ( ThisType &&  src)
inline

Move constructor (C++11).

Parameters
srcSource to move

Member Function Documentation

◆ checkall()

Size checkall ( ) const
inline

Check if all bits are set in subset (const).

  • This scans and checks that all bits are set in subset, and stops early if a bit isn't set
Returns
Whether all bits are set, true if empty

◆ checkany()

bool checkany ( ) const
inline

Check if any bits are set in subset (const).

  • This scans and checks that at least 1 bit is set in subset, and stops early if a bit is set
Returns
Whether at least 1 bit is set, false if all bits are cleared or if empty

◆ clearbit()

bool clearbit ( Size  pos)
inline

Clear bit at position in subset.

  • Bits are counted left-to-right, as if from an array of bools
Parameters
posBit position in subset to clear, 0 for first bit on left, etc
Returns
Whether successful, false if pos out of bounds
See also
clearbits(), setbit(), togglebit(), getbit()

◆ clearbits()

Size clearbits ( Size  pos = 0,
Size  count = ALL 
) const
inline

Clear count bits at position in subset.

  • Bits are counted left-to-right, as if from an array of bools
Parameters
posBit position in subset to clear, 0 for first bit on left, etc
countNumber of bits to clear from pos, ALL for all from pos in subset
Returns
Number of bits modified, 0 if out of bounds or count=0
See also
clearbit(), setbits(), togglebits()

◆ countbits()

Size countbits ( bool  value = true) const
inline

Count number of bits set or cleared in subset (const).

  • This scans through all bits in subset and counts the number of bits set or cleared
  • For best performace see notes for bits_popcount()
Parameters
valueWhether to count bits set, false to count cleared bits (not set)
Returns
Number of bits set or cleared

◆ empty()

bool empty ( ) const
inline

Get whether empty.

Returns
Whether empty

◆ extractl()

U extractl ( Size  pos = 0,
Size  count = ALL 
) const
inline

Extract bits from subset.

  • Bits are stored in chunks but are extracted left-to-right, as if from an array of bools
    • This works the same regardless of chunk size, or system endianness
  • Extracted bits are left-aligned in the return value, and the left-most bit is the "first" bit (as if from an array of bools)
    • Example with 3 extracted bits 111 returned as uint8: 11100000
Template Parameters
UReturn type for extracted bits, must be unsigned (uint, ulong, uint32, etc) – defaults to ulong in C++11 or newer
Parameters
posBit position to extract from, 0 for first bit on left, etc
countNumber of bits to extract, truncated if larger than bitsize or return value
Returns
Extracted bits, aligned to the left
See also
extractr(), get(), store()

◆ extractr()

U extractr ( Size  pos = 0,
Size  count = ALL 
) const
inline

Extract bits from subset.

  • Bits are stored in chunks but are extracted left-to-right, as if from an array of bools
    • This works the same regardless of chunk size, or system endianness
  • Extracted bits returned are right-aligned so the result is an integer
    • Example with 3 extracted bits 111 returned as uint8: 00000111 or 7 in decimal
  • A count larger than bits in return type will be truncated – this should be avoided as it may not be easy to tell how many bits were truncated
    • For example, if you pass count=9 with return type uint8 then this uses count=8
  • Otherwise count may go out of bit array bounds without truncation – bits from out of bounds are set to 0 and still are right-aligned according to count
Template Parameters
UReturn type for extracted bits, must be unsigned (uint, ulong, uint32, etc) – defaults to ulong in C++11 or newer
Parameters
posBit position to extract from, 0 for first bit on left, etc
countNumber of bits to extract, ALL for num bits in return type, truncated if larger than bits in return type
Returns
Extracted bits, aligned to the right
See also
extractl(), get(), store()

◆ format()

bool format ( U &  out,
int  base = fBIN 
)
inline

Format bits from subset to stream or string using base.

  • If there aren't enough bits for the last digit then zero-bits are padded on the right to finish, ex: 1 set bit formatted as hex is 8, or 5 set bits formatted as hex is F8
    • Bits are ordered left-to-right, as if from an array of bools, so this padding is added at the end
Template Parameters
UOutput string/stream type, deduced from arguments
Parameters
outOutput string or stream (or associated Format object) to format to
baseBase to format as, must be a power of 2 between 2-32 (inclusive), add 100 for lowercase letters, or use enum:
  • fBIN for binary
  • fOCT for octal
  • fHEX for hexidecimal
  • fHEXL for lowercase hexadecimal
Returns
Whether successful, false on stream error or if base is invalid

◆ getbit()

bool getbit ( Size  pos) const
inline

Get bit at position in subset (const).

  • Bits are counted left-to-right, as if from an array of bools
Parameters
posBit position in subset to get, 0 for first bit on left, etc
Returns
Bit value, false if pos is out of bounds
See also
setbit(), clearbit(), togglebit()

◆ null()

bool null ( ) const
inline

Get whether null.

  • Always empty when null
Returns
Whether null

◆ offset()

Size offset ( ) const
inline

Get subset offset position in parent.

Returns
Offset position

◆ operator!=()

bool operator!= ( const ThisType data) const
inline

Inequality operator.

Parameters
dataSubset to compare to
Returns
Whether inequal

◆ operator=() [1/4]

ThisType& operator= ( ThisType &&  src)
inline

Move assignment operator (C++11).

Parameters
srcSource to move
Returns
This

◆ operator=() [2/4]

ThisType& operator= ( const ThisType src)
inline

Assignment operator to copy a subset, referencing the same parent.

Parameters
srcSubset to copy from
Returns
This

◆ operator=() [3/4]

ThisType& operator= ( const Parent parent)
inline

Assignment operator to reference a new parent as read-only.

Parameters
parentParent to reference (const)
Returns
This

◆ operator=() [4/4]

ThisType& operator= ( Parent parent)
inline

Assignment operator to reference a new parent.

Parameters
parentParent to reference
Returns
This

◆ operator==()

bool operator== ( const ThisType data) const
inline

Equality operator.

Parameters
dataSubset to compare to
Returns
Whether equal

◆ operator[]()

bool operator[] ( Size  pos) const
inline

Get bit at position in subset (const).

  • Bits are counted left-to-right, as if from an array of bools
Parameters
posBit position in subset to get, 0 for first bit on left, etc
Returns
Bit value, false if pos is out of subset bounds
See also
getbit(), setbit()

◆ parent()

const Parent* parent ( ) const
inline

Get pointer to parent BitArray.

Returns
Parent pointer, NULL if null

◆ parent_nonconst()

Parent* parent_nonconst ( )
inline

Get non-const pointer to parent BitArray.

Returns
Parent pointer, NULL if read-only or null

◆ readonly()

bool readonly ( ) const
inline

Get whether subset is read-only, meaning writes will fail.

  • Null is also considered read-only
Returns
Whether read-only

◆ set() [1/4]

ThisType& set ( )
inline

Set as null.

Returns
This

◆ set() [2/4]

ThisType& set ( const ThisType src,
Size  pos = 0,
Size  count = ALL 
)
inline

Set as subset of another subset.

Parameters
srcSubset to copy from
posBit position for subset of subset
countNumber of bits for subset of subset
Returns
This

◆ set() [3/4]

ThisType& set ( const Parent parent,
Size  pos = 0,
Size  count = ALL 
)
inline

Set as read-only subset of BitArray.

Parameters
parentParent to reference (const)
posBit posiiton in parent for subset
countNumber of bits in parent for subset

◆ set() [4/4]

ThisType& set ( Parent parent,
Size  pos = 0,
Size  count = ALL 
)
inline

Set as subset of BitArray.

Parameters
parentParent to reference
posBit posiiton in parent for subset
countNumber of bits in parent for subset

◆ setbit()

bool setbit ( Size  pos,
bool  value = true 
)
inline

Set or clear bit at position in subset.

  • Bits are counted left-to-right, as if from an array of bools
Parameters
posBit position in subset to set, 0 for first bit on left, etc
valueBit value to set, true to set bit, false to clear bit
Returns
Whether successful, false if pos out of bounds
See also
setbits(), clearbit(), togglebit(), getbit()

◆ setbits()

Size setbits ( Size  pos = 0,
Size  count = ALL,
bool  value = true 
)
inline

Set or clear count bits at position in subset.

  • Bits are counted left-to-right, as if from an array of bools
Parameters
posBit position in subset to set, 0 for first bit on left, etc
countNumber of bits to set from pos, ALL for all from pos in subset
valueBit value to set, true to set bits, false to clear bits
Returns
Number of bits modified, 0 if out of bounds or count=0
See also
setbit(), clearbits(), togglebits()

◆ size()

Size size ( ) const
inline

Get size as number of bits in subset.

Returns
Subset size, 0 if empty

◆ store()

bool store ( Size  pos,
Size  count,
value 
)
inline

Stores bits from value in subset.

  • Bits are stored in chunks but are positioned left-to-right, as if in an array of bools
  • This works the same regardless of chunk size, or system endianness
  • Value bits are taken from the right so integers can be stored
  • Example value 00000111 with count=3 would store 111 (3 right-most bits) or 7 in decimal
  • Bits stored after end of subset are truncated and lost
  • Caution: If count exceeds value size, count is capped at that size – only bits from value are stored
Template Parameters
UValue type to store, signed value is treated as unsigned – inferred from value param
Parameters
posBit position in subset to store at, 0 for first bit on left, etc
countNumber of bits to store from value
valueValue to store (count right-most bits are stored)
Returns
Whether successful, false if pos is out of bounds
See also
setbit(), extractl(), extractr()

◆ togglebit()

bool togglebit ( Size  pos)
inline

Toggle bit at position in subset.

  • Bits are counted left-to-right, as if from an array of bools
Parameters
posBit position in subset to toggle, 0 for first bit on left, etc
Returns
Whether successful, false if pos out of bounds
See also
togglebits(), setbit(), getbit()

◆ togglebits()

Size togglebits ( Size  pos = 0,
uint  count = ALL 
)
inline

Toggle count bits at position in subset.

  • Bits are counted left-to-right, as if from an array of bools
Parameters
posBit position in subset to toggle, 0 for first bit on left, etc
countNumber of bits to toggle from pos, ALL for all from pos in subset
Returns
Number of bits modified, 0 if out of bounds or count=0
See also
togglebit(), setbits()

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