162
|
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")]
|
164
|
15 static readonly Lazy<T[]> _symbols = new Lazy<T[]>(GetSymbols);
|
|
16
|
|
17 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
|
|
18 static readonly Lazy<EnumAlphabet<T>> _fullAlphabet = new Lazy<EnumAlphabet<T>>(CreateEnumAlphabet);
|
|
19
|
|
20 static EnumAlphabet<T> CreateEnumAlphabet() {
|
|
21 var symbols = _symbols.Value;
|
162
|
22
|
164
|
23 if (
|
|
24 symbols[symbols.Length - 1].ToInt32(CultureInfo.InvariantCulture) >= symbols.Length
|
|
25 || symbols[0].ToInt32(CultureInfo.InvariantCulture) != 0
|
|
26 )
|
|
27 throw new InvalidOperationException("The specified enumeration must be zero-based and continuously numbered");
|
|
28
|
|
29 return new EnumAlphabet<T>(symbols.Select(x => x.ToInt32(CultureInfo.InvariantCulture)).ToArray());
|
|
30 }
|
|
31
|
|
32 static T[] GetSymbols() {
|
162
|
33 if (!typeof(T).IsEnum)
|
|
34 throw new InvalidOperationException("Invalid generic parameter, enumeration is required");
|
|
35
|
|
36 if (Enum.GetUnderlyingType(typeof(T)) != typeof(Int32))
|
|
37 throw new InvalidOperationException("Only enums based on Int32 are supported");
|
164
|
38
|
|
39 return ((T[])Enum.GetValues(typeof(T)))
|
162
|
40 .OrderBy(x => x.ToInt32(CultureInfo.InvariantCulture))
|
|
41 .ToArray();
|
|
42 }
|
|
43
|
|
44 public static EnumAlphabet<T> FullAlphabet {
|
|
45 get {
|
164
|
46 return _fullAlphabet.Value;
|
162
|
47 }
|
|
48 }
|
|
49
|
|
50
|
|
51 public EnumAlphabet()
|
164
|
52 : base(_symbols.Value.Length) {
|
162
|
53 }
|
|
54
|
|
55 public EnumAlphabet(int[] map)
|
|
56 : base(map) {
|
164
|
57 Debug.Assert(map.Length == _symbols.Value.Length);
|
162
|
58 }
|
|
59
|
|
60
|
|
61 public override int GetSymbolIndex(T symbol) {
|
|
62 return symbol.ToInt32(CultureInfo.InvariantCulture);
|
|
63 }
|
|
64
|
|
65 public override IEnumerable<T> InputSymbols {
|
164
|
66 get { return _symbols.Value; }
|
162
|
67 }
|
|
68
|
|
69 }
|
|
70 }
|