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

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