Free cookie consent management tool by TermsFeed Policy Generator

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

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

bugfixes in DataRow.Add/ModifyValues (checking indexes, updating min/max-values). #498

File size: 5.7 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      double remVal = dataRow[index];
116      //check if index is valid
117      if (index >= 0 && index < dataRow.Count) {
118        dataRow.RemoveAt(index);
119        OnValueChanged(remVal, index, Action.Deleted);
120      } else {
121        throw new IndexOutOfRangeException();
122      }
123    }
124
125    public override void RemoveValues(int index, int count) {
126      double[] remValues = new double[count]; //removed values
127      int j = 0;
128
129      //check if count is valid
130      if (count > 0) {
131        //check if index is valid
132        if ((index >= 0) && (index + count <= dataRow.Count)) {
133          for (int i = index; i < (index + count); i++) {
134            remValues.SetValue(i, j);
135            dataRow.RemoveAt(i);
136            j++;
137          }
138          OnValuesChanged(remValues, index, Action.Deleted);
139        } else {
140          throw new IndexOutOfRangeException();
141        }
142      } else {
143        throw new Exception("parameter count must be > 0!");
144      }
145    }
146
147    public override int Count {
148      get { return dataRow.Count; }
149    }
150
151    public override double this[int index] {
152      get { return dataRow[index]; }
153      set {
154        dataRow[index] = value;
155        OnValueChanged(value, index, Action.Modified);
156      }
157    }
158
159    public override double MinValue {
160      get { return minValue; }
161    }
162
163    public override double MaxValue {
164      get { return maxValue; }
165    }
166
167    private void UpdateMinMaxValue(double newValue, int oldValueIndex) {
168      if (minValue != dataRow[oldValueIndex] && maxValue != dataRow[oldValueIndex])
169        UpdateMinMaxValue(newValue);
170      else {
171        minValue = double.MaxValue;
172        maxValue = double.MinValue;
173
174        for (int i = 0; i < dataRow.Count; i++) {
175          double value = oldValueIndex != i ? dataRow[i] : newValue;
176          UpdateMinMaxValue(value);
177        }
178      }
179    }
180
181    private void UpdateMinMaxValue(double value) {
182      maxValue = Math.Max(value, maxValue);
183      minValue = Math.Min(value, minValue);
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.