annotate Implab/Automaton/IndexedAlphabetBase.cs @ 209:a867536c68fc v2

Bound promise to CancellationToken Added new states to ExecutionSate enum. Added Safe.Guard() method to handle cleanup of the result of the promise
author cin
date Wed, 16 Nov 2016 03:06:08 +0300
parents c32688129f14
children
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>
167
cin
parents: 164
diff changeset
11 /// <remarks>
cin
parents: 164
diff changeset
12 /// Indexed alphabets are usefull in bulting efficient translations from source alphabet
cin
parents: 164
diff changeset
13 /// to the input alphabet of the automaton. It's assumed that the index to the symbol match
cin
parents: 164
diff changeset
14 /// is well known and documented.
cin
parents: 164
diff changeset
15 /// </remarks>
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
16 public abstract class IndexedAlphabetBase<T> : MapAlphabet<T> {
171
0f70905b4652 Working on regular DFA
cin
parents: 167
diff changeset
17
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
18 protected IndexedAlphabetBase() :base(true, null) {
172
92d5278d1b10 Working on text scanner
cin
parents: 171
diff changeset
19 }
92d5278d1b10 Working on text scanner
cin
parents: 171
diff changeset
20
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
21 public abstract int GetSymbolIndex(T symbol);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
22
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
23 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
24 /// 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
25 /// </summary>
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
26 /// <remarks>
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
27 /// The map is continous and start from the symbol with zero code. The last symbol
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
28 /// in the map is the last classified symbol in the alphabet, i.e. the map can be
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
29 /// shorter then the whole alphabet.
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
30 /// </remarks>
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
31 /// <returns>The translation map.</returns>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
32 public int[] GetTranslationMap() {
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 176
diff changeset
33 var map = new Dictionary<int, int>();
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
34
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 176
diff changeset
35 int max = 0;
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
36 foreach (var p in Mappings) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
37 var index = GetSymbolIndex(p.Key);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
38 max = Math.Max(max, index);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
39 map[index] = p.Value;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
40 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
41
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
42 var result = new int[max + 1];
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
43
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
44 for (int i = 0; i < result.Length; i++)
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
45 map.TryGetValue(i, out result[i]);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
46
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
47 return result;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
48 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
49 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
50 }