Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/AvgAggregator.cs @ 1673

Last change on this file since 1673 was 1607, checked in by cbahner, 16 years ago

#519 test cases + code review

File size: 3.9 KB
Line 
1using System;
2using System.Collections.Generic;
3
4namespace HeuristicLab.Visualization {
5  public class AvgAggregator : DataRowBase {
6    #region IAggregator Members
7
8    public void AddWatch(IDataRow dataRow) {
9      dataRowWatches.Add(dataRow);
10      dataRow.ValueChanged += dataRow_ValueChanged;
11      dataRow.ValuesChanged += dataRow_ValuesChanged;
12      dataRow.DataRowChanged += dataRow_DataRowChanged;
13    }
14
15    public void RemoveWatch(IDataRow dataRow) {
16      dataRowWatches.Remove(dataRow);
17      dataRow.DataRowChanged -= dataRow_DataRowChanged;
18      dataRow.ValuesChanged -= dataRow_ValuesChanged;
19      dataRow.ValueChanged -= dataRow_ValueChanged;
20    }
21
22    #endregion
23
24    private readonly List<IDataRow> dataRowWatches = new List<IDataRow>();
25    private double curAvgValue;
26
27    private void dataRow_ValueChanged(IDataRow row, double value, int index, Action action) {
28      switch (action) {
29        case Action.Added:
30          refreshValue(value, action);
31          break;
32        case Action.Modified:
33          refreshValue();
34          break;
35        case Action.Deleted:
36          refreshValue(value, action);
37          break;
38        default:
39          throw new ArgumentOutOfRangeException("action");
40      }
41
42
43    }
44
45    private void dataRow_ValuesChanged(IDataRow row, double[] values, int index, Action action) {
46      for (int i = 0; i < values.Length; i++) {
47        refreshValue();
48      }
49    }
50
51    private void dataRow_DataRowChanged(IDataRow row) {
52      refreshValue();
53    }
54
55    private int count;
56   
57    public AvgAggregator() {
58      curAvgValue = 0;
59      count = 0;
60    }
61
62    private void refreshValue() {
63      curAvgValue = double.MinValue;
64      double tmpSum = 0;
65      count = 0;
66      foreach (IDataRow rows in dataRowWatches) {
67        for (int i = 0; i < rows.Count; i++) {
68          tmpSum += rows[i];
69          count++;
70        }
71      }
72      if (count == 0) curAvgValue = 0;
73      else curAvgValue = tmpSum / count;
74      OnValueChanged(curAvgValue, 0, Action.Modified);
75    }
76    private void refreshValue(double newVal, Action action) {
77      double temp = curAvgValue * count;
78
79      switch (action) {
80        case Action.Added:
81          temp += newVal;
82          count++;
83          break;
84        case Action.Modified:
85          throw new InvalidOperationException();
86        case Action.Deleted:
87          temp -= newVal;
88          count--;
89          break;
90        default:
91          throw new ArgumentOutOfRangeException("action");
92      }
93
94      curAvgValue = temp / count;
95      OnValueChanged(curAvgValue, 0, Action.Modified);
96    }
97
98    #region IDataRow Members
99
100    public override void AddValue(double value) {
101      throw new NotSupportedException();
102    }
103
104    public override void AddValue(double value, int index) {
105      throw new NotSupportedException();
106    }
107
108    public override void AddValues(double[] values) {
109      throw new NotSupportedException();
110    }
111
112    public override void AddValues(double[] values, int index) {
113      throw new NotSupportedException();
114    }
115
116    public override void ModifyValue(double value, int index) {
117      throw new NotSupportedException();
118    }
119
120    public override void ModifyValues(double[] values, int index) {
121      throw new NotSupportedException();
122    }
123
124    public override void RemoveValue(int index) {
125      throw new NotSupportedException();
126    }
127
128    public override void RemoveValues(int index, int countVals) {
129      throw new NotSupportedException();
130    }
131
132    public override int Count {
133      get { return 1; }
134    }
135
136    public override double this[int index] {
137      get { return curAvgValue; }
138      set { throw new NotSupportedException(); }
139    }
140
141    public override double MinValue {
142      get { return curAvgValue; }
143    }
144
145    public override double MaxValue {
146      get { return curAvgValue; }
147    }
148
149    #endregion
150  }
151}
Note: See TracBrowser for help on using the repository browser.