Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1530 was 1530, checked in by gkronber, 15 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

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