Leaked source code of windows server 2003
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.

183 lines
4.0 KiB

  1. // Copyright (c) 1996 - 1999 Microsoft Corporation. All Rights Reserved.
  2. #ifndef __IntSet_h__
  3. #define __IntSet_h__
  4. #include <stdlib.h>
  5. #include <memory.h>
  6. // To test this class, copy this file into a .cpp,
  7. // load into Dev Studio, define self_test and run.
  8. #ifdef self_test
  9. #include <iostream.h>
  10. #include <assert.h>
  11. #define ASSERT assert
  12. #endif
  13. // Class denoting a set of integers within the range [min..max]
  14. // A number may be added to the set by +=,
  15. // removed from the set by -=,
  16. // and tested by [number].
  17. class IntSet
  18. {
  19. protected:
  20. const int m_min, m_max;
  21. unsigned * m_pBits;
  22. enum { bit_width = 8 * sizeof(unsigned), low_mask = bit_width - 1, high_shift = 5 };
  23. public:
  24. ~IntSet();
  25. IntSet( int min, int max );
  26. int Max() const { return m_max; }
  27. int Min() const { return m_min; }
  28. void Clear()
  29. {
  30. memset( m_pBits, 0, ((m_max - m_min) / bit_width + 1) * sizeof(unsigned) );
  31. }
  32. void operator += ( int elmt )
  33. {
  34. ASSERT( elmt >= m_min && elmt <= m_max );
  35. elmt -= m_min;
  36. m_pBits[elmt >> high_shift] |= 1 << (elmt & low_mask);
  37. }
  38. void operator -= ( int elmt )
  39. {
  40. ASSERT( elmt >= m_min && elmt <= m_max );
  41. elmt -= m_min;
  42. m_pBits[elmt >> high_shift] &= ~(1 << (elmt & low_mask));
  43. }
  44. int operator[] ( int elmt ) const
  45. {
  46. if (elmt < m_min) return 0;
  47. if (elmt > m_max) return 0;
  48. elmt -= m_min;
  49. return (m_pBits[elmt >> high_shift] >> (elmt & low_mask)) & 1;
  50. }
  51. #ifdef self_test
  52. void COut() const
  53. {
  54. for (int i = m_min; i <= m_max; i++) cout << ( (*this)[i] ? 'Y' : 'n' );
  55. cout << endl;
  56. }
  57. #endif
  58. };
  59. inline IntSet::~IntSet()
  60. {
  61. delete [] m_pBits;
  62. }
  63. inline IntSet::IntSet( int min, int max )
  64. : m_min(min), m_max(max)
  65. {
  66. m_pBits = new unsigned [(max-min)/bit_width + 1];
  67. Clear();
  68. }
  69. // Variation that can only hold objects [0..31].
  70. // An initial value can be specified on creation.
  71. // Appart from its construction, this object should
  72. // look like an IntSet.
  73. class IntSmallSet
  74. {
  75. protected:
  76. enum { m_min = 0, m_max = 63 };
  77. unsigned __int64 m_Bits;
  78. public:
  79. static const __int64 One;
  80. IntSmallSet( unsigned __int64 init = 0 )
  81. : m_Bits(init)
  82. {}
  83. int Max() const { return m_max; }
  84. int Min() const { return m_min; }
  85. void Clear()
  86. {
  87. m_Bits = 0;
  88. }
  89. void operator += ( int elmt )
  90. {
  91. ASSERT( elmt >= m_min && elmt <= m_max );
  92. elmt -= m_min;
  93. m_Bits |= One << elmt;
  94. }
  95. void operator -= ( int elmt )
  96. {
  97. ASSERT( elmt >= m_min && elmt <= m_max );
  98. elmt -= m_min;
  99. m_Bits &= ~(One << elmt);
  100. }
  101. int operator[] ( int elmt ) const
  102. {
  103. if (elmt < m_min) return 0;
  104. if (elmt > m_max) return 0;
  105. elmt -= m_min;
  106. // Eliminate compiler warning: The conditional expression is an
  107. // _int64, convert it to an int
  108. return ((m_Bits >> elmt) & 1)? 1 : 0;
  109. }
  110. IntSmallSet operator | ( const IntSmallSet & s ) const
  111. { return IntSmallSet( m_Bits | s.m_Bits ); }
  112. IntSmallSet operator & ( const IntSmallSet & s ) const
  113. { return IntSmallSet( m_Bits & s.m_Bits ); }
  114. IntSmallSet operator ~ () const
  115. { return IntSmallSet( ~m_Bits ); }
  116. int operator == ( const IntSmallSet & s ) const
  117. { return m_Bits == s.m_Bits; }
  118. int operator == ( unsigned i ) const
  119. { return m_Bits == i; }
  120. #ifdef self_test
  121. void COut() const
  122. {
  123. for (int i = m_min; i <= m_max; i++) cout << ( (*this)[i] ? 'Y' : 'n' );
  124. cout << endl;
  125. }
  126. #endif
  127. };
  128. #ifdef self_test
  129. void main()
  130. {
  131. // IntSet Set1(-30,+102);
  132. IntSmallSet Set1;
  133. int i;
  134. for ( i = Set1.Min(); i <= Set1.Max(); i++ )
  135. {
  136. Set1 += i;
  137. Set1.COut();
  138. }
  139. for ( i = Set1.Max(); i >= Set1.Min(); i-- )
  140. {
  141. Set1 -= i;
  142. Set1.COut();
  143. }
  144. }
  145. #endif
  146. #endif