Changeset 16141 for branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs
- Timestamp:
- 09/14/18 11:47:37 (6 years ago)
- Location:
- branches/2817-BinPackingSpeedup
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2817-BinPackingSpeedup
- Property svn:mergeinfo changed
-
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
-
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs
r16140 r16141 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<T>(string variableName, IEnumerable<T> values) {108 if (variableValues.ContainsKey(variableName))109 throw new ArgumentException("Variable " + variableName + " is already present in the dataset.");110 int count = values.Count();111 if (count != rows)112 throw new ArgumentException("The number of values must exactly match the number of rows in the dataset.");113 variableValues[variableName] = new List<T>(values);114 variableNames.Add(variableName);115 OnColumnsChanged();116 OnColumnNamesChanged();117 OnReset();118 }119 145 120 146 public void RemoveVariable(string variableName) { 121 147 if (!variableValues.ContainsKey(variableName)) 122 throw new ArgumentException( "The variable " + variableName + " does not exist in the dataset.");148 throw new ArgumentException(string.Format("The variable {0} does not exist in the dataset.", variableName)); 123 149 variableValues.Remove(variableName); 124 150 variableNames.Remove(variableName); … … 128 154 } 129 155 130 // slow, avoid to use this131 public void RemoveRow(int row) {132 foreach (var list in variableValues.Values)133 list.RemoveAt(row);134 rows--;156 public void ClearValues() { 157 foreach (var list in variableValues.Values) { 158 list.Clear(); 159 } 160 Rows = 0; 135 161 OnRowsChanged(); 136 162 OnReset(); 137 163 } 164 138 165 139 166 public void SetVariableValue(object value, string variableName, int row) { … … 151 178 } 152 179 153 private Type GetVariableType(string variableName) {154 IList list;155 variableValues.TryGetValue(variableName, out list);156 if (list == null)157 throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");158 return list.GetType().GetGenericArguments()[0];159 }160 161 180 bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) { 162 181 var variableName = variableNames[columnIndex];
Note: See TracChangeset
for help on using the changeset viewer.