1+ namespace Explorer . Common
2+ {
3+ using System ;
4+
5+ internal struct NoisyCount : IEquatable < NoisyCount >
6+ {
7+ private NoisyCount ( long count , double variance )
8+ {
9+ Count = count ;
10+ Variance = variance ;
11+ }
12+
13+ public static NoisyCount Zero => default ;
14+
15+ public long Count { get ; private set ; }
16+
17+ public double Variance { get ; private set ; }
18+
19+ public double Noise => Math . Sqrt ( Variance ) ;
20+
21+ public static bool operator == ( NoisyCount left , NoisyCount right ) => left . Equals ( right ) ;
22+
23+ public static bool operator != ( NoisyCount left , NoisyCount right ) => ! ( left == right ) ;
24+
25+ public static NoisyCount operator + ( NoisyCount left , NoisyCount right ) => left . Add ( right ) ;
26+
27+ public static NoisyCount Noiseless ( long count ) => new NoisyCount ( count , 0 ) ;
28+
29+ public static NoisyCount WithNoise ( long count , double noise ) => new NoisyCount ( count , noise * noise ) ;
30+
31+ public static NoisyCount WithVariance ( long count , double variance ) => new NoisyCount ( count , variance ) ;
32+
33+ public static NoisyCount FromCountableRow ( CountableRow row ) => WithNoise ( row . Count , row . CountNoise ?? 0.0 ) ;
34+
35+ public NoisyCount Add ( NoisyCount other ) => new NoisyCount ( Count + other . Count , Variance + other . Variance ) ;
36+
37+ public void Add ( long count , double noise )
38+ {
39+ Count += count ;
40+ Variance += noise * noise ;
41+ }
42+
43+ public void Add ( CountableRow row )
44+ {
45+ Add ( row . Count , row . CountNoise ?? 0.0 ) ;
46+ }
47+
48+ public override bool Equals ( object ? obj )
49+ {
50+ return obj is NoisyCount other && Equals ( other ) ;
51+ }
52+
53+ public override int GetHashCode ( )
54+ {
55+ return HashCode . Combine ( Count , Variance ) ;
56+ }
57+
58+ public bool Equals ( NoisyCount other ) =>
59+ Count == other . Count && Variance == other . Variance ;
60+ }
61+ }
0 commit comments