##// END OF EJS Templates
Merge pull request !2 from ImplabNet v3...
Merge pull request !2 from ImplabNet v3 Changes from branch: V3

File last commit:

r289:95896f882995 v3.0.14 v3
r294:abef3ebaa230 merge default
Show More
IndexedAlphabetBase.cs
50 lines | 1.8 KiB | text/x-csharp | CSharpLexer
/ Implab / src / Automaton / IndexedAlphabetBase.cs
cin
Added tests for Implab.ServiceHost.Unity configuration loader.
r289 using Implab;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Implab.Automaton {
/// <summary>
/// Indexed alphabet is the finite set of symbols where each symbol has a zero-based unique index.
/// </summary>
/// <remarks>
/// Indexed alphabets are usefull in bulting efficient translations from source alphabet
/// to the input alphabet of the automaton. It's assumed that the index to the symbol match
/// is well known and documented.
/// </remarks>
public abstract class IndexedAlphabetBase<T> : MapAlphabet<T> {
protected IndexedAlphabetBase() :base(true, null) {
}
public abstract int GetSymbolIndex(T symbol);
/// <summary>
/// Gets the translation map from the index of the symbol to it's class this is usefull for the optimized input symbols transtaion.
/// </summary>
/// <remarks>
/// The map is continous and start from the symbol with zero code. The last symbol
/// in the map is the last classified symbol in the alphabet, i.e. the map can be
/// shorter then the whole alphabet.
/// </remarks>
/// <returns>The translation map.</returns>
public int[] GetTranslationMap() {
var map = new Dictionary<int, int>();
int max = 0;
foreach (var p in Mappings) {
var index = GetSymbolIndex(p.Key);
max = Math.Max(max, index);
map[index] = p.Value;
}
var result = new int[max + 1];
for (int i = 0; i < result.Length; i++)
map.TryGetValue(i, out result[i]);
return result;
}
}
}