annotate Implab/Parsing/EnumAlphabet.cs @ 159:5558e43c79bb v2

minor fix
author cin
date Thu, 18 Feb 2016 16:06:14 +0300
parents 130781364799
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
156
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 65
diff changeset
1 using System;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
2 using System.Collections.Generic;
59
21611344d366 code cleanup
cin
parents: 55
diff changeset
3 using System.Diagnostics;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
4 using System.Globalization;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
5 using System.Linq;
156
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 65
diff changeset
6 using System.Diagnostics.CodeAnalysis;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
7
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
8 namespace Implab.Parsing {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
9 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
10 /// Алфавит символами которого являются элементы перечислений.
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
11 /// </summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
12 /// <typeparam name="T">Тип перечислений</typeparam>
158
130781364799 refactoring, code cleanup
cin
parents: 156
diff changeset
13 public class EnumAlphabet<T> : IndexedAlphabetBase<T> where T : struct, IConvertible {
156
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 65
diff changeset
14 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
15 static readonly T[] _symbols;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
16 static readonly EnumAlphabet<T> _fullAlphabet;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
17
156
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 65
diff changeset
18 [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
19 static EnumAlphabet() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
20 if (!typeof(T).IsEnum)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
21 throw new InvalidOperationException("Invalid generic parameter, enumeration is required");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
22
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
23 if (Enum.GetUnderlyingType(typeof(T)) != typeof(Int32))
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
24 throw new InvalidOperationException("Only enums based on Int32 are supported");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
25
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
26 _symbols = ((T[])Enum.GetValues(typeof(T)))
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
27 .OrderBy(x => x.ToInt32(CultureInfo.InvariantCulture))
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
28 .ToArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
29
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
30 if (
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
31 _symbols[_symbols.Length - 1].ToInt32(CultureInfo.InvariantCulture) >= _symbols.Length
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
32 || _symbols[0].ToInt32(CultureInfo.InvariantCulture) != 0
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
33 )
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
34 throw new InvalidOperationException("The specified enumeration must be zero-based and continuously numbered");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
35
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
36 _fullAlphabet = new EnumAlphabet<T>(_symbols.Select(x => x.ToInt32(CultureInfo.InvariantCulture)).ToArray());
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
37 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
38
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
39
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
40
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
41 public static EnumAlphabet<T> FullAlphabet {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
42 get {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
43 return _fullAlphabet;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
44 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
45 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
46
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
47
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
48 public EnumAlphabet()
59
21611344d366 code cleanup
cin
parents: 55
diff changeset
49 : base(_symbols.Length) {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
50 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
51
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
52 public EnumAlphabet(int[] map)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
53 : base(map) {
59
21611344d366 code cleanup
cin
parents: 55
diff changeset
54 Debug.Assert(map.Length == _symbols.Length);
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
55 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
56
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
57
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
58 public override int GetSymbolIndex(T symbol) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
59 return symbol.ToInt32(CultureInfo.InvariantCulture);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
60 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
61
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
62 public override IEnumerable<T> InputSymbols {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
63 get { return _symbols; }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
64 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
65
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
66 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
67 }