diff Implab/Formats/CharAlphabet.cs @ 228:6fa235c5a760 v2

Rewritten JsonScanner, JsonParser, fixed naming style
author cin
date Tue, 12 Sep 2017 01:19:12 +0300
parents a0ff6a0e9c44
children 302ca905c19e
line wrap: on
line diff
--- a/Implab/Formats/CharAlphabet.cs	Sat Sep 09 03:53:13 2017 +0300
+++ b/Implab/Formats/CharAlphabet.cs	Tue Sep 12 01:19:12 2017 +0300
@@ -1,6 +1,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using Implab.Automaton;
+using System;
 
 namespace Implab.Formats {
     public class CharAlphabet: IndexedAlphabetBase<char> {
@@ -12,5 +13,24 @@
         public IEnumerable<char> InputSymbols {
             get { return Enumerable.Range(char.MinValue, char.MaxValue).Cast<char>(); }
         }
+
+        public CharMap CreateCharMap() {
+            var map = new Dictionary<int, int>();
+
+            int max = 0, min = char.MaxValue;
+            foreach (var p in Mappings) {
+                var index = GetSymbolIndex(p.Key);
+                max = Math.Max(max, index);
+                min = Math.Min(min, index);
+                map[index] = p.Value;
+            }
+
+            var result = new int[max - min + 1];
+
+            for (int i = 0; i < result.Length; i++)
+                map.TryGetValue(min + i, out result[i]);
+
+            return new CharMap((char)min, result);
+        }
     }
 }