Changeset 16188 for branches/2904_CalculateImpacts/3.4/ModifiableDataset.cs
- Timestamp:
- 09/27/18 09:50:33 (6 years ago)
- Location:
- branches/2904_CalculateImpacts/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2904_CalculateImpacts/3.4
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis/3.4 (added) merged: 15829,15871,16059,16063,16084,16118,16120
- Property svn:mergeinfo changed
-
branches/2904_CalculateImpacts/3.4/ModifiableDataset.cs
r15769 r16188 39 39 40 40 private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) { 41 var variables = variableValues.Keys.ToList(); 42 foreach (var v in variables) { 43 var type = GetVariableType(v); 44 if (type == typeof(DateTime)) { 45 variableValues[v] = GetDateTimeValues(v).ToList(); 46 } else if (type == typeof(double)) { 47 variableValues[v] = GetDoubleValues(v).ToList(); 48 } else if (type == typeof(string)) { 49 variableValues[v] = GetStringValues(v).ToList(); 50 } else { 51 throw new ArgumentException("Unsupported type " + type + " for variable " + v); 41 variableNames = new List<string>(original.variableNames); 42 variableValues = CloneValues(original.variableValues); 43 } 44 45 public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); } 46 47 public ModifiableDataset() { } 48 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 } 55 56 57 public IEnumerable<object> GetRow(int row) { 58 return variableValues.Select(x => x.Value[row]); 59 } 60 61 public void AddRow(IEnumerable<object> values) { 62 var list = values.ToList(); 63 if (list.Count != variableNames.Count) 64 throw new ArgumentException("The number of values must be equal to the number of variable names."); 65 // check if all the values are of the correct type 66 for (int i = 0; i < list.Count; ++i) { 67 if (list[i].GetType() != GetVariableType(variableNames[i])) { 68 throw new ArgumentException("The type of the provided value does not match the variable type."); 52 69 } 53 70 } 54 } 55 public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); } 56 public ModifiableDataset() : base() { } 57 58 public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) : base(variableNames, variableValues) { } 71 // add values 72 for (int i = 0; i < list.Count; ++i) { 73 variableValues[variableNames[i]].Add(list[i]); 74 } 75 Rows++; 76 OnRowsChanged(); 77 OnReset(); 78 } 59 79 60 80 public void ReplaceRow(int row, IEnumerable<object> values) { … … 72 92 variableValues[variableNames[i]][row] = list[i]; 73 93 } 94 OnReset(); 95 } 96 97 // slow, avoid using this 98 public void RemoveRow(int row) { 99 foreach (var list in variableValues.Values) 100 list.RemoveAt(row); 101 Rows--; 102 OnRowsChanged(); 103 OnReset(); 104 } 105 106 // adds a new variable to the dataset 107 public void AddVariable(string variableName, IList values) { 108 InsertVariable(variableName, Columns, values); 109 } 110 111 public void InsertVariable(string variableName, int position, IList values) { 112 if (variableValues.ContainsKey(variableName)) 113 throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName)); 114 115 if (position < 0 || position > Columns) 116 throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns)); 117 118 if (values == null) 119 throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided."); 120 121 if (values.Count != Rows) 122 throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows)); 123 124 if (!IsAllowedType(values)) 125 throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName)); 126 127 variableNames.Insert(position, variableName); 128 variableValues[variableName] = values; 129 130 OnColumnsChanged(); 131 OnColumnNamesChanged(); 74 132 OnReset(); 75 133 } … … 85 143 } 86 144 87 public void AddRow(IEnumerable<object> values) {88 var list = values.ToList();89 if (list.Count != variableNames.Count)90 throw new ArgumentException("The number of values must be equal to the number of variable names.");91 // check if all the values are of the correct type92 for (int i = 0; i < list.Count; ++i) {93 if (list[i].GetType() != GetVariableType(variableNames[i])) {94 throw new ArgumentException("The type of the provided value does not match the variable type.");95 }96 }97 // add values98 for (int i = 0; i < list.Count; ++i) {99 variableValues[variableNames[i]].Add(list[i]);100 }101 rows++;102 OnRowsChanged();103 OnReset();104 }105 106 // adds a new variable to the dataset107 public void AddVariable(string variableName, 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 (values == null || values.Count == 0)112 throw new ArgumentException("Cannot add variable with no values.");113 114 if (!IsAllowedType(values))115 throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));116 117 variableValues[variableName] = values;118 variableNames.Add(variableName);119 120 OnColumnsChanged();121 OnColumnNamesChanged();122 OnReset();123 }124 145 125 146 public void RemoveVariable(string variableName) { … … 133 154 } 134 155 135 // slow, avoid using this136 public void RemoveRow(int row) {137 foreach (var list in variableValues.Values)138 list.RemoveAt(row);139 rows--;156 public void ClearValues() { 157 foreach (var list in variableValues.Values) { 158 list.Clear(); 159 } 160 Rows = 0; 140 161 OnRowsChanged(); 141 162 OnReset(); 142 163 } 164 143 165 144 166 public void SetVariableValue(object value, string variableName, int row) {
Note: See TracChangeset
for help on using the changeset viewer.