NLMISC::CBitSet Class Reference

#include <bit_set.h>


Detailed Description

A BitSet, to test / set flags quickly.
Author:
Lionel Berenguier

Nevrax France

Date:
2000

Definition at line 49 of file bit_set.h.

Public Member Functions

const std::vector< uint32 > & getVector () const
 Return the raw vector.

void serial (NLMISC::IStream &f)
 Serialize.

void setUint (uint32 srcValue, uint i)
 Write an uint32 into the bit set (use with caution, no check).

std::string toString () const
 Return a string representing the bitfield with 1 and 0 (from left to right).

Bit comparisons.
bool allCleared ()
 Return true if all bits are cleared. false if size()==0.

bool allSet ()
 Return true if all bits are set. false if size()==0.

bool compareRestrict (const CBitSet &bs) const
bool operator!= (const CBitSet &bs) const
 operator!=.

bool operator== (const CBitSet &bs) const
 Compare two BitSet. If not of same size, return false.

Object.
 CBitSet (const CBitSet &bs)
 CBitSet (uint numBits)
 CBitSet ()
CBitSetoperator= (const CBitSet &bs)
 ~CBitSet ()
Basics.
void clear (sint bitNumber)
 Set a bit to 0.

void clear ()
 Clear the bitarray so size() return 0.

void clearAll ()
 Set all bits to 0.

bool get (sint bitNumber) const
 Get the value of a bit.

bool operator[] (sint bitNumber) const
 Get the value of a bit.

void resize (uint numBits)
 Resize the bit array. All Bits are reseted.

void resizeNoReset (uint numBits, bool value=false)
 Resize the bit array. Bits are not reseted. New bits are set with value.

void set (sint bitNumber)
 Set a bit to 1.

void set (sint bitNumber, bool value)
 Set a bit to 0 or 1.

void setAll ()
 Set all bits to 1.

uint size () const
 Return size of the bit array.

Bit operations.
void flip ()
 NOT the BitArray.

CBitSet operator & (const CBitSet &bs) const
CBitSetoperator &= (const CBitSet &bs)
CBitSet operator^ (const CBitSet &bs) const
CBitSetoperator^= (const CBitSet &bs)
CBitSet operator| (const CBitSet &bs) const
CBitSetoperator|= (const CBitSet &bs)
CBitSet operator~ () const
 Return The bitarray NOTed.


Private Attributes

std::vector< uint32Array
uint32 MaskLast
sint NumBits


Constructor & Destructor Documentation

NLMISC::CBitSet::CBitSet  ) 
 

Definition at line 44 of file bit_set.cpp.

References MaskLast, and NumBits.

00045 {
00046         NumBits= 0;
00047         MaskLast= 0;
00048 }

NLMISC::CBitSet::CBitSet uint  numBits  ) 
 

Definition at line 49 of file bit_set.cpp.

References MaskLast, NumBits, resize(), and uint.

00050 {
00051         NumBits= 0;
00052         MaskLast= 0;
00053         resize(numBits);
00054 }

NLMISC::CBitSet::CBitSet const CBitSet bs  ) 
 

Definition at line 55 of file bit_set.cpp.

References Array, MaskLast, and NumBits.

00056 {
00057         NumBits= bs.NumBits;
00058         MaskLast= bs.MaskLast;
00059         Array= bs.Array;
00060 }

NLMISC::CBitSet::~CBitSet  ) 
 

Definition at line 61 of file bit_set.cpp.

00062 {
00063 }


Member Function Documentation

bool NLMISC::CBitSet::allCleared  ) 
 

Return true if all bits are cleared. false if size()==0.

Definition at line 280 of file bit_set.cpp.

References NumBits, and sint.

00281 {
00282         if(NumBits==0)
00283                 return false;
00284         for(sint i=0;i<(sint)Array.size();i++)
00285         {
00286                 if( Array[i]!= 0 )
00287                         return false;
00288         }
00289         return true;
00290 }

bool NLMISC::CBitSet::allSet  ) 
 

Return true if all bits are set. false if size()==0.

Definition at line 267 of file bit_set.cpp.

References MaskLast, NumBits, sint, and uint.

