comparison Implab/Parsing/DFADefinition.cs @ 158:130781364799 v2

refactoring, code cleanup
author cin
date Thu, 18 Feb 2016 14:34:02 +0300
parents
children 5558e43c79bb
comparison
equal deleted inserted replaced
157:948c015a9011 158:130781364799
1 using Implab;
2 using System;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.Linq;
6
7 namespace Implab.Parsing {
8 public class DFADefinition : IDFADefinition {
9 readonly List<DFAStateDescriptior> m_states;
10
11 public const int INITIAL_STATE = 1;
12 public const int UNREACHEBLE_STATE = 0;
13
14 DFAStateDescriptior[] m_statesArray;
15 readonly int m_alpabetSize;
16
17 public DFADefinition(int alphabetSize) {
18 m_states = new List<DFAStateDescriptior>();
19 m_alpabetSize = alphabetSize;
20
21 m_states.Add(new DFAStateDescriptior());
22 }
23
24 public DFAStateDescriptior[] States {
25 get {
26 if (m_statesArray == null)
27 m_statesArray = m_states.ToArray();
28 return m_statesArray;
29 }
30 }
31
32 public bool InitialStateIsFinal {
33 get {
34 return m_states[INITIAL_STATE].final;
35 }
36 }
37
38 public int AddState() {
39 var index = m_states.Count;
40 m_states.Add(new DFAStateDescriptior {
41 final = false,
42 transitions = new int[AlphabetSize]
43 });
44 m_statesArray = null;
45
46 return index;
47 }
48
49 public int AddState(int[] tag) {
50 var index = m_states.Count;
51 bool final = tag != null && tag.Length != 0;
52 m_states.Add(new DFAStateDescriptior {
53 final = final,
54 transitions = new int[AlphabetSize],
55 tag = final ? tag : null
56 });
57 m_statesArray = null;
58 return index;
59 }
60
61 public void DefineTransition(int s1,int s2, int symbol) {
62 Safe.ArgumentInRange(s1, 0, m_states.Count-1, "s1");
63 Safe.ArgumentInRange(s2, 0, m_states.Count-1, "s2");
64 Safe.ArgumentInRange(symbol, 0, AlphabetSize-1, "symbol");
65
66 m_states[s1].transitions[symbol] = s2;
67 }
68
69 public void Optimize<TA>(IDFADefinition minimalDFA,IAlphabet<TA> sourceAlphabet, IAlphabet<TA> minimalAlphabet) {
70 Safe.ArgumentNotNull(minimalDFA, "minimalDFA");
71 Safe.ArgumentNotNull(minimalAlphabet, "minimalAlphabet");
72
73 var setComparer = new CustomEqualityComparer<HashSet<int>>(
74 (x, y) => x.SetEquals(y),
75 (s) => s.Sum(x => x.GetHashCode())
76 );
77
78 var arrayComparer = new CustomEqualityComparer<int[]>(
79 (x,y) => (new HashSet<int>(x)).SetEquals(new HashSet<int>(y)),
80 (a) => a.Sum(x => x.GetHashCode())
81 );
82
83 var optimalStates = new HashSet<HashSet<int>>(setComparer);
84 var queue = new HashSet<HashSet<int>>(setComparer);
85
86 foreach (var g in Enumerable
87 .Range(INITIAL_STATE, m_states.Count-1)
88 .Select(i => new {
89 index = i,
90 descriptor = m_states[i]
91 })
92 .Where(x => x.descriptor.final)
93 .GroupBy(x => x.descriptor.tag, arrayComparer)
94 ) {
95 optimalStates.Add(new HashSet<int>(g.Select(x => x.index)));
96 }
97
98 var state = new HashSet<int>(
99 Enumerable
100 .Range(INITIAL_STATE, m_states.Count - 1)
101 .Where(i => !m_states[i].final)
102 );
103 optimalStates.Add(state);
104 queue.Add(state);
105
106 while (queue.Count > 0) {
107 var stateA = queue.First();
108 queue.Remove(stateA);
109
110 for (int c = 0; c < AlphabetSize; c++) {
111 var stateX = new HashSet<int>();
112
113 for(int s = 1; s < m_states.Count; s++) {
114 if (stateA.Contains(m_states[s].transitions[c]))
115 stateX.Add(s);
116 }
117
118 foreach (var stateY in optimalStates.ToArray()) {
119 if (stateX.Overlaps(stateY) && !stateY.IsSubsetOf(stateX)) {
120 var stateR1 = new HashSet<int>(stateY);
121 var stateR2 = new HashSet<int>(stateY);
122
123 stateR1.IntersectWith(stateX);
124 stateR2.ExceptWith(stateX);
125
126 optimalStates.Remove(stateY);
127 optimalStates.Add(stateR1);
128 optimalStates.Add(stateR2);
129
130 if (queue.Contains(stateY)) {
131 queue.Remove(stateY);
132 queue.Add(stateR1);
133 queue.Add(stateR2);
134 } else {
135 queue.Add(stateR1.Count <= stateR2.Count ? stateR1 : stateR2);
136 }
137 }
138 }
139 }
140 }
141
142 // строим карты соотвествия оптимальных состояний с оригинальными
143
144 var initialState = optimalStates.Single(x => x.Contains(INITIAL_STATE));
145
146 // карта получения оптимального состояния по соотвествующему ему простому состоянию
147 int[] reveseOptimalMap = new int[m_states.Count];
148 // карта с индексами оптимальных состояний
149 HashSet<int>[] optimalMap = new HashSet<int>[optimalStates.Count + 1];
150 {
151 optimalMap[0] = new HashSet<int>(); // unreachable state
152 optimalMap[1] = initialState; // initial state
153 foreach (var ss in initialState)
154 reveseOptimalMap[ss] = 1;
155
156 int i = 2;
157 foreach (var s in optimalStates) {
158 if (s.SetEquals(initialState))
159 continue;
160 optimalMap[i] = s;
161 foreach (var ss in s)
162 reveseOptimalMap[ss] = i;
163 i++;
164 }
165 }
166
167 // получаем минимальный алфавит
168
169 var minClasses = new HashSet<HashSet<int>>(setComparer);
170 var alphaQueue = new Queue<HashSet<int>>();
171 alphaQueue.Enqueue(new HashSet<int>(Enumerable.Range(0,AlphabetSize)));
172
173 for (int s = 1 ; s < optimalMap.Length; s++) {
174 var newQueue = new Queue<HashSet<int>>();
175
176 foreach (var A in alphaQueue) {
177 if (A.Count == 1) {
178 minClasses.Add(A);
179 continue;
180 }
181
182 // различаем классы символов, которые переводят в различные оптимальные состояния
183 // optimalState -> alphaClass
184 var classes = new Dictionary<int, HashSet<int>>();
185
186 foreach (var term in A) {
187 // ищем все переходы класса по символу term
188 var s2 = reveseOptimalMap[
189 optimalMap[s].Select(x => m_states[x].transitions[term]).FirstOrDefault(x => x != 0) // первое допустимое элементарное состояние, если есть
190 ];
191
192 HashSet<int> A2;
193 if (!classes.TryGetValue(s2, out A2)) {
194 A2 = new HashSet<int>();
195 newQueue.Enqueue(A2);
196 classes[s2] = A2;
197 }
198 A2.Add(term);
199 }
200 }
201
202 if (newQueue.Count == 0)
203 break;
204 alphaQueue = newQueue;
205 }
206
207 foreach (var A in alphaQueue)
208 minClasses.Add(A);
209
210 var alphabetMap = sourceAlphabet.Reclassify(minimalAlphabet, minClasses);
211
212 // построение автомата
213
214 var states = new int[ optimalMap.Length ];
215 states[0] = UNREACHEBLE_STATE;
216
217 for(var s = INITIAL_STATE; s < states.Length; s++) {
218 var tags = optimalMap[s].SelectMany(x => m_states[x].tag ?? Enumerable.Empty<int>()).Distinct().ToArray();
219 if (tags.Length > 0)
220 states[s] = minimalDFA.AddState(tags);
221 else
222 states[s] = minimalDFA.AddState();
223 }
224
225 Debug.Assert(states[INITIAL_STATE] == INITIAL_STATE);
226
227 for (int s1 = 1; s1 < m_states.Count; s1++) {
228 for (int c = 0; c < AlphabetSize; c++) {
229 var s2 = m_states[s1].transitions[c];
230 if (s2 != UNREACHEBLE_STATE) {
231 minimalDFA.DefineTransition(
232 reveseOptimalMap[s1],
233 reveseOptimalMap[s2],
234 alphabetMap[c]
235 );
236 }
237 }
238 }
239
240 }
241
242 public void PrintDFA<TA>(IAlphabet<TA> alphabet) {
243
244 var reverseMap = alphabet.CreateReverseMap();
245
246 for (int i = 1; i < reverseMap.Length; i++) {
247 Console.WriteLine("C{0}: {1}", i, String.Join(",", reverseMap[i]));
248 }
249
250 for (int i = 1; i < m_states.Count; i++) {
251 var s = m_states[i];
252 for (int c = 0; c < AlphabetSize; c++)
253 if (s.transitions[c] != UNREACHEBLE_STATE)
254 Console.WriteLine("S{0} -{1}-> S{2}{3}", i, String.Join(",", reverseMap[c]), s.transitions[c], m_states[s.transitions[c]].final ? "$" : "");
255 }
256 }
257
258 public int AlphabetSize {
259 get;
260 }
261 }
262 }