bit_set.h

Go to the documentation of this file.
00001 
00005 /* Copyright, 2000 Nevrax Ltd.
00006  *
00007  * This file is part of NEVRAX NEL.
00008  * NEVRAX NEL is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012 
00013  * NEVRAX NEL is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * General Public License for more details.
00017 
00018  * You should have received a copy of the GNU General Public License
00019  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00021  * MA 02111-1307, USA.
00022  */
00023 
00024 #ifndef NL_BIT_SET_H
00025 #define NL_BIT_SET_H
00026 
00027 
00028 #include "types_nl.h"
00029 #include "stream.h"
00030 
00031 
00032 namespace   NLMISC
00033 {
00034 
00035 // Size in bit of base word.
00036 #define NL_BITLEN           (4*8)
00037 #define NL_BITLEN_SHIFT     5
00038 
00039 
00040 // ***************************************************************************
00047 class   CBitSet
00048 {
00049 public:
00050     /* ***********************************************
00051      *  WARNING: This Class/Method must be thread-safe (ctor/dtor/serial): no static access for instance
00052      *  It can be loaded/called through CAsyncFileManager for instance
00053      * ***********************************************/
00054 
00056 
00057     CBitSet();
00058     CBitSet(uint numBits);
00059     CBitSet(const CBitSet &bs);
00060     ~CBitSet();
00061     CBitSet &operator=(const CBitSet &bs);
00063 
00065 
00066 
00067     void    resize (uint numBits);
00069     void    resizeNoReset (uint numBits, bool value=false);
00071     void    clear();
00073     uint    size() const
00074     {
00075         return NumBits;
00076     }
00078     void    set(sint bitNumber, bool value)
00079     {
00080         nlassert(bitNumber>=0 && bitNumber<NumBits);
00081 
00082         uint    mask= bitNumber&(NL_BITLEN-1);
00083         mask= 1<<mask;
00084         if(value)
00085             Array[bitNumber >> NL_BITLEN_SHIFT]|= mask ;
00086         else
00087             Array[bitNumber >> NL_BITLEN_SHIFT]&= ~mask;
00088     }
00090     bool    get(sint bitNumber) const
00091     {
00092         nlassert(bitNumber>=0 && bitNumber<NumBits);
00093 
00094         uint    mask= bitNumber&(NL_BITLEN-1);
00095         mask= 1<<mask;
00096         return (Array[bitNumber >> NL_BITLEN_SHIFT] & mask) != 0;
00097     }
00099     bool    operator[](sint bitNumber) const
00100     {
00101         return get(bitNumber);
00102     }
00104     void    set(sint bitNumber) {set(bitNumber, true);}
00106     void    clear(sint bitNumber) {set(bitNumber, false);}
00108     void    setAll();
00110     void    clearAll();
00112 
00113 
00115 
00116 
00117     CBitSet operator~() const;
00122     CBitSet operator&(const CBitSet &bs) const;
00127     CBitSet operator|(const CBitSet &bs) const;
00132     CBitSet operator^(const CBitSet &bs) const;
00133 
00135     void    flip();
00140     CBitSet &operator&=(const CBitSet &bs);
00145     CBitSet &operator|=(const CBitSet &bs);
00150     CBitSet &operator^=(const CBitSet &bs);
00152 
00153 
00155 
00156 
00160     bool    compareRestrict(const CBitSet &bs) const;
00162     bool    operator==(const CBitSet &bs) const;
00164     bool    operator!=(const CBitSet &bs) const;
00166     bool    allSet();
00168     bool    allCleared();
00170 
00171 
00173     void    serial(NLMISC::IStream &f);
00174 
00176     const std::vector<uint32>& getVector() const { return Array; }
00177 
00179     void    setUint( uint32 srcValue, uint i ) { Array[i] = srcValue; }
00180 
00182     std::string toString() const;
00183 
00184 private:
00185     std::vector<uint32> Array;
00186     sint                NumBits;
00187     uint32              MaskLast;   // Mask for the last uint32.
00188 };
00189 
00190 
00191 }
00192 
00193 
00194 
00195 #endif // NL_BIT_SET_H
00196 
00197 /* End of bit_set.h */

Generated on Thu Jan 7 08:26:22 2010 for NeL by  doxygen 1.6.1