annotate Implab/Parsing/EnumAlphabet.cs @ 155:037df317f126 v2

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