1 #ifndef __BITVECTOR_H__D40A115A_BC2F_4125_A068_D151EFFA2677
2 #define __BITVECTOR_H__D40A115A_BC2F_4125_A068_D151EFFA2677
12 typedef unsigned long BASEUNIT;
14 typedef std::vector<BASEUNIT> BASEUNITVECTOR;
16 BASEUNITVECTOR m_vecBaseUnits;
18 enum { BitsPerUnit =
sizeof(BASEUNIT) * 8 };
36 this->m_vecBaseUnits = that.m_vecBaseUnits ;
42 m_vecBaseUnits = std::move(that.m_vecBaseUnits);
47 inline bool IsBitSet(
const int nBitIndex)
const
49 return nBitIndex >=0 &&
50 nBitIndex < BitsPerUnit *(int)this->m_vecBaseUnits.size() &&
51 (this->m_vecBaseUnits[nBitIndex/BitsPerUnit] & ((BASEUNIT)1 << (nBitIndex % BitsPerUnit)));
54 inline bool operator[](
int nBitIndex)
const
56 _ASSERTE(nBitIndex >=0 && nBitIndex < BitsPerUnit *(
int)this->m_vecBaseUnits.size());
59 return (this->m_vecBaseUnits[nBitIndex/BitsPerUnit] & ((BASEUNIT)1 << (nBitIndex % BitsPerUnit))) ?
true :
false;
62 inline void SetTrue(
int nBitIndex)
67 inline void SetFalse(
int nBitIndex)
76 size_t nMax = other.m_vecBaseUnits.size();
80 while(m_vecBaseUnits.size() < nMax)
81 m_vecBaseUnits.push_back(0);
83 for(
size_t nBaseUnitIndex=0 ; nBaseUnitIndex < nMax ; ++nBaseUnitIndex)
84 m_vecBaseUnits[nBaseUnitIndex] |= other.m_vecBaseUnits[nBaseUnitIndex];
91 size_t nMax = min(m_vecBaseUnits.size(), other.m_vecBaseUnits.size());
93 for(
size_t nBaseUnitIndex=0; nBaseUnitIndex < nMax; ++nBaseUnitIndex)
95 m_vecBaseUnits[nBaseUnitIndex] &= (~other.m_vecBaseUnits[nBaseUnitIndex]);
100 inline CBitVector operator+(
int nBitIndex)
const
103 return tempVector += nBitIndex;
109 while(nBitIndex >= BitsPerUnit * (
int)this->m_vecBaseUnits.size())
110 this->m_vecBaseUnits.push_back(0);
112 this->m_vecBaseUnits[nBitIndex/BitsPerUnit] |= ((BASEUNIT)1 << (nBitIndex % BitsPerUnit));
117 inline CBitVector operator-(
int nBitIndex)
const
120 return tempVector -= nBitIndex;
126 while(nBitIndex >= BitsPerUnit * (
int)this->m_vecBaseUnits.size())
127 this->m_vecBaseUnits.push_back(0);
129 this->m_vecBaseUnits[nBitIndex/BitsPerUnit] &= ~(((BASEUNIT)1 << (nBitIndex % BitsPerUnit)));
135 inline int GetFirstTrueIndex()
const
137 return GetNextIndex(0,
true);
142 inline int GetNextTrueIndex(
int nCurrentIndex)
const
144 return GetNextIndex(nCurrentIndex,
true);
147 inline int GetFirstFalseIndex()
const
149 return GetNextIndex(0,
false);
152 inline int GetNextFalseIndex(
int nCurrentIndex)
const
154 return GetNextIndex(nCurrentIndex,
false);
160 return tempVector |= that;
168 while(m_vecBaseUnits.size() < that.m_vecBaseUnits.size())
169 m_vecBaseUnits.push_back(0);
171 BASEUNITVECTOR::const_iterator iterBegin = that.m_vecBaseUnits.begin();
172 BASEUNITVECTOR::const_iterator iterEnd = that.m_vecBaseUnits.end();
173 BASEUNITVECTOR::const_iterator iter = iterBegin;
175 for(
int nBaseUnitIndex=0 ; iter != iterEnd; ++iter, ++nBaseUnitIndex)
176 m_vecBaseUnits[nBaseUnitIndex] |= *iter;
184 return tempVector &= that;
192 while(m_vecBaseUnits.size() > that.m_vecBaseUnits.size())
193 m_vecBaseUnits.pop_back();
195 BASEUNITVECTOR::iterator iterBegin = m_vecBaseUnits.begin();
196 BASEUNITVECTOR::iterator iterEnd = m_vecBaseUnits.end();
197 BASEUNITVECTOR::iterator iter = iterBegin;
199 for(
int nBaseUnitIndex=0 ; iter != iterEnd; ++iter, ++nBaseUnitIndex)
200 *iter &= that.m_vecBaseUnits[nBaseUnitIndex];
206 inline bool Any()
const
208 for(
int i=0, nMax = (
int) this->m_vecBaseUnits.size(); i < nMax; ++i)
209 if(this->m_vecBaseUnits[i])
215 inline bool None()
const
223 for(
int i=0, nMax = (
int) this->m_vecBaseUnits.size(); i < nMax; ++i)
224 this->m_vecBaseUnits[i] = 0;
228 inline int BitCount()
const
230 BASEUNITVECTOR::const_iterator iter = m_vecBaseUnits.begin();
231 BASEUNITVECTOR::const_iterator iterEnd = m_vecBaseUnits.end();
235 for(
int nBaseUnitIndex=0 ; iter != iterEnd; ++iter, ++nBaseUnitIndex)
237 BASEUNIT nUnit = m_vecBaseUnits[nBaseUnitIndex];
250 inline bool operator==(
const CBitVector& other)
const
252 int nMyTrueIndex = GetFirstTrueIndex();
253 int nOtherTrueIndex = other.GetFirstTrueIndex();
255 while((nMyTrueIndex == nOtherTrueIndex) && (nMyTrueIndex!=-1) && (nOtherTrueIndex!=-1))
257 nMyTrueIndex = GetNextTrueIndex(nMyTrueIndex);
258 nOtherTrueIndex = other.GetNextTrueIndex(nOtherTrueIndex);
261 return nMyTrueIndex == nOtherTrueIndex;
264 inline bool operator!=(
const CBitVector& other)
const
266 return !(*
this == other);
270 int GetNextIndex(
int nCurrentIndex,
bool bVal)
const
274 if(nCurrentIndex <0 || nCurrentIndex >= BitsPerUnit * (
int) m_vecBaseUnits.size())
return -1;
276 unsigned int nBaseUnitIndex = (nCurrentIndex / BitsPerUnit);
277 unsigned int nBaseUnitOffset = (nCurrentIndex % BitsPerUnit);
279 BASEUNITVECTOR::const_iterator iterEnd = m_vecBaseUnits.end();
280 BASEUNITVECTOR::const_iterator iter = m_vecBaseUnits.begin() + nBaseUnitIndex;
282 for( ;iter != iterEnd; ++iter, ++nBaseUnitIndex, nBaseUnitOffset = 0)
284 BASEUNIT nVal = *iter;
286 unsigned int nOffset = nBaseUnitOffset;
292 if((nVal & 1) == bVal)
293 return nBaseUnitIndex * BitsPerUnit + nOffset;
304 OIL::StrUtils_Return_Type ToString()
const
306 OIL::StrUtils_Return_Type str(_T(
"{"));
308 for(
int i=0, nMax = BitsPerUnit * (
int) this->m_vecBaseUnits.size(); i < nMax; ++i)
309 if(this->IsBitSet(i))
310 str += OIL::ToString(i) + _T(
",");