Beispiel #1
0
        public ValueTableRow Add()
        {
            ValueTableRow row = new ValueTableRow(this);

            _rows.Add(row);
            return(row);
        }
Beispiel #2
0
        public ValueTableRow Insert(int index)
        {
            ValueTableRow row = new ValueTableRow(this);

            _rows.Insert(index, row);
            return(row);
        }
Beispiel #3
0
        public ValueTable Copy(IValue Rows = null, string ColumnNames = null)
        {
            ValueTable Result = CopyColumns(ColumnNames);
            List <ValueTableColumn> columns = GetProcessingColumnList(ColumnNames);

            // TODO: Переделать это убожество. Свернуть две ветки в одну, сделать соответствие объектов старых колонок и новых.

            if (Rows == null)
            {
                foreach (ValueTableRow row in _rows)
                {
                    ValueTableRow new_row = Result.Add();
                    foreach (ValueTableColumn Column in columns)
                    {
                        new_row.Set(ValueFactory.Create(Column.Name), row.Get(Column));
                    }
                }
            }
            else
            {
                ArrayImpl a_Rows = Rows as ArrayImpl;
                foreach (IValue irow in a_Rows)
                {
                    ValueTableRow row = irow as ValueTableRow;

                    ValueTableRow new_row = Result.Add();
                    foreach (ValueTableColumn Column in columns)
                    {
                        new_row.Set(ValueFactory.Create(Column.Name), row.Get(Column));
                    }
                }
            }

            return(Result);
        }
Beispiel #4
0
        public ValueTable Copy(IValue rows = null, string columnNames = null)
        {
            ValueTable Result = CopyColumns(columnNames);
            List <ValueTableColumn> columns = GetProcessingColumnList(columnNames);

            IEnumerable <ValueTableRow> requestedRows;

            if (rows == null)
            {
                requestedRows = _rows;
            }
            else
            {
                if (rows.SystemType.Equals(TypeManager.GetTypeByFrameworkType(typeof(StructureImpl))))
                {
                    requestedRows = FindRows(rows).Select(x => x as ValueTableRow);
                }
                else if (rows.SystemType.Equals(TypeManager.GetTypeByFrameworkType(typeof(ArrayImpl))))
                {
                    requestedRows = GetRowsEnumByArray(rows);
                }
                else
                {
                    throw RuntimeException.InvalidArgumentType();
                }
            }

            var columnMap = new Dictionary <ValueTableColumn, ValueTableColumn>();

            foreach (var column in columns)
            {
                var destinationColumn = Result.Columns.FindColumnByName(column.Name);
                columnMap.Add(column, destinationColumn);
            }

            foreach (var row in requestedRows)
            {
                ValueTableRow new_row = Result.Add();
                foreach (ValueTableColumn Column in columns)
                {
                    new_row.Set(columnMap[Column], row.Get(Column));
                }
            }

            return(Result);
        }
