awUtil::BitField256 Class Reference


Detailed Description

awUtil::BitField256 acts as an array of booleans with the maximum size of 256.

Note that we do not need to export this class as it is completelly inlined. If you add non-inline methods, you will need to export it.

#include <awUtilBitField256.h>

List of all members.

Public Member Functions

  BitField256 ()
  The default constructor. All bits set to false.
  ~BitField256 ()
  BitField256 (const BitField256 &)
  Copy constructor.
BitField256 operator= (const BitField256 &)
  Assignment operator.
int  maxAvailableBits () const
  Just a convenience, mostly for unit testing.
void  resetAll ()
  Set all the bits to false.
void  setAll ()
  Set all the bits to true.
BitRef256  operator[] (int)
  Index operator that returns a reference appropriate for LHS usage.
bool  operator[] (int) const
  Constant version of the index operator behaving as testBit(i).
bool  operator== (const BitField256 &) const
  Equals operator needs to compare all 64-bit integers.
bool  operator!= (const BitField256 &) const
  Not-equals operator needs to compare all 64-bit integers.
BitField256 setBit (int, bool)
  Set the specified bit to the specified value.
bool  testBit (int) const
  Return true if the bit is set, false otherwise.
bool  testBits (const BitField256 &) const
  Return true if the bits that are set in 'mask' are also set in this bitfield.
bool  anySet () const
  Return true if any of the bits are set to true, false otherwise.
bool  allSet () const
  Return true iff all the bits are set to true.
int  bitCount () const
  Return number of 1-bits.
BitField256  operator~ () const
  Make a copy and return it.
BitField256  operator^ (const BitField256 &mask) const
  Return a copy that is a result of the ^= operation with the mask.
BitField256  operator& (const BitField256 &mask) const
  Return a copy that is a result of the &= operation with the mask.
BitField256  operator| (const BitField256 &mask) const
  Return a copy that is a result of the |= operation with the mask.
BitField256 operator^= (const BitField256 &mask)
  Update the bits by exclusive or-ing them with the mask bits.
BitField256 operator&= (const BitField256 &mask)
  Update the bits by and-ing them with the mask bits.
BitField256 operator|= (const BitField256 &mask)
  Update the bits by or-ing them with the mask bits.

Constructor & Destructor Documentation

BitField256 ( ) [inline]

The default constructor. All bits set to false.

{
    m_bits[0] = UINT64_C(0);
    m_bits[1] = UINT64_C(0);
    m_bits[2] = UINT64_C(0);
    m_bits[3] = UINT64_C(0);
}
~BitField256 ( ) [inline]
{
}
BitField256 ( const BitField256 rhs ) [inline]

Copy constructor.

{
    m_bits[0] = rhs.m_bits[0];
    m_bits[1] = rhs.m_bits[1];
    m_bits[2] = rhs.m_bits[2];
    m_bits[3] = rhs.m_bits[3];
}

Member Function Documentation

BitField256 & operator= ( const BitField256 rhs ) [inline]

Assignment operator.

{
    m_bits[0] = rhs.m_bits[0];
    m_bits[1] = rhs.m_bits[1];
    m_bits[2] = rhs.m_bits[2];
    m_bits[3] = rhs.m_bits[3];
    return *this;
}
int maxAvailableBits ( ) const [inline]

Just a convenience, mostly for unit testing.

The number of bits is already encoded in the name.

{
    return 4 * kNumberBitsInUint64;
}
void resetAll ( ) [inline]

Set all the bits to false.

{
    m_bits[0] = UINT64_C(0);
    m_bits[1] = UINT64_C(0);
    m_bits[2] = UINT64_C(0);
    m_bits[3] = UINT64_C(0);
}
void setAll ( ) [inline]

Set all the bits to true.

{
    m_bits[0] = ~UINT64_C(0);
    m_bits[1] = ~UINT64_C(0);
    m_bits[2] = ~UINT64_C(0);
    m_bits[3] = ~UINT64_C(0);
}
BitRef256 operator[] ( int  i ) [inline]

Index operator that returns a reference appropriate for LHS usage.

This is why you can do "bits[8] = true;" or something like that.

{
    return BitRef256(*this,i);
}
bool operator[] ( int  i ) const [inline]

Constant version of the index operator behaving as testBit(i).

{
    return testBit(i);
}
bool operator== ( const BitField256 rhs ) const [inline]

Equals operator needs to compare all 64-bit integers.

{
    return ((m_bits[0] == rhs.m_bits[0]) &&
            (m_bits[1] == rhs.m_bits[1]) &&
            (m_bits[2] == rhs.m_bits[2]) &&
            (m_bits[3] == rhs.m_bits[3]));
}
bool operator!= ( const BitField256 rhs ) const [inline]

Not-equals operator needs to compare all 64-bit integers.

{
    return !operator==(rhs);
}
BitField256 & setBit ( int  i,
bool  b 
) [inline]

Set the specified bit to the specified value.

