##// END OF EJS Templates
refactoring complete, JSONParser rewritten
refactoring complete, JSONParser rewritten

File last commit:

r180:c32688129f14 ref20160224
r180:c32688129f14 ref20160224
Show More
RegularDFA.cs
82 lines | 2.7 KiB | text/x-csharp | CSharpLexer
cin
refactoring
r177 using System.Collections.Generic;
cin
Working on text scanner
r172 using System.Linq;
namespace Implab.Automaton.RegularExpressions {
cin
sync
r179 public class RegularDFA<TInput, TTag> : DFATable, ITaggedDFABuilder<TTag> {
cin
Working on text scanner
r172
readonly Dictionary<int,TTag[]> m_tags = new Dictionary<int, TTag[]>();
readonly IAlphabet<TInput> m_alphabet;
cin
sync
r179 public RegularDFA(IAlphabet<TInput> alphabet) {
cin
Working on text scanner
r172 Safe.ArgumentNotNull(alphabet, "aplhabet");
m_alphabet = alphabet;
}
public IAlphabet<TInput> InputAlphabet {
get {
return m_alphabet;
}
}
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
rewritten the text scanner
r176 public TTag[][] CreateTagTable() {
var table = new TTag[StateCount][];
cin
Working on text scanner
r172
cin
rewritten the text scanner
r176 foreach (var pair in m_tags)
table[pair.Key] = pair.Value;
cin
Working on text scanner
r172
return table;
}
/// <summary>
/// Optimize the specified alphabet.
/// </summary>
/// <param name="alphabet">Пустой алфавит, который будет зполнен в процессе оптимизации.</param>
cin
sync
r179 public RegularDFA<TInput,TTag> Optimize(IAlphabetBuilder<TInput> alphabet) {
cin
Working on text scanner
r172 Safe.ArgumentNotNull(alphabet, "alphabet");
cin
sync
r179 var dfa = new RegularDFA<TInput, TTag>(alphabet);
cin
Working on text scanner
r172
var alphaMap = new Dictionary<int,int>();
var stateMap = new Dictionary<int,int>();
Optimize(dfa, alphaMap, stateMap);
// mark tags in the new DFA
foreach (var g in m_tags.Where(x => x.Key < StateCount).GroupBy(x => stateMap[x.Key], x => x.Value ))
dfa.SetStateTag(g.Key, g.SelectMany(x => x).ToArray());
// make the alphabet for the new DFA
foreach (var pair in alphaMap)
alphabet.DefineClass(m_alphabet.GetSymbols(pair.Key), pair.Value);
return dfa;
}
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));
}
}
}