Fleet  0.0.9
Inference in the LOT
StreamingSum.h
Go to the documentation of this file.
1 #pragma once
2 
3 template<typename T>
4 struct StreamingSum {
5  T total;
6  T low; // low order bits
7  // TODO: Maybe lets pick an even better version? https://en.wikipedia.org/wiki/Kahan_summation_algorithm
8  StreamingSum(const T v=0) : total(v), low(0) { }
9 
10  T operator<<(const T x) {
11  T y = x - low;
12  volatile T t = total + y; // see here: https://en.wikipedia.org/wiki/Kahan_summation_algorithm
13  volatile T z = t-total;
14  low = z-y;
15 
16  total = t;
17 
18  return x; // NOT the total
19  }
20 
21  T operator=(const T v) {
22  total = v;
23  low = 0;
24  return v;
25  }
26 
27  T operator+=(const T v) {
28  return this->operator<<(v);
29  }
30 
31  operator double() const { return double(total+low); }
32 };
33 
34 
43 template<typename T>
45  T total;
46 
47  VanillaStreamingSum() : total(0) { }
48 
49  T operator<<(T x) {
50  total += x;
51 
52  return x; // NOT the total
53  }
54 
55  operator double() const { return double(total); }
56 };
57 
58 
59 
60 template<typename T>
61 struct StreamingMean {
62  size_t n;
64 
65  StreamingMean() : n(0) {
66  }
67 
68  T operator<<(T v) {
69  sum << v;
70  this->n++;
71  return v;
72  }
73 
74  operator T() const {
75  return T(sum)/this->n; // NOTE: Doesn't behave right if ints...
76  }
77 
78  float as_double() {
79  return double(this->s)/double(this->n);
80  }
81 
82 };
T low
Definition: StreamingSum.h:6
StreamingSum(const T v=0)
Definition: StreamingSum.h:8
VanillaStreamingSum()
Definition: StreamingSum.h:47
T total
Definition: StreamingSum.h:5
T operator<<(T v)
Definition: StreamingSum.h:68
Definition: StreamingSum.h:4
T operator=(const T v)
Definition: StreamingSum.h:21
Definition: StreamingSum.h:44
size_t n
Definition: StreamingSum.h:62
T operator<<(T x)
Definition: StreamingSum.h:49
float as_double()
Definition: StreamingSum.h:78
StreamingMean()
Definition: StreamingSum.h:65
Definition: StreamingSum.h:61
T operator<<(const T x)
Definition: StreamingSum.h:10
T operator+=(const T v)
Definition: StreamingSum.h:27
T total
Definition: StreamingSum.h:45
StreamingSum< T > sum
Definition: StreamingSum.h:63