##// END OF EJS Templates
Working on regular DFA
Working on regular DFA

File last commit:

r171:0f70905b4652 ref20160224
r171:0f70905b4652 ref20160224
Show More
RegularDFADefinition.cs
69 lines | 2.3 KiB | text/x-csharp | CSharpLexer
cin
Almost complete DFA refactoring
r164 using System;
cin
DFA refactoring, rx based dfa.
r170 using System.Collections.Generic;
using System.Linq;
cin
Almost complete DFA refactoring
r164
namespace Implab.Automaton.RegularExpressions {
cin
DFA refactoring
r169 public class RegularDFADefinition<TInput, TTag> : DFATable {
cin
Almost complete DFA refactoring
r164
cin
DFA refactoring, rx based dfa.
r170 readonly Dictionary<int,TTag[]> m_tags = new Dictionary<int, TTag[]>();
cin
Almost complete DFA refactoring
r164 readonly IAlphabet<TInput> m_alphabet;
cin
DFA refactoring
r165 public RegularDFADefinition(IAlphabet<TInput> alphabet) {
cin
Almost complete DFA refactoring
r164 Safe.ArgumentNotNull(alphabet, "aplhabet");
m_alphabet = alphabet;
}
public IAlphabet<TInput> InputAlphabet {
get {
return m_alphabet;
}
}
cin
DFA refactoring, rx based dfa.
r170 public void MarkFinalState(int s, TTag[] tags) {
MarkFinalState(s);
SetStateTag(s, tags);
}
public void SetStateTag(int s, TTag[] tags) {
Safe.ArgumentNotNull(tags, "tags");
m_tags[s] = tags;
}
public TTag[] GetStateTag(int s) {
TTag[] tags;
return m_tags.TryGetValue(s, out tags) ? tags : new TTag[0];
}
cin
Almost complete DFA refactoring
r164 /// <summary>
/// Optimize the specified alphabet.
/// </summary>
/// <param name="alphabet">Пустой алфавит, который будет зполнен в процессе оптимизации.</param>
cin
DFA refactoring, rx based dfa.
r170 public RegularDFADefinition<TInput,TTag> Optimize(IAlphabetBuilder<TInput> alphabet) {
cin
Almost complete DFA refactoring
r164 Safe.ArgumentNotNull(alphabet, "alphabet");
cin
DFA refactoring, rx based dfa.
r170
var dfaTable = new RegularDFADefinition<TInput, TTag>(alphabet);
var states = new DummyAlphabet(StateCount);
cin
Working on regular DFA
r171 var alphaMap = new Dictionary<int,int>();
var stateMap = new Dictionary<int,int>();
Optimize(dfaTable, alphaMap, stateMap);
cin
Almost complete DFA refactoring
r164
cin
Working on regular DFA
r171 foreach (var g in m_tags.Where(x => x.Key < StateCount).GroupBy(x => stateMap[x.Key], x => x.Value ))
cin
DFA refactoring, rx based dfa.
r170 dfaTable.SetStateTag(g.Key, g.SelectMany(x => x).ToArray());
return dfaTable;
cin
Almost complete DFA refactoring
r164 }
cin
Working on regular DFA
r171 protected override IEnumerable<HashSet<int>> GroupFinalStates() {
var arrayComparer = new CustomEqualityComparer<TTag[]>(
(x,y) => x.Length == y.Length && x.All(it => y.Contains(it)),
x => x.Sum(it => x.GetHashCode())
);
return FinalStates.GroupBy(x => m_tags[x], arrayComparer).Select(g => new HashSet<int>(g));
}
cin
Almost complete DFA refactoring
r164
}
}