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
RevLine 
[761]1using System;
2using System.Drawing;
[860]3using System.Collections.Generic;
[761]4
5namespace HeuristicLab.Visualization {
[867]6  public enum Action {
7    Added,
8    Modified,
9    Deleted
10  }
11
[761]12  public delegate void DataRowChangedHandler(IDataRow row);
[867]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);
[761]15
[1350]16  public class DataRow : DataRowBase {
[1233]17    private readonly List<double> dataRow = new List<double>();
[761]18
[1285]19    private double minValue = double.MaxValue;
20    private double maxValue = double.MinValue;
21
[868]22    public DataRow() {
23    }
[1190]24   
[867]25    public DataRow(string label) {
[1962]26      this.RowSettings.Label = label;
[867]27    }
28
29    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow) {
[1962]30      this.RowSettings.Label = label;
31      this.RowSettings.Color = color;
32      this.RowSettings.Thickness = thickness;
[1972]33      this.RowSettings.Style = style;
[867]34      this.dataRow = dataRow;
[1972]35      this.RowSettings.ShowMarkers = true;
[867]36    }
37
[1561]38    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow, bool showMarkers) {
[1962]39      this.RowSettings.Label = label;
40      this.RowSettings.Color = color;
41      this.RowSettings.Thickness = thickness;
[1972]42      this.RowSettings.Style = style;
43      this.RowSettings.ShowMarkers = showMarkers;
[1561]44      this.dataRow = dataRow;
45    }
46
[1350]47    public override void AddValue(double value) {
[1285]48      UpdateMinMaxValue(value);
49
[860]50      dataRow.Add(value);
[867]51      OnValueChanged(value, dataRow.Count - 1, Action.Added);
[761]52    }
53
[1350]54    public override void AddValue(double value, int index) {
[867]55      //check if index is valid
56      if (index >= 0 && index < dataRow.Count) {
[1982]57        UpdateMinMaxValue(value);
[867]58        dataRow.Insert(index, value);
59        OnValueChanged(value, index, Action.Added);
60      } else {
[1233]61        throw new IndexOutOfRangeException();
[867]62      }   
[761]63    }
64
[1350]65    public override void AddValues(double[] values) {
[1978]66      int startIndex = dataRow.Count;
[860]67
[1978]68      foreach (double value in values) {
69        UpdateMinMaxValue(value);
70        dataRow.Add(value);
[860]71      }
[1978]72
73      OnValuesChanged(values, startIndex, Action.Added);
[761]74    }
75
[1350]76    public override void AddValues(double[] values, int index) {
[1983]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);
[860]82        }
[867]83        OnValuesChanged(values, index, Action.Added);
[860]84      } else {
[1233]85        throw new IndexOutOfRangeException();
[860]86      }
[761]87    }
88
[1350]89    public override void ModifyValue(double value, int index) {
[867]90      //check if index is valid
91      if (index >= 0 && index < dataRow.Count) {
[1983]92        UpdateMinMaxValue(value, index); // bad runtime but works
[867]93        dataRow[index] = value;
94        OnValueChanged(value, index, Action.Modified);
95      } else {
[1233]96        throw new IndexOutOfRangeException();
[867]97      }
[761]98    }
99
[1350]100    public override void ModifyValues(double[] values, int index) {
[867]101      //check if index to start modification is valid
[1983]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;
[867]107        }
[1983]108        OnValuesChanged(values, index, Action.Modified);
[867]109      } else {
[1233]110        throw new IndexOutOfRangeException();
[867]111      }
[761]112    }
113
[1350]114    public override void RemoveValue(int index) {
[867]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 {
[1233]121        throw new IndexOutOfRangeException();
[867]122      }
[761]123    }
124
[1350]125    public override void RemoveValues(int index, int count) {
[867]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 {
[1233]140          throw new IndexOutOfRangeException();
[867]141        }
142      } else {
[1233]143        throw new Exception("parameter count must be > 0!");
[867]144      }
[761]145    }
146
[1350]147    public override int Count {
[860]148      get { return dataRow.Count; }
[761]149    }
150
[1350]151    public override double this[int index] {
[860]152      get { return dataRow[index]; }
[761]153      set {
[860]154        dataRow[index] = value;
[867]155        OnValueChanged(value, index, Action.Modified);
[761]156      }
157    }
[1285]158
[1350]159    public override double MinValue {
[1285]160      get { return minValue; }
161    }
162
[1350]163    public override double MaxValue {
[1285]164      get { return maxValue; }
165    }
166
[1983]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
[1285]181    private void UpdateMinMaxValue(double value) {
182      maxValue = Math.Max(value, maxValue);
183      minValue = Math.Min(value, minValue);
184    }
[761]185  }
186}
Note: See TracBrowser for help on using the repository browser.