コード例 #1
0
 public SaveDataCommand(IHostEnvironment env, Arguments args)
     : base(env, args, nameof(SaveDataCommand))
 {
     Contracts.CheckNonEmpty(args.OutputDataFile, nameof(args.OutputDataFile));
     Utils.CheckOptionalUserDirectory(args.OutputDataFile, nameof(args.OutputDataFile));
 }
コード例 #2
0
ファイル: RouteInfo.cs プロジェクト: yjsyyyjszf/reverse-proxy
 public RouteInfo(string routeId)
 {
     Contracts.CheckNonEmpty(routeId, nameof(routeId));
     RouteId = routeId;
 }
コード例 #3
0
 public ColumnType GetMetadataTypeOrNull(string kind, int col)
 {
     Contracts.CheckNonEmpty(kind, nameof(kind));
     Contracts.CheckParam(0 <= col && col < ColumnCount, nameof(col));
     return(Input.GetMetadataTypeOrNull(kind, Infos[col].Source));
 }
コード例 #4
0
 public void GetMetadata <TValue>(string kind, int col, ref TValue value)
 {
     Contracts.CheckNonEmpty(kind, nameof(kind));
     Contracts.CheckParam(0 <= col && col < ColumnCount, nameof(col));
     Input.GetMetadata(kind, Infos[col].Source, ref value);
 }
コード例 #5
0
        /// <summary>
        /// Extract all values of one column of the data view in a form of an <see cref="IEnumerable{T}"/>.
        /// </summary>
        /// <typeparam name="T">The type of the values. This must match the actual column type.</typeparam>
        /// <param name="data">The data view to get the column from.</param>
        /// <param name="column">The column to be extracted.</param>
        public static IEnumerable <T> GetColumn <T>(this IDataView data, DataViewSchema.Column column)
        {
            Contracts.CheckValue(data, nameof(data));
            Contracts.CheckNonEmpty(column.Name, nameof(column));

            var colIndex = column.Index;
            var colType  = column.Type;
            var colName  = column.Name;

            // Use column index as the principle address of the specified input column and check if that address in data contains
            // the column indicated.
            if (data.Schema[colIndex].Name != colName || data.Schema[colIndex].Type != colType)
            {
                throw Contracts.ExceptParam(nameof(column), string.Format("column with name {0}, type {1}, and index {2} cannot be found in {3}",
                                                                          colName, colType, colIndex, nameof(data)));
            }

            // There are two decisions that we make here:
            // - Is the T an array type?
            //     - If yes, we need to map VBuffer to array and densify.
            //     - If no, this is not needed.
            // - Does T (or item type of T if it's an array) equal to the data view type?
            //     - If this is the same type, we can map directly.
            //     - Otherwise, we need a conversion delegate.

            if (colType.RawType == typeof(T))
            {
                // Direct mapping is possible.
                return(GetColumnDirect <T>(data, colIndex));
            }
            else if (typeof(T) == typeof(string) && colType is TextDataViewType)
            {
                // Special case of ROM<char> to string conversion.
                Delegate convert = (Func <ReadOnlyMemory <char>, string>)((ReadOnlyMemory <char> txt) => txt.ToString());
                Func <IDataView, int, Func <int, T>, IEnumerable <T> > del = GetColumnConvert;
                var meth = del.Method.GetGenericMethodDefinition().MakeGenericMethod(typeof(T), colType.RawType);
                return((IEnumerable <T>)(meth.Invoke(null, new object[] { data, colIndex, convert })));
            }
            else if (typeof(T).IsArray)
            {
                // Output is an array type.
                if (!(colType is VectorType colVectorType))
                {
                    throw Contracts.ExceptParam(nameof(column), string.Format("Cannot load vector type, {0}, specified in {1} to the user-defined type, {2}.", column.Type, nameof(column), typeof(T)));
                }
                var elementType = typeof(T).GetElementType();
                if (elementType == colVectorType.ItemType.RawType)
                {
                    // Direct mapping of items.
                    Func <IDataView, int, IEnumerable <int[]> > del = GetColumnArrayDirect <int>;
                    var meth = del.Method.GetGenericMethodDefinition().MakeGenericMethod(elementType);
                    return((IEnumerable <T>)meth.Invoke(null, new object[] { data, colIndex }));
                }
                else if (elementType == typeof(string) && colVectorType.ItemType is TextDataViewType)
                {
                    // Conversion of DvText items to string items.
                    Delegate convert = (Func <ReadOnlyMemory <char>, string>)((ReadOnlyMemory <char> txt) => txt.ToString());
                    Func <IDataView, int, Func <int, long>, IEnumerable <long[]> > del = GetColumnArrayConvert;
                    var meth = del.Method.GetGenericMethodDefinition().MakeGenericMethod(elementType, colVectorType.ItemType.RawType);
                    return((IEnumerable <T>)meth.Invoke(null, new object[] { data, colIndex, convert }));
                }
                // Fall through to the failure.
            }

            throw Contracts.ExceptParam(nameof(column), string.Format("Cannot map column (name: {0}, type: {1}) in {2} to the user-defined type, {3}.",
                                                                      column.Name, column.Type, nameof(data), typeof(T)));
        }