Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/DataRow.cs @ 1984

Last change on this file since 1984 was 1984, checked in by mstoeger, 15 years ago

bugfixes in DataRow.RemoveValues (updating min/max-values). #498

File size: 6.1 KB
Line 
1using System;
2using System.Drawing;
3using System.Collections.Generic;
4
5namespace HeuristicLab.Visualization {
6  public enum Action {
7    Added,
8    Modified,
9    Deleted
10  }
11
12  public delegate void DataRowChangedHandler(IDataRow row);
13  public delegate void ValuesChangedHandler(IDataRow row, double[] values, int index, Action action);
14  public delegate void ValueChangedHandler(IDataRow row, double value, int index, Action action);
15
16  public class DataRow : DataRowBase {
17    private readonly List<double> dataRow = new List<double>();
18
19    private double minValue = double.MaxValue;
20    private double maxValue = double.MinValue;
21
22    public DataRow() {
23    }
24   
25    public DataRow(string label) {
26      this.RowSettings.Label = label;
27    }
28
29    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow) {
30      this.RowSettings.Label = label;
31      this.RowSettings.Color = color;
32      this.RowSettings.Thickness = thickness;
33      this.RowSettings.Style = style;
34      this.dataRow = dataRow;
35      this.RowSettings.ShowMarkers = true;
36    }
37
38    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow, bool showMarkers) {
39      this.RowSettings.Label = label;
40      this.RowSettings.Color = color;
41      this.RowSettings.Thickness = thickness;
42      this.RowSettings.Style = style;
43      this.RowSettings.ShowMarkers = showMarkers;
44      this.dataRow = dataRow;
45    }
46
47    public override void AddValue(double value) {
48      UpdateMinMaxValue(value);
49
50      dataRow.Add(value);
51      OnValueChanged(value, dataRow.Count - 1, Action.Added);
52    }
53
54    public override void AddValue(double value, int index) {
55      //check if index is valid
56      if (index >= 0 && index < dataRow.Count) {
57        UpdateMinMaxValue(value);
58        dataRow.Insert(index, value);
59        OnValueChanged(value, index, Action.Added);
60      } else {
61        throw new IndexOutOfRangeException();
62      }   
63    }
64
65    public override void AddValues(double[] values) {
66      int startIndex = dataRow.Count;
67
68      foreach (double value in values) {
69        UpdateMinMaxValue(value);
70        dataRow.Add(value);
71      }
72
73      OnValuesChanged(values, startIndex, Action.Added);
74    }
75
76    public override void AddValues(double[] values, int index) {
77      if (index >= 0 && index < dataRow.Count) {
78        for (int i = 0; i < values.Length; i++) {
79          double value = values[i];
80          UpdateMinMaxValue(value);
81          dataRow.Insert(index + i, value);
82        }
83        OnValuesChanged(values, index, Action.Added);
84      } else {
85        throw new IndexOutOfRangeException();
86      }
87    }
88
89    public override void ModifyValue(double value, int index) {
90      //check if index is valid
91      if (index >= 0 && index < dataRow.Count) {
92        UpdateMinMaxValue(value, index); // bad runtime but works
93        dataRow[index] = value;
94        OnValueChanged(value, index, Action.Modified);
95      } else {
96        throw new IndexOutOfRangeException();
97      }
98    }
99
100    public override void ModifyValues(double[] values, int index) {
101      //check if index to start modification is valid
102      if (index >= 0 && index + values.Length < dataRow.Count) {
103        for (int i = 0; i < values.Length; i++) {
104          double value = values[i];
105          UpdateMinMaxValue(value, index + i); // bad runtime but works
106          dataRow[index+i] = value;
107        }
108        OnValuesChanged(values, index, Action.Modified);
109      } else {
110        throw new IndexOutOfRangeException();
111      }
112    }
113
114    public override void RemoveValue(int index) {
115      if (index >= 0 && index < dataRow.Count) {
116        UpdateMinMaxValueForRemovedValue(index); // bad runtime but works
117        double removedValue = dataRow[index];
118        dataRow.RemoveAt(index);
119        OnValueChanged(removedValue, index, Action.Deleted);
120      } else {
121        throw new IndexOutOfRangeException();
122      }
123    }
124
125    public override void RemoveValues(int index, int count) {
126      if (count > 0) {
127        if ((index >= 0) && (index + count <= dataRow.Count)) {
128          double[] removedValues = new double[count];
129          for (int i = 0; i < count; i++) {
130            removedValues[i] = dataRow[index + i];
131            UpdateMinMaxValueForRemovedValue(index); // bad runtime but works
132            dataRow.RemoveAt(index);
133          }
134          OnValuesChanged(removedValues, index, Action.Deleted);
135        } else {
136          throw new IndexOutOfRangeException();
137        }
138      } else {
139        throw new Exception("parameter count must be > 0!");
140      }
141    }
142
143    public override int Count {
144      get { return dataRow.Count; }
145    }
146
147    public override double this[int index] {
148      get { return dataRow[index]; }
149      set {
150        dataRow[index] = value;
151        OnValueChanged(value, index, Action.Modified);
152      }
153    }
154
155    public override double MinValue {
156      get { return minValue; }
157    }
158
159    public override double MaxValue {
160      get { return maxValue; }
161    }
162
163    private void UpdateMinMaxValueForRemovedValue(int removedValueIndex) {
164      if (minValue == dataRow[removedValueIndex] || maxValue == dataRow[removedValueIndex]) {
165        minValue = double.MaxValue;
166        maxValue = double.MinValue;
167
168        for (int i = 0; i < dataRow.Count; i++) {
169          if (i != removedValueIndex) {
170            UpdateMinMaxValue(dataRow[i]);
171          }
172        }
173      }
174    }
175
176    private void UpdateMinMaxValue(double newValue, int oldValueIndex) {
177      if (minValue != dataRow[oldValueIndex] && maxValue != dataRow[oldValueIndex])
178        UpdateMinMaxValue(newValue);
179      else {
180        minValue = double.MaxValue;
181        maxValue = double.MinValue;
182
183        for (int i = 0; i < dataRow.Count; i++) {
184          double value = oldValueIndex != i ? dataRow[i] : newValue;
185          UpdateMinMaxValue(value);
186        }
187      }
188    }
189
190    private void UpdateMinMaxValue(double value) {
191      maxValue = Math.Max(value, maxValue);
192      minValue = Math.Min(value, minValue);
193    }
194  }
195}
Note: See TracBrowser for help on using the repository browser.