Make sure that the bit argument is in [0,256) range or bad things happen.

{
    assert((i >= 0) && (i < 256));
    int iEl = i / kNumberBitsInUint64;
    uint64_t bit = UINT64_C(1) << (i-(iEl*kNumberBitsInUint64));
    if (b) m_bits[iEl] |= bit;      //  Set bit
    else  m_bits[iEl] &= ~bit;      //  Clear bit
    return *this;
}
bool testBit ( int  i ) const [inline]

Return true if the bit is set, false otherwise.

Make sure that the argument is in [0,256) range or bad things happen.

{
    assert((i >= 0) && (i < 256));
    int iEl = i / kNumberBitsInUint64;
    uint64_t bit = UINT64_C(1) << (i-(iEl*kNumberBitsInUint64));
    return ((m_bits[iEl] & bit) == bit);
}
bool testBits ( const BitField256 mask ) const [inline]

Return true if the bits that are set in 'mask' are also set in this bitfield.

{
    for (int i = 0; i < 4; ++i)
    {
        const uint64_t b  = m_bits[i];
        const uint64_t bm = mask.m_bits[i];

        if (bm & b ^ bm)
        {
            return false;
        }
    }
    return true;
}
bool anySet ( ) const [inline]

Return true if any of the bits are set to true, false otherwise.

{
    return ((UINT64_C(0) != m_bits[0]) ||
            (UINT64_C(0) != m_bits[1]) ||
            (UINT64_C(0) != m_bits[2]) ||
            (UINT64_C(0) != m_bits[3]));
}
bool allSet ( ) const [inline]

Return true iff all the bits are set to true.

{
    return ((~UINT64_C(0) == m_bits[0]) &&
            (~UINT64_C(0) == m_bits[1]) &&
            (~UINT64_C(0) == m_bits[2]) &&
            (~UINT64_C(0) == m_bits[3]));
}
int bitCount ( ) const [inline]

Return number of 1-bits.

{
    const uint64_t m1  = UINT64_C(0x5555555555555555); //binary: 0101...
    const uint64_t m2  = UINT64_C(0x3333333333333333); //binary: 00110011..
    const uint64_t m4  = UINT64_C(0x0f0f0f0f0f0f0f0f); //binary:  4 zeros,  4 ones ...
    const uint64_t h01 = UINT64_C(0x0101010101010101); //the sum of 256 to the power of 0,1,2,3...

    int n = 0;
    for (int i = 0; i < 4; ++i)
    {
        uint64_t b = m_bits[i];

        b -= (b >> 1) & m1;              //put count of each 2 bits into those 2 bits
        b  = (b & m2) + ((b >> 2) & m2); //put count of each 4 bits into those 4 bits 
        b  = (b + (b >> 4)) & m4;        //put count of each 8 bits into those 8 bits 
        n += (int)((b * h01) >> 56);     //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
    }
    return n;
}
BitField256 operator~ ( ) const [inline]

Make a copy and return it.

The bits in the copy are reversed.

{
    BitField256 res;
    res.m_bits[0] = ~m_bits[0];
    res.m_bits[1] = ~m_bits[1];
    res.m_bits[2] = ~m_bits[2];
    res.m_bits[3] = ~m_bits[3];
    return res;
}
BitField256 operator^ ( const BitField256 mask ) const [inline]

Return a copy that is a result of the ^= operation with the mask.

{
    BitField256 res( *this );
    res ^= mask;
    return res;
}
BitField256 operator& ( const BitField256 mask ) const [inline]

Return a copy that is a result of the &= operation with the mask.

{
    BitField256 res( *this );
    res &= mask;
    return res;
}
BitField256 operator| ( const BitField256 mask ) const [inline]

Return a copy that is a result of the |= operation with the mask.

{
    BitField256 res( *this );
    res |= mask;
    return res;
}
BitField256 & operator^= ( const BitField256 mask ) [inline]

Update the bits by exclusive or-ing them with the mask bits.

{
    m_bits[0] ^= mask.m_bits[0];
    m_bits[1] ^= mask.m_bits[1];
    m_bits[2] ^= mask.m_bits[2];
    m_bits[3] ^= mask.m_bits[3];
    return *this;
}
BitField256 & operator&= ( const BitField256 mask ) [inline]

Update the bits by and-ing them with the mask bits.

{
    m_bits[0] &= mask.m_bits[0];
    m_bits[1] &= mask.m_bits[1];
    m_bits[2] &= mask.m_bits[2];
    m_bits[3] &= mask.m_bits[3];
    return *this;
}
BitField256 & operator|= ( const BitField256 mask ) [inline]

Update the bits by or-ing them with the mask bits.

{
    m_bits[0] |= mask.m_bits[0];
    m_bits[1] |= mask.m_bits[1];
    m_bits[2] |= mask.m_bits[2];
    m_bits[3] |= mask.m_bits[3];
    return *this;
}

awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256
awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256 awUtil::BitField256