annotate Implab/Automaton/RegularExpressions/RegularDFA.cs @ 176:0c3c69fe225b ref20160224

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