00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
00046 #include <assert.h>
00047 #include <iostream>
00048
00049 using std::ostream;
00050
00051
00052 class BitSetIterator;
00053
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
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
00103
00104 bool operator[](int i) const;
00105
00106
00107
00108
00109
00111 void setTrue(int i);
00112
00114 void setFalse(int i);
00115
00117
00120 bool isEmpty() const;
00121
00123
00126 bool isFull() const;
00127
00129 int size() const;
00130 static int initialize();
00131
00132
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;
00142
00143 static WORD trueMasks[WORDSIZE];
00144 };
00145
00146
00147
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
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 class BitSetIterator
00182 {
00183 public:
00185 BitSetIterator();
00186
00188 BitSetIterator(const BitSet& bitset);
00189
00190
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