Mercurial > pub > ImplabNet
comparison Implab/Parsing/DFADefinition.cs @ 160:5802131432e4 v2
fixed regression: race condition in Promise
DFA refactoring
author | cin |
---|---|
date | Thu, 18 Feb 2016 19:38:54 +0300 |
parents | 5558e43c79bb |
children | 2a8466f0cb8a |
comparison
equal
deleted
inserted
replaced
159:5558e43c79bb | 160:5802131432e4 |
---|---|
64 Safe.ArgumentInRange(symbol, 0, AlphabetSize-1, "symbol"); | 64 Safe.ArgumentInRange(symbol, 0, AlphabetSize-1, "symbol"); |
65 | 65 |
66 m_states[s1].transitions[symbol] = s2; | 66 m_states[s1].transitions[symbol] = s2; |
67 } | 67 } |
68 | 68 |
69 public void Optimize<TA>(IDFADefinition minimalDFA,IAlphabet<TA> sourceAlphabet, IAlphabet<TA> minimalAlphabet) { | 69 protected IDFADefinition Optimize<TA>(Func<IAlphabet<TA>, IDFADefinition> dfaFactory,IAlphabet<TA> sourceAlphabet, IAlphabet<TA> minimalAlphabet) { |
70 Safe.ArgumentNotNull(minimalDFA, "minimalDFA"); | 70 Safe.ArgumentNotNull(dfaFactory, "dfaFactory"); |
71 Safe.ArgumentNotNull(minimalAlphabet, "minimalAlphabet"); | 71 Safe.ArgumentNotNull(minimalAlphabet, "minimalAlphabet"); |
72 | 72 |
73 var setComparer = new CustomEqualityComparer<HashSet<int>>( | 73 var setComparer = new CustomEqualityComparer<HashSet<int>>( |
74 (x, y) => x.SetEquals(y), | 74 (x, y) => x.SetEquals(y), |
75 (s) => s.Sum(x => x.GetHashCode()) | 75 (s) => s.Sum(x => x.GetHashCode()) |
209 | 209 |
210 var alphabetMap = sourceAlphabet.Reclassify(minimalAlphabet, minClasses); | 210 var alphabetMap = sourceAlphabet.Reclassify(minimalAlphabet, minClasses); |
211 | 211 |
212 // построение автомата | 212 // построение автомата |
213 | 213 |
214 var minimalDFA = dfaFactory(minimalAlphabet); | |
215 | |
214 var states = new int[ optimalMap.Length ]; | 216 var states = new int[ optimalMap.Length ]; |
215 states[0] = UNREACHEBLE_STATE; | 217 states[0] = UNREACHEBLE_STATE; |
216 | 218 |
217 for(var s = INITIAL_STATE; s < states.Length; s++) { | 219 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(); | 220 var tags = optimalMap[s].SelectMany(x => m_states[x].tag ?? Enumerable.Empty<int>()).Distinct().ToArray(); |
235 ); | 237 ); |
236 } | 238 } |
237 } | 239 } |
238 } | 240 } |
239 | 241 |
242 return minimalDFA; | |
240 } | 243 } |
241 | 244 |
242 public void PrintDFA<TA>(IAlphabet<TA> alphabet) { | 245 public void PrintDFA<TA>(IAlphabet<TA> alphabet) { |
243 | 246 |
244 var reverseMap = alphabet.CreateReverseMap(); | 247 var reverseMap = alphabet.CreateReverseMap(); |