|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
| 5 | +using System; |
| 6 | + |
5 | 7 | namespace Microsoft.ML.Data
|
6 | 8 | {
|
7 | 9 | /// <summary>
|
@@ -76,5 +78,77 @@ public static bool SameSizeAndItemType(this ColumnType columnType, ColumnType ot
|
76 | 78 | return false;
|
77 | 79 | return vectorType.Size == otherVectorType.Size;
|
78 | 80 | }
|
| 81 | + |
| 82 | + public static PrimitiveType PrimitiveTypeFromType(Type type) |
| 83 | + { |
| 84 | + if (type == typeof(ReadOnlyMemory<char>) || type == typeof(string)) |
| 85 | + return TextType.Instance; |
| 86 | + if (type == typeof(bool)) |
| 87 | + return BoolType.Instance; |
| 88 | + if (type == typeof(TimeSpan)) |
| 89 | + return TimeSpanType.Instance; |
| 90 | + if (type == typeof(DateTime)) |
| 91 | + return DateTimeType.Instance; |
| 92 | + if (type == typeof(DateTimeOffset)) |
| 93 | + return DateTimeOffsetType.Instance; |
| 94 | + return NumberTypeFromType(type); |
| 95 | + } |
| 96 | + |
| 97 | + public static PrimitiveType PrimitiveTypeFromKind(DataKind kind) |
| 98 | + { |
| 99 | + if (kind == DataKind.TX) |
| 100 | + return TextType.Instance; |
| 101 | + if (kind == DataKind.BL) |
| 102 | + return BoolType.Instance; |
| 103 | + if (kind == DataKind.TS) |
| 104 | + return TimeSpanType.Instance; |
| 105 | + if (kind == DataKind.DT) |
| 106 | + return DateTimeType.Instance; |
| 107 | + if (kind == DataKind.DZ) |
| 108 | + return DateTimeOffsetType.Instance; |
| 109 | + return NumberTypeFromKind(kind); |
| 110 | + } |
| 111 | + |
| 112 | + public static NumberType NumberTypeFromType(Type type) |
| 113 | + { |
| 114 | + DataKind kind; |
| 115 | + if (type.TryGetDataKind(out kind)) |
| 116 | + return NumberTypeFromKind(kind); |
| 117 | + |
| 118 | + Contracts.Assert(false); |
| 119 | + throw new InvalidOperationException($"Bad type in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromType)}: {type}"); |
| 120 | + } |
| 121 | + |
| 122 | + public static NumberType NumberTypeFromKind(DataKind kind) |
| 123 | + { |
| 124 | + switch (kind) |
| 125 | + { |
| 126 | + case DataKind.I1: |
| 127 | + return NumberType.I1; |
| 128 | + case DataKind.U1: |
| 129 | + return NumberType.U1; |
| 130 | + case DataKind.I2: |
| 131 | + return NumberType.I2; |
| 132 | + case DataKind.U2: |
| 133 | + return NumberType.U2; |
| 134 | + case DataKind.I4: |
| 135 | + return NumberType.I4; |
| 136 | + case DataKind.U4: |
| 137 | + return NumberType.U4; |
| 138 | + case DataKind.I8: |
| 139 | + return NumberType.I8; |
| 140 | + case DataKind.U8: |
| 141 | + return NumberType.U8; |
| 142 | + case DataKind.R4: |
| 143 | + return NumberType.R4; |
| 144 | + case DataKind.R8: |
| 145 | + return NumberType.R8; |
| 146 | + case DataKind.UG: |
| 147 | + return NumberType.UG; |
| 148 | + } |
| 149 | + |
| 150 | + Contracts.Assert(false); |
| 151 | + throw new InvalidOperationException($"Bad data kind in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromKind)}: {kind}"); |
| 152 | + } |
79 | 153 | }
|
80 | 154 | }
|
0 commit comments