00268 {
00269         if(NumBits==0)
00270                 return false;
00271         for(sint i=0;i<(sint)Array.size()-1;i++)
00272         {
00273                 if( Array[i]!= (~((uint)0)) )
00274                         return false;
00275         }
00276         if( Array[Array.size()-1]!= MaskLast )
00277                 return false;
00278         return true;
00279 }

void NLMISC::CBitSet::clear sint  bitNumber  )  [inline]
 

Set a bit to 0.

Definition at line 103 of file bit_set.h.

References set(), and sint.

00103 {set(bitNumber, false);}

void NLMISC::CBitSet::clear  ) 
 

Clear the bitarray so size() return 0.

Definition at line 75 of file bit_set.cpp.

References MaskLast, and NumBits.

Referenced by NLMISC::CEventListenerAsync::operator()(), resize(), and resizeNoReset().

00076 {
00077         Array.clear();
00078         NumBits= 0;
00079         MaskLast=0;
00080 }

void NLMISC::CBitSet::clearAll  ) 
 

Set all bits to 0.

Definition at line 125 of file bit_set.cpp.

Referenced by NL3D::CZone::clip(), NL3D::CZone::clipPatchs(), NLMISC::CEventListenerAsync::operator()(), NLMISC::CEventListenerAsync::reset(), and resize().

00126 {
00127         fill_n(Array.begin(), Array.size(), 0);
00128 }

bool NLMISC::CBitSet::compareRestrict const CBitSet bs  )  const
 

Compare two BitSet not necessarely of same size. The comparison is done on N bits, where N=min(this->size(), bs.size())

Returns:
true if the N common bits of this and bs are the same. false otherwise.

Definition at line 240 of file bit_set.cpp.

References Array, min, NL_BITLEN, NumBits, sint, and uint.

00241 {
00242         sint    n=min(NumBits, bs.NumBits);
00243         if(n==0) return true;
00244 
00245         sint    nA= (n+NL_BITLEN-1) / NL_BITLEN;
00246         uint    mask;
00247 
00248         uint    nLastBits= n & (NL_BITLEN-1) ;
00249         // Generate the mask for the last common word.
00250         if(nLastBits==0)
00251                 mask= ~((uint)0);
00252         else
00253                 mask= (1<< nLastBits) -1;
00254 
00255 
00256         for(sint i=0;i<nA-1;i++)
00257         {
00258                 if(Array[i]!=bs.Array[i])
00259                         return false;
00260         }
00261         if( (Array[nA-1]&mask) != (bs.Array[nA-1]&mask) )
00262                 return false;
00263 
00264 
00265         return true;
00266 }

void NLMISC::CBitSet::flip  ) 
 

NOT the BitArray.

Definition at line 167 of file bit_set.cpp.

References MaskLast, NumBits, and sint.

Referenced by operator~().

00168 {
00169         if(NumBits==0)
00170                 return;
00171 
00172         for(sint i=0;i<(sint)Array.size();i++)
00173                 Array[i]= ~Array[i];
00174 
00175         Array[Array.size()-1]&= MaskLast;
00176 }

bool NLMISC::CBitSet::get sint  bitNumber  )  const [inline]
 

Get the value of a bit.

Definition at line 87 of file bit_set.h.

References NL_BITLEN, NL_BITLEN_SHIFT, nlassert, NumBits, sint, and uint.

Referenced by NLMISC::CEventListenerAsync::isKeyDown(), NLMISC::CEventListenerAsync::isKeyPushed(), NL3D::CZone::isPatchRenderClipped(), NLMISC::CEventListenerAsync::operator()(), operator[](), serial(), and toString().

00088         {
00089                 nlassert(bitNumber>=0 && bitNumber<NumBits);
00090 
00091                 uint    mask= bitNumber&(NL_BITLEN-1);
00092                 mask= 1<<mask;
00093                 return (Array[bitNumber >> NL_BITLEN_SHIFT] & mask) != 0;
00094         }

const std::vector<uint32>& NLMISC::CBitSet::getVector  )  const [inline]
 

Return the raw vector.

Definition at line 173 of file bit_set.h.

