annotate Implab/Automaton/DFATransitionTable.cs @ 168:8fb9c9507a26 ref20160224

sync
author cin
date Wed, 02 Mar 2016 19:59:16 +0300
parents 96681e9d0cea
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
1 using Implab;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
2 using System;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
3 using System.Collections.Generic;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
4 using System.Linq;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
5
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
6 namespace Implab.Automaton {
167
cin
parents: 165
diff changeset
7 public class DFATransitionTable : IDFATableBuilder {
165
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
8 DFAStateDescriptior[] m_dfaTable;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
9
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
10 int m_stateCount;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
11 int m_symbolCount;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
12 int m_initialState;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
13
167
cin
parents: 165
diff changeset
14 readonly HashSet<int> m_finalStates = new HashSet<int>();
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
15 readonly HashSet<AutomatonTransition> m_transitions = new HashSet<AutomatonTransition>();
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
16
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
17
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
18 #region IDFADefinition implementation
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
19
165
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
20 public DFAStateDescriptior[] GetTransitionTable() {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
21 if (m_dfaTable == null) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
22 if (m_stateCount <= 0)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
23 throw new InvalidOperationException("Invalid automaton definition: states count = {0}", m_stateCount);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
24 if (m_symbolCount <= 0)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
25 throw new InvalidOperationException("Invalid automaton definition: symbols count = {0}", m_symbolCount);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
26
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
27 m_dfaTable = ConstructTransitionTable();
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
28 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
29 return m_dfaTable;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
30 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
31
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
32 public bool IsFinalState(int s) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
33 Safe.ArgumentInRange(s, 0, m_stateCount, "s");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
34
167
cin
parents: 165
diff changeset
35 return m_dfaTable != null ? m_dfaTable[s].final : m_finalStates.Contains(s);
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
36 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
37
167
cin
parents: 165
diff changeset
38 public IEnumerable<int> FinalStates {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
39 get {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
40 return m_finalStates;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
41 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
42 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
43
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
44 public int StateCount {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
45 get { return m_stateCount; }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
46 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
47
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
48 public int AlphabetSize {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
49 get { return m_symbolCount; }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
50 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
51
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
52 public int InitialState {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
53 get { return m_initialState; }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
54 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
55
168
cin
parents: 167
diff changeset
56 int[] NewTransitionArray() {
cin
parents: 167
diff changeset
57 var t = new int[m_symbolCount];
cin
parents: 167
diff changeset
58
cin
parents: 167
diff changeset
59 for (var i = 0; i < m_symbolCount; i++)
cin
parents: 167
diff changeset
60 t[i] = DFAConst.UNREACHABLE_STATE;
cin
parents: 167
diff changeset
61 return t;
cin
parents: 167
diff changeset
62 }
cin
parents: 167
diff changeset
63
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
64 #endregion
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
65
167
cin
parents: 165
diff changeset
66 protected virtual DFAStateDescriptior[] ConstructTransitionTable() {
cin
parents: 165
diff changeset
67 var dfaTable = new DFAStateDescriptior[m_stateCount];
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
68
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
69
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
70 foreach (var t in m_transitions) {
168
cin
parents: 167
diff changeset
71 if (dfaTable[t.s1].transitions == null)
cin
parents: 167
diff changeset
72 dfaTable[t.s1] = new DFAStateDescriptior(NewTransitionArray(), m_finalStates.Contains(t.s1));
cin
parents: 167
diff changeset
73
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
74 dfaTable[t.s1].transitions[t.edge] = t.s2;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
75 }
168
cin
parents: 167
diff changeset
76
cin
parents: 167
diff changeset
77 foreach (var s in m_finalStates)
cin
parents: 167
diff changeset
78 if (!dfaTable[s].final)
cin
parents: 167
diff changeset
79 m_dfaTable[s] = new DFAStateDescriptior(NewTransitionArray, true);
cin
parents: 167
diff changeset
80
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
81 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
82
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
83 #region IDFADefinitionBuilder
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
84
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
85 public void DefineTransition(int s1, int s2, int symbol) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
86 if (m_dfaTable != null)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
87 throw new InvalidOperationException("The transition table is already built");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
88
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
89 Safe.ArgumentAssert(s1 > 0, "s1");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
90 Safe.ArgumentAssert(s2 > 0, "s2");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
91 Safe.ArgumentAssert(symbol >= 0, "symbol");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
92
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
93 m_stateCount = Math.Max(Math.Max(m_stateCount, s1 + 1), s2 + 1);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
94 m_symbolCount = Math.Max(m_symbolCount, symbol + 1);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
95
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
96 m_transitions.Add(new AutomatonTransition(s1, s2, symbol));
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
97 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
98
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
99 public void MarkFinalState(int state, params TTag[] tags) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
100 if (m_dfaTable != null)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
101 throw new InvalidOperationException("The transition table is already built");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
102
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
103 m_finalStates[state] = tags;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
104 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
105
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
106 public void SetInitialState(int s) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
107 Safe.ArgumentAssert(s >= 0, "s");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
108 m_initialState = s;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
109 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
110
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
111
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
112 #endregion
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
113
168
cin
parents: 167
diff changeset
114 protected virtual IEnumerable<HashSet<int>> GroupFinalStates() {
cin
parents: 167
diff changeset
115 return new HashSet<int>[] { m_finalStates };
cin
parents: 167
diff changeset
116 }
cin
parents: 167
diff changeset
117
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
118 protected void Optimize<TInput, TState>(
168
cin
parents: 167
diff changeset
119 IDFATableBuilder optimalDFA,
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
120 IAlphabet<TInput> inputAlphabet,
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
121 IAlphabetBuilder<TInput> optimalInputAlphabet,
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
122 IAlphabet<TState> stateAlphabet,
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
123 IAlphabetBuilder<TState> optimalStateAlphabet
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
124 ) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
125 Safe.ArgumentNotNull(optimalDFA, "dfa");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
126 Safe.ArgumentNotNull(optimalInputAlphabet, "optimalInputAlphabet");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
127 Safe.ArgumentNotNull(optimalStateAlphabet, "optimalStateAlphabet");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
128 Safe.ArgumentNotNull(inputAlphabet, "inputAlphabet");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
129 Safe.ArgumentNotNull(stateAlphabet, "stateAlphabet");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
130
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
131 if (inputAlphabet.Count != m_symbolCount)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
132 throw new InvalidOperationException("The input symbols aphabet mismatch");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
133 if (stateAlphabet.Count != m_stateCount)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
134 throw new InvalidOperationException("The states alphabet mismatch");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
135
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
136 var setComparer = new CustomEqualityComparer<HashSet<int>>(
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
137 (x, y) => x.SetEquals(y),
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
138 s => s.Sum(x => x.GetHashCode())
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
139 );
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
140
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
141 var optimalStates = new HashSet<HashSet<int>>(setComparer);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
142 var queue = new HashSet<HashSet<int>>(setComparer);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
143
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
144 // получаем конечные состояния, сгруппированные по маркерам
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
145 optimalStates.UnionWith(
168
cin
parents: 167
diff changeset
146 GroupFinalStates()
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
147 );
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
148
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
149 var state = new HashSet<int>(
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
150 Enumerable
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
151 .Range(0, m_stateCount - 1)
168
cin
parents: 167
diff changeset
152 .Where(i => !m_finalStates.Contains(i))
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
153 );
168
cin
parents: 167
diff changeset
154
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
155 optimalStates.Add(state);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
156 queue.Add(state);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
157
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
158 var rmap = m_transitions
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
159 .GroupBy(t => t.s2)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
160 .ToLookup(
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
161 g => g.Key, // s2
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
162 g => g.ToLookup(t => t.edge, t => t.s1)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
163 );
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
165 while (queue.Count > 0) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
166 var stateA = queue.First();
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
167 queue.Remove(stateA);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
168
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
169 for (int c = 0; c < m_symbolCount; c++) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
170 var stateX = new HashSet<int>();
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
171 foreach(var a in stateA)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
172 stateX.UnionWith(rmap[a][c]); // all states from wich 'c' leads to 'a'
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
173
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
174 foreach (var stateY in optimalStates.ToArray()) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
175 if (stateX.Overlaps(stateY) && !stateY.IsSubsetOf(stateX)) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
176 var stateR1 = new HashSet<int>(stateY);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
177 var stateR2 = new HashSet<int>(stateY);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
178
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
179 stateR1.IntersectWith(stateX);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
180 stateR2.ExceptWith(stateX);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
181
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
182 optimalStates.Remove(stateY);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
183 optimalStates.Add(stateR1);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
184 optimalStates.Add(stateR2);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
185
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
186 if (queue.Contains(stateY)) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
187 queue.Remove(stateY);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
188 queue.Add(stateR1);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
189 queue.Add(stateR2);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
190 } else {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
191 queue.Add(stateR1.Count <= stateR2.Count ? stateR1 : stateR2);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
192 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
193 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
194 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
195 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
196 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
197
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
198 // карта получения оптимального состояния по соотвествующему ему простому состоянию
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
199 var statesMap = stateAlphabet.Reclassify(optimalStateAlphabet, optimalStates);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
200
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
201 // получаем минимальный алфавит
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
202 // входные символы не различимы, если Move(s,a1) == Move(s,a2)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
203 var optimalAlphabet = m_transitions
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
204 .GroupBy(t => Tuple.Create(statesMap[t.s1], statesMap[t.s2]), t => t.edge);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
205
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
206 var alphabetMap = inputAlphabet.Reclassify(optimalInputAlphabet, optimalAlphabet);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
207
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
208 var optimalTags = m_finalStates
168
cin
parents: 167
diff changeset
209 .GroupBy(s => statesMap[s])
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
210 .ToDictionary(
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
211 g => g.Key,
168
cin
parents: 167
diff changeset
212 g => g.ToArray()
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
213 );
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
214
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
215 // построение автомата
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
216 optimalDFA.SetInitialState(statesMap[m_initialState]);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
217
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
218 foreach (var pair in optimalTags)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
219 optimalDFA.MarkFinalState(pair.Key, pair.Value);
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
220
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
221 foreach (var t in m_transitions.Select(t => new AutomatonTransition(statesMap[t.s1],statesMap[t.s2],alphabetMap[t.edge])).Distinct())
168
cin
parents: 167
diff changeset
222 optimalDFA.Add(new AutomatonTransition(t.s1, t.s2, t.edge));
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
223
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
224 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
225
168
cin
parents: 167
diff changeset
226 public void MarkFinalState(int state) {
cin
parents: 167
diff changeset
227 throw new NotImplementedException();
cin
parents: 167
diff changeset
228 }
cin
parents: 167
diff changeset
229
cin
parents: 167
diff changeset
230 public void Add(AutomatonTransition item) {
cin
parents: 167
diff changeset
231 throw new NotImplementedException();
cin
parents: 167
diff changeset
232 }
cin
parents: 167
diff changeset
233
cin
parents: 167
diff changeset
234 public void Clear() {
cin
parents: 167
diff changeset
235 throw new NotImplementedException();
cin
parents: 167
diff changeset
236 }
cin
parents: 167
diff changeset
237
cin
parents: 167
diff changeset
238 public bool Contains(AutomatonTransition item) {
cin
parents: 167
diff changeset
239 throw new NotImplementedException();
cin
parents: 167
diff changeset
240 }
cin
parents: 167
diff changeset
241
cin
parents: 167
diff changeset
242 public void CopyTo(AutomatonTransition[] array, int arrayIndex) {
cin
parents: 167
diff changeset
243 throw new NotImplementedException();
cin
parents: 167
diff changeset
244 }
cin
parents: 167
diff changeset
245
cin
parents: 167
diff changeset
246 public bool Remove(AutomatonTransition item) {
cin
parents: 167
diff changeset
247 throw new NotImplementedException();
cin
parents: 167
diff changeset
248 }
cin
parents: 167
diff changeset
249
cin
parents: 167
diff changeset
250 public int Count {
cin
parents: 167
diff changeset
251 get {
cin
parents: 167
diff changeset
252 throw new NotImplementedException();
cin
parents: 167
diff changeset
253 }
cin
parents: 167
diff changeset
254 }
cin
parents: 167
diff changeset
255
cin
parents: 167
diff changeset
256 public bool IsReadOnly {
cin
parents: 167
diff changeset
257 get {
cin
parents: 167
diff changeset
258 throw new NotImplementedException();
cin
parents: 167
diff changeset
259 }
cin
parents: 167
diff changeset
260 }
cin
parents: 167
diff changeset
261
cin
parents: 167
diff changeset
262 public IEnumerator<AutomatonTransition> GetEnumerator() {
cin
parents: 167
diff changeset
263 throw new NotImplementedException();
cin
parents: 167
diff changeset
264 }
cin
parents: 167
diff changeset
265
cin
parents: 167
diff changeset
266 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
cin
parents: 167
diff changeset
267 throw new NotImplementedException();
cin
parents: 167
diff changeset
268 }
cin
parents: 167
diff changeset
269
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
270 protected void PrintDFA<TInput, TState>(IAlphabet<TInput> inputAlphabet, IAlphabet<TState> stateAlphabet) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
271 Safe.ArgumentNotNull(inputAlphabet, "inputAlphabet");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
272 Safe.ArgumentNotNull(stateAlphabet, "stateAlphabet");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
273
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
274 var inputMap = inputAlphabet.CreateReverseMap();
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
275 var stateMap = stateAlphabet.CreateReverseMap();
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
276
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
277 for (int i = 0; i < inputMap.Length; i++)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
278 Console.WriteLine("C{0}: {1}", i, String.Join(",", inputMap[i]));
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
279
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
280
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
281 foreach(var t in m_transitions)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
282 Console.WriteLine(
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
283 "[{0}] -{{{1}}}-> [{2}]{3}",
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
284 stateMap[t.s1],
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
285 String.Join(",", inputMap[t.edge]),
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
286 stateMap[t.s2],
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
287 m_finalStates.ContainsKey(t.s2) ? "$" : ""
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
288 );
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
289
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
290 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
291
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
292 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
293 }