Mercurial > pub > ImplabNet
comparison Implab/Automaton/EnumAlphabet.cs @ 162:0526412bbb26 ref20160224
DFA refactoring
author | cin |
---|---|
date | Wed, 24 Feb 2016 08:39:53 +0300 |
parents | |
children | ec35731ae299 |
comparison
equal
deleted
inserted
replaced
161:2a8466f0cb8a | 162:0526412bbb26 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.Diagnostics; | |
4 using System.Globalization; | |
5 using System.Linq; | |
6 using System.Diagnostics.CodeAnalysis; | |
7 | |
8 namespace Implab.Automaton { | |
9 /// <summary> | |
10 /// Алфавит символами которого являются элементы перечислений. | |
11 /// </summary> | |
12 /// <typeparam name="T">Тип перечислений</typeparam> | |
13 public class EnumAlphabet<T> : IndexedAlphabetBase<T> where T : struct, IConvertible { | |
14 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] | |
15 static readonly T[] _symbols; | |
16 static readonly EnumAlphabet<T> _fullAlphabet; | |
17 | |
18 [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")] | |
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() | |
49 : base(_symbols.Length) { | |
50 } | |
51 | |
52 public EnumAlphabet(int[] map) | |
53 : base(map) { | |
54 Debug.Assert(map.Length == _symbols.Length); | |
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 } |