annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
1 using System;
170
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
2 using System.Collections.Generic;
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
3 using System.Linq;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
4
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
5 namespace Implab.Automaton.RegularExpressions {
169
54270c2f29f2 DFA refactoring
cin
parents: 165
diff changeset
6 public class RegularDFADefinition<TInput, TTag> : DFATable {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
7
170
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
8 readonly Dictionary<int,TTag[]> m_tags = new Dictionary<int, TTag[]>();
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
9 readonly IAlphabet<TInput> m_alphabet;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
10
165
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
11 public RegularDFADefinition(IAlphabet<TInput> alphabet) {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
12 Safe.ArgumentNotNull(alphabet, "aplhabet");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
13
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
14 m_alphabet = alphabet;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
15 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
16
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
17
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
18 public IAlphabet<TInput> InputAlphabet {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
19 get {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
20 return m_alphabet;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
21 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
22 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
23
169
54270c2f29f2 DFA refactoring
cin
parents: 165
diff changeset
24 protected override DFAStateDescriptior[] ConstructTransitionTable() {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
25 if (InputAlphabet.Count != m_alphabet.Count)
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
26 throw new InvalidOperationException("The alphabet doesn't match the transition table");
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
27
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
28 return base.ConstructTransitionTable();
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
29 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
30
170
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
31 public void MarkFinalState(int s, TTag[] tags) {
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
32 MarkFinalState(s);
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
33 SetStateTag(s, tags);
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
34 }
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
35
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
36 public void SetStateTag(int s, TTag[] tags) {
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
37 Safe.ArgumentNotNull(tags, "tags");
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
38 m_tags[s] = tags;
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
39 }
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
40
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
41 public TTag[] GetStateTag(int s) {
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
42 TTag[] tags;
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
43 return m_tags.TryGetValue(s, out tags) ? tags : new TTag[0];
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
44 }
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
45
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
46 /// <summary>
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
47 /// Optimize the specified alphabet.
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
48 /// </summary>
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
49 /// <param name="alphabet">Пустой алфавит, который будет зполнен в процессе оптимизации.</param>
170
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
50 public RegularDFADefinition<TInput,TTag> Optimize(IAlphabetBuilder<TInput> alphabet) {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
51 Safe.ArgumentNotNull(alphabet, "alphabet");
170
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
52
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
53 var dfaTable = new RegularDFADefinition<TInput, TTag>(alphabet);
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
54
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
55 var states = new DummyAlphabet(StateCount);
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
56 var map = new MapAlphabet<int>();
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
57
170
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
58 Optimize(dfaTable, InputAlphabet, alphabet, states, map);
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
59
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
60 foreach (var g in m_tags.Where(x => x.Key < StateCount).GroupBy(x => map.Translate(x.Key), x => x.Value ))
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
61 dfaTable.SetStateTag(g.Key, g.SelectMany(x => x).ToArray());
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
62
181119ef3b39 DFA refactoring, rx based dfa.
cin
parents: 169
diff changeset
63 return dfaTable;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
64 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
65
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
66
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
67 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
68 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
69