#include <bit_set.h>
Nevrax France
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 () | |
| CBitSet & | operator= (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 |
| CBitSet & | operator &= (const CBitSet &bs) |
| CBitSet | operator^ (const CBitSet &bs) const |
| CBitSet & | operator^= (const CBitSet &bs) |
| CBitSet | operator| (const CBitSet &bs) const |
| CBitSet & | operator|= (const CBitSet &bs) |
| CBitSet | operator~ () const |
| Return The bitarray NOTed. | |
Private Attributes | |
| std::vector< uint32 > | Array |
| uint32 | MaskLast |
| sint | NumBits |
|
|
Definition at line 44 of file bit_set.cpp. References MaskLast, and NumBits.
|
|
|
Definition at line 49 of file bit_set.cpp. References MaskLast, NumBits, resize(), and uint.
|
|
|
Definition at line 55 of file bit_set.cpp. References Array, MaskLast, and NumBits.
|
|
|
Definition at line 61 of file bit_set.cpp.
00062 {
00063 }
|
|
|
Return true if all bits are cleared. false if size()==0.
Definition at line 280 of file bit_set.cpp.
|
|
|
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.
|
|
|
Set a bit to 0.
Definition at line 103 of file bit_set.h.
00103 {set(bitNumber, false);}
|
|
|
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().
|
|
|
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().
|
|
|
Compare two BitSet not necessarely of same size. The comparison is done on N bits, where N=min(this->size(), bs.size())
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 }
|
|
|
NOT the BitArray.
Definition at line 167 of file bit_set.cpp. References MaskLast, NumBits, and sint. Referenced by operator~().
|
|
|
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 }
|
|
|
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; }
|
|
|
Return this ANDed with bs. The result BitSet is of size of Definition at line 140 of file bit_set.cpp.
00141 {
00142 CBitSet ret;
00143
00144 ret= *this;
00145 ret&=bs;
00146 return ret;
00147 }
|
|
|
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 }
|
|
|
operator!=.
Definition at line 236 of file bit_set.cpp. References operator==().
00237 {
00238 return (!operator==(bs));
00239 }
|
|
|
Definition at line 64 of file bit_set.cpp. References Array, MaskLast, and NumBits.
|
|
|
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!=().
|
|
|
Get the value of a bit.
Definition at line 96 of file bit_set.h.
00097 {
00098 return get(bitNumber);
00099 }
|
|
|
Return this XORed with bs. The result BitSet is of size of Definition at line 156 of file bit_set.cpp.
00157 {
00158 CBitSet ret;
00159
00160 ret= *this;
00161 ret^=bs;
00162 return ret;
00163 }
|
|
|
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 }
|
|
|
Return this ORed with bs. The result BitSet is of size of Definition at line 148 of file bit_set.cpp.
00149 {
00150 CBitSet ret;
00151
00152 ret= *this;
00153 ret|=bs;
00154 return ret;
00155 }
|
|
|
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 }
|
|
|
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 }
|
|
|
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 }
|
|
||||||||||||
|
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 }
|
|
|
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 }
|
|
|
Set a bit to 1.
Definition at line 101 of file bit_set.h.
00101 {set(bitNumber, true);}
|
|
||||||||||||
|
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 }
|
|
|
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().
|
|
||||||||||||
|
Write an uint32 into the bit set (use with caution, no check).
Definition at line 176 of file bit_set.h. Referenced by NLMISC::CBitMemStream::readBits().
00176 { Array[i] = srcValue; }
|
|
|
Return size of the bit array.
Definition at line 70 of file bit_set.h. Referenced by NLMISC::CBitMemStream::pokeBits(), NLMISC::CBitMemStream::readBits(), serial(), and toString().
00071 {
00072 return NumBits;
00073 }
|
|
|
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().
|
|
|
Definition at line 182 of file bit_set.h. Referenced by CBitSet(), compareRestrict(), operator &=(), operator=(), operator==(), operator^=(), and operator|=(). |
|
|
Definition at line 184 of file bit_set.h. Referenced by allSet(), CBitSet(), clear(), flip(), operator &=(), operator=(), operator^=(), operator|=(), resize(), resizeNoReset(), and setAll(). |
|
|
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(). |
1.3.6