annotate Implab/Automaton/MapAlphabet.cs @ 260:547a2fc0d93e v3 v3.0.6

minor fixes
author cin
date Fri, 13 Apr 2018 19:14:59 +0300
parents 302ca905c19e
children
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
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
42 foreach (var symbol in symbols)
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
43 m_map[symbol] = cls;
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
44 return cls;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
45 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
46
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
47 #endregion
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
48
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
49 #region IAlphabet implementation
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
50
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
51 public int Translate(T symbol) {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
52 int cls;
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
53 if (m_map.TryGetValue(symbol, out cls))
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
54 return cls;
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
55 if (!m_supportUnclassified)
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
56 throw new ArgumentOutOfRangeException("symbol", "The specified symbol isn't in the alphabet");
236
302ca905c19e JsonReader optimizations
cin
parents: 181
diff changeset
57 return AutomatonConst.UnclassifiedInput;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
58 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
59
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
60 public int Count {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
61 get {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
62 return m_nextCls;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
63 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
64 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
65
171
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
66 public bool Contains(T symbol) {
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
67 return m_supportUnclassified || m_map.ContainsKey(symbol);
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
68 }
0f70905b4652 Working on regular DFA
cin
parents: 164
diff changeset
69
172
92d5278d1b10 Working on text scanner
cin
parents: 171
diff changeset
70
92d5278d1b10 Working on text scanner
cin
parents: 171
diff changeset
71 public IEnumerable<T> GetSymbols(int cls) {
181
b2b6a6640aa3 minor fixes and debug
cin
parents: 178
diff changeset
72 Safe.ArgumentAssert(!m_supportUnclassified || cls > 0, "cls");
172
92d5278d1b10 Working on text scanner
cin
parents: 171
diff changeset
73 return m_map.Where(p => p.Value == cls).Select(p => p.Key);
92d5278d1b10 Working on text scanner
cin
parents: 171
diff changeset
74 }
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
75 #endregion
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
76
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
77 public IEnumerable<KeyValuePair<T,int>> Mappings {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
78 get {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
79 return m_map;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
80 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
81 }
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
82 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
83 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
84