Beispiel #5
0
        private bool CheckFilterCriteria(ValueTableRow Row, StructureImpl Filter)
        {
            foreach (KeyAndValueImpl kv in Filter)
            {
                ValueTableColumn Column = Columns.FindColumnByName(kv.Key.AsString());
                if (Column == null)
                {
                    throw RuntimeException.PropNotFoundException(kv.Key.AsString());
                }

                IValue current = Row.Get(Column);
                if (!current.Equals(kv.Value))
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #6
0
        public void Move(IValue row, int offset)
        {
            row = row.GetRawValue();

            int index_source;

            if (row is ValueTableRow)
            {
                index_source = _rows.IndexOf(row as ValueTableRow);
            }
            else if (row.DataType == Machine.DataType.Number)
            {
                index_source = decimal.ToInt32(row.AsNumber());
            }
            else
            {
                throw RuntimeException.InvalidArgumentType();
            }

            if (index_source < 0 || index_source >= _rows.Count())
            {
                throw RuntimeException.InvalidArgumentValue();
            }

            int index_dest = (index_source + offset) % _rows.Count();

            while (index_dest < 0)
            {
                index_dest += _rows.Count();
            }

            ValueTableRow tmp = _rows[index_source];

            if (index_source < index_dest)
            {
                _rows.Insert(index_dest + 1, tmp);
                _rows.RemoveAt(index_source);
            }
            else
            {
                _rows.RemoveAt(index_source);
                _rows.Insert(index_dest, tmp);
            }
        }
Beispiel #7
0
        public void Move(IValue row, int offset)
        {
            int index_source = IndexByValue(row);

            int index_dest = index_source + offset;

            if (index_dest < 0 || index_dest >= _rows.Count())
            {
                throw RuntimeException.InvalidNthArgumentValue(2);
            }

            ValueTableRow tmp = _rows[index_source];

            if (index_source < index_dest)
            {
                _rows.Insert(index_dest + 1, tmp);
                _rows.RemoveAt(index_source);
            }
            else
            {
                _rows.RemoveAt(index_source);
                _rows.Insert(index_dest, tmp);
            }
        }
Beispiel #8
0
        public void GroupBy(string groupColumnNames, string aggregateColumnNames = null)
        {
            // TODO: Сворачиваем за N^2. Переделать на N*log(N)

            List <ValueTableColumn> GroupColumns     = GetProcessingColumnList(groupColumnNames, true);
            List <ValueTableColumn> AggregateColumns = GetProcessingColumnList(aggregateColumnNames, true);

            List <ValueTableRow> new_rows = new List <ValueTableRow>();

            foreach (ValueTableRow row in _rows)
            {
                StructureImpl search = new StructureImpl();

                foreach (ValueTableColumn Column in GroupColumns)
                {
                    search.Insert(Column.Name, row.Get(Column));
                }

                ValueTableRow new_row = null;

                foreach (ValueTableRow nrow in new_rows)
                {
                    if (CheckFilterCriteria(nrow, search))
                    {
                        new_row = nrow;
                        break;
                    }
                }

                if (new_row == null)
                {
                    new_row = new ValueTableRow(this);
                    foreach (ValueTableColumn Column in GroupColumns)
                    {
                        new_row.Set(Column, row.Get(Column));
                    }

                    new_rows.Add(new_row);
                }

                foreach (ValueTableColumn Column in AggregateColumns)
                {
                    IValue  old = new_row.Get(Column);
                    decimal d_old;

                    if (old.DataType != Machine.DataType.Number)
                    {
                        d_old = 0;
                    }
                    else
                    {
                        d_old = old.AsNumber();
                    }

                    IValue  current = row.Get(Column);
                    decimal d_current;

                    if (current.DataType != Machine.DataType.Number)
                    {
                        d_current = 0;
                    }
                    else
                    {
                        d_current = current.AsNumber();
                    }

                    new_row.Set(Column, ValueFactory.Create(d_old + d_current));
                }
            }

            _rows.Clear();
            _rows.AddRange(new_rows);

            {
                int i = 0;
                while (i < _columns.Count())
                {
                    ValueTableColumn Column = _columns.FindColumnByIndex(i);
                    if (GroupColumns.IndexOf(Column) == -1 && AggregateColumns.IndexOf(Column) == -1)
                    {
                        _columns.Delete(Column);
                    }
                    else
                    {
                        ++i;
                    }
                }
            }
        }
Beispiel #9
0
        public void GroupBy(string groupColumnNames, string aggregateColumnNames = null)
        {
            List <ValueTableColumn> GroupColumns     = GetProcessingColumnList(groupColumnNames, true);
            List <ValueTableColumn> AggregateColumns = GetProcessingColumnList(aggregateColumnNames, true);

            foreach (ValueTableColumn group_column in GroupColumns)
            {
                if (AggregateColumns.Find(x => x.Name == group_column.Name) != null)
                {
                    throw ColumnsMixedException(group_column.Name);
                }
            }

            var uniqueRows = new Dictionary <ValueTableRow, ValueTableRow>(new RowsByColumnsEqComparer(GroupColumns));
            int new_idx    = 0;

            foreach (ValueTableRow row in _rows)
            {
                if (uniqueRows.ContainsKey(row))
                {
                    ValueTableRow old_row = uniqueRows[row];

                    foreach (var Column in AggregateColumns)
                    {
                        IValue current = row.Get(Column);
                        if (current.DataType == DataType.Number)
                        {
                            decimal sum = old_row.Get(Column).AsNumber() + current.AsNumber();
                            old_row.Set(Column, ValueFactory.Create(sum));
                        }
                    }
                }
                else
                {
                    ValueTableRow new_row = _rows[new_idx++];

                    foreach (var Column in GroupColumns)
                    {
                        new_row.Set(Column, row.Get(Column));
                    }

                    foreach (var Column in AggregateColumns)
                    {
                        if (row.Get(Column).DataType != DataType.Number)
                        {
                            new_row.Set(Column, ValueFactory.Create(0));
                        }
                        else
                        {
                            new_row.Set(Column, row.Get(Column));
                        }
                    }

                    uniqueRows.Add(new_row, new_row);
                }
            }

            _rows.RemoveRange(new_idx, _rows.Count() - new_idx);

            int i = 0;

            while (i < _columns.Count())
            {
                ValueTableColumn Column = _columns.FindColumnByIndex(i);
                if (GroupColumns.IndexOf(Column) == -1 && AggregateColumns.IndexOf(Column) == -1)
                {
                    _columns.Delete(Column);
                }
                else
                {
                    ++i;
                }
            }
        }