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>
|
167
|
11 /// <remarks>
|
|
12 /// Indexed alphabets are usefull in bulting efficient translations from source alphabet
|
|
13 /// to the input alphabet of the automaton. It's assumed that the index to the symbol match
|
|
14 /// is well known and documented.
|
|
15 /// </remarks>
|
176
|
16 public abstract class IndexedAlphabetBase<T> : MapAlphabet<T> {
|
171
|
17
|
176
|
18 protected IndexedAlphabetBase() :base(true, null) {
|
172
|
19 }
|
|
20
|
162
|
21 public abstract int GetSymbolIndex(T symbol);
|
|
22
|
|
23 /// <summary>
|
|
24 /// Gets the translation map from the index of the symbol to it's class this is usefull for the optimized input symbols transtaion.
|
|
25 /// </summary>
|
176
|
26 /// <remarks>
|
|
27 /// The map is continous and start from the symbol with zero code. The last symbol
|
|
28 /// in the map is the last classified symbol in the alphabet, i.e. the map can be
|
|
29 /// shorter then the whole alphabet.
|
|
30 /// </remarks>
|
162
|
31 /// <returns>The translation map.</returns>
|
|
32 public int[] GetTranslationMap() {
|
180
|
33 var map = new Dictionary<int, int>();
|
176
|
34
|
180
|
35 int max = 0;
|
176
|
36 foreach (var p in Mappings) {
|
|
37 var index = GetSymbolIndex(p.Key);
|
|
38 max = Math.Max(max, index);
|
|
39 map[index] = p.Value;
|
|
40 }
|
|
41
|
|
42 var result = new int[max + 1];
|
|
43
|
|
44 for (int i = 0; i < result.Length; i++)
|
|
45 map.TryGetValue(i, out result[i]);
|
|
46
|
|
47 return result;
|
162
|
48 }
|
|
49 }
|
|
50 }
|