arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

bitVector.h

Go to the documentation of this file.
00001 /************************************************************************
00002 * CVS version control block - do not edit manually
00003 *  $RCSfile: bitVector.h,v $
00004 *  $Revision: 1.1 $
00005 *  $Date: 2003/09/05 04:20:11 $
00006 *  $Source: /usr/sci/projects/chimera/cvsroot/arbeit/gutz/mathGutz/bitVector.h,v $
00007 *************************************************************************
00008 * Aaron Lefohn
00009 * Userid:       lefohn
00010 * Email:        lefohn@sci.utah.edu
00011 **************************************************************************
00012 * gutz::BitVector - An arbitrary length bit vector class
00013 *                                 - This class _owns_ its data
00014 *                                 - TODO: Implement shift operators
00015 *************************************************************************/
00016 
00017 #ifndef __BITVEC_H__
00018 #define __BITVEC_H__
00019 
00020 #include <assert.h>
00021 #include <math.h>
00022 #include <vector>
00023 #include <string>
00024 #include <iostream>
00025 
00026 #include <mathGutz.h>   // Gutz Files   
00027 #include <arrayGutz.h>
00028 
00029 namespace gutz {
00030 
00031 #ifdef WIN32
00032         #ifdef _DEBUG
00033                 // Disable the warning about template-explosion names being 
00034                 // longer than 255 chars...we really don't care.
00035                 #pragma warning(disable : 4786)
00036         #endif
00037 #endif
00038 
00039 // Typedefs
00040 class BitVector;
00041 typedef std::vector<BitVector>  VecBitVec;
00042 typedef std::vector<BitVector*> VecBitVecP;
00043 typedef std::vector<VecBitVec>  VecVecBitVec;
00044 
00045 extern const uint g_BITS_PER_WORD;
00046 extern const uint g_POW_OF_2_BITS_PER_WORD;
00047 
00048 inline uint bitNum2wordNum(const uint bitNum);
00049 inline uint bitMask(const uint bitNum);
00050 inline uint bits2Words(const uint numBits);
00051 inline uint fillWord(const bool initVal);
00052 inline uint getBinary(const uint word, const uint bitNum);
00053 
00054 class BitRef;//Forward Decl
00055 
00056 class BitVector
00057 {
00058 public:
00059   //Constructors
00060   BitVector(const int numBits=0);                   // Create uninitialized BitVec of 'size' 
00061   BitVector(const int numBits, const bool initVal); // Create BitVec of 'size', with all elts init'd to 'initVal' 
00062   BitVector(const std::string& bitstring);                          // Create BitVec from a 0/1 character string 
00063   BitVector( uint word );                                                       // Create BitVec from single word
00064   BitVector(const arrayw1ui& data );                            // Create BitVec from array of unsigned ints
00065   BitVector(const arrayw1ub& data );                            // Create BitVec from array of unsigned bytes
00066   BitVector(const BitVector& rhs);
00067   ~BitVector();
00068   
00069   //Pointer to the internal data. 
00070   // - Despite the danger, these must exist for efficient use when a low-level
00071   //   API is writing the bit vector and copying into a new BitVector is prohibitive.
00072   // - MAKE SURE the sizes all match up!!
00073   ubyte* dataub() const {return (ubyte*)(m_vec.data());}
00074   uint*  dataui() const {return (uint*)(m_vec.data());}
00075 
00076   const uint            size() const {return m_numBits;}       // Return length of bit vector 
00077   const uint            numWords() const {return m_vec.size();}
00078   inline BitRef         operator[] (const int i);           // Return ref to ith element of bit vector 
00079   inline const bool operator[] (const int i) const; // Return 'true' if ith elt is 1, 'false' if 0
00080   BitVector&            operator= (const BitVector& value);    // Copy a bit vector
00081   BitVector&            operator= (const uint valWord);            // Fill bitvector with word-sized value
00082   BitVector&            operator= (const bool value);          // Fill a bit_vector with value 
00083 
00084   std::vector<uint> trueBits() const; // Return a vector of bitIndices for all true bits in the bitVector
00085   void                          trueBits( arrayw1ui& trueIndices ) const;//Non-memory-allocating version of 'trueBits() const'
00086   uint                          numTrueBits() const;  // Returns number of true bits in vector
00087   
00088   //For all ops that need 2 BitVectors as args, both vecs MUST be same length. Assertion failure if not same size.
00089   friend BitVector operator~ (const BitVector&);                  // Boolean NOT on a bit vector 
00090   friend BitVector operator& (const BitVector&, const BitVector&);// Boolean AND operation on two bit vectors 
00091   friend BitVector operator| (const BitVector&, const BitVector&);// Boolean OR operation on two bit vectors 
00092   friend BitVector operator^ (const BitVector&, const BitVector&);// Boolean XOR operation on two bit vectors 
00093   friend BitVector operator- (const BitVector&, const BitVector&);// Difference set on two bit vectors 
00094 
00095   BitVector& not ();                                       // Boolean NOT on a bit vector in-place 
00096   BitVector& operator&= (const BitVector&);                // Boolean AND operation on two bit vectors in-place 
00097   BitVector& operator|= (const BitVector&);                // Boolean OR operation on two bit vectors in-place 
00098   BitVector& operator^= (const BitVector&);                // Boolean XOR operation on two bit vectors in-place 
00099   BitVector& operator-= (const BitVector&);                // Difference set on two bit vectors in-place 
00100 
00101   bool operator== (const bool u);                          // Do all elts in bitVec have value 'u'?
00102   bool operator!= (const bool u);                          // Check if no bit of vector has the value u 
00103   bool operator== (const BitVector& bv);                   // Compare two bit vectors for EQUALITY 
00104   bool operator!= (const BitVector& bv);                   // Compare two bit vectors for INEQUALITY 
00105 
00106   friend std::ostream &operator<<(std::ostream &, const BitVector& bv);
00107   
00108   /*bool operator< (const BitVector& bv);                  // Compare two bit vectors for LESS THAN (unsigned) 
00109   bool operator> (const BitVector& bv);                    // Compare two bit vectors for GREATER THAN (unsigned) 
00110   bool operator<= (const BitVector& bv);                   // Compare two bit vectors for LESS OR EQUAL (unsigned) 
00111   bool operator>= (const BitVector&bv);                    // Compare two bit vectors for GREATER OR EQUAL (unsigned) 
00112   */
00113 
00114 private:
00115   arrayo1ui m_vec;
00116   uint          m_numBits;
00117   
00118   const bool getBoolVal(const uint bitNum) const; // Return true if 'bitNum' is 1, false otherwise.  
00119 };
00120 
00121 // Allows reference to a particular bit in a bit vector
00122 // NOTE: This is meant to be an accessory class to BitVector
00123 //       and should not need to be used
00124 class BitRef
00125 {
00126 public:
00127   BitRef( uint* data, uint bit);// Constructor 
00128   BitRef( const BitRef& rhs);  // Copy Constructor
00129 
00130   BitRef& operator=(const BitRef& rhs);//Assignment operator
00131   bool    operator=(const bool u);     // Set value 
00132   bool    operator() () const;         // Test whether bit is TRUE or FALSE 
00133 
00134   friend std::ostream &operator<<(std::ostream &, const BitRef& bv);
00135   /*void operator&= (const bool u);// Perform boolean AND operation in-place 
00136   void operator|= (const bool u);  // Perform boolean OR operation in-place 
00137   void operator^= (const bool u);  // Perform boolean XOR operation in-place 
00138   */
00139 private:
00140   uint* m_wordPtr;// Ptr to a uint word that holds the bit of interest
00141   uint  m_mask;   // Mask for the bit represented by this instance
00142 };
00143 
00144 
00145 // Return a reference to the bitNum'th elt of bit vector 
00146 inline BitRef BitVector::operator[] (const int bitNum) {
00147   assert( (uint)bitNum < m_numBits );
00148         return BitRef( &m_vec[bitNum2wordNum(bitNum)], bitNum );
00149 }
00150 
00151 // Return 'true' if bitNum'th elt is 1, 'false' if 0 
00152 inline const bool BitVector::operator[] (const int bitNum) const { 
00153   assert( (uint)bitNum < m_numBits );
00154   return getBoolVal(bitNum);
00155 }
00156 
00157 // Return true if 'bitNum' is 1, false otherwise.
00158 // 'bitNum' is bit index in bitVector
00159 inline const bool BitVector::getBoolVal(const uint bitNum) const {
00160   uint wordNum = bitNum2wordNum(bitNum);// In which array elt is the i'th bit?
00161   uint mask    = bitMask(bitNum);       // Create mask to get the i'th bit
00162   uint result  = m_vec[wordNum] & mask;
00163   return (result > 0);
00164 }
00165 
00166 // Given an array of uint words, in which array elt is the bit 'bitNum'.
00167 inline uint bitNum2wordNum(const uint bitNum) {
00168   return (bitNum >> g_POW_OF_2_BITS_PER_WORD);
00169 }
00170 
00171 // Return a uint word mask for the (bitNum % sizeof(uint))'th bit.
00172 inline uint bitMask(const uint bitNum) {
00173   uint bit = modPowOf2( bitNum, g_POW_OF_2_BITS_PER_WORD );
00174   return 1 << (g_BITS_PER_WORD - bit - 1);
00175 }
00176 
00177 // Returns the number of uint words necessary to represent 'numBits'
00178 inline uint bits2Words(const uint numBits) {
00179   return uint( ceil(numBits/(float)g_BITS_PER_WORD) );
00180 }
00181 
00182 // Returns a uint word of either all 1's or 0's depending on 'initVal'
00183 inline uint fillWord(const bool initVal) {
00184   return initVal ? UINT_MAX : 0;
00185 }
00186 
00187 //Return the value of the 'bitNum'th bit in 'word' (0 or 1)
00188 inline uint getBinary(const uint word, const uint bitNum) {
00189   return (uint)((word & (1<<bitNum)) >> bitNum);
00190 }
00191 
00192 } // End of namespace gutz
00193 
00194 #endif
00195 
00196 
00197 

Send questions, comments, and bug reports to:
jmk