bitset.h
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 5k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #ifndef _INC_CSCVIEW_BITSET_H
  2. #define _INC_CSCVIEW_BITSET_H
  3. //
  4. // Macros & Constants
  5. //
  6. // ELEMENT_TYPE
  7. //      This macro defines the type of each "element" in the bit buffer.
  8. //      This type must be an integral type that can be used as an operand
  9. //      in bitwise expressions.  If it is changed, the macros
  10. //      ELEMENT_BITNUM_MASK and ELEMENT_SHIFT must also be changed as
  11. //      follows:
  12. //
  13. //      ELEMENT_TYPE     ELEMENT_BITNUM_MASK     ELEMENT_SHIFT
  14. //      ---------------- ----------------------- -------------------
  15. //      BYTE             0x00000007              3
  16. //      WORD             0x0000000F              4
  17. //      DWORD            0x0000001F              5
  18. //
  19. // ELEMENT_BITNUM_MASK
  20. //      This macro defines the mask used to extract the number of the
  21. //      bit in the array element from the number of the bit being accessed.
  22. //
  23. // ELEMENT_SHIFT
  24. //      This macro defines the number of bits to right shift the bit number
  25. //      to obtain the number of the array element.
  26. //
  27. //
  28. #define ELEMENT_TYPE            BYTE
  29. const int ELEMENT_BITNUM_MASK = 0x00000007;  
  30. const int ELEMENT_SHIFT       = 3;
  31. //
  32. // Forward declaration.
  33. //
  34. class BitSet;
  35. //
  36. // This is a "helper" class that aids BitSet::operator[].  So that 
  37. // BitSet::operator[] functions properly for both lvalue and rvalue
  38. // conditions, it returns a "Bit" object.  The bit object retains the
  39. // bit number and a pointer to the "owner" BitSet object.  It also 
  40. // overloads the bool conversion operator and assignment operator.
  41. // Knowing the bit number and the owner BitSet, the class can set or 
  42. // obtain the value of the bit in the BitSet's array.
  43. //
  44. class Bit 
  45. {
  46.     public:
  47.         Bit(int iBit, BitSet *pOwnerSet = NULL) throw()
  48.             : m_iBit(iBit),
  49.               m_pOwnerSet(pOwnerSet) { }
  50.         Bit(Bit& bit) throw()
  51.             : m_iBit(bit.m_iBit),
  52.               m_pOwnerSet(bit.m_pOwnerSet) { }
  53.         //
  54.         // Convert value of bit to a bool.
  55.         //
  56.         operator bool () const;
  57.         //
  58.         // Set bit to a bool value.
  59.         //
  60.         bool operator = (bool bState);
  61.     private:
  62.         DWORD m_iBit;        // Number of bit in BitSet [0 to n-1]
  63.         BitSet *m_pOwnerSet; // Ptr to BitSet object.
  64. };
  65. class BitSet 
  66. {
  67.     public:
  68.         BitSet(int cBits = 1);
  69.         ~BitSet(void) throw();
  70.         BitSet(const BitSet& rhs);
  71.         BitSet& operator = (const BitSet& rhs);
  72.         void Initialize(int cBits);
  73.         int Count(void) const throw()
  74.             { return m_cBits; }
  75.         int CountSet(void) const throw()
  76.             { return m_cSet; }
  77.         int CountClr(void) const throw()
  78.             { return m_cBits - m_cSet; }
  79.         bool IsSet(int iBit) const throw()
  80.             { return GetBitState(iBit); }
  81.         bool IsClr(int iBit) const throw()
  82.             { return !GetBitState(iBit); }
  83.         void Complement(void) throw();
  84.         //
  85.         // SetBitState and GetBitState are the fastest
  86.         // ways to alter or retrieve the state of a bit.
  87.         //
  88.         void SetBitState(int iBit, bool bSet);
  89.         bool GetBitState(int iBit) const;
  90.         //
  91.         // Set and Clr are the next fastest ways to alter
  92.         // or retrieve the state of a bit.
  93.         //
  94.         void Set(int iBit)
  95.             { SetBitState(iBit, TRUE); }
  96.         void Clr(int iBit)
  97.             { SetBitState(iBit, FALSE); }
  98.         //
  99.         // Using the subscript operator is the slowest way
  100.         // to alter/retrieve the state of a bit.
  101.         //
  102.         Bit operator [] (int iBit)
  103.             { ValidateBitNumber(iBit); return Bit(iBit, this); }
  104.         void ClrAll(void) throw()
  105.             { ZeroMemory(m_pBuffer, sizeof(ELEMENT_TYPE) * m_cElements); m_cSet = 0; }
  106.         void SetAll(void) throw()
  107.             { FillMemory(m_pBuffer, sizeof(ELEMENT_TYPE) * m_cElements, (BYTE)0xFF); m_cSet = m_cBits; }
  108.         void Dump(void) const;
  109.     private:
  110.         ELEMENT_TYPE *m_pBuffer;    // Array of elements.
  111.         int m_cBits;     // Bits supported in set.
  112.         int m_cSet;      // Number of bits set to '1'.
  113.         int m_cElements; // Number of elements in array.
  114.         //
  115.         // Inline functions for calculating bit/element positions in array.
  116.         //
  117.         DWORD BitInElement(int iBit) const throw()
  118.             { return (iBit & ELEMENT_BITNUM_MASK); }
  119.         DWORD ElementInBuffer(int iBit) const throw()
  120.             { return (iBit >> ELEMENT_SHIFT); }
  121.         ELEMENT_TYPE MaskFromBit(int iBit) const throw()
  122.             { return 1 << BitInElement(iBit); }
  123.         void ValidateBitNumber(int iBit) const
  124.             { 
  125.                 if (iBit >= m_cBits)
  126.                     throw CException(ERROR_INVALID_INDEX);
  127.             }
  128.         friend class Bit;
  129. };
  130. inline
  131. Bit::operator bool () const
  132. {
  133. return m_pOwnerSet->GetBitState(m_iBit); 
  134. }
  135. inline bool 
  136. Bit::operator = (bool bState)
  137. {
  138.     m_pOwnerSet->SetBitState(m_iBit, bState);
  139. return bState;
  140. }
  141. #endif // _INC_CSCVIEW_BITSET_H