|
5 | 5 | using System; |
6 | 6 | using Microsoft.ML.Runtime; |
7 | 7 |
|
8 | | -namespace Microsoft.ML.Data |
| 8 | +namespace Microsoft.ML.Data; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Extension methods related to the ColumnType class. |
| 12 | +/// </summary> |
| 13 | +[BestFriend] |
| 14 | +internal static class ColumnTypeExtensions |
9 | 15 | { |
10 | 16 | /// <summary> |
11 | | - /// Extension methods related to the ColumnType class. |
| 17 | + /// Whether this type is a standard scalar type completely determined by its <see cref="DataViewType.RawType"/> |
| 18 | + /// (not a <see cref="KeyDataViewType"/> or <see cref="StructuredDataViewType"/>, etc). |
| 19 | + /// </summary> |
| 20 | + public static bool IsStandardScalar(this DataViewType columnType) => |
| 21 | + (columnType is NumberDataViewType) || (columnType is TextDataViewType) || (columnType is BooleanDataViewType) || |
| 22 | + (columnType is RowIdDataViewType) || (columnType is TimeSpanDataViewType) || |
| 23 | + (columnType is DateTimeDataViewType) || (columnType is DateTimeOffsetDataViewType); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Zero return means it's not a key type. |
| 27 | + /// </summary> |
| 28 | + public static ulong GetKeyCount(this DataViewType columnType) => (columnType as KeyDataViewType)?.Count ?? 0; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Sometimes it is necessary to cast the Count to an int. This performs overflow check. |
| 32 | + /// Zero return means it's not a key type. |
12 | 33 | /// </summary> |
13 | | - [BestFriend] |
14 | | - internal static class ColumnTypeExtensions |
| 34 | + public static int GetKeyCountAsInt32(this DataViewType columnType, IExceptionContext ectx = null) |
15 | 35 | { |
16 | | - /// <summary> |
17 | | - /// Whether this type is a standard scalar type completely determined by its <see cref="DataViewType.RawType"/> |
18 | | - /// (not a <see cref="KeyDataViewType"/> or <see cref="StructuredDataViewType"/>, etc). |
19 | | - /// </summary> |
20 | | - public static bool IsStandardScalar(this DataViewType columnType) => |
21 | | - (columnType is NumberDataViewType) || (columnType is TextDataViewType) || (columnType is BooleanDataViewType) || |
22 | | - (columnType is RowIdDataViewType) || (columnType is TimeSpanDataViewType) || |
23 | | - (columnType is DateTimeDataViewType) || (columnType is DateTimeOffsetDataViewType); |
24 | | - |
25 | | - /// <summary> |
26 | | - /// Zero return means it's not a key type. |
27 | | - /// </summary> |
28 | | - public static ulong GetKeyCount(this DataViewType columnType) => (columnType as KeyDataViewType)?.Count ?? 0; |
29 | | - |
30 | | - /// <summary> |
31 | | - /// Sometimes it is necessary to cast the Count to an int. This performs overflow check. |
32 | | - /// Zero return means it's not a key type. |
33 | | - /// </summary> |
34 | | - public static int GetKeyCountAsInt32(this DataViewType columnType, IExceptionContext ectx = null) |
35 | | - { |
36 | | - ulong count = columnType.GetKeyCount(); |
37 | | - ectx.Check(count <= int.MaxValue, nameof(KeyDataViewType) + "." + nameof(KeyDataViewType.Count) + " exceeds int.MaxValue."); |
38 | | - return (int)count; |
39 | | - } |
| 36 | + ulong count = columnType.GetKeyCount(); |
| 37 | + ectx.Check(count <= int.MaxValue, nameof(KeyDataViewType) + "." + nameof(KeyDataViewType.Count) + " exceeds int.MaxValue."); |
| 38 | + return (int)count; |
| 39 | + } |
40 | 40 |
|
41 | | - /// <summary> |
42 | | - /// For non-vector types, this returns the column type itself (i.e., return <paramref name="columnType"/>). |
43 | | - /// For vector types, this returns the type of the items stored as values in vector. |
44 | | - /// </summary> |
45 | | - public static DataViewType GetItemType(this DataViewType columnType) => (columnType as VectorDataViewType)?.ItemType ?? columnType; |
46 | | - |
47 | | - /// <summary> |
48 | | - /// Zero return means either it's not a vector or the size is unknown. |
49 | | - /// </summary> |
50 | | - public static int GetVectorSize(this DataViewType columnType) => (columnType as VectorDataViewType)?.Size ?? 0; |
51 | | - |
52 | | - /// <summary> |
53 | | - /// For non-vectors, this returns one. For unknown size vectors, it returns zero. |
54 | | - /// For known sized vectors, it returns size. |
55 | | - /// </summary> |
56 | | - public static int GetValueCount(this DataViewType columnType) => (columnType as VectorDataViewType)?.Size ?? 1; |
57 | | - |
58 | | - /// <summary> |
59 | | - /// Whether this is a vector type with known size. Returns false for non-vector types. |
60 | | - /// Equivalent to <c><see cref="GetVectorSize"/> > 0</c>. |
61 | | - /// </summary> |
62 | | - public static bool IsKnownSizeVector(this DataViewType columnType) => columnType.GetVectorSize() > 0; |
63 | | - |
64 | | - /// <summary> |
65 | | - /// Gets the equivalent <see cref="InternalDataKind"/> for the <paramref name="columnType"/>'s RawType. |
66 | | - /// This can return default(<see cref="InternalDataKind"/>) if the RawType doesn't have a corresponding |
67 | | - /// <see cref="InternalDataKind"/>. |
68 | | - /// </summary> |
69 | | - public static InternalDataKind GetRawKind(this DataViewType columnType) |
70 | | - { |
71 | | - columnType.RawType.TryGetDataKind(out InternalDataKind result); |
72 | | - return result; |
73 | | - } |
| 41 | + /// <summary> |
| 42 | + /// For non-vector types, this returns the column type itself (i.e., return <paramref name="columnType"/>). |
| 43 | + /// For vector types, this returns the type of the items stored as values in vector. |
| 44 | + /// </summary> |
| 45 | + public static DataViewType GetItemType(this DataViewType columnType) => (columnType as VectorDataViewType)?.ItemType ?? columnType; |
74 | 46 |
|
75 | | - /// <summary> |
76 | | - /// Equivalent to calling Equals(ColumnType) for non-vector types. For vector type, |
77 | | - /// returns true if current and other vector types have the same size and item type. |
78 | | - /// </summary> |
79 | | - public static bool SameSizeAndItemType(this DataViewType columnType, DataViewType other) |
80 | | - { |
81 | | - if (other == null) |
82 | | - return false; |
83 | | - |
84 | | - if (columnType.Equals(other)) |
85 | | - return true; |
86 | | - |
87 | | - // For vector types, we don't care about the factoring of the dimensions. |
88 | | - if (!(columnType is VectorDataViewType vectorType) || !(other is VectorDataViewType otherVectorType)) |
89 | | - return false; |
90 | | - if (!vectorType.ItemType.Equals(otherVectorType.ItemType)) |
91 | | - return false; |
92 | | - return vectorType.Size == otherVectorType.Size; |
93 | | - } |
| 47 | + /// <summary> |
| 48 | + /// Zero return means either it's not a vector or the size is unknown. |
| 49 | + /// </summary> |
| 50 | + public static int GetVectorSize(this DataViewType columnType) => (columnType as VectorDataViewType)?.Size ?? 0; |
94 | 51 |
|
95 | | - public static PrimitiveDataViewType PrimitiveTypeFromType(Type type) |
96 | | - { |
97 | | - if (type == typeof(ReadOnlyMemory<char>) || type == typeof(string)) |
98 | | - return TextDataViewType.Instance; |
99 | | - if (type == typeof(bool)) |
100 | | - return BooleanDataViewType.Instance; |
101 | | - if (type == typeof(TimeSpan)) |
102 | | - return TimeSpanDataViewType.Instance; |
103 | | - if (type == typeof(DateTime)) |
104 | | - return DateTimeDataViewType.Instance; |
105 | | - if (type == typeof(DateTimeOffset)) |
106 | | - return DateTimeOffsetDataViewType.Instance; |
107 | | - if (type == typeof(DataViewRowId)) |
108 | | - return RowIdDataViewType.Instance; |
109 | | - return NumberTypeFromType(type); |
110 | | - } |
| 52 | + /// <summary> |
| 53 | + /// For non-vectors, this returns one. For unknown size vectors, it returns zero. |
| 54 | + /// For known sized vectors, it returns size. |
| 55 | + /// </summary> |
| 56 | + public static int GetValueCount(this DataViewType columnType) => (columnType as VectorDataViewType)?.Size ?? 1; |
111 | 57 |
|
112 | | - public static PrimitiveDataViewType PrimitiveTypeFromKind(InternalDataKind kind) |
113 | | - { |
114 | | - if (kind == InternalDataKind.TX) |
115 | | - return TextDataViewType.Instance; |
116 | | - if (kind == InternalDataKind.BL) |
117 | | - return BooleanDataViewType.Instance; |
118 | | - if (kind == InternalDataKind.TS) |
119 | | - return TimeSpanDataViewType.Instance; |
120 | | - if (kind == InternalDataKind.DT) |
121 | | - return DateTimeDataViewType.Instance; |
122 | | - if (kind == InternalDataKind.DZ) |
123 | | - return DateTimeOffsetDataViewType.Instance; |
124 | | - if (kind == InternalDataKind.UG) |
125 | | - return RowIdDataViewType.Instance; |
126 | | - return NumberTypeFromKind(kind); |
127 | | - } |
| 58 | + /// <summary> |
| 59 | + /// Whether this is a vector type with known size. Returns false for non-vector types. |
| 60 | + /// Equivalent to <c><see cref="GetVectorSize"/> > 0</c>. |
| 61 | + /// </summary> |
| 62 | + public static bool IsKnownSizeVector(this DataViewType columnType) => columnType.GetVectorSize() > 0; |
128 | 63 |
|
129 | | - public static NumberDataViewType NumberTypeFromType(Type type) |
130 | | - { |
131 | | - InternalDataKind kind; |
132 | | - if (type.TryGetDataKind(out kind)) |
133 | | - return NumberTypeFromKind(kind); |
| 64 | + /// <summary> |
| 65 | + /// Gets the equivalent <see cref="InternalDataKind"/> for the <paramref name="columnType"/>'s RawType. |
| 66 | + /// This can return default(<see cref="InternalDataKind"/>) if the RawType doesn't have a corresponding |
| 67 | + /// <see cref="InternalDataKind"/>. |
| 68 | + /// </summary> |
| 69 | + public static InternalDataKind GetRawKind(this DataViewType columnType) |
| 70 | + { |
| 71 | + columnType.RawType.TryGetDataKind(out InternalDataKind result); |
| 72 | + return result; |
| 73 | + } |
134 | 74 |
|
135 | | - Contracts.Assert(false); |
136 | | - throw new InvalidOperationException($"Bad type in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromType)}: {type}"); |
137 | | - } |
| 75 | + /// <summary> |
| 76 | + /// Equivalent to calling Equals(ColumnType) for non-vector types. For vector type, |
| 77 | + /// returns true if current and other vector types have the same size and item type. |
| 78 | + /// </summary> |
| 79 | + public static bool SameSizeAndItemType(this DataViewType columnType, DataViewType other) |
| 80 | + { |
| 81 | + if (other == null) |
| 82 | + return false; |
| 83 | + |
| 84 | + if (columnType.Equals(other)) |
| 85 | + return true; |
| 86 | + |
| 87 | + // For vector types, we don't care about the factoring of the dimensions. |
| 88 | + if (!(columnType is VectorDataViewType vectorType) || !(other is VectorDataViewType otherVectorType)) |
| 89 | + return false; |
| 90 | + if (!vectorType.ItemType.Equals(otherVectorType.ItemType)) |
| 91 | + return false; |
| 92 | + return vectorType.Size == otherVectorType.Size; |
| 93 | + } |
138 | 94 |
|
139 | | - private static NumberDataViewType NumberTypeFromKind(InternalDataKind kind) |
| 95 | + public static PrimitiveDataViewType PrimitiveTypeFromType(Type type) |
| 96 | + { |
| 97 | + if (type == typeof(ReadOnlyMemory<char>) || type == typeof(string)) |
| 98 | + return TextDataViewType.Instance; |
| 99 | + if (type == typeof(bool)) |
| 100 | + return BooleanDataViewType.Instance; |
| 101 | + if (type == typeof(TimeSpan)) |
| 102 | + return TimeSpanDataViewType.Instance; |
| 103 | + if (type == typeof(DateTime)) |
| 104 | + return DateTimeDataViewType.Instance; |
| 105 | + if (type == typeof(DateTimeOffset)) |
| 106 | + return DateTimeOffsetDataViewType.Instance; |
| 107 | + if (type == typeof(DataViewRowId)) |
| 108 | + return RowIdDataViewType.Instance; |
| 109 | + return NumberTypeFromType(type); |
| 110 | + } |
| 111 | + |
| 112 | + public static PrimitiveDataViewType PrimitiveTypeFromKind(InternalDataKind kind) |
| 113 | + { |
| 114 | + if (kind == InternalDataKind.TX) |
| 115 | + return TextDataViewType.Instance; |
| 116 | + if (kind == InternalDataKind.BL) |
| 117 | + return BooleanDataViewType.Instance; |
| 118 | + if (kind == InternalDataKind.TS) |
| 119 | + return TimeSpanDataViewType.Instance; |
| 120 | + if (kind == InternalDataKind.DT) |
| 121 | + return DateTimeDataViewType.Instance; |
| 122 | + if (kind == InternalDataKind.DZ) |
| 123 | + return DateTimeOffsetDataViewType.Instance; |
| 124 | + if (kind == InternalDataKind.UG) |
| 125 | + return RowIdDataViewType.Instance; |
| 126 | + return NumberTypeFromKind(kind); |
| 127 | + } |
| 128 | + |
| 129 | + public static NumberDataViewType NumberTypeFromType(Type type) |
| 130 | + { |
| 131 | + InternalDataKind kind; |
| 132 | + if (type.TryGetDataKind(out kind)) |
| 133 | + return NumberTypeFromKind(kind); |
| 134 | + |
| 135 | + Contracts.Assert(false); |
| 136 | + throw new InvalidOperationException($"Bad type in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromType)}: {type}"); |
| 137 | + } |
| 138 | + |
| 139 | + private static NumberDataViewType NumberTypeFromKind(InternalDataKind kind) |
| 140 | + { |
| 141 | + switch (kind) |
140 | 142 | { |
141 | | - switch (kind) |
142 | | - { |
143 | | - case InternalDataKind.I1: |
144 | | - return NumberDataViewType.SByte; |
145 | | - case InternalDataKind.U1: |
146 | | - return NumberDataViewType.Byte; |
147 | | - case InternalDataKind.I2: |
148 | | - return NumberDataViewType.Int16; |
149 | | - case InternalDataKind.U2: |
150 | | - return NumberDataViewType.UInt16; |
151 | | - case InternalDataKind.I4: |
152 | | - return NumberDataViewType.Int32; |
153 | | - case InternalDataKind.U4: |
154 | | - return NumberDataViewType.UInt32; |
155 | | - case InternalDataKind.I8: |
156 | | - return NumberDataViewType.Int64; |
157 | | - case InternalDataKind.U8: |
158 | | - return NumberDataViewType.UInt64; |
159 | | - case InternalDataKind.R4: |
160 | | - return NumberDataViewType.Single; |
161 | | - case InternalDataKind.R8: |
162 | | - return NumberDataViewType.Double; |
163 | | - } |
164 | | - |
165 | | - Contracts.Assert(false); |
166 | | - throw new InvalidOperationException($"Bad data kind in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromKind)}: {kind}"); |
| 143 | + case InternalDataKind.I1: |
| 144 | + return NumberDataViewType.SByte; |
| 145 | + case InternalDataKind.U1: |
| 146 | + return NumberDataViewType.Byte; |
| 147 | + case InternalDataKind.I2: |
| 148 | + return NumberDataViewType.Int16; |
| 149 | + case InternalDataKind.U2: |
| 150 | + return NumberDataViewType.UInt16; |
| 151 | + case InternalDataKind.I4: |
| 152 | + return NumberDataViewType.Int32; |
| 153 | + case InternalDataKind.U4: |
| 154 | + return NumberDataViewType.UInt32; |
| 155 | + case InternalDataKind.I8: |
| 156 | + return NumberDataViewType.Int64; |
| 157 | + case InternalDataKind.U8: |
| 158 | + return NumberDataViewType.UInt64; |
| 159 | + case InternalDataKind.R4: |
| 160 | + return NumberDataViewType.Single; |
| 161 | + case InternalDataKind.R8: |
| 162 | + return NumberDataViewType.Double; |
167 | 163 | } |
| 164 | + |
| 165 | + Contracts.Assert(false); |
| 166 | + throw new InvalidOperationException($"Bad data kind in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromKind)}: {kind}"); |
168 | 167 | } |
169 | 168 | } |
0 commit comments