Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16118


Ignore:
Timestamp:
08/30/18 12:30:28 (6 years ago)
Author:
mkommend
Message:

#2939: Organized methods in ModifiableDataset for row and variable modification.

File:
1 edited

Legend:

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

    r16084 r16118  
    5353      return new Dataset(variableNames, variableNames.Select(v => variableValues[v]));
    5454    }
     55
     56
     57    public void AddRow(IEnumerable<object> values) {
     58      var list = values.ToList();
     59      if (list.Count != variableNames.Count)
     60        throw new ArgumentException("The number of values must be equal to the number of variable names.");
     61      // check if all the values are of the correct type
     62      for (int i = 0; i < list.Count; ++i) {
     63        if (list[i].GetType() != GetVariableType(variableNames[i])) {
     64          throw new ArgumentException("The type of the provided value does not match the variable type.");
     65        }
     66      }
     67      // add values
     68      for (int i = 0; i < list.Count; ++i) {
     69        variableValues[variableNames[i]].Add(list[i]);
     70      }
     71      Rows++;
     72      OnRowsChanged();
     73      OnReset();
     74    }
     75
    5576    public void ReplaceRow(int row, IEnumerable<object> values) {
    5677      var list = values.ToList();
     
    6788        variableValues[variableNames[i]][row] = list[i];
    6889      }
     90      OnReset();
     91    }
     92
     93    // slow, avoid using this
     94    public void RemoveRow(int row) {
     95      foreach (var list in variableValues.Values)
     96        list.RemoveAt(row);
     97      Rows--;
     98      OnRowsChanged();
     99      OnReset();
     100    }
     101
     102    // adds a new variable to the dataset
     103    public void AddVariable(string variableName, IList values) {
     104      InsertVariable(variableName, Columns, values);
     105    }
     106
     107    public void InsertVariable(string variableName, int position, IList values) {
     108      if (variableValues.ContainsKey(variableName))
     109        throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
     110
     111      if (position < 0 || position > Columns)
     112        throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
     113
     114      if (values == null)
     115        throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided.");
     116
     117      if (values.Count != Rows)
     118        throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
     119
     120      if (!IsAllowedType(values))
     121        throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
     122
     123      variableNames.Insert(position, variableName);
     124      variableValues[variableName] = values;
     125
     126      OnColumnsChanged();
     127      OnColumnNamesChanged();
    69128      OnReset();
    70129    }
     
    80139    }
    81140
    82     public void AddRow(IEnumerable<object> values) {
    83       var list = values.ToList();
    84       if (list.Count != variableNames.Count)
    85         throw new ArgumentException("The number of values must be equal to the number of variable names.");
    86       // check if all the values are of the correct type
    87       for (int i = 0; i < list.Count; ++i) {
    88         if (list[i].GetType() != GetVariableType(variableNames[i])) {
    89           throw new ArgumentException("The type of the provided value does not match the variable type.");
    90         }
    91       }
    92       // add values
    93       for (int i = 0; i < list.Count; ++i) {
    94         variableValues[variableNames[i]].Add(list[i]);
    95       }
    96       Rows++;
    97       OnRowsChanged();
    98       OnReset();
    99     }
    100 
    101     // adds a new variable to the dataset
    102     public void AddVariable(string variableName, IList values) {
    103       InsertVariable(variableName, Columns, values);
    104     }
    105 
    106 
    107     public void InsertVariable(string variableName, int position, IList values) {
    108       if (variableValues.ContainsKey(variableName))
    109         throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
    110 
    111       if (position < 0 || position > Columns)
    112         throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
    113 
    114       if (values == null)
    115         throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided.");
    116 
    117       if (values.Count != Rows)
    118         throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
    119 
    120       if (!IsAllowedType(values))
    121         throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
    122 
    123       variableNames.Insert(position, variableName);
    124       variableValues[variableName] = values;
    125 
    126       OnColumnsChanged();
    127       OnColumnNamesChanged();
    128       OnReset();
    129     }
    130141
    131142    public void RemoveVariable(string variableName) {
     
    139150    }
    140151
    141     // slow, avoid using this
    142     public void RemoveRow(int row) {
    143       foreach (var list in variableValues.Values)
    144         list.RemoveAt(row);
    145       Rows--;
    146       OnRowsChanged();
    147       OnReset();
    148     }
    149152
    150153    public void SetVariableValue(object value, string variableName, int row) {
Note: See TracChangeset for help on using the changeset viewer.