|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace Amplifier |
| 6 | +{ |
| 7 | + public enum DType |
| 8 | + { |
| 9 | + Float32 = 0, |
| 10 | + Float64 = 1, |
| 11 | + //Int8 = 2, |
| 12 | + //Int16 = 3, |
| 13 | + Int32 = 4, |
| 14 | + //Int64 = 5, |
| 15 | + UInt8 = 6, |
| 16 | + //UInt16 = 7, |
| 17 | + //UInt32 = 8, |
| 18 | + //UInt64 = 8 |
| 19 | + } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Class DTypeExtensions. |
| 23 | + /// </summary> |
| 24 | + public static class DTypeExtensions |
| 25 | + { |
| 26 | + /// <summary> |
| 27 | + /// Sizes the specified value. |
| 28 | + /// </summary> |
| 29 | + /// <param name="value">The value.</param> |
| 30 | + /// <returns>System.Int32.</returns> |
| 31 | + /// <exception cref="NotSupportedException">Element type " + value + " not supported.</exception> |
| 32 | + public static int Size(this DType value) |
| 33 | + { |
| 34 | + switch (value) |
| 35 | + { |
| 36 | + case DType.Float32: return 4; |
| 37 | + case DType.Float64: return 8; |
| 38 | + case DType.Int32: return 4; |
| 39 | + case DType.UInt8: return 1; |
| 40 | + //case DType.Int8: return 1; |
| 41 | + //case DType.UInt32: return 4; |
| 42 | + //case DType.Int16: return 2; |
| 43 | + //case DType.UInt16: return 2; |
| 44 | + //case DType.Int64: return 8; |
| 45 | + //case DType.UInt64: return 8; |
| 46 | + default: |
| 47 | + throw new NotSupportedException("Element type " + value + " not supported."); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Converts to clrtype. |
| 53 | + /// </summary> |
| 54 | + /// <param name="value">The value.</param> |
| 55 | + /// <returns>Type.</returns> |
| 56 | + /// <exception cref="NotSupportedException">Element type " + value + " not supported.</exception> |
| 57 | + public static Type ToCLRType(this DType value) |
| 58 | + { |
| 59 | + switch (value) |
| 60 | + { |
| 61 | + case DType.Float32: return typeof(float); |
| 62 | + case DType.Float64: return typeof(double); |
| 63 | + case DType.Int32: return typeof(int); |
| 64 | + case DType.UInt8: return typeof(byte); |
| 65 | + //case DType.Int8: return typeof(sbyte); |
| 66 | + //case DType.UInt32: return typeof(uint); |
| 67 | + //case DType.Int16: return typeof(short); |
| 68 | + //case DType.UInt16: return typeof(ushort); |
| 69 | + //case DType.Int64: return typeof(long); |
| 70 | + //case DType.UInt64: return typeof(ulong); |
| 71 | + default: |
| 72 | + throw new NotSupportedException("Element type " + value + " not supported."); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Class DTypeBuilder. |
| 79 | + /// </summary> |
| 80 | + public static class DTypeBuilder |
| 81 | + { |
| 82 | + /// <summary> |
| 83 | + /// Froms the type of the color. |
| 84 | + /// </summary> |
| 85 | + /// <param name="type">The type.</param> |
| 86 | + /// <returns>DType.</returns> |
| 87 | + /// <exception cref="NotSupportedException">No corresponding DType value for CLR type " + type</exception> |
| 88 | + public static DType FromCLRType(Type type) |
| 89 | + { |
| 90 | + if (type.Name.Contains("Single")) return DType.Float32; |
| 91 | + else if (type.Name.Contains("Double")) return DType.Float64; |
| 92 | + else if (type.Name.Contains("Int32")) return DType.Int32; |
| 93 | + else if (type.Name.Contains("Byte")) return DType.UInt8; |
| 94 | + else |
| 95 | + throw new NotSupportedException("No corresponding DType value for CLR type " + type); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments