Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/AvgLineAggregator.cs @ 1605

Last change on this file since 1605 was 1605, checked in by cbahner, 15 years ago

#519 implemented performance improvements for avg aggregators

File size: 6.1 KB
Line 
1using System;
2using System.Collections.Generic;
3
4namespace HeuristicLab.Visualization {
5  public class AvgLineAggregator : DataRowBase {
6
7    private readonly List<double> dataRow = new List<double>();
8
9    #region IAggregator Members
10
11    public void AddWatch(IDataRow dataRow) {
12      dataRowWatches.Add(dataRow);
13      dataRow.ValueChanged += dataRow_ValueChanged;
14      dataRow.ValuesChanged += dataRow_ValuesChanged;
15      dataRow.DataRowChanged += dataRow_DataRowChanged;
16    }
17
18    public void RemoveWatch(IDataRow dataRow) {
19
20      dataRowWatches.Remove(dataRow);
21      dataRow.DataRowChanged -= dataRow_DataRowChanged;
22      dataRow.ValuesChanged -= dataRow_ValuesChanged;
23      dataRow.ValueChanged -= dataRow_ValueChanged;
24    }
25
26
27    #endregion
28
29    List<IDataRow> dataRowWatches = new List<IDataRow>();
30
31    void dataRow_ValueChanged(IDataRow row, double value, int index, Action action) {
32      switch (action) {
33        case Action.Added:
34          refreshValue(row, value, Action.Added);
35          break;
36        case Action.Modified:
37          refreshValue();
38          break;
39        case Action.Deleted:
40          refreshValue(row, value, Action.Deleted);
41          break;
42        default:
43          throw new ArgumentOutOfRangeException("action");
44      }
45
46    }
47
48    void dataRow_ValuesChanged(IDataRow row, double[] values, int index, Action action) {
49      for (int i = 0; i < values.Length; i++) {
50        refreshValue();
51      }
52    }
53
54    void dataRow_DataRowChanged(IDataRow row) {
55      refreshValue();
56    }
57
58    private void refreshValue() {
59      double tmpSum = 0;
60      int count = dataRowWatches.Count;
61
62      IDataRow firstRow = dataRowWatches[0];
63      int count1 = firstRow.Count;
64      System.Console.WriteLine("count: " + count1);
65
66      dataRow.Clear();
67
68      if (dataRowWatches.Count >= 2) {
69        for (int i = 0; i < count1; i++) {
70          tmpSum = 0;
71          for (int j = 0; j < count; j++) {
72            if (dataRowWatches[j].Count > i) {
73              tmpSum += dataRowWatches[j][i];
74            }
75          }
76
77          this.dataRow.Add(tmpSum / count);
78          OnValueChanged(tmpSum / count, dataRow.Count - 1, Action.Added);
79        }
80      }
81    }
82
83    private void refreshValue(IDataRow row, double newVal, Action action) {
84
85      int index = row.Count - 1;
86
87
88     
89     
90      double curAvg = 0;
91//      if (dataRow.Count > 0) {
92//        curAvg = dataRow[0];   //?
93//      } else {
94//        curAvg = 0;
95//      }
96
97
98      foreach (IDataRow watch in dataRowWatches) {
99        if (watch.Count >= index +1) {
100          curAvg += watch[index];
101         
102        }
103        //curAvg += watch[watch.Count - 1];
104      }
105
106      if (dataRowWatches.Count > 0)
107        curAvg /= dataRowWatches.Count;
108
109
110      if (dataRow.Count <= index) {
111        dataRow.Add(curAvg);
112        OnValueChanged(curAvg, dataRow.Count - 1, Action.Added);     
113      }
114      else {
115        dataRow[index] = curAvg;
116        OnValueChanged(curAvg, dataRow.Count - 1, Action.Modified); 
117      }
118
119
120
121//      curAvg *= dataRow.Count * dataRowWatches.Count;
122//      switch (action) {
123//        case Action.Added:
124//          curAvg += newVal;
125//          break;
126//        case Action.Modified:
127//          throw new InvalidOperationException();
128//        case Action.Deleted:
129//          curAvg -= newVal;
130//          break;
131//        default:
132//          throw new ArgumentOutOfRangeException("action");
133//      }
134//
135//      dataRow.Add((curAvg / (dataRow.Count + 1)) / dataRowWatches.Count);
136//      OnValueChanged((curAvg / (dataRow.Count + 1)) / dataRowWatches.Count, dataRow.Count - 1, Action.Added); // nicht immer adden!
137
138      //      double tmpSum = 0;
139      //      int count = dataRowWatches.Count;
140      //
141      //      IDataRow firstRow = dataRowWatches[0];
142      //      int count1 = firstRow.Count;
143      //      System.Console.WriteLine("count: " + count1);
144      //
145      //      dataRow.Clear();
146      //
147      //      if (dataRowWatches.Count >= 2) {
148      //        for (int i = 0; i < count1; i++) {
149      //          tmpSum = 0;
150      //          for (int j = 0; j < count; j++) {
151      //            if (dataRowWatches[j].Count > i) {
152      //              tmpSum += dataRowWatches[j][i];
153      //            }
154      //          }
155      //
156      //          this.dataRow.Add(tmpSum/count);
157      //          OnValueChanged(tmpSum / count, dataRow.Count - 1, Action.Added);
158      //        }
159      //      }
160
161      // evtl nur feuern wenn sich geändert hat (jedes mal?)
162      //OnDataRowChanged(this);
163    }
164
165    #region IDataRow Members
166
167    public override void AddValue(double value) {
168      dataRow.Add(value);
169      OnValueChanged(value, dataRow.Count - 1, Action.Added);
170    }
171
172    public override void AddValue(double value, int index) {
173      throw new NotImplementedException();
174    }
175
176    public override void AddValues(double[] values) {
177      throw new NotSupportedException();
178    }
179
180    public override void AddValues(double[] values, int index) {
181      throw new NotSupportedException();
182    }
183
184    public override void ModifyValue(double value, int index) {
185      throw new NotSupportedException();
186    }
187
188    public override void ModifyValues(double[] values, int index) {
189      throw new NotSupportedException();
190    }
191
192    public override void RemoveValue(int index) {
193      throw new NotSupportedException();
194    }
195
196    public override void RemoveValues(int index, int count) {
197      throw new NotSupportedException();
198    }
199
200    public override int Count {
201      get { return dataRow.Count; } //return dataRowWatches.Count; }
202    }
203
204    public override double this[int index] {
205      get { return dataRow[index]; }
206      set {
207        dataRow[index] = value;
208        OnValueChanged(value, index, Action.Modified);
209      }
210    }
211
212    // TODO calculate min value
213    public override double MinValue {
214      get { return 0; }
215    }
216
217    // TODO calculate max value
218    public override double MaxValue {
219      get { return 0; }
220    }
221
222    #endregion
223  }
224}
Note: See TracBrowser for help on using the repository browser.