Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.8 KiB

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