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.

241 lines
6.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1991, Microsoft Corporation.
  4. //
  5. // File: XlatStat.cxx
  6. //
  7. // Contents: State translation class.
  8. //
  9. // Classes: CXlatState
  10. //
  11. // History: 01-20-92 KyleP Created
  12. // 03-11-97 arunk Modified for regex lib
  13. //
  14. //--------------------------------------------------------------------------
  15. // Local includes:
  16. #include <xlatstat.hxx>
  17. #include <stateset.hxx>
  18. //+-------------------------------------------------------------------------
  19. //
  20. // Member: CXlatState::CXlatState, public
  21. //
  22. // Synopsis: Initialize state translator (no states)
  23. //
  24. // Arguments: [MaxState] -- Largest ordinal of any state that may
  25. // appear in an equivalence class.
  26. //
  27. // History: 20-Jan-92 KyleP Created
  28. //
  29. //--------------------------------------------------------------------------
  30. CXlatState::CXlatState( UINT MaxState )
  31. : _cUsed( 0 ),
  32. _pulStates( 0 ),
  33. _cStates( 0 )
  34. {
  35. //
  36. // Compute the number of DWords / state, assuring at least 1 dword is
  37. // always allocated.
  38. //
  39. _StateSize = (MaxState + sizeof(ULONG) * 8 ) / (sizeof( ULONG ) * 8);
  40. _Realloc();
  41. };
  42. //+-------------------------------------------------------------------------
  43. //
  44. // Member: CXlatState::~CXlatState, public
  45. //
  46. // Synopsis: Destroys class.
  47. //
  48. // History: 20-Jan-92 KyleP Created
  49. //
  50. //--------------------------------------------------------------------------
  51. CXlatState::~CXlatState()
  52. {
  53. delete _pulStates;
  54. }
  55. //+-------------------------------------------------------------------------
  56. //
  57. // Member: CXlatState::XlatToOne, public
  58. //
  59. // Synopsis: Maps a state set to its single state equivalent. If
  60. // there is no mapping, then one is created.
  61. //
  62. // Arguments: [ss] -- State set to search for.
  63. //
  64. // Returns: The single state equivalent.
  65. //
  66. // History: 20-Jun-92 KyleP Created
  67. //
  68. //--------------------------------------------------------------------------
  69. UINT CXlatState::XlatToOne( CStateSet const & ss )
  70. {
  71. UINT state = _Search( ss );
  72. if ( state == 0 )
  73. {
  74. state = _Create( ss );
  75. }
  76. return( state );
  77. }
  78. //+-------------------------------------------------------------------------
  79. //
  80. // Member: CXlatState::XlatToMany, public
  81. //
  82. // Synopsis: Maps a state to its matching state set.
  83. //
  84. // Arguments: [iState] -- State to map.
  85. // [ss] -- On return contains the mapped state set.
  86. //
  87. // History: 20-Jan-92 KyleP Created
  88. //
  89. //--------------------------------------------------------------------------
  90. void CXlatState::XlatToMany( UINT iState, CStateSet & ss )
  91. {
  92. if ( iState <= _cUsed )
  93. {
  94. ULONG * pState = _pulStates + iState * _StateSize;
  95. for ( int i = _StateSize * sizeof(ULONG) * 8 - 1; i >= 0; i-- )
  96. {
  97. if ( pState[i / (sizeof(ULONG) * 8) ] &
  98. ( 1L << i % (sizeof(ULONG) * 8 ) ) )
  99. {
  100. ss.Add( i+1 );
  101. }
  102. }
  103. }
  104. }
  105. //+-------------------------------------------------------------------------
  106. //
  107. // Member: CXlatState::_Search, private
  108. //
  109. // Arguments: [ss] -- State set to search for.
  110. //
  111. // Returns: Single state mapping for [ss] or 0 if none.
  112. //
  113. // History: 20-Jan-92 KyleP Created
  114. //
  115. // Notes: The first state set is used as temp space.
  116. //
  117. //--------------------------------------------------------------------------
  118. UINT CXlatState::_Search( CStateSet const & ss )
  119. {
  120. memset( _pulStates, 0, _StateSize * sizeof(ULONG) );
  121. _BuildState( _pulStates, ss );
  122. UINT cState = 1;
  123. while ( cState <= _cUsed )
  124. {
  125. if ( memcmp( _pulStates + cState * _StateSize,
  126. _pulStates,
  127. _StateSize * sizeof(ULONG) ) == 0 )
  128. {
  129. return( cState );
  130. }
  131. else
  132. {
  133. ++cState;
  134. }
  135. }
  136. return( 0 );
  137. }
  138. //+-------------------------------------------------------------------------
  139. //
  140. // Member: CXlatState::_Create, private
  141. //
  142. // Synopsis: Adds new state set to array.
  143. //
  144. // Arguments: [ss] -- State set to add.
  145. //
  146. // Returns: Single state mapping for [ss]
  147. //
  148. // History: 20-Jan-92 KyleP Created
  149. //
  150. //--------------------------------------------------------------------------
  151. UINT CXlatState::_Create( CStateSet const & ss )
  152. {
  153. //
  154. // _cStates-1 because the first state is temp space.
  155. //
  156. if ( _cStates-1 == _cUsed )
  157. _Realloc();
  158. ++_cUsed;
  159. _BuildState( _pulStates + _cUsed * _StateSize, ss );
  160. return( _cUsed );
  161. }
  162. //+-------------------------------------------------------------------------
  163. //
  164. // Member: CXlatState::_BuildState, private
  165. //
  166. // Synopsis: Formats state set.
  167. //
  168. // Arguments: [ss] -- Format state set...
  169. // [pState] -- into this memory.
  170. //
  171. // History: 20-Jan-92 KyleP Created
  172. //
  173. //--------------------------------------------------------------------------
  174. void CXlatState::_BuildState( ULONG * pState, CStateSet const & ss )
  175. {
  176. for ( UINT i = ss.Count(); i > 0; i-- )
  177. {
  178. UINT StateNum = ss.State( i ) - 1;
  179. pState[ StateNum / (sizeof(ULONG) * 8) ] |=
  180. 1L << StateNum % (sizeof(ULONG) * 8);
  181. }
  182. }
  183. //+-------------------------------------------------------------------------
  184. //
  185. // Member: CXlatState::_Realloc, private
  186. //
  187. // Synopsis: Grows state set array.
  188. //
  189. // History: 20-Jan-92 KyleP Created
  190. //
  191. // Notes: Grows linearly, since the number of state sets is likely
  192. // to be small and we don't want to waste space.
  193. //
  194. //--------------------------------------------------------------------------
  195. void CXlatState::_Realloc()
  196. {
  197. UINT oldcStates = _cStates;
  198. ULONG * oldpulStates = _pulStates;
  199. _cStates += 10;
  200. _pulStates = new ULONG [ _cStates * _StateSize ];
  201. memcpy( _pulStates,
  202. oldpulStates,
  203. oldcStates * _StateSize * sizeof( ULONG ) );
  204. memset( _pulStates + oldcStates * _StateSize,
  205. 0,
  206. (_cStates - oldcStates) * _StateSize * sizeof(ULONG) );
  207. delete [] oldpulStates;
  208. }