annotate Implab/Automaton/EnumAlphabet.cs @ 230:3e26338eb977 v2

slowly cutting off mono specific settings
author cin
date Wed, 13 Sep 2017 16:55:13 +0300
parents c32688129f14
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
1 using System;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
2 using System.Collections.Generic;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
3 using System.Diagnostics;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
4 using System.Globalization;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
5 using System.Linq;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
6 using System.Diagnostics.CodeAnalysis;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
7
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
8 namespace Implab.Automaton {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
9 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
10 /// Алфавит символами которого являются элементы перечислений.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
11 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
12 /// <typeparam name="T">Тип перечислений</typeparam>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
13 public class EnumAlphabet<T> : IndexedAlphabetBase<T> where T : struct, IConvertible {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
14 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
15 static readonly Lazy<T[]> _symbols = new Lazy<T[]>(GetSymbols);
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
16
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
17 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
18 static readonly Lazy<EnumAlphabet<T>> _fullAlphabet = new Lazy<EnumAlphabet<T>>(CreateEnumAlphabet);
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
19
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
20 static EnumAlphabet<T> CreateEnumAlphabet() {
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
21 var symbols = _symbols.Value;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
22
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
23 if (
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
24 symbols[symbols.Length - 1].ToInt32(CultureInfo.InvariantCulture) >= symbols.Length
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
25 || symbols[0].ToInt32(CultureInfo.InvariantCulture) != 0
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
26 )
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
27 throw new InvalidOperationException("The specified enumeration must be zero-based and continuously numbered");
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
28
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
29 return new EnumAlphabet<T>(symbols.Select(x => x.ToInt32(CultureInfo.InvariantCulture)).ToArray());
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
30 }
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
31
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
32 static T[] GetSymbols() {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
33 if (!typeof(T).IsEnum)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
34 throw new InvalidOperationException("Invalid generic parameter, enumeration is required");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
35
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
36 if (Enum.GetUnderlyingType(typeof(T)) != typeof(Int32))
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
37 throw new InvalidOperationException("Only enums based on Int32 are supported");
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
38
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
39 return ((T[])Enum.GetValues(typeof(T)))
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
40 .OrderBy(x => x.ToInt32(CultureInfo.InvariantCulture))
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
41 .ToArray();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
42 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
43
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
44 public static EnumAlphabet<T> FullAlphabet {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
45 get {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
46 return _fullAlphabet.Value;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
47 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
48 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
49
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
50
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
51 public EnumAlphabet()
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
52 : base(_symbols.Value.Length) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
53 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
54
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
55 public EnumAlphabet(int[] map)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
56 : base(map) {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
57 Debug.Assert(map.Length == _symbols.Value.Length);
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
58 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
59
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
60
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
61 public override int GetSymbolIndex(T symbol) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
62 return symbol.ToInt32(CultureInfo.InvariantCulture);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
63 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
64
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
65 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
66 }