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 BITSETWORD should be
00030 // BITSETWORD is the smallest efficiently accessible memory
00031 // object.  BITSETWORD_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 BITSETWORD unsigned int
00043 const int  BITSETWORDSIZE = 8*sizeof(unsigned int);
00044 
00045 // BITSETWORDSIZE is no the same as sizeof(BITSETWORD).  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, BITSETWORDSIZE=8:
00059 
00060    m_index = 5 (need at least 5 BITSETWORDS 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/BITSETWORDSIZE = 4       , hence, we are in the fourth BITSETWORD.
00069    mask  = 35 - 4*BITSETWORDSIZE =  3    hence, we are in the third bit of the fourth word
00070 
00071    now, we can use the masks:
00072 static BITSETWORD trueMasks[BITSETWORDSIZE]; //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   int linearSize() const;
00133 
00134   void linearIn(const void* const inBuf);
00135 
00136   void linearOut(void* const a_outBuf) const;
00137 
00138   // not for public, used by memory tracker.
00139   static long int bytes;
00140   static long int peak;
00141 
00142 private:
00143   friend class BitSetIterator;
00144 
00145   BITSETWORD* m_bits;
00146   int   m_size;
00147   int   m_length;  //length of char array, not bit length
00148 
00149   static BITSETWORD trueMasks[BITSETWORDSIZE]; //10000000, 01000000, 00100000, ....
00150 };
00151 
00152 // somewhat slow random access. Fast iterating done
00153 // with BitSetIterator
00154 inline bool BitSet::operator[](int i) const
00155 {
00156   assert(i>=0);
00157   assert(i<m_size);
00158   int index = i/BITSETWORDSIZE;
00159   assert(index < m_length);
00160   int remainder = i-BITSETWORDSIZE*index;
00161   BITSETWORD tmp = m_bits[index] & trueMasks[remainder];
00162   return tmp > 0;
00163 }
00164 
00165 inline int BitSet::size() const
00166 {
00167   return m_size;
00168 }
00169 
00171 /* Iterate over bits in a BitSet.  return true if bit is 1
00172 
00173    example: 35 bits in bitset, BITSETWORDSIZE=8:
00174 
00175    currently at the 22nd bit: (bit # =21)
00176 
00177    |-m_index = 2---|
00178    +-------+-------+-------+-------+-------+
00179    1111111110001111111100011111111100011111
00180                    ^    ^   
00181        m_remainder |----| = 6      ^  ^
00182                                    |--| m_partialBits = 3
00183 
00184   returns: false for operator()
00185 .
00186 */   
00187 class BitSetIterator
00188 {
00189 public:
00191   BitSetIterator();
00192 
00194   BitSetIterator(const BitSet& bitset);
00195 
00196   // copy and assign should be fine
00197 
00199   bool operator()() const;
00200 
00202   bool ok() const;
00203 
00205   void operator++();
00206 
00208   void begin();
00209 
00211   void end();
00212 
00213 private:
00214   int m_index;
00215   int m_remainder;
00216   int m_length;
00217 
00218   int m_partialBits;
00219   const BitSet* m_bitset;
00220   static BitSet emptyBitSet;
00221 };
00222 
00223 inline
00224 BitSetIterator::BitSetIterator()
00225   : m_index(0), m_remainder(0), m_length(0), m_partialBits(0), m_bitset(&emptyBitSet)
00226 {;}
00227 
00228 inline
00229 BitSetIterator::BitSetIterator(const BitSet& a_bitset)
00230   :m_index(0), m_remainder(0), m_length(a_bitset.m_length - 1),
00231    m_partialBits(a_bitset.m_size - BITSETWORDSIZE*(a_bitset.m_length - 1)),
00232    m_bitset(&a_bitset)
00233 {
00234   if(m_partialBits == BITSETWORDSIZE)
00235     { 
00236       m_partialBits = 0;
00237       m_length++;
00238     }
00239 }
00240 
00241 inline
00242 bool BitSetIterator::operator()() const
00243 {
00244   return (m_bitset->m_bits[m_index] & BitSet::trueMasks[m_remainder] ) > 0;
00245 }
00246 
00247 inline
00248 bool BitSetIterator::ok() const
00249 {
00250   if(m_index < m_length) return true;
00251   if(m_remainder < m_partialBits) return true;
00252   return false;
00253 }
00254 
00255 inline
00256 void BitSetIterator::operator++()
00257 {
00258   ++m_remainder;
00259   if(m_remainder == BITSETWORDSIZE)
00260     {
00261       m_remainder = 0;
00262       ++m_index;
00263     }
00264 }
00265 
00266 #endif

Generated on Wed Jun 2 13:53:32 2004 for Chombo&INSwithParticles by doxygen 1.3.2