55
|
1 using Implab;
|
|
2 using System;
|
|
3 using System.Collections.Generic;
|
59
|
4 using System.Diagnostics;
|
55
|
5 using System.Globalization;
|
|
6 using System.Linq;
|
|
7 using System.Text;
|
|
8 using System.Threading.Tasks;
|
|
9
|
|
10 namespace Implab.Parsing {
|
|
11 /// <summary>
|
|
12 /// Алфавит символами которого являются элементы перечислений.
|
|
13 /// </summary>
|
|
14 /// <typeparam name="T">Тип перечислений</typeparam>
|
|
15 public class EnumAlphabet<T> : AlphabetBase<T> where T : struct, IConvertible {
|
|
16 static readonly T[] _symbols;
|
|
17 static readonly EnumAlphabet<T> _fullAlphabet;
|
|
18
|
|
19 static EnumAlphabet() {
|
|
20 if (!typeof(T).IsEnum)
|
|
21 throw new InvalidOperationException("Invalid generic parameter, enumeration is required");
|
|
22
|
|
23 if (Enum.GetUnderlyingType(typeof(T)) != typeof(Int32))
|
|
24 throw new InvalidOperationException("Only enums based on Int32 are supported");
|
|
25
|
|
26 _symbols = ((T[])Enum.GetValues(typeof(T)))
|
|
27 .OrderBy(x => x.ToInt32(CultureInfo.InvariantCulture))
|
|
28 .ToArray();
|
|
29
|
|
30 if (
|
|
31 _symbols[_symbols.Length - 1].ToInt32(CultureInfo.InvariantCulture) >= _symbols.Length
|
|
32 || _symbols[0].ToInt32(CultureInfo.InvariantCulture) != 0
|
|
33 )
|
|
34 throw new InvalidOperationException("The specified enumeration must be zero-based and continuously numbered");
|
|
35
|
|
36 _fullAlphabet = new EnumAlphabet<T>(_symbols.Select(x => x.ToInt32(CultureInfo.InvariantCulture)).ToArray());
|
|
37 }
|
|
38
|
|
39
|
|
40
|
|
41 public static EnumAlphabet<T> FullAlphabet {
|
|
42 get {
|
|
43 return _fullAlphabet;
|
|
44 }
|
|
45 }
|
|
46
|
|
47
|
|
48 public EnumAlphabet()
|
59
|
49 : base(_symbols.Length) {
|
55
|
50 }
|
|
51
|
|
52 public EnumAlphabet(int[] map)
|
|
53 : base(map) {
|
59
|
54 Debug.Assert(map.Length == _symbols.Length);
|
55
|
55 }
|
|
56
|
|
57
|
|
58 public override int GetSymbolIndex(T symbol) {
|
|
59 return symbol.ToInt32(CultureInfo.InvariantCulture);
|
|
60 }
|
|
61
|
|
62 public override IEnumerable<T> InputSymbols {
|
|
63 get { return _symbols; }
|
|
64 }
|
|
65
|
|
66 }
|
|
67 }
|