protected override bool TryParse(string str)
            {
                Contracts.AssertNonEmpty(str);

                // We accept N:T:S where N is the new column name, T is the new type,
                // and S is source column names.
                string extra;

                if (!base.TryParse(str, out extra))
                {
                    return(false);
                }
                if (extra == null)
                {
                    return(true);
                }

                InternalDataKind kind;

                if (!TypeParsingUtils.TryParseDataKind(extra, out kind, out KeyCount))
                {
                    return(false);
                }
                ResultType = InternalDataKind2DataKind(kind);
                return(true);
            }
            private protected override bool TryParse(string str)
            {
                Contracts.AssertNonEmpty(str);

                // We accept N:T:S where N is the new column name, T is the new type,
                // and S is source column names.
                if (!base.TryParse(str, out string extra))
                    return false;
                if (extra == null)
                    return true;

                if (!TypeParsingUtils.TryParseDataKind(extra, out DataKind kind, out KeyCount))
                    return false;
                ResultType = kind == default ? default(DataKind?) : kind;
                return true;
            }
示例#3
0
        private static bool TryCreateEx(IExceptionContext ectx, ColInfo info, DataKind kind, KeyCount range,
                                        out PrimitiveDataViewType itemType, out ColInfoEx ex)
        {
            ectx.AssertValue(info);
            ectx.Assert(Enum.IsDefined(typeof(DataKind), kind));

            ex = null;

            var typeSrc = info.TypeSrc;

            if (range != null)
            {
                itemType = TypeParsingUtils.ConstructKeyType(SchemaHelper.DataKind2InternalDataKind(kind), range);
            }
            else if (!typeSrc.ItemType().IsKey())
            {
                itemType = ColumnTypeHelper.PrimitiveFromKind(kind);
            }
            else if (!ColumnTypeHelper.IsValidDataKind(kind))
            {
                itemType = ColumnTypeHelper.PrimitiveFromKind(kind);
                return(false);
            }
            else
            {
                var key = typeSrc.ItemType().AsKey();
                ectx.Assert(ColumnTypeHelper.IsValidDataKind(key.RawKind()));
                ulong count = key.Count;
                // Technically, it's an error for the counts not to match, but we'll let the Conversions
                // code return false below. There's a possibility we'll change the standard conversions to
                // map out of bounds values to zero, in which case, this is the right thing to do.
                ulong max = (ulong)kind;
                if ((ulong)count > max)
                {
                    count = max;
                }
                itemType = new KeyDataViewType(SchemaHelper.DataKind2ColumnType(kind).RawType, count);
            }

            // Ensure that the conversion is legal. We don't actually cache the delegate here. It will get
            // re-fetched by the utils code when needed.
            bool     identity;
            Delegate del;

            if (!Conversions.DefaultInstance.TryGetStandardConversion(typeSrc.ItemType(), itemType, out del, out identity))
            {
                if (typeSrc.ItemType().RawKind() == itemType.RawKind())
                {
                    switch (typeSrc.ItemType().RawKind())
                    {
                    case DataKind.UInt32:
                        // Key starts at 1.
                        // Multiclass future issue
                        uint plus = (itemType.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                        identity = false;
                        ValueMapper <uint, uint> map_ = (in uint src, ref uint dst) => { dst = src + plus; };
                        del = (Delegate)map_;
                        if (del == null)
                        {
                            throw Contracts.ExceptNotSupp("Issue with casting");
                        }
                        break;

                    default:
                        throw Contracts.Except("Not suppoted type {0}", typeSrc.ItemType().RawKind());
                    }
                }
                else if (typeSrc.ItemType().RawKind() == DataKind.Int64 && kind == DataKind.UInt64)
                {
                    ulong plus = (itemType.IsKey() ? (ulong)1 : (ulong)0) - (typeSrc.IsKey() ? (ulong)1 : (ulong)0);
                    identity = false;
                    ValueMapper <long, ulong> map_ = (in long src, ref ulong dst) =>
                    {
                        CheckRange(src, dst, ectx); dst = (ulong)src + plus;
                    };
                    del = (Delegate)map_;
                    if (del == null)
                    {
                        throw Contracts.ExceptNotSupp("Issue with casting");
                    }
                }
                else if (typeSrc.ItemType().RawKind() == DataKind.Single && kind == DataKind.UInt64)
                {
                    ulong plus = (itemType.IsKey() ? (ulong)1 : (ulong)0) - (typeSrc.IsKey() ? (ulong)1 : (ulong)0);
                    identity = false;
                    ValueMapper <float, ulong> map_ = (in float src, ref ulong dst) =>
                    {
                        CheckRange(src, dst, ectx); dst = (ulong)src + plus;
                    };
                    del = (Delegate)map_;
                    if (del == null)
                    {
                        throw Contracts.ExceptNotSupp("Issue with casting");
                    }
                }
                else if (typeSrc.ItemType().RawKind() == DataKind.Int64 && kind == DataKind.UInt32)
                {
                    // Multiclass future issue
                    uint plus = (itemType.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                    identity = false;
                    ValueMapper <long, uint> map_ = (in long src, ref uint dst) =>
                    {
                        CheckRange(src, dst, ectx); dst = (uint)src + plus;
                    };
                    del = (Delegate)map_;
                    if (del == null)
                    {
                        throw Contracts.ExceptNotSupp("Issue with casting");
                    }
                }
                else if (typeSrc.ItemType().RawKind() == DataKind.Single && kind == DataKind.UInt32)
                {
                    // Multiclass future issue
                    uint plus = (itemType.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                    identity = false;
                    ValueMapper <float, uint> map_ = (in float src, ref uint dst) =>
                    {
                        CheckRange(src, dst, ectx); dst = (uint)src + plus;
                    };
                    del = (Delegate)map_;
                    if (del == null)
                    {
                        throw Contracts.ExceptNotSupp("Issue with casting");
                    }
                }
                else if (typeSrc.ItemType().RawKind() == DataKind.Single && kind == DataKind.String)
                {
                    // Multiclass future issue
                    identity = false;
                    ValueMapper <float, DvText> map_ = (in float src, ref DvText dst) =>
                    {
                        dst = new DvText(string.Format("{0}", (int)src));
                    };
                    del = (Delegate)map_;
                    if (del == null)
                    {
                        throw Contracts.ExceptNotSupp("Issue with casting");
                    }
                }
                else
                {
                    return(false);
                }
            }

            DataViewType typeDst = itemType;

            if (typeSrc.IsVector())
            {
                typeDst = new VectorDataViewType(itemType, typeSrc.AsVector().Dimensions.ToArray());
            }

            // An output column is transposable iff the input column was transposable.
            VectorDataViewType slotType = null;

            if (info.SlotTypeSrc != null)
            {
                slotType = new VectorDataViewType(itemType, info.SlotTypeSrc.Dimensions.ToArray());
            }

            ex = new ColInfoEx(kind, range != null, typeDst, slotType);
            return(true);
        }
示例#4
0
        private static bool TryCreateEx(IExceptionContext ectx, ColInfo info, DataKind kind, KeyRange range, out PrimitiveType itemType, out ColInfoEx ex)
        {
            ectx.AssertValue(info);
            ectx.Assert(Enum.IsDefined(typeof(DataKind), kind));

            ex = null;

            var typeSrc = info.TypeSrc;

            if (range != null)
            {
                itemType = TypeParsingUtils.ConstructKeyType(kind, range);
                if (!typeSrc.ItemType.IsKey && !typeSrc.ItemType.IsText)
                {
                    return(false);
                }
            }
            else if (!typeSrc.ItemType.IsKey)
            {
                itemType = PrimitiveType.FromKind(kind);
            }
            else if (!KeyType.IsValidDataKind(kind))
            {
                itemType = PrimitiveType.FromKind(kind);
                return(false);
            }
            else
            {
                var key = typeSrc.ItemType.AsKey;
                ectx.Assert(KeyType.IsValidDataKind(key.RawKind));
                int count = key.Count;
                // Technically, it's an error for the counts not to match, but we'll let the Conversions
                // code return false below. There's a possibility we'll change the standard conversions to
                // map out of bounds values to zero, in which case, this is the right thing to do.
                ulong max = kind.ToMaxInt();
                if ((ulong)count > max)
                {
                    count = (int)max;
                }
                itemType = new KeyType(kind, key.Min, count, key.Contiguous);
            }

            // Ensure that the conversion is legal. We don't actually cache the delegate here. It will get
            // re-fetched by the utils code when needed.
            bool     identity;
            Delegate del;

            if (!Runtime.Data.Conversion.Conversions.Instance.TryGetStandardConversion(typeSrc.ItemType, itemType, out del, out identity))
            {
                return(false);
            }

            ColumnType typeDst = itemType;

            if (typeSrc.IsVector)
            {
                typeDst = new VectorType(itemType, typeSrc.AsVector);
            }

            // An output column is transposable iff the input column was transposable.
            VectorType slotType = null;

            if (info.SlotTypeSrc != null)
            {
                slotType = new VectorType(itemType, info.SlotTypeSrc);
            }

            ex = new ColInfoEx(kind, range != null, typeDst, slotType);
            return(true);
        }