164
|
1 using System;
|
170
|
2 using System.Collections.Generic;
|
|
3 using System.Linq;
|
164
|
4
|
|
5 namespace Implab.Automaton.RegularExpressions {
|
169
|
6 public class RegularDFADefinition<TInput, TTag> : DFATable {
|
164
|
7
|
170
|
8 readonly Dictionary<int,TTag[]> m_tags = new Dictionary<int, TTag[]>();
|
164
|
9 readonly IAlphabet<TInput> m_alphabet;
|
|
10
|
165
|
11 public RegularDFADefinition(IAlphabet<TInput> alphabet) {
|
164
|
12 Safe.ArgumentNotNull(alphabet, "aplhabet");
|
|
13
|
|
14 m_alphabet = alphabet;
|
|
15 }
|
|
16
|
|
17
|
|
18 public IAlphabet<TInput> InputAlphabet {
|
|
19 get {
|
|
20 return m_alphabet;
|
|
21 }
|
|
22 }
|
|
23
|
169
|
24 protected override DFAStateDescriptior[] ConstructTransitionTable() {
|
164
|
25 if (InputAlphabet.Count != m_alphabet.Count)
|
|
26 throw new InvalidOperationException("The alphabet doesn't match the transition table");
|
|
27
|
|
28 return base.ConstructTransitionTable();
|
|
29 }
|
|
30
|
170
|
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
|
164
|
46 /// <summary>
|
|
47 /// Optimize the specified alphabet.
|
|
48 /// </summary>
|
|
49 /// <param name="alphabet">Пустой алфавит, который будет зполнен в процессе оптимизации.</param>
|
170
|
50 public RegularDFADefinition<TInput,TTag> Optimize(IAlphabetBuilder<TInput> alphabet) {
|
164
|
51 Safe.ArgumentNotNull(alphabet, "alphabet");
|
170
|
52
|
|
53 var dfaTable = new RegularDFADefinition<TInput, TTag>(alphabet);
|
|
54
|
|
55 var states = new DummyAlphabet(StateCount);
|
|
56 var map = new MapAlphabet<int>();
|
164
|
57
|
170
|
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;
|
164
|
64 }
|
|
65
|
|
66
|
|
67 }
|
|
68 }
|
|
69
|