Referenced by NL3D::CZone::clip(), and NLMISC::CBitMemStream::pokeBits().

00173 { return Array; }

CBitSet NLMISC::CBitSet::operator & const CBitSet bs  )  const
 

Return this ANDed with bs. The result BitSet is of size of *this. Any missing bits into bs will be considered as 0.

Definition at line 140 of file bit_set.cpp.

00141 {
00142         CBitSet ret;
00143 
00144         ret= *this;
00145         ret&=bs;
00146         return ret;
00147 }

CBitSet & NLMISC::CBitSet::operator &= const CBitSet bs  ) 
 

AND the bitArray with bs. The bitset size is not changed. Any missing bits into bs will be considered as 0.

Definition at line 177 of file bit_set.cpp.

References Array, MaskLast, min, NumBits, and sint.

00178 {
00179         if(NumBits==0)
00180                 return *this;
00181 
00182         sint    minSize= min(Array.size(), bs.Array.size());
00183         sint    i;
00184         for(i=0;i<minSize;i++)
00185                 Array[i]= Array[i] & bs.Array[i];
00186         for(i=minSize;i<(sint)Array.size();i++)
00187                 Array[i]=0;
00188 
00189         Array[Array.size()-1]&= MaskLast;
00190 
00191         return *this;
00192 }

bool NLMISC::CBitSet::operator!= const CBitSet bs  )  const
 

operator!=.

Definition at line 236 of file bit_set.cpp.

References operator==().

00237 {
00238         return (!operator==(bs));
00239 }

CBitSet & NLMISC::CBitSet::operator= const CBitSet bs  ) 
 

Definition at line 64 of file bit_set.cpp.

References Array, MaskLast, and NumBits.

00065 {
00066         NumBits= bs.NumBits;
00067         MaskLast= bs.MaskLast;
00068         Array= bs.Array;
00069 
00070         return *this;
00071 }

bool NLMISC::CBitSet::operator== const CBitSet bs  )  const
 

Compare two BitSet. If not of same size, return false.

Definition at line 224 of file bit_set.cpp.

References Array, NumBits, and sint.

Referenced by operator!=().

00225 {
00226         if(NumBits!=bs.NumBits)
00227                 return false;
00228 
00229         for(sint i=0;i<(sint)Array.size();i++)
00230         {
00231                 if(Array[i]!=bs.Array[i])
00232                         return false;
00233         }
00234         return true;
00235 }

bool NLMISC::CBitSet::operator[] sint  bitNumber  )  const [inline]
 

Get the value of a bit.

Definition at line 96 of file bit_set.h.

References get(), and sint.

00097         {
00098                 return get(bitNumber);
00099         }

CBitSet NLMISC::CBitSet::operator^ const CBitSet bs  )  const
 

Return this XORed with bs. The result BitSet is of size of *this. Any missing bits into bs will be considered as 0.

Definition at line 156 of file bit_set.cpp.

00157 {
00158         CBitSet ret;
00159 
00160         ret= *this;
00161         ret^=bs;
00162         return ret;
00163 }

CBitSet & NLMISC::CBitSet::operator^= const CBitSet bs  ) 
 

XOR the bitArray with bs. The bitset size is not changed. Any missing bits into bs will be considered as 0.

Definition at line 207 of file bit_set.cpp.

References Array, MaskLast, min, NumBits, and sint.

00208 {
00209         if(NumBits==0)
00210                 return *this;
00211 
00212         sint    minSize= min(Array.size(), bs.Array.size());
00213         for(sint i=0;i<minSize;i++)
00214                 Array[i]= Array[i] ^ bs.Array[i];
00215         // Do nothing for bits word from minSize to Array.size().
00216 
00217         Array[Array.size()-1]&= MaskLast;
00218 
00219         return *this;
00220 }

CBitSet NLMISC::CBitSet::operator| const CBitSet bs  )  const
 

Return this ORed with bs. The result BitSet is of size of *this. Any missing bits into bs will be considered as 0.

Definition at line 148 of file bit_set.cpp.

00149 {
00150         CBitSet ret;
00151 
00152         ret= *this;
00153         ret|=bs;
00154         return ret;
00155 }

