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.

148 lines
2.3 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include "bitset.h"
  4. BitSet::BitSet(
  5. int cBits
  6. ) : m_pBuffer(NULL),
  7. m_cBits(0),
  8. m_cSet(0),
  9. m_cElements(0)
  10. {
  11. Initialize(cBits);
  12. }
  13. BitSet::BitSet(
  14. const BitSet& rhs
  15. ) : m_pBuffer(NULL),
  16. m_cBits(0),
  17. m_cSet(0),
  18. m_cElements(0)
  19. {
  20. *this = rhs;
  21. }
  22. BitSet&
  23. BitSet::operator = (
  24. const BitSet& rhs
  25. )
  26. {
  27. if (this != &rhs)
  28. {
  29. Initialize(rhs.m_cBits);
  30. for (int i = 0; i < m_cElements; i++)
  31. {
  32. m_pBuffer[i] = rhs.m_pBuffer[i];
  33. }
  34. }
  35. return *this;
  36. }
  37. BitSet::~BitSet(
  38. void
  39. )
  40. {
  41. delete [] m_pBuffer;
  42. }
  43. void
  44. BitSet::Initialize(
  45. int cBits
  46. )
  47. {
  48. delete [] m_pBuffer;
  49. m_cSet = 0;
  50. m_cBits = cBits;
  51. m_cElements = ElementInBuffer(m_cBits) + 1;
  52. m_pBuffer = new ELEMENT_TYPE[m_cElements];
  53. ClrAll();
  54. }
  55. bool
  56. BitSet::GetBitState(
  57. int iBit
  58. ) const
  59. {
  60. ValidateBitNumber(iBit);
  61. int iElement = ElementInBuffer(iBit);
  62. bool bBitState = false;
  63. ELEMENT_TYPE mask = MaskFromBit(iBit);
  64. bBitState = (*(m_pBuffer + iElement) & mask) != 0;
  65. return bBitState;
  66. }
  67. void
  68. BitSet::SetBitState(
  69. int iBit,
  70. bool bSet
  71. )
  72. {
  73. ValidateBitNumber(iBit);
  74. int iElement = ElementInBuffer(iBit);
  75. ELEMENT_TYPE mask = MaskFromBit(iBit);
  76. if (bSet)
  77. {
  78. *(m_pBuffer + iElement) |= mask;
  79. m_cSet++;
  80. }
  81. else
  82. {
  83. *(m_pBuffer + iElement) &= ~mask;
  84. m_cSet--;
  85. }
  86. }
  87. void
  88. BitSet::Complement(
  89. void
  90. )
  91. {
  92. for (int i = 0; i < m_cElements; i++)
  93. {
  94. m_pBuffer[i] = ~m_pBuffer[i];
  95. }
  96. m_cSet = CountClr();
  97. }
  98. #if DBG
  99. void
  100. BitSet::Dump(
  101. void
  102. ) const
  103. {
  104. #define BitSetOUT(s) *pszOut++ = TEXT('\r'); \
  105. *pszOut++ = TEXT('\n'); \
  106. *pszOut++ = TEXT('\0'); \
  107. OutputDebugString(s)
  108. TCHAR szOut[80];
  109. LPTSTR pszOut = szOut;
  110. const TCHAR szBits[] = TEXT("01");
  111. for (int i = 0; i < m_cBits; i++)
  112. {
  113. if (i % 8 == 0)
  114. {
  115. if (i % 32 == 0)
  116. {
  117. BitSetOUT(szOut);
  118. pszOut = szOut;
  119. }
  120. else
  121. *pszOut++ = TEXT(' ');
  122. }
  123. *pszOut++ = szBits[int(GetBitState(i))];
  124. }
  125. BitSetOUT(szOut);
  126. }
  127. #endif // DBG