Skip to content

Replace DvText with .NET Standard type. #705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Api/ApiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private static OpCode GetAssignmentOpCode(Type t)
// REVIEW: This should be a Dictionary<Type, OpCode> based solution.
// DvTypes, strings, arrays, all nullable types, VBuffers and UInt128.
if (t == typeof(DvInt8) || t == typeof(DvInt4) || t == typeof(DvInt2) || t == typeof(DvInt1) ||
t == typeof(DvBool) || t == typeof(DvText) || t == typeof(string) || t.IsArray ||
t == typeof(DvBool) || t == typeof(ReadOnlyMemory<char>) || t == typeof(string) || t.IsArray ||
(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(VBuffer<>)) ||
(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) ||
t == typeof(DvDateTime) || t == typeof(DvDateTimeZone) || t == typeof(DvTimeSpan) || t == typeof(UInt128))
Expand Down
32 changes: 16 additions & 16 deletions src/Microsoft.ML.Api/DataViewConstructionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ private Delegate CreateGetter(int index)
if (outputType.IsArray)
{
Ch.Assert(colType.IsVector);
// String[] -> VBuffer<DvText>
// String[] -> ReadOnlyMemory<char>
if (outputType.GetElementType() == typeof(string))
{
Ch.Assert(colType.ItemType.IsText);
return CreateConvertingArrayGetterDelegate<String, DvText>(index, x => x == null ? DvText.NA : new DvText(x));
return CreateConvertingArrayGetterDelegate<string, ReadOnlyMemory<char>>(index, x => x != null ? x.AsMemory() : "".AsMemory() );
}
else if (outputType.GetElementType() == typeof(int))
{
Expand Down Expand Up @@ -193,7 +193,7 @@ private Delegate CreateGetter(int index)
else if (colType.IsVector)
{
// VBuffer<T> -> VBuffer<T>
// REVIEW: Do we care about accomodating VBuffer<string> -> VBuffer<DvText>?
// REVIEW: Do we care about accomodating VBuffer<string> -> ReadOnlyMemory<char>?
Ch.Assert(outputType.IsGenericType);
Ch.Assert(outputType.GetGenericTypeDefinition() == typeof(VBuffer<>));
Ch.Assert(outputType.GetGenericArguments()[0] == colType.ItemType.RawType);
Expand All @@ -204,9 +204,9 @@ private Delegate CreateGetter(int index)
{
if (outputType == typeof(string))
{
// String -> DvText
// String -> ReadOnlyMemory<char>
Ch.Assert(colType.IsText);
return CreateConvertingGetterDelegate<String, DvText>(index, x => x == null ? DvText.NA : new DvText(x));
return CreateConvertingGetterDelegate<String, ReadOnlyMemory<char>>(index, x => x != null ? x.AsMemory() : "".AsMemory());
}
else if (outputType == typeof(bool))
{
Expand Down Expand Up @@ -805,12 +805,12 @@ public override ValueGetter<TDst> GetGetter<TDst>()
var itemType = typeT.GetElementType();
var dstItemType = typeof(TDst).GetGenericArguments()[0];

// String[] -> VBuffer<DvText>
// String[] -> VBuffer<ReadOnlyMemory<char>>
if (itemType == typeof(string))
{
Contracts.Check(dstItemType == typeof(DvText));
Contracts.Check(dstItemType == typeof(ReadOnlyMemory<char>));

ValueGetter<VBuffer<DvText>> method = GetStringArray;
ValueGetter<VBuffer<ReadOnlyMemory<char>>> method = GetStringArray;
return method as ValueGetter<TDst>;
}

Expand All @@ -825,7 +825,7 @@ public override ValueGetter<TDst> GetGetter<TDst>()
if (MetadataType.IsVector)
{
// VBuffer<T> -> VBuffer<T>
// REVIEW: Do we care about accomodating VBuffer<string> -> VBuffer<DvText>?
// REVIEW: Do we care about accomodating VBuffer<string> -> VBuffer<ReadOnlyMemory<char>>?

Contracts.Assert(typeT.IsGenericType);
Contracts.Check(typeof(TDst).IsGenericType);
Expand All @@ -845,9 +845,9 @@ public override ValueGetter<TDst> GetGetter<TDst>()
{
if (typeT == typeof(string))
{
// String -> DvText
// String -> ReadOnlyMemory<char>
Contracts.Assert(MetadataType.IsText);
ValueGetter<DvText> m = GetString;
ValueGetter<ReadOnlyMemory<char>> m = GetString;
return m as ValueGetter<TDst>;
}
// T -> T
Expand All @@ -861,14 +861,14 @@ public class TElement
{
}

private void GetStringArray(ref VBuffer<DvText> dst)
private void GetStringArray(ref VBuffer<ReadOnlyMemory<char>> dst)
{
var value = (string[])(object)Value;
var n = Utils.Size(value);
dst = new VBuffer<DvText>(n, Utils.Size(dst.Values) < n ? new DvText[n] : dst.Values, dst.Indices);
dst = new VBuffer<ReadOnlyMemory<char>>(n, Utils.Size(dst.Values) < n ? new ReadOnlyMemory<char>[n] : dst.Values, dst.Indices);

for (int i = 0; i < n; i++)
dst.Values[i] = new DvText(value[i]);
dst.Values[i] = value[i].AsMemory();

}

Expand All @@ -890,9 +890,9 @@ private ValueGetter<VBuffer<TDst>> GetVBufferGetter<TDst>()
return (ref VBuffer<TDst> dst) => castValue.CopyTo(ref dst);
}

private void GetString(ref DvText dst)
private void GetString(ref ReadOnlyMemory<char> dst)
{
dst = new DvText((string)(object)Value);
dst = ((string)(object)Value).AsMemory();
}

private void GetDirectValue<TDst>(ref TDst dst)
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.ML.Api/Microsoft.ML.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup>
<PackageReference Include="System.CodeDom" Version="$(SystemCodeDomPackageVersion)" />
<PackageReference Include="System.Memory" Version="4.5.1" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="$(SystemReflectionEmitLightweightPackageVersion)" />
</ItemGroup>

Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.ML.Api/TypedCursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ private Action<TRow> GenerateSetter(IRow input, int index, InternalSchemaDefinit
if (fieldType.IsArray)
{
Ch.Assert(colType.IsVector);
// VBuffer<DvText> -> String[]
// VBuffer<ReadOnlyMemory<char>> -> String[]
if (fieldType.GetElementType() == typeof(string))
{
Ch.Assert(colType.ItemType.IsText);
return CreateConvertingVBufferSetter<DvText, string>(input, index, poke, peek, x => x.ToString());
return CreateConvertingVBufferSetter<ReadOnlyMemory<char>, string>(input, index, poke, peek, x => x.ToString());
}
else if (fieldType.GetElementType() == typeof(bool))
{
Expand Down Expand Up @@ -344,7 +344,7 @@ private Action<TRow> GenerateSetter(IRow input, int index, InternalSchemaDefinit
else if (colType.IsVector)
{
// VBuffer<T> -> VBuffer<T>
// REVIEW: Do we care about accomodating VBuffer<string> -> VBuffer<DvText>?
// REVIEW: Do we care about accomodating VBuffer<string> -> VBuffer<ReadOnlyMemory<char>>?
Ch.Assert(fieldType.IsGenericType);
Ch.Assert(fieldType.GetGenericTypeDefinition() == typeof(VBuffer<>));
Ch.Assert(fieldType.GetGenericArguments()[0] == colType.ItemType.RawType);
Expand All @@ -355,10 +355,10 @@ private Action<TRow> GenerateSetter(IRow input, int index, InternalSchemaDefinit
{
if (fieldType == typeof(string))
{
// DvText -> String
// ReadOnlyMemory<char> -> String
Ch.Assert(colType.IsText);
Ch.Assert(peek == null);
return CreateConvertingActionSetter<DvText, string>(input, index, poke, x => x.ToString());
return CreateConvertingActionSetter<ReadOnlyMemory<char>, string>(input, index, poke, x => x.ToString());
}
else if (fieldType == typeof(bool))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Core/Data/ColumnType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public static TextType Instance
}

private TextType()
: base(typeof(DvText), DataKind.TX)
: base(typeof(ReadOnlyMemory<char>), DataKind.TX)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.ML.Core/Data/DataKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static Type ToType(this DataKind kind)
case DataKind.R8:
return typeof(Double);
case DataKind.TX:
return typeof(DvText);
return typeof(ReadOnlyMemory<char>);
case DataKind.BL:
return typeof(DvBool);
case DataKind.TS:
Expand Down Expand Up @@ -205,7 +205,7 @@ public static bool TryGetDataKind(this Type type, out DataKind kind)
kind = DataKind.R4;
else if (type == typeof(Double)|| type == typeof(Double?))
kind = DataKind.R8;
else if (type == typeof(DvText))
else if (type == typeof(ReadOnlyMemory<char>) || type == typeof(string))
kind = DataKind.TX;
else if (type == typeof(DvBool) || type == typeof(bool) || type == typeof(bool?))
kind = DataKind.BL;
Expand Down
Loading