annotate Implab/Automaton/DFAutomaton.cs @ 163:419aa51b04fd ref20160224

JSON moved to Formats namespace Working in RegularDFA
author cin
date Wed, 24 Feb 2016 20:12:52 +0300
parents 0526412bbb26
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
1 using Implab;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
2 using System;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
3 using System.Collections.Generic;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
4 using System.Diagnostics;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
5 using System.Linq;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
6 using System.Text;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
7 using System.Threading.Tasks;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
8
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
9 namespace Implab.Automaton {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
10 public abstract class DFAutomaton<T> {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
11 protected struct ContextFrame {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
12 public DFAStateDescriptior[] states;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
13 public int current;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
14 public T info;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
15 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
16
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
17 public const int INITIAL_STATE = DFADefinition.INITIAL_STATE;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
18 public const int UNREACHEBLE_STATE = DFADefinition.UNREACHEBLE_STATE;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
19
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
20 protected ContextFrame m_context;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
21 Stack<ContextFrame> m_contextStack = new Stack<ContextFrame>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
22
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
23 protected int Level {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
24 get { return m_contextStack.Count; }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
25 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
26
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
27 protected DFAutomaton(DFAStateDescriptior[] states, int startState, T info) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
28 Safe.ArgumentNotNull(states, "states");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
29 Safe.ArgumentInRange(startState, 0, states.Length - 1, "startState");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
30
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
31 m_context.states = states;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
32 m_context.current = startState;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
33 m_context.info = info;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
34 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
35
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
36 protected void Switch(DFAStateDescriptior[] states, int current, T info) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
37 Debug.Assert(states != null);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
38 Debug.Assert(current >= 0 && current < states.Length);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
39 m_contextStack.Push(m_context);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
40 m_context.states = states;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
41 m_context.current = current;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
42 m_context.info = info;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
43 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
44
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
45 protected void Restore() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
46 Debug.Assert(m_contextStack.Count > 0);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
47
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
48 m_context = m_contextStack.Pop();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
49 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
50
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
51 protected void Move(int input) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
52 Debug.Assert(input > 0 && input < m_context.states[m_context.current].transitions.Length);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
53 m_context.current = m_context.states[m_context.current].transitions[input];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
54 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
55
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
56 protected bool CanMove(int input) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
57 Debug.Assert(input > 0 && input < m_context.states[m_context.current].transitions.Length);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
58 return m_context.states[m_context.current].transitions[input] != UNREACHEBLE_STATE;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
59 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
60 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
61 }