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.

290 lines
7.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1991, Microsoft Corporation.
  4. //
  5. // File: State.cxx
  6. //
  7. // Contents: Finite automata state classes
  8. //
  9. // Classes:
  10. //
  11. // History: 01-21-92 KyleP Created
  12. // 03-11-97 arunk Modified for Kessel
  13. //--------------------------------------------------------------------------
  14. // Local includes:
  15. #include <state.hxx>
  16. #include <stateset.hxx>
  17. #include <string.h>
  18. //+-------------------------------------------------------------------------
  19. //
  20. // Member: CNFAState::CNFAState, public
  21. //
  22. // Synopsis: Copy constructor
  23. //
  24. // Arguments: [src] -- Source
  25. //
  26. // History: 13-Jul-95 KyleP Created
  27. //
  28. //--------------------------------------------------------------------------
  29. CNFAState::CNFAState( CNFAState const & src )
  30. : CFAState( src ),
  31. _pmoveRest( 0 ),
  32. _cmoveRest( src._cmoveRest ),
  33. _cMove( src._cMove )
  34. {
  35. if ( _cmoveRest > 0 )
  36. {
  37. _pmoveRest = new CMove [_cmoveRest];
  38. // RtlCopyMemory( _pmoveRest, src._pmoveRest, _cmoveRest * sizeof(_pmoveRest[0]) );
  39. memcpy( _pmoveRest, src._pmoveRest, _cmoveRest * sizeof(_pmoveRest[0]) );
  40. }
  41. //RtlCopyMemory( _moveFirst, src._moveFirst, sizeof(_moveFirst) );
  42. memcpy( _moveFirst, src._moveFirst, sizeof(_moveFirst) );
  43. }
  44. //+-------------------------------------------------------------------------
  45. //
  46. // Member: CNFAState::Init, public
  47. //
  48. // Synopsis: Copy initializer
  49. //
  50. // Arguments: [src] -- Source
  51. //
  52. // History: 15-Jul-96 KyleP Created
  53. //
  54. //--------------------------------------------------------------------------
  55. void CNFAState::Init( CNFAState & src )
  56. {
  57. CFAState::Init( src.StateNumber() );
  58. if ( src.IsFinal() )
  59. MakeFinal();
  60. _cMove = src._cMove;
  61. //RtlCopyMemory( _moveFirst, src._moveFirst, sizeof(_moveFirst) );
  62. memcpy( _moveFirst, src._moveFirst, sizeof(_moveFirst) );
  63. _cmoveRest = src._cmoveRest;
  64. _pmoveRest = src._pmoveRest;
  65. src._cMove = 0;
  66. src._cmoveRest = 0;
  67. src._pmoveRest = 0;
  68. }
  69. //+-------------------------------------------------------------------------
  70. //
  71. // Member: CNFAState::operator =, public
  72. //
  73. // Synopsis: Assignment operator
  74. //
  75. // Arguments: [src] -- Source
  76. //
  77. // History: 13-Jul-95 KyleP Created
  78. //
  79. //--------------------------------------------------------------------------
  80. inline void * operator new( size_t, CNFAState * pthis )
  81. {
  82. return pthis;
  83. }
  84. #if _MSC_VER >= 1200
  85. inline void operator delete(void *, CNFAState*)
  86. { }
  87. #endif
  88. CNFAState & CNFAState::operator =( CNFAState const & src )
  89. {
  90. CNFAState::~CNFAState();
  91. new (this) CNFAState( src );
  92. return *this;
  93. }
  94. //+-------------------------------------------------------------------------
  95. //
  96. // Member: CNFAState::AddTransition, public
  97. //
  98. // Synopsis: Add new state transition.
  99. //
  100. // Arguments: [symbol] -- On this symbol to...
  101. // [StateNum] -- this state.
  102. //
  103. // History: 20-Jan-92 KyleP Created
  104. //
  105. //--------------------------------------------------------------------------
  106. void CNFAState::AddTransition( UINT symbol, UINT StateNum )
  107. {
  108. //
  109. // First look for an exact match. If this transition exists
  110. // then don't add it again.
  111. //
  112. for ( int i = CNFAState_cFirst-1; i >= 0; i--)
  113. {
  114. if ( _moveFirst[i]._symbol == symbol &&
  115. _moveFirst[i]._iState == StateNum )
  116. {
  117. return;
  118. }
  119. }
  120. if ( _cMove > CNFAState_cFirst )
  121. {
  122. for ( int i = _cMove - CNFAState_cFirst - 1; i >= 0; i--)
  123. {
  124. if ( _pmoveRest[i]._symbol == symbol &&
  125. _pmoveRest[i]._iState == StateNum )
  126. {
  127. return;
  128. }
  129. }
  130. }
  131. //
  132. // New transition. Add it.
  133. //
  134. if ( _cMove < CNFAState_cFirst )
  135. {
  136. //
  137. // Fits in the first (object based) set of moves.
  138. //
  139. _moveFirst[_cMove] = CMove( symbol, StateNum );
  140. }
  141. else
  142. {
  143. //
  144. // Overflow set of moves.
  145. //
  146. if ( _cMove - CNFAState_cFirst >= _cmoveRest )
  147. {
  148. CMove * oldpmoveRest = _pmoveRest;
  149. UINT oldcmoveRest = _cmoveRest;
  150. _cmoveRest = (_cmoveRest == 0) ? 2 : _cmoveRest + 5;
  151. _pmoveRest = (CMove *) new char [ _cmoveRest * sizeof( CMove ) ];
  152. memcpy( _pmoveRest, oldpmoveRest, oldcmoveRest * sizeof( CMove ) );
  153. delete oldpmoveRest;
  154. }
  155. _pmoveRest[_cMove - CNFAState_cFirst] = CMove( symbol, StateNum );
  156. }
  157. ++_cMove;
  158. }
  159. //+-------------------------------------------------------------------------
  160. //
  161. // Member: CNFAState::RemoveTransition, public
  162. //
  163. // Synopsis: Removes a transition.
  164. //
  165. // Arguments: [symbol] -- On this symbol to...
  166. // [StateNum] -- this state.
  167. //
  168. // History: 20-Jan-92 KyleP Created
  169. //
  170. //--------------------------------------------------------------------------
  171. void CNFAState::RemoveTransition( UINT symbol, UINT StateNum )
  172. {
  173. //
  174. // Find the transition
  175. //
  176. for ( int i = CNFAState_cFirst-1; i >= 0; i--)
  177. {
  178. if ( _moveFirst[i]._symbol == symbol &&
  179. _moveFirst[i]._iState == StateNum )
  180. {
  181. //
  182. // Move the last transition to this place.
  183. //
  184. if ( _cMove > CNFAState_cFirst )
  185. {
  186. _moveFirst[i] = _pmoveRest[_cMove - CNFAState_cFirst - 1];
  187. }
  188. else
  189. {
  190. _moveFirst[i] = _moveFirst[ _cMove - 1 ];
  191. }
  192. _cMove--;
  193. return;
  194. }
  195. }
  196. if ( _cMove > CNFAState_cFirst )
  197. {
  198. for ( int i = _cMove - CNFAState_cFirst - 1; i >= 0; i--)
  199. {
  200. if ( _pmoveRest[i]._symbol == symbol &&
  201. _pmoveRest[i]._iState == StateNum )
  202. {
  203. _pmoveRest[i] = _pmoveRest[ _cMove - CNFAState_cFirst - 1 ];
  204. _cMove--;
  205. return;
  206. }
  207. }
  208. }
  209. }
  210. //+-------------------------------------------------------------------------
  211. //
  212. // Member: CNFAState::Move, public
  213. //
  214. // Effects: Adds to [ss] the set of states that can be reached from
  215. // this state on [symbol].
  216. //
  217. // Arguments: [ss] -- Output state set.
  218. // [symbol] -- Input symbol.
  219. //
  220. // History: 20-Jan-92 KyleP Created
  221. //
  222. //--------------------------------------------------------------------------
  223. void CNFAState::Move( CStateSet & ss, UINT symbol )
  224. {
  225. for ( int i = (_cMove <= CNFAState_cFirst) ? _cMove-1 : CNFAState_cFirst-1;
  226. i >= 0;
  227. i--)
  228. {
  229. if ( _moveFirst[i]._symbol == symbol ||
  230. ( _moveFirst[i]._symbol == symAny &&
  231. symbol != symEpsilon ))
  232. {
  233. ss.Add( _moveFirst[i]._iState );
  234. }
  235. }
  236. for ( i = _cMove - CNFAState_cFirst - 1; i >= 0; i-- )
  237. {
  238. if ( _pmoveRest[i]._symbol == symbol ||
  239. ( _pmoveRest[i]._symbol == symAny &&
  240. symbol != symEpsilon &&
  241. symbol != symDot ))
  242. {
  243. ss.Add( _pmoveRest[i]._iState );
  244. }
  245. }
  246. }