annotate Implab/Automaton/MapAlphabet.cs @ 171:0f70905b4652 ref20160224

Working on regular DFA
author cin
date Thu, 10 Mar 2016 01:19:33 +0300
parents ec35731ae299
children 92d5278d1b10
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
1 using System;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
2 using System.Collections.Generic;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
3 using System.Linq;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
4
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
5 namespace Implab.Automaton {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
6 public class MapAlphabet<T> : IAlphabetBuilder<T> {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
7 readonly Dictionary<T,int> m_map;
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
8 int m_nextCls;
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
9 readonly bool m_supportUnclassified;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
10
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
11 public MapAlphabet(bool supportUnclassified, IEqualityComparer<T> comparer) {
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
12 m_map = comparer != null ? new Dictionary<T, int>(comparer) : new Dictionary<T,int>();
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
13 m_supportUnclassified = supportUnclassified;
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
14 m_nextCls = supportUnclassified ? 1 : 0;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
15 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
16
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
17 #region IAlphabetBuilder implementation
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
18
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
19 public int DefineSymbol(T symbol) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
20 int cls;
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
21 return m_map.TryGetValue(symbol, out cls) ? cls : DefineSymbol(symbol, m_nextCls);
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
22 }
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
23
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
24 public int DefineSymbol(T symbol, int cls) {
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
25 Safe.ArgumentAssert(cls >= 0, "cls");
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
26
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
27 m_nextCls = Math.Max(cls + 1, m_nextCls);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
28 m_map.Add(symbol, cls);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
29 return cls;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
30 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
31
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
32 public int DefineClass(IEnumerable<T> symbols) {
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
33 return DefineClass(symbols, m_nextCls);
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
34 }
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
35
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
36 public int DefineClass(IEnumerable<T> symbols, int cls) {
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
37 Safe.ArgumentAssert(cls >= 0, "cls");
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
38 Safe.ArgumentNotNull(symbols, "symbols");
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
39
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
40 m_nextCls = Math.Max(cls + 1, m_nextCls);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
41 symbols = symbols.Distinct();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
42
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
43 foreach (var symbol in symbols)
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
44 m_map[symbol] = cls;
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
45 return cls;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
46 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
47
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
48 #endregion
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
49
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
50 #region IAlphabet implementation
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
51
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
52 public int Translate(T symbol) {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
53 int cls;
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
54 if (m_map.TryGetValue(symbol, out cls))
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
55 return cls;
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
56 if (!m_supportUnclassified)
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
57 throw new ArgumentOutOfRangeException("symbol", "The specified symbol isn't in the alphabet");
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
58 return DFAConst.UNCLASSIFIED_INPUT;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
59 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
60
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
61 public int Count {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
62 get {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
63 return m_nextCls;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
64 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
65 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
66
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
67 public bool Contains(T symbol) {
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
68 return m_supportUnclassified || m_map.ContainsKey(symbol);
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
69 }
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
70
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
71 #endregion
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
72 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
73 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
74