annotate Implab/Automaton/MapAlphabet.cs @ 163:419aa51b04fd ref20160224

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