# HG changeset patch # User cin # Date 2016-03-03 22:56:31 # Node ID 181119ef3b3997445cb634f520a456a50d04cdba # Parent 54270c2f29f27676ebbedddea3123e3b4f1b0c6a DFA refactoring, rx based dfa. diff --git a/Implab/Automaton/RegularExpressions/EndToken.cs b/Implab/Automaton/RegularExpressions/EndToken.cs --- a/Implab/Automaton/RegularExpressions/EndToken.cs +++ b/Implab/Automaton/RegularExpressions/EndToken.cs @@ -14,7 +14,7 @@ namespace Implab.Automaton.RegularExpres } public EndToken() - : this(0) { + : this(default(TTag)) { } public TTag Tag { diff --git a/Implab/Automaton/RegularExpressions/IDFATable2.cs b/Implab/Automaton/RegularExpressions/IDFATable2.cs deleted file mode 100644 --- a/Implab/Automaton/RegularExpressions/IDFATable2.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace Implab.Automaton.RegularExpressions { - public interface IDFATable2 : IDFATable { - void MarkFinalState(int state, TTag[] tags); - - } -} - diff --git a/Implab/Automaton/RegularExpressions/RegularDFABuilder.cs b/Implab/Automaton/RegularExpressions/RegularDFABuilder.cs --- a/Implab/Automaton/RegularExpressions/RegularDFABuilder.cs +++ b/Implab/Automaton/RegularExpressions/RegularDFABuilder.cs @@ -122,7 +122,7 @@ namespace Implab.Automaton.RegularExpres m_ends.Add(m_idx, token.Tag); } - public void BuildDFA(IDFATableBuilder dfa) { + public void BuildDFA(IDFATableBuilder dfa) { Safe.ArgumentNotNull(dfa,"dfa"); var states = new MapAlphabet>(new CustomEqualityComparer>( @@ -165,7 +165,7 @@ namespace Implab.Automaton.RegularExpres queue.Enqueue(next); } - dfa.DefineTransition(s1, s2, a); + dfa.Add(new AutomatonTransition(s1, s2, a)); } } } diff --git a/Implab/Automaton/RegularExpressions/RegularDFADefinition.cs b/Implab/Automaton/RegularExpressions/RegularDFADefinition.cs --- a/Implab/Automaton/RegularExpressions/RegularDFADefinition.cs +++ b/Implab/Automaton/RegularExpressions/RegularDFADefinition.cs @@ -1,8 +1,11 @@ using System; +using System.Collections.Generic; +using System.Linq; namespace Implab.Automaton.RegularExpressions { public class RegularDFADefinition : DFATable { + readonly Dictionary m_tags = new Dictionary(); readonly IAlphabet m_alphabet; public RegularDFADefinition(IAlphabet alphabet) { @@ -25,16 +28,39 @@ namespace Implab.Automaton.RegularExpres return base.ConstructTransitionTable(); } + 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]; + } + /// /// Optimize the specified alphabet. /// - /// /// Пустой алфавит, который будет зполнен в процессе оптимизации. - public void Optimize(IDFATableBuilder dfaTable, IAlphabetBuilder alphabet) { + public RegularDFADefinition Optimize(IAlphabetBuilder alphabet) { Safe.ArgumentNotNull(alphabet, "alphabet"); - Safe.ArgumentNotNull(dfaTable, "dfaTable"); + + var dfaTable = new RegularDFADefinition(alphabet); + + var states = new DummyAlphabet(StateCount); + var map = new MapAlphabet(); - Optimize(dfaTable, InputAlphabet, alphabet, new DummyAlphabet(StateCount), new MapAlphabet()); + Optimize(dfaTable, InputAlphabet, alphabet, states, map); + + foreach (var g in m_tags.Where(x => x.Key < StateCount).GroupBy(x => map.Translate(x.Key), x => x.Value )) + dfaTable.SetStateTag(g.Key, g.SelectMany(x => x).ToArray()); + + return dfaTable; } diff --git a/Implab/Implab.csproj b/Implab/Implab.csproj --- a/Implab/Implab.csproj +++ b/Implab/Implab.csproj @@ -190,7 +190,6 @@ -