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

JSON moved to Formats namespace Working in RegularDFA
author cin
date Wed, 24 Feb 2016 20:12:52 +0300
parents 0526412bbb26
children ec35731ae299
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 public const int UNCLASSIFIED = 0;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
13
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
14 int m_nextId = 1;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
15 readonly int[] m_map;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
16
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
17 public int Count {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
18 get { return m_nextId; }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
19 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
20
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
21 protected IndexedAlphabetBase(int mapSize) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
22 m_map = new int[mapSize];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
23 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
24
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
25 protected IndexedAlphabetBase(int[] map) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
26 Debug.Assert(map != null);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
27
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
28 m_map = map;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
29 m_nextId = map.Max() + 1;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
30 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
31
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
32 public int DefineSymbol(T symbol) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
33 var index = GetSymbolIndex(symbol);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
34 if (m_map[index] == UNCLASSIFIED)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
35 m_map[index] = m_nextId++;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
36 return m_map[index];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
37 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
38
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
39 public int DefineClass(IEnumerable<T> symbols) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
40 Safe.ArgumentNotNull(symbols, "symbols");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
41 symbols = symbols.Distinct();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
42
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
43 foreach (var symbol in symbols) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
44 var index = GetSymbolIndex(symbol);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
45 if (m_map[index] == UNCLASSIFIED)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
46 m_map[GetSymbolIndex(symbol)] = m_nextId;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
47 else
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
48 throw new InvalidOperationException(String.Format("Symbol '{0}' already in use", symbol));
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
49 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
50 return m_nextId++;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
51 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
52
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
53 public List<T>[] CreateReverseMap() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
54 return
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
55 Enumerable.Range(UNCLASSIFIED, Count)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
56 .Select(
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
57 i => InputSymbols
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
58 .Where(x => i != UNCLASSIFIED && m_map[GetSymbolIndex(x)] == i)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
59 .ToList()
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
60 )
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
61 .ToArray();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
62 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
63
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
64 public int[] Reclassify(IAlphabetBuilder<T> newAlphabet, IEnumerable<IEnumerable<int>> classes) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
65 Safe.ArgumentNotNull(newAlphabet, "newAlphabet");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
66 Safe.ArgumentNotNull(classes, "classes");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
67 var reverseMap = CreateReverseMap();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
68
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
69 var translationMap = new int[Count];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
70
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
71 foreach (var scl in classes) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
72 // skip if the supper class contains the unclassified element
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
73 if (scl.Contains(UNCLASSIFIED))
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
74 continue;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
75 var range = new List<T>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
76 foreach (var cl in scl) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
77 if (cl < 0 || cl >= reverseMap.Length)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
78 throw new ArgumentOutOfRangeException(String.Format("Class {0} is not valid for the current alphabet", cl));
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
79 range.AddRange(reverseMap[cl]);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
80 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
81 var newClass = newAlphabet.DefineClass(range);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
82 foreach (var cl in scl)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
83 translationMap[cl] = newClass;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
84 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
85
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
86 return translationMap;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
87 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
88
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
89 public virtual int Translate(T symbol) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
90 return m_map[GetSymbolIndex(symbol)];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
91 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
92
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
93 public abstract int GetSymbolIndex(T symbol);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
94
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
95 public abstract IEnumerable<T> InputSymbols { get; }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
96
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
97 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
98 /// 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
99 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
100 /// <returns>The translation map.</returns>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
101 public int[] GetTranslationMap() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
102 return m_map;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
103 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
104 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
105 }