228
|
1 using Implab.Automaton;
|
|
2 using System;
|
|
3 using System.Collections.Generic;
|
|
4 using System.Linq;
|
|
5 using System.Runtime.CompilerServices;
|
|
6 using System.Text;
|
|
7 using System.Threading.Tasks;
|
|
8
|
|
9 namespace Implab.Formats {
|
|
10 public class CharMap : IAlphabet<char> {
|
|
11 readonly char m_min;
|
|
12 readonly char m_max;
|
|
13 readonly int[] m_map;
|
|
14
|
|
15 public CharMap(char min, int[] map) {
|
|
16 Safe.ArgumentNotNull(map, nameof(map));
|
|
17 Count = map.Max()+1;
|
|
18 m_min = min;
|
|
19 m_map = map;
|
|
20 m_max = (char)(min + map.Length);
|
|
21 }
|
|
22
|
|
23 public int Count {
|
|
24 get; private set;
|
|
25 }
|
|
26
|
|
27 public bool Contains(char symbol) {
|
236
|
28 return symbol >= m_min && symbol <= m_max && m_map[symbol-m_min] != AutomatonConst.UnclassifiedInput;
|
228
|
29 }
|
|
30
|
|
31 public IEnumerable<char> GetSymbols(int cls) {
|
|
32 for (var i = 0; i < m_map.Length; i++)
|
|
33 if (m_map[i] == cls)
|
|
34 yield return (char)(i + m_min);
|
|
35 }
|
|
36
|
|
37 [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
38 public int Translate(char symbol) {
|
236
|
39 return symbol >= m_min && symbol <= m_max ? m_map[symbol-m_min] : AutomatonConst.UnclassifiedInput;
|
228
|
40 }
|
|
41 }
|
|
42 }
|