162
|
1 using Implab;
|
|
2 using System;
|
|
3 using System.Collections.Generic;
|
|
4 using System.Diagnostics;
|
|
5 using System.Linq;
|
|
6
|
|
7 namespace Implab.Automaton {
|
|
8 /// <summary>
|
|
9 /// Indexed alphabet is the finite set of symbols where each symbol has a zero-based unique index.
|
|
10 /// </summary>
|
|
11 public abstract class IndexedAlphabetBase<T> : IAlphabetBuilder<T> {
|
|
12 int m_nextId = 1;
|
|
13 readonly int[] m_map;
|
|
14
|
|
15 public int Count {
|
|
16 get { return m_nextId; }
|
|
17 }
|
|
18
|
|
19 protected IndexedAlphabetBase(int mapSize) {
|
|
20 m_map = new int[mapSize];
|
|
21 }
|
|
22
|
|
23 protected IndexedAlphabetBase(int[] map) {
|
|
24 Debug.Assert(map != null);
|
|
25
|
|
26 m_map = map;
|
|
27 m_nextId = map.Max() + 1;
|
|
28 }
|
|
29
|
|
30 public int DefineSymbol(T symbol) {
|
|
31 var index = GetSymbolIndex(symbol);
|
164
|
32 if (m_map[index] == DFAConst.UNCLASSIFIED_INPUT)
|
162
|
33 m_map[index] = m_nextId++;
|
|
34 return m_map[index];
|
|
35 }
|
|
36
|
|
37 public int DefineClass(IEnumerable<T> symbols) {
|
|
38 Safe.ArgumentNotNull(symbols, "symbols");
|
|
39 symbols = symbols.Distinct();
|
|
40
|
|
41 foreach (var symbol in symbols) {
|
|
42 var index = GetSymbolIndex(symbol);
|
164
|
43 if (m_map[index] == DFAConst.UNCLASSIFIED_INPUT)
|
162
|
44 m_map[GetSymbolIndex(symbol)] = m_nextId;
|
|
45 else
|
|
46 throw new InvalidOperationException(String.Format("Symbol '{0}' already in use", symbol));
|
|
47 }
|
|
48 return m_nextId++;
|
|
49 }
|
|
50
|
|
51 public List<T>[] CreateReverseMap() {
|
|
52 return
|
164
|
53 Enumerable.Range(0, Count)
|
162
|
54 .Select(
|
|
55 i => InputSymbols
|
164
|
56 .Where(x => i != DFAConst.UNCLASSIFIED_INPUT && m_map[GetSymbolIndex(x)] == i)
|
162
|
57 .ToList()
|
|
58 )
|
|
59 .ToArray();
|
|
60 }
|
|
61
|
|
62 public int[] Reclassify(IAlphabetBuilder<T> newAlphabet, IEnumerable<IEnumerable<int>> classes) {
|
|
63 Safe.ArgumentNotNull(newAlphabet, "newAlphabet");
|
|
64 Safe.ArgumentNotNull(classes, "classes");
|
|
65 var reverseMap = CreateReverseMap();
|
|
66
|
|
67 var translationMap = new int[Count];
|
|
68
|
|
69 foreach (var scl in classes) {
|
|
70 // skip if the supper class contains the unclassified element
|
164
|
71 if (scl.Contains(DFAConst.UNCLASSIFIED_INPUT))
|
162
|
72 continue;
|
|
73 var range = new List<T>();
|
|
74 foreach (var cl in scl) {
|
|
75 if (cl < 0 || cl >= reverseMap.Length)
|
|
76 throw new ArgumentOutOfRangeException(String.Format("Class {0} is not valid for the current alphabet", cl));
|
|
77 range.AddRange(reverseMap[cl]);
|
|
78 }
|
|
79 var newClass = newAlphabet.DefineClass(range);
|
|
80 foreach (var cl in scl)
|
|
81 translationMap[cl] = newClass;
|
|
82 }
|
|
83
|
|
84 return translationMap;
|
|
85 }
|
|
86
|
|
87 public virtual int Translate(T symbol) {
|
|
88 return m_map[GetSymbolIndex(symbol)];
|
|
89 }
|
|
90
|
|
91 public abstract int GetSymbolIndex(T symbol);
|
|
92
|
|
93 public abstract IEnumerable<T> InputSymbols { get; }
|
|
94
|
|
95 /// <summary>
|
|
96 /// Gets the translation map from the index of the symbol to it's class this is usefull for the optimized input symbols transtaion.
|
|
97 /// </summary>
|
|
98 /// <returns>The translation map.</returns>
|
|
99 public int[] GetTranslationMap() {
|
|
100 return m_map;
|
|
101 }
|
|
102 }
|
|
103 }
|