CBitSet & NLMISC::CBitSet::operator|= const CBitSet bs  ) 
 

OR the bitArray with bs. The bitset size is not changed. Any missing bits into bs will be considered as 0.

Definition at line 193 of file bit_set.cpp.

References Array, MaskLast, min, NumBits, and sint.

00194 {
00195         if(NumBits==0)
00196                 return *this;
00197 
00198         sint    minSize= min(Array.size(), bs.Array.size());
00199         for(sint i=0;i<minSize;i++)
00200                 Array[i]= Array[i] | bs.Array[i];
00201         // Do nothing for bits word from minSize to Array.size().
00202 
00203         Array[Array.size()-1]&= MaskLast;
00204 
00205         return *this;
00206 }

CBitSet NLMISC::CBitSet::operator~  )  const
 

Return The bitarray NOTed.

Definition at line 132 of file bit_set.cpp.

References flip().

00133 {
00134         CBitSet ret;
00135 
00136         ret= *this;
00137         ret.flip();
00138         return ret;
00139 }

void NLMISC::CBitSet::resize uint  numBits  ) 
 

Resize the bit array. All Bits are reseted.

Definition at line 81 of file bit_set.cpp.

References clear(), clearAll(), MaskLast, NL_BITLEN, NumBits, and uint.

Referenced by NL3D::CZone::build(), CBitSet(), NLMISC::CEventListenerAsync::CEventListenerAsync(), NLMISC::CBitMemStream::getSerialItem(), NL3D::CZone::serial(), and serial().

00082 {
00083         if(numBits==0)
00084                 clear();
00085 
00086         NumBits= numBits;
00087         Array.resize( (NumBits+NL_BITLEN-1) / NL_BITLEN );
00088         uint    nLastBits= NumBits & (NL_BITLEN-1) ;
00089         // Generate the mask for the last word.
00090         if(nLastBits==0)
00091                 MaskLast= ~((uint)0);
00092         else
00093                 MaskLast= (1<< nLastBits) -1;
00094 
00095         // reset to 0.
00096         clearAll();
00097 }

void NLMISC::CBitSet::resizeNoReset uint  numBits,
bool  value = false
 

Resize the bit array. Bits are not reseted. New bits are set with value.

Definition at line 98 of file bit_set.cpp.

References clear(), MaskLast, NL_BITLEN, NumBits, set(), uint, and value.

00099 {
00100         if(numBits==0)
00101                 clear();
00102 
00103         uint oldNum=NumBits;
00104         NumBits= numBits;
00105         Array.resize( (NumBits+NL_BITLEN-1) / NL_BITLEN );
00106         uint    nLastBits= NumBits & (NL_BITLEN-1) ;
00107         // Generate the mask for the last word.
00108         if(nLastBits==0)
00109                 MaskLast= ~((uint)0);
00110         else
00111                 MaskLast= (1<< nLastBits) -1;
00112 
00113         // Set new bit to value
00114         for (uint i=oldNum; i<(uint)NumBits; i++)
00115                 set(i, value);
00116 }

void NLMISC::CBitSet::serial NLMISC::IStream f  ) 
 

Serialize.

Definition at line 294 of file bit_set.cpp.

References get(), NLMISC::IStream::isReading(), resize(), NLMISC::IStream::serial(), NLMISC::IStream::serialCont(), NLMISC::IStream::serialVersion(), set(), sint, size(), and uint32.

00295 {
00296         (void)f.serialVersion(0);
00297         uint32  sz=0;
00298         vector<uint32>  array32;
00299 
00300         // Must support any size of uint.
00301         if(f.isReading())
00302         {
00303                 f.serial(sz);
00304                 resize(sz);
00305 
00306                 f.serialCont(array32);
00307                 for(sint i=0;i<(sint)sz;i++)
00308                 {
00309                         uint32  a=array32[i/32];
00310                         a&= 1<<(i&31);
00311                         set(i, a!=0);
00312                 }
00313         }
00314         else
00315         {
00316                 sz= size();
00317                 f.serial(sz);
00318 
00319                 array32.resize(sz/32);
00320                 fill_n(array32.begin(), array32.size(), 0);
00321                 for(sint i=0;i<(sint)sz;i++)
00322                 {
00323                         if(get(i))
00324                                 array32[i/32]|= 1<<(i&31);
00325                 }
00326                 f.serialCont(array32);
00327         }
00328 }

