Skip to content

Commit 7c690d7

Browse files
bors[bot]syvb
andauthored
Merge #602
602: Make stats_agg::M3/M4 modules instead of structs r=Smittyvb a=Smittyvb Makes `M3` and `M4` in the stats-agg crate modules instead of empty structs, which is more idiomatic. Co-authored-by: Smitty <[email protected]>
2 parents ae7691f + 76fbb69 commit 7c690d7

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

crates/stats-agg/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ pub mod stats2d;
5959

6060
// This will wrap the logic for incrementing the sum for the third moment of a series of floats (i.e. Sum (i=1..N) of (i-avg)^3)
6161
// Math is sourced from https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Higher-order_statistics
62-
pub(crate) struct M3 {}
63-
impl M3 {
62+
mod m3 {
63+
use super::*;
64+
6465
// Add a value x to the set. n, sx, sxx, sx3 are the values from prior to including x.
6566
pub(crate) fn accum<T: FloatLike>(n: T, sx: T, sxx: T, sx3: T, x: T) -> T {
6667
let delta = x - (sx / n);
@@ -119,8 +120,9 @@ impl M3 {
119120

120121
// This will wrap the logic for incrementing the sum for the fourth moment of a series of floats (i.e. Sum (i=1..N) of (i-avg)^4)
121122
// Math is sourced from https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Higher-order_statistics
122-
pub(crate) struct M4 {}
123-
impl M4 {
123+
mod m4 {
124+
use super::*;
125+
124126
// Add a value x to the set. n, sx, sxx, sx3, sx4 are the values from prior to including x.
125127
pub(crate) fn accum<T: FloatLike>(n: T, sx: T, sxx: T, sx3: T, sx4: T, x: T) -> T {
126128
let delta = x - (sx / n);

crates/stats-agg/src/stats1d.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{FloatLike, StatsError, INV_FLOATING_ERROR_THRESHOLD, M3, M4};
1+
use crate::{m3, m4, FloatLike, StatsError, INV_FLOATING_ERROR_THRESHOLD};
22
use serde::{Deserialize, Serialize};
33
use twofloat::TwoFloat;
44

@@ -77,8 +77,8 @@ where
7777
let tmpx = p * self.n64() - self.sx;
7878
let scale = T::one() / (self.n64() * old.n64());
7979
self.sx2 += tmpx * tmpx * scale;
80-
self.sx3 = M3::accum(old.n64(), old.sx, old.sx2, old.sx3, p);
81-
self.sx4 = M4::accum(old.n64(), old.sx, old.sx2, old.sx3, old.sx4, p);
80+
self.sx3 = m3::accum(old.n64(), old.sx, old.sx2, old.sx3, p);
81+
self.sx4 = m4::accum(old.n64(), old.sx, old.sx2, old.sx3, old.sx4, p);
8282

8383
if self.has_infinite() {
8484
if self.check_overflow(&old, p) {
@@ -170,8 +170,8 @@ where
170170
let tmpx = p * self.n64() - self.sx;
171171
let scale = (self.n64() * new.n64()).recip();
172172
new.sx2 = self.sx2 - tmpx * tmpx * scale;
173-
new.sx3 = M3::remove(new.n64(), new.sx, new.sx2, self.sx3, p);
174-
new.sx4 = M4::remove(new.n64(), new.sx, new.sx2, new.sx3, self.sx4, p);
173+
new.sx3 = m3::remove(new.n64(), new.sx, new.sx2, self.sx3, p);
174+
new.sx4 = m4::remove(new.n64(), new.sx, new.sx2, new.sx3, self.sx4, p);
175175

176176
Some(new)
177177
}
@@ -207,7 +207,7 @@ where
207207
+ other.sx2
208208
+ self.n64() * other.n64() * tmp * tmp
209209
/ <T as num_traits::cast::NumCast>::from(n).unwrap(),
210-
sx3: M3::combine(
210+
sx3: m3::combine(
211211
self.n64(),
212212
other.n64(),
213213
self.sx,
@@ -217,7 +217,7 @@ where
217217
self.sx3,
218218
other.sx3,
219219
),
220-
sx4: M4::combine(
220+
sx4: m4::combine(
221221
self.n64(),
222222
other.n64(),
223223
self.sx,
@@ -264,7 +264,7 @@ where
264264
let tmp = part.sx / part.n64() - remove.sx / remove.n64(); //gets squared so order doesn't matter
265265
part.sx2 =
266266
combined.sx2 - remove.sx2 - part.n64() * remove.n64() * tmp * tmp / combined.n64();
267-
part.sx3 = M3::remove_combined(
267+
part.sx3 = m3::remove_combined(
268268
part.n64(),
269269
remove.n64(),
270270
part.sx,
@@ -274,7 +274,7 @@ where
274274
self.sx3,
275275
remove.sx3,
276276
);
277-
part.sx4 = M4::remove_combined(
277+
part.sx4 = m4::remove_combined(
278278
part.n64(),
279279
remove.n64(),
280280
part.sx,

crates/stats-agg/src/stats2d.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 2D stats are based on the Youngs-Cramer implementation in PG here:
22
// https://github.com/postgres/postgres/blob/472e518a44eacd9caac7d618f1b6451672ca4481/src/backend/utils/adt/float.c#L3260
3-
use crate::{FloatLike, StatsError, XYPair, INV_FLOATING_ERROR_THRESHOLD, M3, M4};
3+
use crate::{m3, m4, FloatLike, StatsError, XYPair, INV_FLOATING_ERROR_THRESHOLD};
44
use serde::{Deserialize, Serialize};
55
use twofloat::TwoFloat;
66

@@ -88,11 +88,11 @@ impl<T: FloatLike> StatsSummary2D<T> {
8888
let tmpy = p.y * self.n64() - self.sy;
8989
let scale = (self.n64() * old.n64()).recip();
9090
self.sx2 += tmpx * tmpx * scale;
91-
self.sx3 = M3::accum(old.n64(), old.sx, old.sx2, old.sx3, p.x);
92-
self.sx4 = M4::accum(old.n64(), old.sx, old.sx2, old.sx3, old.sx4, p.x);
91+
self.sx3 = m3::accum(old.n64(), old.sx, old.sx2, old.sx3, p.x);
92+
self.sx4 = m4::accum(old.n64(), old.sx, old.sx2, old.sx3, old.sx4, p.x);
9393
self.sy2 += tmpy * tmpy * scale;
94-
self.sy3 = M3::accum(old.n64(), old.sy, old.sy2, old.sy3, p.y);
95-
self.sy4 = M4::accum(old.n64(), old.sy, old.sy2, old.sy3, old.sy4, p.y);
94+
self.sy3 = m3::accum(old.n64(), old.sy, old.sy2, old.sy3, p.y);
95+
self.sy4 = m4::accum(old.n64(), old.sy, old.sy2, old.sy3, old.sy4, p.y);
9696
self.sxy += tmpx * tmpy * scale;
9797
if self.has_infinite() {
9898
if self.check_overflow(&old, p) {
@@ -230,11 +230,11 @@ impl<T: FloatLike> StatsSummary2D<T> {
230230
let tmpy = p.y * self.n64() - self.sy;
231231
let scale = (self.n64() * new.n64()).recip();
232232
new.sx2 = self.sx2 - tmpx * tmpx * scale;
233-
new.sx3 = M3::remove(new.n64(), new.sx, new.sx2, self.sx3, p.x);
234-
new.sx4 = M4::remove(new.n64(), new.sx, new.sx2, new.sx3, self.sx4, p.x);
233+
new.sx3 = m3::remove(new.n64(), new.sx, new.sx2, self.sx3, p.x);
234+
new.sx4 = m4::remove(new.n64(), new.sx, new.sx2, new.sx3, self.sx4, p.x);
235235
new.sy2 = self.sy2 - tmpy * tmpy * scale;
236-
new.sy3 = M3::remove(new.n64(), new.sy, new.sy2, self.sy3, p.y);
237-
new.sy4 = M4::remove(new.n64(), new.sy, new.sy2, new.sy3, self.sy4, p.y);
236+
new.sy3 = m3::remove(new.n64(), new.sy, new.sy2, self.sy3, p.y);
237+
new.sy4 = m4::remove(new.n64(), new.sy, new.sy2, new.sy3, self.sy4, p.y);
238238
new.sxy = self.sxy - tmpx * tmpy * scale;
239239
Some(new)
240240
}
@@ -293,7 +293,7 @@ impl<T: FloatLike> StatsSummary2D<T> {
293293
n,
294294
sx: self.sx + other.sx,
295295
sx2: self.sx2 + other.sx2 + self.n64() * other.n64() * tmpx * tmpx / T::from_u64(n),
296-
sx3: M3::combine(
296+
sx3: m3::combine(
297297
self.n64(),
298298
other.n64(),
299299
self.sx,
@@ -303,7 +303,7 @@ impl<T: FloatLike> StatsSummary2D<T> {
303303
self.sx3,
304304
other.sx3,
305305
),
306-
sx4: M4::combine(
306+
sx4: m4::combine(
307307
self.n64(),
308308
other.n64(),
309309
self.sx,
@@ -317,7 +317,7 @@ impl<T: FloatLike> StatsSummary2D<T> {
317317
),
318318
sy: self.sy + other.sy,
319319
sy2: self.sy2 + other.sy2 + self.n64() * other.n64() * tmpy * tmpy / T::from_u64(n),
320-
sy3: M3::combine(
320+
sy3: m3::combine(
321321
self.n64(),
322322
other.n64(),
323323
self.sy,
@@ -327,7 +327,7 @@ impl<T: FloatLike> StatsSummary2D<T> {
327327
self.sy3,
328328
other.sy3,
329329
),
330-
sy4: M4::combine(
330+
sy4: m4::combine(
331331
self.n64(),
332332
other.n64(),
333333
self.sy,
@@ -382,7 +382,7 @@ impl<T: FloatLike> StatsSummary2D<T> {
382382
let tmpy = part.sy / part.n64() - remove.sy / remove.n64();
383383
part.sx2 =
384384
combined.sx2 - remove.sx2 - part.n64() * remove.n64() * tmpx * tmpx / combined.n64();
385-
part.sx3 = M3::remove_combined(
385+
part.sx3 = m3::remove_combined(
386386
part.n64(),
387387
remove.n64(),
388388
part.sx,
@@ -392,7 +392,7 @@ impl<T: FloatLike> StatsSummary2D<T> {
392392
self.sx3,
393393
remove.sx3,
394394
);
395-
part.sx4 = M4::remove_combined(
395+
part.sx4 = m4::remove_combined(
396396
part.n64(),
397397
remove.n64(),
398398
part.sx,
@@ -406,7 +406,7 @@ impl<T: FloatLike> StatsSummary2D<T> {
406406
);
407407
part.sy2 =
408408
combined.sy2 - remove.sy2 - part.n64() * remove.n64() * tmpy * tmpy / combined.n64();
409-
part.sy3 = M3::remove_combined(
409+
part.sy3 = m3::remove_combined(
410410
part.n64(),
411411
remove.n64(),
412412
part.sy,
@@ -416,7 +416,7 @@ impl<T: FloatLike> StatsSummary2D<T> {
416416
self.sy3,
417417
remove.sy3,
418418
);
419-
part.sy4 = M4::remove_combined(
419+
part.sy4 = m4::remove_combined(
420420
part.n64(),
421421
remove.n64(),
422422
part.sy,

0 commit comments

Comments
 (0)