Changeset 5552 for branches/DataAnalysis Refactoring
- Timestamp:
- 02/23/11 13:54:20 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs
r5484 r5552 39 39 data = original.data; 40 40 } 41 public override IDeepCloneable Clone(Cloner cloner) { 42 return new Dataset(this, cloner); 43 } 44 41 45 public Dataset(IEnumerable<string> variableNames, double[,] data) 42 46 : base() { … … 46 50 } 47 51 this.data = (double[,])data.Clone(); 48 this.variableNames = variableNames.ToArray();52 VariableNames = variableNames; 49 53 } 50 54 55 56 private Dictionary<string, int> variableNames; 51 57 [Storable] 52 private string[] variableNames;53 58 public IEnumerable<string> VariableNames { 54 get { return variableNames; } 59 get { return variableNames.Keys; } 60 private set { 61 if (variableNames != null) throw new InvalidOperationException("VariableNames can only be set once."); 62 this.variableNames = new Dictionary<string, int>(); 63 int i = 0; 64 foreach (string variableName in value) { 65 this.variableNames.Add(variableName, i); 66 i++; 67 } 68 } 55 69 } 56 70 … … 75 89 return GetVariableValues(variableIndex, 0, Rows); 76 90 } 77 public double[] GetVariableValues(int variableIndex, int start, int end) {78 if (start < 0 || !(start <= end))79 throw new ArgumentException("Start must be between 0 and end (" + end + ").");80 if (end > Rows || end < start)81 throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");82 83 double[] values = new double[end - start];84 for (int i = 0; i < end - start; i++)85 values[i] = data[i + start, variableIndex];86 return values;87 }88 91 public double[] GetVariableValues(string variableName) { 89 92 return GetVariableValues(GetVariableIndex(variableName), 0, Rows); 93 } 94 public double[] GetVariableValues(int variableIndex, int start, int end) { 95 return GetEnumeratedVariableValues(variableIndex, start, end).ToArray(); 90 96 } 91 97 public double[] GetVariableValues(string variableName, int start, int end) { … … 101 107 if (end > Rows || end < start) 102 108 throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ")."); 103 for (int i = 0; i < end - start; i++) 104 yield return data[i + start, variableIndex]; 109 110 for (int i = start; i < end; i++) 111 yield return data[i, variableIndex]; 105 112 } 106 113 public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, IEnumerable<int> rows) { … … 120 127 121 128 public string GetVariableName(int variableIndex) { 122 return variableNames[variableIndex];129 return VariableNames.ElementAt(variableIndex); 123 130 } 124 125 131 public int GetVariableIndex(string variableName) { 126 for (int i = 0; i < variableNames.Length; i++){127 if (variableNames[i].Equals(variableName)) return i;132 try { 133 return variableNames[variableName]; 128 134 } 129 throw new ArgumentException("The variable name " + variableName + " was not found."); 130 } 131 132 public double[,] GetClonedData() { 133 return (double[,])data.Clone(); 134 } 135 136 public override IDeepCloneable Clone(Cloner cloner) { 137 return new Dataset(this, cloner); 138 } 139 140 public event EventHandler Reset; 141 private void OnReset(EventArgs e) { 142 var listeners = Reset; 143 if (listeners != null) listeners(this, e); 135 catch (KeyNotFoundException ex) { 136 throw new ArgumentException("The variable name " + variableName + " was not found.", ex); 137 } 144 138 } 145 139 146 140 #region IStringConvertibleMatrix Members 147 148 141 public int Rows { 149 142 get { return data.GetLength(0); } 150 143 set { throw new NotSupportedException(); } 151 144 } 152 153 145 public int Columns { 154 146 get { return data.GetLength(1); } … … 160 152 set { throw new NotSupportedException(); } 161 153 } 162 163 154 public bool ReadOnly { 164 155 get { return true; } … … 169 160 set { throw new NotSupportedException(); } 170 161 } 171 172 162 IEnumerable<string> IStringConvertibleMatrix.RowNames { 173 get { return new List<string>(); }163 get { return Enumerable.Empty<string>(); } 174 164 set { throw new NotSupportedException(); } 175 }176 177 public bool Validate(string value, out string errorMessage) {178 throw new NotSupportedException();179 165 } 180 166 … … 182 168 return data[rowIndex, columnIndex].ToString(); 183 169 } 184 185 170 public bool SetValue(string value, int rowIndex, int columnIndex) { 186 171 throw new NotSupportedException(); 187 172 } 173 public bool Validate(string value, out string errorMessage) { 174 throw new NotSupportedException(); 175 } 188 176 189 public event EventHandler ColumnsChanged; 190 private void OnColumnsChanged() { 191 var handler = ColumnsChanged; 192 if (handler != null) handler(this, EventArgs.Empty); 193 } 194 public event EventHandler RowsChanged; 195 private void OnRowsChanged() { 196 var handler = RowsChanged; 197 if (handler != null) handler(this, EventArgs.Empty); 198 } 199 public event EventHandler ColumnNamesChanged; 200 private void OnColumnNamesChanged() { 201 EventHandler listeners = ColumnNamesChanged; 202 if (listeners != null) 203 listeners(this, EventArgs.Empty); 204 } 205 public event EventHandler RowNamesChanged; 206 private void OnRowNamesChanged() { 207 EventHandler listeners = RowNamesChanged; 208 if (listeners != null) 209 listeners(this, EventArgs.Empty); 210 } 211 public event EventHandler SortableViewChanged; 212 private void OnSortableViewChanged() { 213 EventHandler listeners = SortableViewChanged; 214 if (listeners != null) 215 listeners(this, EventArgs.Empty); 216 } 217 public event EventHandler<EventArgs<int, int>> ItemChanged; 218 private void OnItemChanged(int rowIndex, int columnIndex) { 219 var listeners = ItemChanged; 220 if (listeners != null) 221 listeners(this, new EventArgs<int, int>(rowIndex, columnIndex)); 222 OnToStringChanged(); 223 } 177 public event EventHandler ColumnsChanged { add { } remove { } } 178 public event EventHandler RowsChanged { add { } remove { } } 179 public event EventHandler ColumnNamesChanged { add { } remove { } } 180 public event EventHandler RowNamesChanged { add { } remove { } } 181 public event EventHandler SortableViewChanged { add { } remove { } } 182 public event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } } 224 183 #endregion 225 184 }
Note: See TracChangeset
for help on using the changeset viewer.