/// <summary>
 /// Saves a type into a stream.
 /// </summary>
 public static void WriteType(ModelSaveContext ctx, DataViewType type)
 {
     ctx.Writer.Write(type.IsVector());
     if (type.IsVector())
     {
         ctx.Writer.Write(type.AsVector().DimCount());
         for (int i = 0; i < type.AsVector().DimCount(); ++i)
         {
             ctx.Writer.Write(type.AsVector().GetDim(i));
         }
         ctx.Writer.Write((byte)type.AsVector().ItemType().RawKind());
     }
     else if (type.IsKey())
     {
         throw Contracts.ExceptNotImpl("Key cannot be serialized yet.");
     }
     else
     {
         ctx.Writer.Write((byte)type.RawKind());
     }
 }
Beispiel #2
0
        private static ValueGetter <TDst> GetGetterAsCore <TSrc, TDst>(DataViewType typeSrc, DataViewType typeDst, DataViewRow row, DataViewSchema.Column col)
        {
            Contracts.Assert(typeof(TSrc) == typeSrc.RawType);
            Contracts.Assert(typeof(TDst) == typeDst.RawType);

            bool identity;

            if (typeSrc.RawKind() == DataKind.UInt32 && typeDst.RawKind() == DataKind.UInt32)
            {
                var getter = row.GetGetter <uint>(col);
                // Multiclass future issue
                uint plus = (typeDst.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                identity = true;
                var src = default(uint);
                ValueGetter <uint> mapu =
                    (ref uint dst) =>
                {
                    getter(ref src);
                    dst = src + plus;
                };
                return(mapu as ValueGetter <TDst>);
            }
            else if (typeSrc.RawKind() == DataKind.Single && typeDst.RawKind() == DataKind.UInt32)
            {
                var getter = row.GetGetter <float>(col);
                // Multiclass future issue
                uint plus = (typeDst.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                identity = true;
                var src = default(float);
                ValueGetter <uint> mapu =
                    (ref uint dst) =>
                {
                    getter(ref src);
                    dst = (uint)src + plus;
                };
                return(mapu as ValueGetter <TDst>);
            }
            else if (typeSrc.RawKind() == DataKind.Int64 && typeDst.RawKind() == DataKind.UInt64)
            {
                ulong plus   = (typeDst.IsKey() ? (ulong)1 : (ulong)0) - (typeSrc.IsKey() ? (ulong)1 : (ulong)0);
                var   getter = row.GetGetter <long>(col);
                identity = true;
                var src = default(long);
                ValueGetter <ulong> mapu =
                    (ref ulong dst) =>
                {
                    getter(ref src);
                    CheckRange(src, dst);
                    dst = (ulong)src + plus;
                };
                return(mapu as ValueGetter <TDst>);
            }
            else if (typeSrc.RawKind() == DataKind.Single && typeDst.RawKind() == DataKind.UInt64)
            {
                ulong plus   = (ulong)((typeDst.IsKey() ? 1 : 0) - (typeSrc.IsKey() ? 1 : 0));
                var   getter = row.GetGetter <ulong>(col);
                identity = true;
                var src = default(ulong);
                ValueGetter <ulong> mapu =
                    (ref ulong dst) =>
                {
                    getter(ref src);
                    CheckRange(src, dst);
                    dst = (ulong)src + plus;
                };
                return(mapu as ValueGetter <TDst>);
            }
            else if (typeSrc.RawKind() == DataKind.Int64 && typeDst.RawKind() == DataKind.UInt32)
            {
                // Multiclass future issue
                uint plus   = (typeDst.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                var  getter = row.GetGetter <long>(col);
                identity = true;
                var src = default(long);
                ValueGetter <uint> mapu =
                    (ref uint dst) =>
                {
                    getter(ref src);
                    CheckRange(src, dst);
                    dst = (uint)src + plus;
                };
                return(mapu as ValueGetter <TDst>);
            }
            else if (typeSrc.RawKind() == DataKind.Single && typeDst.RawKind() == DataKind.String)
            {
                // Multiclass future issue
                var getter = row.GetGetter <float>(col);
                identity = true;
                var src = default(float);
                ValueGetter <DvText> mapu =
                    (ref DvText dst) =>
                {
                    getter(ref src);
                    dst = new DvText(string.Format("{0}", (int)src));
                };
                return(mapu as ValueGetter <TDst>);
            }
            else
            {
                var getter = row.GetGetter <TSrc>(col);
                var conv   = Conversions.DefaultInstance.GetStandardConversion <TSrc, TDst>(typeSrc, typeDst, out identity);
                if (identity)
                {
                    Contracts.Assert(typeof(TSrc) == typeof(TDst));
                    return((ValueGetter <TDst>)(Delegate) getter);
                }

                var src = default(TSrc);
                return
                    ((ref TDst dst) =>
                {
                    getter(ref src);
                    conv(in src, ref dst);
                });
            }
        }