annotate Implab/Automaton/RegularExpressions/RegularDFA.cs @ 179:478ef706906a ref20160224

sync
author cin
date Wed, 23 Mar 2016 19:52:08 +0300
parents d5c5db0335ee
children c32688129f14
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
177
a0ff6a0e9c44 refactoring
cin
parents: 176
diff changeset
1 using System.Collections.Generic;
172
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
2 using System.Linq;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
3
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
4 namespace Implab.Automaton.RegularExpressions {
179
cin
parents: 178
diff changeset
5 public class RegularDFA<TInput, TTag> : DFATable, ITaggedDFABuilder<TTag> {
172
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
6
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
7 readonly Dictionary<int,TTag[]> m_tags = new Dictionary<int, TTag[]>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
8 readonly IAlphabet<TInput> m_alphabet;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
9
179
cin
parents: 178
diff changeset
10 public RegularDFA(IAlphabet<TInput> alphabet) {
172
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
11 Safe.ArgumentNotNull(alphabet, "aplhabet");
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
12
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
13 m_alphabet = alphabet;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
14 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
15
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
16
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
17 public IAlphabet<TInput> InputAlphabet {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
18 get {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
19 return m_alphabet;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
20 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
21 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
22
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
23 public void MarkFinalState(int s, TTag[] tags) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
24 MarkFinalState(s);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
25 SetStateTag(s, tags);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
26 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
27
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
28 public void SetStateTag(int s, TTag[] tags) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
29 Safe.ArgumentNotNull(tags, "tags");
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
30 m_tags[s] = tags;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
31 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
32
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
33 public TTag[] GetStateTag(int s) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
34 TTag[] tags;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
35 return m_tags.TryGetValue(s, out tags) ? tags : new TTag[0];
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
36 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
37
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
38 public TTag[][] CreateTagTable() {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
39 var table = new TTag[StateCount][];
172
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
40
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
41 foreach (var pair in m_tags)
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
42 table[pair.Key] = pair.Value;
172
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
43
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
44 return table;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
45 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
46
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
47 /// <summary>
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
48 /// Optimize the specified alphabet.
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
49 /// </summary>
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
50 /// <param name="alphabet">Пустой алфавит, который будет зполнен в процессе оптимизации.</param>
179
cin
parents: 178
diff changeset
51 public RegularDFA<TInput,TTag> Optimize(IAlphabetBuilder<TInput> alphabet) {
172
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
52 Safe.ArgumentNotNull(alphabet, "alphabet");
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
53
179
cin
parents: 178
diff changeset
54 var dfa = new RegularDFA<TInput, TTag>(alphabet);
172
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
55
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
56 var states = new DummyAlphabet(StateCount);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
57 var alphaMap = new Dictionary<int,int>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
58 var stateMap = new Dictionary<int,int>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
59
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
60 Optimize(dfa, alphaMap, stateMap);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
61
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
62 // mark tags in the new DFA
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
63 foreach (var g in m_tags.Where(x => x.Key < StateCount).GroupBy(x => stateMap[x.Key], x => x.Value ))
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
64 dfa.SetStateTag(g.Key, g.SelectMany(x => x).ToArray());
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
65
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
66 // make the alphabet for the new DFA
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
67 foreach (var pair in alphaMap)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
68 alphabet.DefineClass(m_alphabet.GetSymbols(pair.Key), pair.Value);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
69
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
70 return dfa;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
71 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
72
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
73 protected override IEnumerable<HashSet<int>> GroupFinalStates() {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
74 var arrayComparer = new CustomEqualityComparer<TTag[]>(
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
75 (x,y) => x.Length == y.Length && x.All(it => y.Contains(it)),
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
76 x => x.Sum(it => x.GetHashCode())
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
77 );
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
78 return FinalStates.GroupBy(x => m_tags[x], arrayComparer).Select(g => new HashSet<int>(g));
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
79 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
80
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
81 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
82 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
83