Changeset 16063


Ignore:
Timestamp:
08/07/18 16:12:04 (14 months ago)
Author:
mkommend
Message:

#2935:

  • Implemented ModifiableDataset.InsertVariable
  • Added ModifiableDataset.ToDataset for convience reasons
  • Altered ModifiableDataset ctor to allow specification of whether the values should be cloned
  • Simplified code of Dataset.ToModifiable
  • Added protected setter to Rows in the Dataset and made the corresponding field private
Location:
trunk/HeuristicLab.Problems.DataAnalysis/3.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs

    r15829 r16063  
    116116
    117117    public ModifiableDataset ToModifiable() {
    118       var values = new List<IList>();
    119       foreach (var v in variableNames) {
    120         if (VariableHasType<double>(v)) {
    121           values.Add(new List<double>((IList<double>)variableValues[v]));
    122         } else if (VariableHasType<string>(v)) {
    123           values.Add(new List<string>((IList<string>)variableValues[v]));
    124         } else if (VariableHasType<DateTime>(v)) {
    125           values.Add(new List<DateTime>((IList<DateTime>)variableValues[v]));
    126         } else {
    127           throw new ArgumentException("Unknown variable type.");
    128         }
    129       }
    130       return new ModifiableDataset(variableNames, values);
     118      return new ModifiableDataset(variableNames, variableNames.Select(v => variableValues[v]),true);
    131119    }
    132120
     
    141129    }
    142130
    143     protected Dataset(Dataset dataset) : this(dataset.variableNames, dataset.variableValues.Values) { }
     131
    144132
    145133    #region Backwards compatible code, remove with 3.5
     
    318306    #region IStringConvertibleMatrix Members
    319307    [Storable]
    320     protected int rows;
     308    private int rows;
    321309    public int Rows {
    322310      get { return rows; }
     311      protected set { rows = value; }
    323312    }
    324313    int IStringConvertibleMatrix.Rows {
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs

    r15829 r16063  
    4747    public ModifiableDataset() { }
    4848
    49     public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) :
    50       base(variableNames, variableValues, cloneValues: false) { }
    51 
     49    public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues, bool cloneValues = false) :
     50      base(variableNames, variableValues, cloneValues) { }
     51
     52    public Dataset ToDataset() {
     53      return new Dataset(variableNames, variableNames.Select(v => variableValues[v]));
     54    }
    5255    public void ReplaceRow(int row, IEnumerable<object> values) {
    5356      var list = values.ToList();
     
    9194        variableValues[variableNames[i]].Add(list[i]);
    9295      }
    93       rows++;
     96      Rows++;
    9497      OnRowsChanged();
    9598      OnReset();
     
    104107        throw new ArgumentException("Cannot add variable with no values.");
    105108
     109      if (values.Count != Rows)
     110        throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
     111
    106112      if (!IsAllowedType(values))
    107113        throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
     
    109115      variableValues[variableName] = values;
    110116      variableNames.Add(variableName);
     117
     118      OnColumnsChanged();
     119      OnColumnNamesChanged();
     120      OnReset();
     121    }
     122
     123
     124    public void InsertVariable(string variableName, int position, IList values) {
     125      if (variableValues.ContainsKey(variableName))
     126        throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
     127
     128      if (position < 0 || position > Columns)
     129        throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
     130
     131      if (values == null || values.Count == 0)
     132        throw new ArgumentException("Cannot add variable with no values.");
     133
     134      if (values.Count != Rows)
     135        throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
     136
     137      if (!IsAllowedType(values))
     138        throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
     139
     140      variableNames.Insert(position, variableName);
     141      variableValues[variableName] = values;
    111142
    112143      OnColumnsChanged();
     
    129160      foreach (var list in variableValues.Values)
    130161        list.RemoveAt(row);
    131       rows--;
     162      Rows--;
    132163      OnRowsChanged();
    133164      OnReset();
Note: See TracChangeset for help on using the changeset viewer.