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.

281 lines
6.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1991, Microsoft Corporation.
  4. //
  5. // File: State.hxx
  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 regex lib
  13. //--------------------------------------------------------------------------
  14. #ifndef __STATE_HXX__
  15. #define __STATE_HXX__
  16. #include <windows.h>
  17. #include <xlatchar.hxx>
  18. class CStateSet;
  19. class CSymbolSet;
  20. //
  21. // Special transition 'states'.
  22. //
  23. UINT const stateUncomputed = (1 << (sizeof( UINT ) * 8)) - 1; // Uncomputed move.
  24. UINT const stateUndefined = (1 << (sizeof( UINT ) * 8)) - 2; // Undefined move.
  25. UINT const stateUninitialized = (1 << (sizeof( UINT ) * 8)) - 3; // Uninitialized
  26. //+-------------------------------------------------------------------------
  27. //
  28. // Class: CFAState
  29. //
  30. // Purpose: Basic finite automata state.
  31. //
  32. // History: 20-Jan-92 KyleP Created
  33. //
  34. //--------------------------------------------------------------------------
  35. class CFAState
  36. {
  37. public:
  38. inline CFAState() {};
  39. inline CFAState( UINT iState, BOOL fFinal = false );
  40. inline void Init( UINT iState );
  41. inline UINT StateNumber() const;
  42. inline BOOL IsFinal() const;
  43. inline void MakeFinal();
  44. protected:
  45. UINT _iState;
  46. BOOL _fFinal;
  47. };
  48. //+-------------------------------------------------------------------------
  49. //
  50. // Class: CNFAState
  51. //
  52. // Purpose: Nondeterministic finite automata state.
  53. //
  54. // History: 20-Jan-92 KyleP Created
  55. //
  56. // Notes: The array of moves is split into two pieces. Since most
  57. // states only have a small number of transitions each
  58. // CNFAState will hold a small number in the CNFAState
  59. // object itself. Overflow is on the heap.
  60. //
  61. //--------------------------------------------------------------------------
  62. const int CNFAState_cFirst = 2; // Default number of moves per state
  63. class CNFAState : public CFAState
  64. {
  65. public:
  66. inline CNFAState();
  67. CNFAState( CNFAState const & src );
  68. CNFAState & operator=( CNFAState const & src );
  69. void Init( CNFAState & src );
  70. void Init( UINT iState ) { CFAState::Init( iState ); }
  71. inline ~CNFAState();
  72. void AddTransition( UINT symbol, UINT StateNum );
  73. void RemoveTransition( UINT symbol, UINT StateNum );
  74. void Move( CStateSet & ss, UINT symbol = symEpsilon );
  75. void Move( CStateSet & ss, CSymbolSet & symset );
  76. protected:
  77. class CMove
  78. {
  79. public:
  80. inline CMove();
  81. inline CMove( UINT symbol, UINT iState );
  82. UINT _symbol;
  83. UINT _iState;
  84. };
  85. CMove _moveFirst[CNFAState_cFirst];
  86. CMove * _pmoveRest;
  87. int _cmoveRest;
  88. int _cMove;
  89. };
  90. //+-------------------------------------------------------------------------
  91. //
  92. // Member: CFAState::CFAState, public
  93. //
  94. // Synopsis: Initialize new state.
  95. //
  96. // Arguments: [iState] -- State number.
  97. // [fFinal] -- true if state is a final state.
  98. //
  99. // History: 20-Jan-92 KyleP Created
  100. //
  101. //--------------------------------------------------------------------------
  102. inline CFAState::CFAState( UINT iState, BOOL fFinal )
  103. : _iState( iState ),
  104. _fFinal( fFinal )
  105. {
  106. }
  107. //+-------------------------------------------------------------------------
  108. //
  109. // Member: CFAState::Init, public
  110. //
  111. // Synopsis: Initialize a new state.
  112. //
  113. // Arguments: [iState] -- State number.
  114. //
  115. // History: 10-Jul-92 KyleP Created
  116. // 23-Jul-92 KyleP Default states to non-final.
  117. //
  118. //--------------------------------------------------------------------------
  119. inline void CFAState::Init( UINT iState )
  120. {
  121. _iState = iState;
  122. _fFinal = false;
  123. }
  124. //+-------------------------------------------------------------------------
  125. //
  126. // Member: CFAState::StateNumber, public
  127. //
  128. // Returns: The state number.
  129. //
  130. // History: 20-Jan-92 KyleP Created
  131. //
  132. //--------------------------------------------------------------------------
  133. inline UINT CFAState::StateNumber() const
  134. {
  135. return( _iState );
  136. }
  137. //+-------------------------------------------------------------------------
  138. //
  139. // Member: CFAState::IsFinal, public
  140. //
  141. // Returns: true if this state is a final state.
  142. //
  143. // History: 20-Jan-92 KyleP Created
  144. //
  145. //--------------------------------------------------------------------------
  146. inline BOOL CFAState::IsFinal() const
  147. {
  148. return( _fFinal );
  149. }
  150. //+-------------------------------------------------------------------------
  151. //
  152. // Member: CFAState::MakeFinal, public
  153. //
  154. // Synopsis: Indicates state is a final state.
  155. //
  156. // History: 20-Jan-92 KyleP Created
  157. //
  158. //--------------------------------------------------------------------------
  159. inline void CFAState::MakeFinal()
  160. {
  161. _fFinal = true;
  162. }
  163. //+-------------------------------------------------------------------------
  164. //
  165. // Member: CNFAState::CMove::CMove, private
  166. //
  167. // Synopsis: Initializes move.
  168. //
  169. // History: 20-Jan-92 KyleP Created
  170. //
  171. //--------------------------------------------------------------------------
  172. inline CNFAState::CMove::CMove()
  173. {
  174. _symbol = symInvalid;
  175. }
  176. //+-------------------------------------------------------------------------
  177. //
  178. // Member: CNFAState::CMove::CMove, private
  179. //
  180. // Synopsis: Initializes move.
  181. //
  182. // Arguments: [symbol] -- On symbol, move to ...
  183. // [iState] -- this new state.
  184. //
  185. // History: 20-Jan-92 KyleP Created
  186. //
  187. //--------------------------------------------------------------------------
  188. inline CNFAState::CMove::CMove( UINT symbol, UINT iState )
  189. : _symbol( symbol ),
  190. _iState( iState )
  191. {
  192. }
  193. //+-------------------------------------------------------------------------
  194. //
  195. // Member: CNFAState::CNFAState, public
  196. //
  197. // Synopsis: Initialize a state (no transitions)
  198. //
  199. // History: 20-Jan-92 KyleP Created
  200. //--------------------------------------------------------------------------
  201. inline CNFAState::CNFAState()
  202. : _cMove( 0 ),
  203. _pmoveRest( 0 ),
  204. _cmoveRest( 0 )
  205. {
  206. }
  207. //+-------------------------------------------------------------------------
  208. //
  209. // Member: CNFAState::~CNFAState, public
  210. //
  211. // Effects: Clean up any spillover moves.
  212. //
  213. // History: 20-Jan-92 KyleP Created
  214. //
  215. //--------------------------------------------------------------------------
  216. inline CNFAState::~CNFAState()
  217. {
  218. delete _pmoveRest;
  219. }
  220. #endif // __STATE_HXX__