Ejemplo n.º 1
0
 public InputRow(IHostEnvironment env, InternalSchemaDefinition schemaDef)
     : base(env, SchemaExtensions.MakeSchema(GetSchemaColumns(schemaDef)), schemaDef, MakePeeks(schemaDef), c => true)
 {
     _position = -1;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a new instance of <see cref="ColumnBindings"/>.
        /// </summary>
        /// <param name="input">The input schema that we're adding columns to.</param>
        /// <param name="addedColumns">The columns being added.</param>
        public ColumnBindings(DataViewSchema input, DataViewSchema.DetachedColumn[] addedColumns)
        {
            Contracts.CheckValue(input, nameof(input));
            Contracts.CheckValue(addedColumns, nameof(addedColumns));

            InputSchema = input;

            // Construct the indices.
            var indices   = new List <int>();
            var namesUsed = new HashSet <string>();

            for (int i = 0; i < input.Count; i++)
            {
                namesUsed.Add(input[i].Name);
                indices.Add(i);
            }

            for (int i = 0; i < addedColumns.Length; i++)
            {
                string name = addedColumns[i].Name;
                if (namesUsed.Add(name))
                {
                    // New name. Append to the end.
                    indices.Add(~i);
                }
                else
                {
                    // Old name. Find last instance and add after it.
                    for (int j = indices.Count - 1; j >= 0; j--)
                    {
                        var colName = indices[j] >= 0 ? input[indices[j]].Name : addedColumns[~indices[j]].Name;
                        if (colName == name)
                        {
                            indices.Insert(j + 1, ~i);
                            break;
                        }
                    }
                }
            }
            Contracts.Assert(indices.Count == addedColumns.Length + input.Count);

            // Create the output schema.
            var schemaColumns = indices.Select(idx => idx >= 0 ? new DataViewSchema.DetachedColumn(input[idx]) : addedColumns[~idx]);

            Schema = SchemaExtensions.MakeSchema(schemaColumns);

            // Memorize column maps.
            _colMap = indices.ToArray();
            var addedIndices = new int[addedColumns.Length];

            for (int i = 0; i < _colMap.Length; i++)
            {
                int colIndex = _colMap[i];
                if (colIndex < 0)
                {
                    Contracts.Assert(addedIndices[~colIndex] == 0);
                    addedIndices[~colIndex] = i;
                }
            }

            AddedColumnIndices = addedIndices.AsReadOnly();
        }