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.

187 lines
4.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1991, Microsoft Corporation.
  4. //
  5. // File: StateSet.hxx
  6. //
  7. // Contents:
  8. //
  9. // Classes:
  10. //
  11. // History: 01-20-92 KyleP Created
  12. //
  13. //--------------------------------------------------------------------------
  14. #include <pch.cxx>
  15. #pragma hdrstop
  16. #include "stateset.hxx"
  17. //+-------------------------------------------------------------------------
  18. //
  19. // Member: CStateSet::Clear, public
  20. //
  21. // Synopsis: Re-initializes a state set to empty.
  22. //
  23. // History: 20-Jan-92 KyleP Created
  24. //
  25. //--------------------------------------------------------------------------
  26. void CStateSet::Clear()
  27. {
  28. delete _puiRest;
  29. _cStates = 0;
  30. _cRest = 0;
  31. _puiRest = 0;
  32. _cuiRest = 0;
  33. }
  34. //+-------------------------------------------------------------------------
  35. //
  36. // Member: CStateSet::Add, public
  37. //
  38. // Synopsis: Adds a state to the state set.
  39. //
  40. // Arguments: [state] -- State to add.
  41. //
  42. // History: 20-Jan-92 KyleP Created
  43. //
  44. //--------------------------------------------------------------------------
  45. void CStateSet::Add( UINT state )
  46. {
  47. if ( IsMember( state ) )
  48. return;
  49. if ( _cStates < CStateSet_cFirst )
  50. {
  51. _auiFirst[ _cStates ] = state;
  52. }
  53. else
  54. {
  55. if ( _cRest >= _cuiRest )
  56. {
  57. UINT * oldpuiRest = _puiRest;
  58. UINT oldcuiRest = _cuiRest;
  59. _cuiRest += 10;
  60. _puiRest = new UINT [ _cuiRest ];
  61. memcpy( _puiRest, oldpuiRest, oldcuiRest * sizeof( UINT ) );
  62. delete oldpuiRest;
  63. }
  64. _puiRest[ _cRest ] = state;
  65. ++_cRest;
  66. }
  67. ++_cStates;
  68. }
  69. //+-------------------------------------------------------------------------
  70. //
  71. // Member: CStateSet::State, public
  72. //
  73. // Arguments: [iState] -- Index of state to return.
  74. //
  75. // Returns: The [iState] state in the state set.
  76. //
  77. // History: 20-Jan-92 KyleP Created
  78. //
  79. //--------------------------------------------------------------------------
  80. UINT CStateSet::State( UINT iState ) const
  81. {
  82. if ( iState <= CStateSet_cFirst )
  83. {
  84. return( _auiFirst[ iState - 1 ] );
  85. }
  86. else
  87. {
  88. return( _puiRest[ iState - CStateSet_cFirst - 1 ] );
  89. }
  90. }
  91. //+-------------------------------------------------------------------------
  92. //
  93. // Member: CStateSet::IsMember, public
  94. //
  95. // Arguments: [state] -- State to look for.
  96. //
  97. // Returns: TRUE if [state] is in set.
  98. //
  99. // History: 20-Jan-92 KyleP Created
  100. //
  101. //--------------------------------------------------------------------------
  102. BOOL CStateSet::IsMember( UINT state )
  103. {
  104. if ( _cStates == 0 )
  105. return( FALSE );
  106. //
  107. // Naive implementation. As long as state sets are small this
  108. // will work fine.
  109. //
  110. for ( int i = min( _cStates, CStateSet_cFirst) - 1;
  111. i >= 0;
  112. i-- )
  113. {
  114. if( _auiFirst[ i ] == state )
  115. return( TRUE );
  116. }
  117. if ( _cStates > CStateSet_cFirst )
  118. {
  119. for ( int i = _cStates - CStateSet_cFirst - 1;
  120. i >= 0;
  121. i-- )
  122. {
  123. if( _puiRest[ i ] == state )
  124. return( TRUE );
  125. }
  126. }
  127. return( FALSE );
  128. }
  129. //
  130. // Debug methods
  131. //
  132. int compare( void const * p1, void const * p2 )
  133. {
  134. return( *(UINT*)p1 - *(UINT*)p2 );
  135. }
  136. #if (CIDBG == 1)
  137. void CStateSet::Display()
  138. {
  139. #if 0
  140. //
  141. // Just to always print something close to sorted.
  142. //
  143. qsort( _auiFirst,
  144. ( _cStates < CStateSet_cFirst ) ? _cStates : CStateSet_cFirst,
  145. sizeof(UINT),
  146. compare );
  147. #endif
  148. vqDebugOut(( DEB_REGEX | DEB_NOCOMPNAME, "(" ));
  149. for (UINT i = 0; i < _cStates; i++)
  150. {
  151. if ( i <= CStateSet_cFirst-1 )
  152. vqDebugOut(( DEB_REGEX | DEB_NOCOMPNAME, " %u", _auiFirst[ i ] ));
  153. else
  154. vqDebugOut(( DEB_REGEX | DEB_NOCOMPNAME,
  155. " %u", _puiRest[ i - CStateSet_cFirst ] ));
  156. }
  157. vqDebugOut(( DEB_REGEX | DEB_NOCOMPNAME, " )" ));
  158. }
  159. #endif // (CIDBG == 1)