annotate Implab/Automaton/EnumAlphabet.cs @ 163:419aa51b04fd ref20160224

JSON moved to Formats namespace Working in RegularDFA
author cin
date Wed, 24 Feb 2016 20:12:52 +0300
parents 0526412bbb26
children ec35731ae299
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")]
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
15 static readonly T[] _symbols;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
16 static readonly EnumAlphabet<T> _fullAlphabet;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
17
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
18 [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
19 static EnumAlphabet() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
20 if (!typeof(T).IsEnum)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
21 throw new InvalidOperationException("Invalid generic parameter, enumeration is required");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
22
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
23 if (Enum.GetUnderlyingType(typeof(T)) != typeof(Int32))
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
24 throw new InvalidOperationException("Only enums based on Int32 are supported");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
25
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
26 _symbols = ((T[])Enum.GetValues(typeof(T)))
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
27 .OrderBy(x => x.ToInt32(CultureInfo.InvariantCulture))
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
28 .ToArray();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
29
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
30 if (
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
31 _symbols[_symbols.Length - 1].ToInt32(CultureInfo.InvariantCulture) >= _symbols.Length
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
32 || _symbols[0].ToInt32(CultureInfo.InvariantCulture) != 0
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
33 )
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
34 throw new InvalidOperationException("The specified enumeration must be zero-based and continuously numbered");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
35
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
36 _fullAlphabet = new EnumAlphabet<T>(_symbols.Select(x => x.ToInt32(CultureInfo.InvariantCulture)).ToArray());
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
37 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
38
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
39
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
40
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
41 public static EnumAlphabet<T> FullAlphabet {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
42 get {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
43 return _fullAlphabet;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
44 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
45 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
46
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
47
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
48 public EnumAlphabet()
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
49 : base(_symbols.Length) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
50 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
51
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
52 public EnumAlphabet(int[] map)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
53 : base(map) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
54 Debug.Assert(map.Length == _symbols.Length);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
55 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
56
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
57
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
58 public override int GetSymbolIndex(T symbol) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
59 return symbol.ToInt32(CultureInfo.InvariantCulture);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
60 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
61
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
62 public override IEnumerable<T> InputSymbols {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
63 get { return _symbols; }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
64 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
65
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
66 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
67 }