Free cookie consent management tool by TermsFeed Policy Generator

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

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

#519 test cases + code review

File size: 3.9 KB
RevLine 
[1325]1using System;
2using System.Collections.Generic;
3
4namespace HeuristicLab.Visualization {
[1350]5  public class AvgAggregator : DataRowBase {
[1325]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
[1607]24    private readonly List<IDataRow> dataRowWatches = new List<IDataRow>();
[1350]25    private double curAvgValue;
[1325]26
[1350]27    private void dataRow_ValueChanged(IDataRow row, double value, int index, Action action) {
[1605]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
[1325]43    }
44
[1350]45    private void dataRow_ValuesChanged(IDataRow row, double[] values, int index, Action action) {
[1325]46      for (int i = 0; i < values.Length; i++) {
[1605]47        refreshValue();
[1325]48      }
49    }
50
[1350]51    private void dataRow_DataRowChanged(IDataRow row) {
[1605]52      refreshValue();
[1325]53    }
54
[1605]55    private int count;
56   
57    public AvgAggregator() {
58      curAvgValue = 0;
59      count = 0;
60    }
[1325]61
[1605]62    private void refreshValue() {
[1325]63      curAvgValue = double.MinValue;
64      double tmpSum = 0;
[1605]65      count = 0;
[1350]66      foreach (IDataRow rows in dataRowWatches) {
[1325]67        for (int i = 0; i < rows.Count; i++) {
68          tmpSum += rows[i];
69          count++;
70        }
71      }
[1605]72      if (count == 0) curAvgValue = 0;
73      else curAvgValue = tmpSum / count;
[1325]74      OnValueChanged(curAvgValue, 0, Action.Modified);
75    }
[1605]76    private void refreshValue(double newVal, Action action) {
77      double temp = curAvgValue * count;
[1325]78
[1605]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
[1325]98    #region IDataRow Members
99
[1350]100    public override void AddValue(double value) {
[1325]101      throw new NotSupportedException();
102    }
103
[1350]104    public override void AddValue(double value, int index) {
[1325]105      throw new NotSupportedException();
106    }
107
[1350]108    public override void AddValues(double[] values) {
[1325]109      throw new NotSupportedException();
110    }
111
[1350]112    public override void AddValues(double[] values, int index) {
[1325]113      throw new NotSupportedException();
114    }
115
[1350]116    public override void ModifyValue(double value, int index) {
[1325]117      throw new NotSupportedException();
118    }
119
[1350]120    public override void ModifyValues(double[] values, int index) {
[1325]121      throw new NotSupportedException();
122    }
123
[1350]124    public override void RemoveValue(int index) {
[1325]125      throw new NotSupportedException();
126    }
127
[1607]128    public override void RemoveValues(int index, int countVals) {
[1325]129      throw new NotSupportedException();
130    }
131
[1350]132    public override int Count {
[1325]133      get { return 1; }
134    }
135
[1350]136    public override double this[int index] {
137      get { return curAvgValue; }
138      set { throw new NotSupportedException(); }
[1325]139    }
140
[1350]141    public override double MinValue {
142      get { return curAvgValue; }
[1327]143    }
144
[1350]145    public override double MaxValue {
146      get { return curAvgValue; }
[1327]147    }
148
[1325]149    #endregion
150  }
[1350]151}
Note: See TracBrowser for help on using the repository browser.