Mercurial > pub > ImplabNet
comparison Implab/Automaton/RegularExpressions/RegularDFADefinition.cs @ 170:181119ef3b39 ref20160224
DFA refactoring, rx based dfa.
author | cin |
---|---|
date | Fri, 04 Mar 2016 01:56:31 +0300 |
parents | 54270c2f29f2 |
children | 0f70905b4652 |
comparison
equal
deleted
inserted
replaced
169:54270c2f29f2 | 170:181119ef3b39 |
---|---|
1 using System; | 1 using System; |
2 using System.Collections.Generic; | |
3 using System.Linq; | |
2 | 4 |
3 namespace Implab.Automaton.RegularExpressions { | 5 namespace Implab.Automaton.RegularExpressions { |
4 public class RegularDFADefinition<TInput, TTag> : DFATable { | 6 public class RegularDFADefinition<TInput, TTag> : DFATable { |
5 | 7 |
8 readonly Dictionary<int,TTag[]> m_tags = new Dictionary<int, TTag[]>(); | |
6 readonly IAlphabet<TInput> m_alphabet; | 9 readonly IAlphabet<TInput> m_alphabet; |
7 | 10 |
8 public RegularDFADefinition(IAlphabet<TInput> alphabet) { | 11 public RegularDFADefinition(IAlphabet<TInput> alphabet) { |
9 Safe.ArgumentNotNull(alphabet, "aplhabet"); | 12 Safe.ArgumentNotNull(alphabet, "aplhabet"); |
10 | 13 |
23 throw new InvalidOperationException("The alphabet doesn't match the transition table"); | 26 throw new InvalidOperationException("The alphabet doesn't match the transition table"); |
24 | 27 |
25 return base.ConstructTransitionTable(); | 28 return base.ConstructTransitionTable(); |
26 } | 29 } |
27 | 30 |
31 public void MarkFinalState(int s, TTag[] tags) { | |
32 MarkFinalState(s); | |
33 SetStateTag(s, tags); | |
34 } | |
35 | |
36 public void SetStateTag(int s, TTag[] tags) { | |
37 Safe.ArgumentNotNull(tags, "tags"); | |
38 m_tags[s] = tags; | |
39 } | |
40 | |
41 public TTag[] GetStateTag(int s) { | |
42 TTag[] tags; | |
43 return m_tags.TryGetValue(s, out tags) ? tags : new TTag[0]; | |
44 } | |
45 | |
28 /// <summary> | 46 /// <summary> |
29 /// Optimize the specified alphabet. | 47 /// Optimize the specified alphabet. |
30 /// </summary> | 48 /// </summary> |
31 /// <param name = "dfaTable"></param> | |
32 /// <param name="alphabet">Пустой алфавит, который будет зполнен в процессе оптимизации.</param> | 49 /// <param name="alphabet">Пустой алфавит, который будет зполнен в процессе оптимизации.</param> |
33 public void Optimize(IDFATableBuilder<TTag> dfaTable, IAlphabetBuilder<TInput> alphabet) { | 50 public RegularDFADefinition<TInput,TTag> Optimize(IAlphabetBuilder<TInput> alphabet) { |
34 Safe.ArgumentNotNull(alphabet, "alphabet"); | 51 Safe.ArgumentNotNull(alphabet, "alphabet"); |
35 Safe.ArgumentNotNull(dfaTable, "dfaTable"); | |
36 | 52 |
37 Optimize(dfaTable, InputAlphabet, alphabet, new DummyAlphabet(StateCount), new MapAlphabet<int>()); | 53 var dfaTable = new RegularDFADefinition<TInput, TTag>(alphabet); |
54 | |
55 var states = new DummyAlphabet(StateCount); | |
56 var map = new MapAlphabet<int>(); | |
57 | |
58 Optimize(dfaTable, InputAlphabet, alphabet, states, map); | |
59 | |
60 foreach (var g in m_tags.Where(x => x.Key < StateCount).GroupBy(x => map.Translate(x.Key), x => x.Value )) | |
61 dfaTable.SetStateTag(g.Key, g.SelectMany(x => x).ToArray()); | |
62 | |
63 return dfaTable; | |
38 } | 64 } |
39 | 65 |
40 | 66 |
41 } | 67 } |
42 } | 68 } |