annotate Implab/Formats/CharAlphabet.cs @ 244:eee3e49dd1ff v3

working on promises
author cin
date Thu, 25 Jan 2018 19:09:16 +0300
parents 302ca905c19e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
165
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
1 using System.Collections.Generic;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
2 using System.Linq;
165
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
3 using Implab.Automaton;
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
4 using System;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
5
165
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
6 namespace Implab.Formats {
236
302ca905c19e JsonReader optimizations
cin
parents: 228
diff changeset
7 public class CharAlphabet : IndexedAlphabetBase<char> {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
8
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
9 public override int GetSymbolIndex(char symbol) {
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
10 return symbol;
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
11 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
12
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
13 public IEnumerable<char> InputSymbols {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
14 get { return Enumerable.Range(char.MinValue, char.MaxValue).Cast<char>(); }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
15 }
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
16
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
17 public CharMap CreateCharMap() {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
18 var map = new Dictionary<int, int>();
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
19
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
20 int max = 0, min = char.MaxValue;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
21 foreach (var p in Mappings) {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
22 var index = GetSymbolIndex(p.Key);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
23 max = Math.Max(max, index);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
24 min = Math.Min(min, index);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
25 map[index] = p.Value;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
26 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
27
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
28 var result = new int[max - min + 1];
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
29
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
30 for (int i = 0; i < result.Length; i++)
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
31 map.TryGetValue(min + i, out result[i]);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
32
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
33 return new CharMap((char)min, result);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 177
diff changeset
34 }
164
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
35 }
ec35731ae299 Almost complete DFA refactoring
cin
parents:
diff changeset
36 }