void NLMISC::CBitSet::set sint  bitNumber  )  [inline]
 

Set a bit to 1.

Definition at line 101 of file bit_set.h.

References set(), and sint.

00101 {set(bitNumber, true);}

void NLMISC::CBitSet::set sint  bitNumber,
bool  value
[inline]
 

Set a bit to 0 or 1.

Definition at line 75 of file bit_set.h.

References NL_BITLEN, NL_BITLEN_SHIFT, nlassert, NumBits, sint, uint, and value.

Referenced by clear(), NL3D::CZone::clipPatchs(), NLMISC::CEventListenerAsync::isKeyPushed(), NLMISC::CEventListenerAsync::operator()(), NL3D::CPatch::release(), NL3D::CZone::resetRenderFarAndDeleteVBFV(), resizeNoReset(), serial(), and set().

00076         {
00077                 nlassert(bitNumber>=0 && bitNumber<NumBits);
00078 
00079                 uint    mask= bitNumber&(NL_BITLEN-1);
00080                 mask= 1<<mask;
00081                 if(value)
00082                         Array[bitNumber >> NL_BITLEN_SHIFT]|= mask ;
00083                 else
00084                         Array[bitNumber >> NL_BITLEN_SHIFT]&= ~mask;
00085         }

void NLMISC::CBitSet::setAll  ) 
 

Set all bits to 1.

Definition at line 117 of file bit_set.cpp.

References MaskLast, s, and uint.

Referenced by NL3D::CZone::build(), NL3D::CZone::clip(), and NL3D::CZone::serial().

00118 {
00119         const uint s = Array.size();
00120         fill_n(Array.begin(), s, ~((uint)0));
00121 
00122         if (s)
00123                 Array[s-1]&= MaskLast;
00124 }

void NLMISC::CBitSet::setUint uint32  srcValue,
uint  i
[inline]
 

Write an uint32 into the bit set (use with caution, no check).

Definition at line 176 of file bit_set.h.

References uint, and uint32.

Referenced by NLMISC::CBitMemStream::readBits().

00176 { Array[i] = srcValue; }

uint NLMISC::CBitSet::size  )  const [inline]
 

Return size of the bit array.

Definition at line 70 of file bit_set.h.

References NumBits, and uint.

Referenced by NLMISC::CBitMemStream::pokeBits(), NLMISC::CBitMemStream::readBits(), serial(), and toString().

00071         {
00072                 return NumBits;
00073         }

std::string NLMISC::CBitSet::toString  )  const
 

Return a string representing the bitfield with 1 and 0 (from left to right).

Definition at line 334 of file bit_set.cpp.

References get(), s, sint, and size().

Referenced by NLMISC::CBitMemStream::getSerialItem().

00335 {
00336         string s;
00337         for ( sint i=0; i!=(sint)size(); ++i )
00338         {
00339                 s += (get(i) ? '1' : '0');
00340         }
00341         return s;
00342 }


Field Documentation

std::vector<uint32> NLMISC::CBitSet::Array [private]
 

Definition at line 182 of file bit_set.h.

Referenced by CBitSet(), compareRestrict(), operator &=(), operator=(), operator==(), operator^=(), and operator|=().

uint32 NLMISC::CBitSet::MaskLast [private]
 

Definition at line 184 of file bit_set.h.

Referenced by allSet(), CBitSet(), clear(), flip(), operator &=(), operator=(), operator^=(), operator|=(), resize(), resizeNoReset(), and setAll().

sint NLMISC::CBitSet::NumBits [private]
 

Definition at line 183 of file bit_set.h.

Referenced by allCleared(), allSet(), CBitSet(), clear(), compareRestrict(), flip(), get(), operator &=(), operator=(), operator==(), operator^=(), operator|=(), resize(), resizeNoReset(), set(), and size().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:05:00 2004 for NeL by doxygen 1.3.6