using System.Collections.Generic; using System.Linq; namespace Implab.Automaton.RegularExpressions { public class RegularDFA : DFATable, ITaggedDFABuilder { readonly Dictionary m_tags = new Dictionary(); readonly IAlphabet m_alphabet; public RegularDFA(IAlphabet alphabet) { Safe.ArgumentNotNull(alphabet, "aplhabet"); m_alphabet = alphabet; } public IAlphabet InputAlphabet { get { return m_alphabet; } } public void MarkFinalState(int s, TTag[] tags) { MarkFinalState(s); SetStateTag(s, tags); } public void SetStateTag(int s, TTag[] tags) { Safe.ArgumentNotNull(tags, "tags"); m_tags[s] = tags; } public TTag[] GetStateTag(int s) { TTag[] tags; return m_tags.TryGetValue(s, out tags) ? tags : new TTag[0]; } public TTag[][] CreateTagTable() { var table = new TTag[StateCount][]; foreach (var pair in m_tags) table[pair.Key] = pair.Value; return table; } /// /// Optimize the specified alphabet. /// /// Пустой алфавит, который будет зполнен в процессе оптимизации. public RegularDFA Optimize(IAlphabetBuilder alphabet) { Safe.ArgumentNotNull(alphabet, "alphabet"); var dfa = new RegularDFA(alphabet); var states = new DummyAlphabet(StateCount); var alphaMap = new Dictionary(); var stateMap = new Dictionary(); Optimize(dfa, alphaMap, stateMap); // mark tags in the new DFA foreach (var g in m_tags.Where(x => x.Key < StateCount).GroupBy(x => stateMap[x.Key], x => x.Value )) dfa.SetStateTag(g.Key, g.SelectMany(x => x).ToArray()); // make the alphabet for the new DFA foreach (var pair in alphaMap) alphabet.DefineClass(m_alphabet.GetSymbols(pair.Key), pair.Value); return dfa; } protected override IEnumerable> GroupFinalStates() { var arrayComparer = new CustomEqualityComparer( (x,y) => x.Length == y.Length && x.All(it => y.Contains(it)), x => x.Sum(it => x.GetHashCode()) ); return FinalStates.GroupBy(x => m_tags[x], arrayComparer).Select(g => new HashSet(g)); } } }