Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs @ 11642

Last change on this file since 11642 was 11589, checked in by bburlacu, 10 years ago

#2276: Unsealed Dataset class, refactored GetDoubleValues, GetStringValues, GetDateTimeValues methods to internally use the same generic method, added ModifiableDataset class which adds the functionality of replacing, adding or removing rows in the dataset.

File size: 3.7 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections;
26using System.Collections.Generic;
27using System.Linq;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  [Item("ModifiableDataset", "Represents a dataset containing data that should be analyzed, which can be modified by adding or replacing variables and values.")]
34  [StorableClass]
35  public class ModifiableDataset : Dataset {
36    [StorableConstructor]
37    private ModifiableDataset(bool deserializing) : base(deserializing) { }
38    private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) { }
39    public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
40    public ModifiableDataset() : base() { }
41    public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) : base(variableNames, variableValues) { }
42
43    public void ReplaceRow(int row, IEnumerable<object> values) {
44      var list = values.ToList();
45      if (list.Count != variableNames.Count)
46        throw new ArgumentException("The number of values must be equal to the number of variable names.");
47      // check if all the values are of the correct type
48      for (int i = 0; i < list.Count; ++i) {
49        if (list[i].GetType() != GetVariableType(variableNames[i])) {
50          throw new ArgumentException("The type of the provided value does not match the variable type.");
51        }
52      }
53      // replace values
54      for (int i = 0; i < list.Count; ++i) {
55        variableValues[variableNames[i]][row] = list[i];
56      }
57    }
58
59    public void AddRow(IEnumerable<object> values) {
60      var list = values.ToList();
61      if (list.Count != variableNames.Count)
62        throw new ArgumentException("The number of values must be equal to the number of variable names.");
63      // check if all the values are of the correct type
64      for (int i = 0; i < list.Count; ++i) {
65        if (list[i].GetType() != GetVariableType(variableNames[i])) {
66          throw new ArgumentException("The type of the provided value does not match the variable type.");
67        }
68      }
69      // add values
70      for (int i = 0; i < list.Count; ++i) {
71        variableValues[variableNames[i]].Add(list[i]);
72      }
73      rows++;
74    }
75
76    // slow, avoid to use this
77    public void RemoveRow(int row) {
78      foreach (var list in variableValues.Values)
79        list.RemoveAt(row);
80      rows--;
81    }
82
83    private Type GetVariableType(string variableName) {
84      IList list;
85      variableValues.TryGetValue(variableName, out list);
86      if (list == null)
87        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
88      return list[0].GetType();
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.