Skip to content

Commit

Permalink
adding stats functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kevloui committed Aug 1, 2020
1 parent b601901 commit 6cc0772
Showing 1 changed file with 51 additions and 61 deletions.
112 changes: 51 additions & 61 deletions amadeus-core/src/par_sink/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@ extern crate num;

use derive_new::new;
use educe::Educe;
use num::ToPrimitive;
use serde::{Deserialize, Serialize};
use std::{iter, marker::PhantomData, mem};
use std::marker::PhantomData;

use super::{folder_par_sink, FolderSync, FolderSyncReducer, ParallelPipe, ParallelSink};
use super::{
folder_par_sink, FolderSync, FolderSyncReducer, ParallelPipe, ParallelSink
};

#[derive(new)]
#[must_use]
pub struct Mean<P, B> {
pipe: P,
marker: PhantomData<fn() -> B>,
pub struct Mean<P> {
pipe: P,
}

impl_par_dist! {
impl<P: ParallelPipe<Item>, Item, B> ParallelSink<Item> for Mean<P, B>
where
B: iter::Sum<P::Output> + iter::Sum<B> + ToPrimitive + Send + 'static,
{
impl<P: ParallelPipe<f64>, f64> ParallelSink<f64> for Mean<P> {
folder_par_sink!(
MeanFolder<B, StepA>,
MeanFolder<B, StepB>,
MeanFolder<StepA>,
MeanFolder<StepB>,
self,
MeanFolder::new(),
MeanFolder::new()
Expand All @@ -34,67 +31,60 @@ impl_par_dist! {
#[educe(Clone)]
#[serde(bound = "")]

pub struct MeanFolder<B, Step> {
marker: PhantomData<fn() -> (B, Step)>,
pub struct MeanFolder<Step> {
marker: PhantomData<fn() -> Step>,
}

pub struct StepA;
pub struct StepB;

impl<B, Item> FolderSync<Item> for MeanFolder<B, StepA>
where
B: iter::Sum<Item> + iter::Sum<B> + ToPrimitive,
{
type State = (B, usize);
type Done = Self::State;
impl FolderSync<f64> for MeanFolder<StepA> {
type State = (f64, usize);
type Done = f64;

#[inline(always)]
fn zero(&mut self) -> Self::State {
(0.0,0)
}

#[inline(always)]
fn push(&mut self, state: &mut Self::State, item: f64) {
state.0 += item;
state.1 += 1;
}

#[inline(always)]
fn done(&mut self, state: Self::State) -> Self::Done {
let sum = state.0;
let count = state.1 as f64;
return sum / count
}
}

#[inline(always)]
fn zero(&mut self) -> Self::State {
(iter::empty::<B>().sum(), 0)
}

#[inline(always)]
fn push(&mut self, state: &mut Self::State, item: Item) {
let zero = iter::empty::<B>().sum();
let left = mem::replace(&mut state.0, zero);
let right = iter::once(item).sum::<B>();

state.0 = B::sum(iter::once(left).chain(iter::once(right)));
state.1 += 1;
}

#[inline(always)]
fn done(&mut self, state: Self::State) -> Self::Done {
state
}
}
impl FolderSync<(f64, usize)> for MeanFolder<StepB> {
type State = (f64, usize);
type Done = f64;

impl<B> FolderSync<(B, usize)> for MeanFolder<B, StepB>
where
B: iter::Sum<B> + ToPrimitive,
{
type State = (B, usize);
type Done = f64;
#[inline(always)]
fn zero(&mut self) -> Self::State {
(0.0,0)
}

#[inline(always)]
fn zero(&mut self) -> Self::State {
(iter::empty().sum(), 0)
}
#[inline(always)]
fn push(&mut self, state: &mut Self::State, item: (f64, usize)) {
state.0 += item.0;
state.1 += 1;
}

#[inline(always)]
fn push(&mut self, state: &mut Self::State, item: (B, usize)) {
let zero = iter::empty().sum();
let left = mem::replace(&mut state.0, zero);
let right = iter::once(item.0).sum();
#[inline(always)]
fn done(&mut self, state: Self::State) -> Self::Done {
let sum = state.0;
let count = state.1 as f64;
return sum / count;

state.0 = B::sum(iter::once(left).chain(iter::once(right)));
state.1 += 1;
}
}

#[inline(always)]
fn done(&mut self, state: Self::State) -> Self::Done {
let sum = state.0;
let count = state.1 as f64;
B::to_f64(&sum).map(|sum| sum / count).unwrap()
}
}

0 comments on commit 6cc0772

Please sign in to comment.