annotate Implab/Automaton/MapAlphabet.cs @ 164:ec35731ae299 ref20160224

Almost complete DFA refactoring
author cin
date Thu, 25 Feb 2016 02:11:13 +0300
parents 419aa51b04fd
children 0f70905b4652
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;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
8 int m_nextCls = 1;
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
9
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
10 public MapAlphabet() {
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
11 m_map = new Dictionary<T, int>();
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
12 }
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
13
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
14 public MapAlphabet(IEqualityComparer<T> comparer) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
15 m_map = new Dictionary<T, int>(comparer);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
16 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
17
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
18 #region IAlphabetBuilder implementation
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
19
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
20 public int DefineSymbol(T symbol) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
21 int cls;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
22 if (m_map.TryGetValue(symbol, out cls))
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
23 return cls;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
24
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
25 cls = m_nextCls++;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
26
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
27 m_map.Add(symbol, cls);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
28
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) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
33 Safe.ArgumentNotNull(symbols, "symbols");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
34 symbols = symbols.Distinct();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
35
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
36 foreach (var symbol in symbols) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
37 if (!m_map.Contains(symbol))
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
38 m_map.Add(symbol, m_nextCls);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
39 else
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
40 throw new InvalidOperationException(String.Format("Symbol '{0}' already in use", symbol));
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
41 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
42 return m_nextCls++;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
43 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
44
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
45 #endregion
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
46
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
47 #region IAlphabet implementation
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
48
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
49 public List<T>[] CreateReverseMap() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
50 var empty = new List<T>();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
51 var rmap = new List<T>[m_nextCls];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
52
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
53 for (int i = 0; i < rmap.Length; i++)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
54 rmap[i] = empty;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
55
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
56 foreach (var pair in m_map) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
57 var symbols = rmap[pair.Value];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
58 if (symbols == null) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
59 symbols = new List<T>();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
60 rmap[pair.Value] = symbols;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
61 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
62
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
63 symbols.Add(pair.Key);
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 return rmap;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
67 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
68
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
69 public int[] Reclassify(IAlphabetBuilder<T> newAlphabet, IEnumerable<IEnumerable<int>> classes) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
70 Safe.ArgumentNotNull(newAlphabet, "newAlphabet");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
71 Safe.ArgumentNotNull(classes, "classes");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
72
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
73 var rmap = CreateReverseMap();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
74 var map = new int[rmap.Length];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
75
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
76 foreach (var cls in classes) {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
77 if (cls.Contains(DFAConst.UNCLASSIFIED_INPUT))
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
78 continue;
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
79
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
80 var symbols = new List<T>();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
81 foreach (var id in cls) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
82 if (id < 0 || id >= rmap.Length)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
83 throw new ArgumentOutOfRangeException(String.Format("Class {0} is not valid for the current alphabet", id));
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
84 if (rmap[id] != null)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
85 symbols.AddRange(rmap[id]);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
86 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
87
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
88 var newId = newAlphabet.DefineClass(symbols);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
89
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
90 foreach (var id in cls)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
91 map[id] = newId;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
92 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
93 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
94
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
95 public int Translate(T symobl) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
96 int cls;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
97 return m_map.TryGetValue(symobl, out cls) ? cls : DFAConst.UNCLASSIFIED_INPUT;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
98 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
99
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
100 public int Count {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
101 get {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
102 return m_nextCls;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
103 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
104 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
105
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
106 #endregion
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
107 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
108 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
109