Mercurial > pub > ImplabNet
annotate Implab/Parsing/EnumAlphabet.cs @ 158:130781364799 v2
refactoring, code cleanup
author | cin |
---|---|
date | Thu, 18 Feb 2016 14:34:02 +0300 |
parents | 97fbbf816844 |
children |
rev | line source |
---|---|
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
65
diff
changeset
|
1 using System; |
55 | 2 using System.Collections.Generic; |
59 | 3 using System.Diagnostics; |
55 | 4 using System.Globalization; |
5 using System.Linq; | |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
65
diff
changeset
|
6 using System.Diagnostics.CodeAnalysis; |
55 | 7 |
8 namespace Implab.Parsing { | |
9 /// <summary> | |
10 /// Алфавит символами которого являются элементы перечислений. | |
11 /// </summary> | |
12 /// <typeparam name="T">Тип перечислений</typeparam> | |
158 | 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 | 15 static readonly T[] _symbols; |
16 static readonly EnumAlphabet<T> _fullAlphabet; | |
17 | |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
65
diff
changeset
|
18 [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")] |
55 | 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 } |