EnumAlphabet.cs
70 lines
| 2.6 KiB
| text/x-csharp
|
CSharpLexer
|
|
r162 | using System; | ||
| using System.Collections.Generic; | ||||
| using System.Diagnostics; | ||||
| using System.Globalization; | ||||
| using System.Linq; | ||||
| using System.Diagnostics.CodeAnalysis; | ||||
| namespace Implab.Automaton { | ||||
| /// <summary> | ||||
| /// Алфавит символами которого являются элементы перечислений. | ||||
| /// </summary> | ||||
| /// <typeparam name="T">Тип перечислений</typeparam> | ||||
| public class EnumAlphabet<T> : IndexedAlphabetBase<T> where T : struct, IConvertible { | ||||
| [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] | ||||
|
|
r164 | static readonly Lazy<T[]> _symbols = new Lazy<T[]>(GetSymbols); | ||
| [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] | ||||
| static readonly Lazy<EnumAlphabet<T>> _fullAlphabet = new Lazy<EnumAlphabet<T>>(CreateEnumAlphabet); | ||||
| static EnumAlphabet<T> CreateEnumAlphabet() { | ||||
| var symbols = _symbols.Value; | ||||
|
|
r162 | |||
|
|
r164 | if ( | ||
| symbols[symbols.Length - 1].ToInt32(CultureInfo.InvariantCulture) >= symbols.Length | ||||
| || symbols[0].ToInt32(CultureInfo.InvariantCulture) != 0 | ||||
| ) | ||||
| throw new InvalidOperationException("The specified enumeration must be zero-based and continuously numbered"); | ||||
| return new EnumAlphabet<T>(symbols.Select(x => x.ToInt32(CultureInfo.InvariantCulture)).ToArray()); | ||||
| } | ||||
| static T[] GetSymbols() { | ||||
|
|
r162 | if (!typeof(T).IsEnum) | ||
| throw new InvalidOperationException("Invalid generic parameter, enumeration is required"); | ||||
| if (Enum.GetUnderlyingType(typeof(T)) != typeof(Int32)) | ||||
| throw new InvalidOperationException("Only enums based on Int32 are supported"); | ||||
|
|
r164 | |||
| return ((T[])Enum.GetValues(typeof(T))) | ||||
|
|
r162 | .OrderBy(x => x.ToInt32(CultureInfo.InvariantCulture)) | ||
| .ToArray(); | ||||
| } | ||||
| public static EnumAlphabet<T> FullAlphabet { | ||||
| get { | ||||
|
|
r164 | return _fullAlphabet.Value; | ||
|
|
r162 | } | ||
| } | ||||
| public EnumAlphabet() | ||||
|
|
r164 | : base(_symbols.Value.Length) { | ||
|
|
r162 | } | ||
| public EnumAlphabet(int[] map) | ||||
| : base(map) { | ||||
|
|
r164 | Debug.Assert(map.Length == _symbols.Value.Length); | ||
|
|
r162 | } | ||
| public override int GetSymbolIndex(T symbol) { | ||||
| return symbol.ToInt32(CultureInfo.InvariantCulture); | ||||
| } | ||||
| public override IEnumerable<T> InputSymbols { | ||||
|
|
r164 | get { return _symbols.Value; } | ||
|
|
r162 | } | ||
| } | ||||
| } | ||||
