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

BitSet.H

Go to the documentation of this file.
00001 /* _______              __
00002   / ___/ /  ___  __ _  / /  ___
00003  / /__/ _ \/ _ \/  ' \/ _ \/ _ \
00004  \___/_//_/\___/_/_/_/_.__/\___/ 
00005 */
00006 //
00007 // This software is copyright (C) by the Lawrence Berkeley
00008 // National Laboratory.  Permission is granted to reproduce
00009 // this software for non-commercial purposes provided that
00010 // this notice is left intact.
00011 // 
00012 // It is acknowledged that the U.S. Government has rights to
00013 // this software under Contract DE-AC03-765F00098 between
00014 // the U.S.  Department of Energy and the University of
00015 // California.
00016 //
00017 // This software is provided as a professional and academic
00018 // contribution for joint exchange. Thus it is experimental,
00019 // is provided ``as is'', with no warranties of any kind
00020 // whatsoever, no support, no promise of updates, or printed
00021 // documentation. By using this software, you acknowledge
00022 // that the Lawrence Berkeley National Laboratory and
00023 // Regents of the University of California shall have no
00024 // liability with respect to the infringement of other
00025 // copyrights by any part of this software.
00026 //
00027 
00028 
00029 // arch dependant setting for what a WORD should be
00030 // WORD is the smallest efficiently accessible memory
00031 // object.  WORD_BITS is the size, in bits, of this object
00032 //
00033 //  Later I can make this object use a Pool, and we can
00034 // avoid all the memory system call overhead for creation and
00035 // use placement new.
00036 //
00037 
00038 #ifndef BITSET_H
00039 #define BITSET_H
00040 
00041 
00042 #define WORD unsigned int
00043 const int  WORDSIZE = 8*sizeof(unsigned int);
00044 
00045 // WORDSIZE is no the same as sizeof(WORD).  This is the number of bits, not chars
00046 #include <assert.h>
00047 #include <iostream>
00048 
00049 using std::ostream;
00050 
00051 
00052 class BitSetIterator;
00053 
00055 /* stores a contiguous memory chunk and represents true with a 1 bit
00056    and false with a 0 bit. 
00057 
00058    example: 35 bits, set to true, WORDSIZE=8:
00059 
00060    m_index = 5 (need at least 5 WORDS to hold that many bits)
00061    m_size  = 35
00062 
00063    +-------+-------+-------+-------+-------+
00064    1111111111111111111111111111111111111111
00065 
00066    now, set bit 35 to 0
00067 
00068    index = 35/WORDSIZE = 4       , hence, we are in the fourth WORD.
00069    mask  = 35 - 4*WORDSIZE =  3    hence, we are in the third bit of the fourth word
00070 
00071    now, we can use the masks:
00072 static WORD trueMasks[WORDSIZE]; //10000000, 01000000, 00100000, ....
00073 
00074    word[index] = word[index] & ~trueMasks[mask]  (~trueMasks[3] = 11101111)
00075 
00076    now we have:
00077 
00078    +-------+-------+-------+-------+-------+
00079    1111111111111111111111111111111111101111
00080 */
00081 class BitSet
00082 {
00083 public:
00085   BitSet();
00086 
00088   BitSet(int bits, bool init);
00089 
00091   void define(int bits, bool init);
00092 
00094   BitSet(const BitSet& rhs);
00095 
00097   BitSet& operator=(const BitSet& rhs);
00098 
00100   ~BitSet();
00101 
00102   // somewhat slow random access. Fast iterating done
00103   // with BitSetIterator
00104   bool operator[](int i) const;
00105 
00106   /* 
00107       logic operations 
00108   */
00109 
00111   void setTrue(int i); // set bit to 1
00112 
00114   void setFalse(int i); // set bit to 0
00115 
00117 
00120   bool isEmpty() const;
00121 
00123 
00126   bool isFull() const;
00127 
00129   int size() const;
00130   static int initialize();
00131 
00132   // not for public, used by memory tracker.
00133   static long int bytes;
00134   static long int peak;
00135 
00136 private:
00137   friend class BitSetIterator;
00138 
00139   WORD* m_bits;
00140   int   m_size;
00141   int   m_length;  //length of char array, not bit length
00142 
00143   static WORD trueMasks[WORDSIZE]; //10000000, 01000000, 00100000, ....
00144 };
00145 
00146 // somewhat slow random access. Fast iterating done
00147 // with BitSetIterator
00148 inline bool BitSet::operator[](int i) const
00149 {
00150   assert(i>=0);
00151   assert(i<m_size);
00152   int index = i/WORDSIZE;
00153   assert(index < m_length);
00154   int remainder = i-WORDSIZE*index;
00155   WORD tmp = m_bits[index] & trueMasks[remainder];
00156   return tmp > 0;
00157 }
00158 
00159 inline int BitSet::size() const
00160 {
00161   return m_size;
00162 }
00163 
00165 /* Iterate over bits in a BitSet.  return true if bit is 1
00166 
00167    example: 35 bits in bitset, WORDSIZE=8:
00168 
00169    currently at the 22nd bit: (bit # =21)
00170 
00171    |-m_index = 2---|
00172    +-------+-------+-------+-------+-------+
00173    1111111110001111111100011111111100011111
00174                    ^    ^   
00175        m_remainder |----| = 6      ^  ^
00176                                    |--| m_partialBits = 3
00177 
00178   returns: false for operator()
00179 .
00180 */   
00181 class BitSetIterator
00182 {
00183 public:
00185   BitSetIterator();
00186 
00188   BitSetIterator(const BitSet& bitset);
00189 
00190   // copy and assign should be fine
00191 
00193   bool operator()() const;
00194 
00196   bool ok() const;
00197 
00199   void operator++();
00200 
00202   void begin();
00203 
00205   void end();
00206 
00207 private:
00208   int m_index;
00209   int m_remainder;
00210   int m_length;
00211 
00212   int m_partialBits;
00213   const BitSet* m_bitset;
00214   static BitSet emptyBitSet;
00215 };
00216 
00217 inline
00218 BitSetIterator::BitSetIterator()
00219   : m_index(0), m_remainder(0), m_length(0), m_partialBits(0), m_bitset(&emptyBitSet)
00220 {;}
00221 
00222 inline
00223 BitSetIterator::BitSetIterator(const BitSet& a_bitset)
00224   :m_index(0), m_remainder(0), m_length(a_bitset.m_length - 1),
00225    m_partialBits(a_bitset.m_size - WORDSIZE*(a_bitset.m_length - 1)),
00226    m_bitset(&a_bitset)
00227 {
00228   if(m_partialBits == WORDSIZE)
00229     { 
00230       m_partialBits = 0;
00231       m_length++;
00232     }
00233 }
00234 
00235 inline
00236 bool BitSetIterator::operator()() const
00237 {
00238   return (m_bitset->m_bits[m_index] & BitSet::trueMasks[m_remainder] ) > 0;
00239 }
00240 
00241 inline
00242 bool BitSetIterator::ok() const
00243 {
00244   if(m_index < m_length) return true;
00245   if(m_remainder < m_partialBits) return true;
00246   return false;
00247 }
00248 
00249 inline
00250 void BitSetIterator::operator++()
00251 {
00252   ++m_remainder;
00253   if(m_remainder == WORDSIZE)
00254     {
00255       m_remainder = 0;
00256       ++m_index;
00257     }
00258 }
00259 
00260 #endif

Generated on Wed Apr 30 18:14:22 2003 for Chombo&INS by doxygen1.2.16