00001
00154 #ifndef INC_MI32_BITSET_H
00155 #define INC_MI32_BITSET_H
00156
00157 #ifndef INC_MI32_STDDEFNS_H
00158 #include <mi32/stddefns.h>
00159 #endif
00160
00161 #ifndef INC_MEMORY_H
00162 #include <memory.h>
00163 #define INC_MEMORY_H
00164 #endif
00165
00166
00167 #ifdef MISYSTEMDLL
00168 #define CLASSLIBEXPORT MI_DLLCLASSEXPORT
00169 #else
00170 #define CLASSLIBEXPORT MI_DLLCLASSIMPORT
00171 #endif
00172
00173
00174 #ifndef GENERATING_DOXYGEN_OUTPUT
00175 class BITSET_SHARED;
00176 #endif // GENERATING_DOXYGEN_OUTPUT
00177
00178
00179 enum SET_OPERATION {
00180 SET_OPERATION_Null = 0,
00181 SET_OPERATION_Clear = 1,
00182 SET_OPERATION_Set = 2,
00183 SET_OPERATION_Negate = 3,
00184 SET_OPERATION_Copy = 4,
00185 SET_OPERATION_Union = 5,
00186 SET_OPERATION_Add = SET_OPERATION_Union,
00187 SET_OPERATION_Subtract = 6,
00188 SET_OPERATION_ExUnion = 7,
00189 SET_OPERATION_Intersection = 8,
00190 };
00191
00192
00193
00196 class CLASSLIBEXPORT BITSET_UNOWNED {
00197
00198 protected:
00199 UINT8 *m_Array;
00200 UINT32 m_NumEntries;
00201 UINT32 m_ArraySize;
00202
00203 public:
00204
00205 class ITERATOR;
00206 friend class ITERATOR;
00207
00209 class CLASSLIBEXPORT ITERATOR {
00210 public:
00211
00213 ITERATOR ();
00214
00216 UINT32 operator* (
00217 ) const {
00218 return (m_SetItemIndex);
00219 }
00220
00222 operator UINT32 (
00223 ) const {
00224 return (m_SetItemIndex);
00225 }
00226
00228 ITERATOR& operator++ (
00229 );
00230
00232 bool operator== (
00233 const ITERATOR& rhs
00234 ) const {
00235 return (m_BitSet == rhs.m_BitSet && (m_SetItemIndex == rhs.m_SetItemIndex || (m_SetItemIndex >= m_BitSet->m_NumEntries && rhs.m_SetItemIndex >= m_BitSet->m_NumEntries)));
00236 }
00237
00239 bool operator!= (
00240 const ITERATOR& rhs
00241 ) const {
00242 return (!(*this == rhs));
00243 }
00244
00245 protected:
00246 #ifndef GENERATING_DOXYGEN_OUTPUT
00247 const BITSET_UNOWNED* GetBitSet () const { return m_BitSet; }
00248 #endif
00249
00250 private:
00251 #ifndef GENERATING_DOXYGEN_OUTPUT
00252 const BITSET_UNOWNED* m_BitSet;
00253 UINT32 m_SetItemIndex;
00254 UINT8 m_TestBit;
00255 UINT8 m_SkipByte;
00256
00257
00258 ITERATOR (
00259 const BITSET_UNOWNED* BitSet,
00260 bool testvalue
00261 );
00262
00263
00264 ITERATOR (
00265 const BITSET_UNOWNED* BitSet
00266 ) :
00267 m_BitSet(BitSet),
00268 m_SetItemIndex(BitSet->GetNumEntries()),
00269 m_TestBit(0),
00270 m_SkipByte(0)
00271 {
00272 }
00273
00274 friend class BITSET_UNOWNED;
00275 #endif // GENERATING_DOXYGEN_OUTPUT
00276 };
00277
00278
00280 BITSET_UNOWNED (
00281 );
00282
00284 BITSET_UNOWNED (
00285 UINT8 *set,
00286 UINT32 NumEntries
00287 );
00288
00290 virtual ~BITSET_UNOWNED (
00291 );
00292
00294 operator UINT8* (
00295 ) {
00296 return (m_Array);
00297 }
00298
00300 operator const UINT8* (
00301 ) const {
00302 return (m_Array);
00303 }
00304
00307 BITSET_UNOWNED& operator&= (
00308 const BITSET_UNOWNED& rhs
00309 );
00310
00313 BITSET_UNOWNED& operator|= (
00314 const BITSET_UNOWNED& rhs
00315 );
00316
00319 BITSET_UNOWNED& operator^= (
00320 const BITSET_UNOWNED& rhs
00321 );
00322
00324 virtual ERRVALUE Assign (
00325 UINT8 *set,
00326 UINT32 NumEntries
00327 );
00328
00331 ITERATOR Begin (
00332 bool value = true
00333 ) const {
00334 return (ITERATOR(this, value));
00335 }
00336
00338 void Clear (
00339 UINT32 posn
00340 ) {
00341 if (posn < m_NumEntries) {
00342 m_Array[posn/8] &= ~(1 << (posn & 7));
00343 }
00344 return;
00345 }
00346
00349 void ClearAll (
00350 ) {
00351 if (m_ArraySize > 0) memset(m_Array, 0, m_ArraySize);
00352 return;
00353 }
00354
00356 void ClearRange (
00357 UINT32 min,
00358 UINT32 max
00359 );
00360
00363 void CopyBit (
00364 UINT32 DestPosn,
00365 UINT32 SourcePosn
00366 );
00367
00370 INT32 CountValues (
00371 bool value = true
00372 ) const;
00373
00376 ITERATOR End (
00377 ) const {
00378 return (ITERATOR(this));
00379 }
00380
00383 bool GetBit (
00384 UINT32 posn
00385 ) const {
00386 if (posn >= m_NumEntries) return (false);
00387 return ((m_Array[posn/8] & (1 << (posn & 7))) != 0);
00388 }
00389
00391 UINT32 GetNumEntries (
00392 ) const {
00393 return (m_NumEntries);
00394 }
00395
00398 bool GetRange (
00399 UINT32 StartPosn,
00400 UINT32 MaxEnd,
00401 UINT32& EndPosn
00402 ) const;
00403
00405 bool HasEntries (
00406 ) const {
00407 return (m_NumEntries > 0);
00408 }
00409
00411 bool HasMultiple (
00412 bool value
00413 ) const;
00414
00416 bool HasValue (
00417 bool value
00418 ) const { return (Begin(value) != End()); }
00419
00421 void Invert (
00422 UINT32 posn
00423 ) {
00424 if (posn >= m_NumEntries) return;
00425 m_Array[posn/8] ^= 1 << (posn & 7);
00426 return;
00427 }
00428
00430 void InvertAll (
00431 );
00432
00434 void InvertRange (
00435 UINT32 min,
00436 UINT32 max
00437 );
00438
00451 bool IsEqual (
00452 const BITSET_UNOWNED& rhs
00453 ) const;
00454
00457 void Set (
00458 UINT32 posn
00459 ) {
00460 if (posn < m_NumEntries) {
00461 m_Array[posn/8] |= 1 << (posn & 7);
00462 }
00463 return;
00464 }
00465
00468 void SetAll (
00469 );
00470
00473 void SetBit (
00474 UINT32 posn,
00475 bool value
00476 ) {
00477 if (posn >= m_NumEntries) return;
00478 if (value) {
00479 m_Array[posn/8] |= 1 << (posn & 7);
00480 }
00481 else {
00482 m_Array[posn/8] &= ~(1 << (posn & 7));
00483 }
00484 return;
00485 }
00486
00488 void SetRange (
00489 UINT32 min,
00490 UINT32 max
00491 );
00492
00493 private:
00494 #ifndef GENERATING_DOXYGEN_OUTPUT
00495
00496 const UINT8& operator[](int) const;
00497 UINT8& operator[](int);
00498 #endif // GENERATING_DOXYGEN_OUTPUT
00499
00500 };
00501
00502
00504 class CLASSLIBEXPORT BITSET : public BITSET_UNOWNED {
00505
00506 public:
00507
00509 BITSET (
00510 );
00511
00513 BITSET (
00514 const BITSET& rhs
00515 );
00516
00518 BITSET (
00519 const BITSET_UNOWNED& rhs
00520 );
00521
00523 BITSET (
00524 UINT8 *set,
00525 UINT32 NumEntries
00526 );
00527
00529 virtual ~BITSET (
00530 );
00531
00533 BITSET& operator= (
00534 const BITSET& rhs
00535 );
00536
00538 BITSET& operator= (
00539 const BITSET_UNOWNED& rhs
00540 );
00541
00543 virtual ERRVALUE Assign (
00544 UINT8 *set,
00545 UINT32 NumEntries
00546 );
00547
00559 void Attach (
00560 UINT8*& items,
00561 int numitems
00562 );
00563
00572 UINT8* Detach (
00573 );
00574
00576 void Exchange (
00577 BITSET& other
00578 );
00579
00581 void Free (
00582 );
00583
00585 ERRVALUE Resize (
00586 UINT32 NumEntries
00587 );
00588
00589 private:
00590 #ifndef GENERATING_DOXYGEN_OUTPUT
00591 friend class BITSET_SHARED;
00592 #endif // GENERATING_DOXYGEN_OUTPUT
00593 };
00594
00595
00596
00598 class CLASSLIBEXPORT BITSET_SHARED : public BITSET_UNOWNED {
00599
00600 private:
00601 #ifndef GENERATING_DOXYGEN_OUTPUT
00602 struct BITSET_COUNT {
00603 mutable UINT32 m_RefCount;
00604 BITSET m_Bitset;
00605 BITSET_COUNT () : m_RefCount(1) {}
00606 BITSET_COUNT (const BITSET_UNOWNED& rhs) : m_RefCount(1), m_Bitset(rhs) {}
00607 BITSET_COUNT (UINT8 *set, UINT32 NumEntries) : m_RefCount(1), m_Bitset(set, NumEntries) {}
00608 ERRVALUE Resize (UINT32 NumEntries) {return (m_Bitset.Resize(NumEntries));}
00609 };
00610
00611 BITSET_COUNT* m_SharedBitset;
00612 #endif // GENERATING_DOXYGEN_OUTPUT
00613
00614 public:
00615
00617 BITSET_SHARED (
00618 );
00619
00621 BITSET_SHARED (
00622 const BITSET_SHARED& rhs
00623 );
00624
00626 BITSET_SHARED (
00627 const BITSET_UNOWNED& rhs
00628 );
00629
00631 BITSET_SHARED (
00632 UINT8 *set,
00633 UINT32 NumEntries
00634 );
00635
00637 virtual ~BITSET_SHARED (
00638 );
00639
00641 BITSET_SHARED& operator= (
00642 const BITSET_SHARED& rhs
00643 );
00644
00646 BITSET_SHARED& operator= (
00647 const BITSET_UNOWNED& rhs
00648 );
00649
00651 virtual ERRVALUE Assign (
00652 UINT8 *set,
00653 UINT32 NumEntries
00654 );
00655
00667 void Attach (
00668 UINT8*& items,
00669 int numitems
00670 );
00671
00672
00673
00675 void Free (
00676 );
00677
00679 ERRVALUE Resize (
00680 UINT32 NumEntries
00681 );
00682
00683 private:
00684 #ifndef GENERATING_DOXYGEN_OUTPUT
00685 void AttachPointers ();
00686 void DecrementCount ();
00687 #endif // GENERATING_DOXYGEN_OUTPUT
00688
00689 };
00690
00691
00692
00693 #undef CLASSLIBEXPORT
00694
00695 #endif // INC_MI32_BITSET_H