pub trait Accumulator: Sized {
type Input;
type Output;
// Required methods
fn new() -> Self;
fn push(&mut self, value: Self::Input);
fn combine(&mut self, other: Self);
fn result(self) -> Self::Output;
// Provided methods
fn new_sequential<I>(values: I) -> Self
where I: IntoIterator<Item = Self::Input> { ... }
fn new_parallel<I>(values: I) -> Self
where Self: Send,
I: IntoParallelIterator<Item = Self::Input> { ... }
}Expand description
Base trait for incremental accumulation over a sequence of values.
An accumulator collects items one at a time via push and
produces a final result via result. Partial accumulators
can be merged with combine, enabling chunked and parallel
reductions.
§Correctness requirement for combine
new() must be the identity element for combine: for any
accumulator x, both combine(new(), x) and combine(x, new()) must
produce an accumulator equivalent to x. This invariant is required by
new_parallel.
Required Associated Types§
Required Methods§
Sourcefn combine(&mut self, other: Self)
fn combine(&mut self, other: Self)
Merges other into self.
After this call self represents the accumulation of all values pushed
into either self or other. Used by new_parallel
to merge per-chunk partial results.
Provided Methods§
Sourcefn new_sequential<I>(values: I) -> Selfwhere
I: IntoIterator<Item = Self::Input>,
fn new_sequential<I>(values: I) -> Selfwhere
I: IntoIterator<Item = Self::Input>,
Creates a new accumulator by sequentially adding all values from values.
This is the simplest way to build an accumulator from owned input data.
For chunked or parallel reductions, prefer Accumulator::combine
on partial accumulators.
§Example
use num_valid::algorithms::accumulators::*;
let values = vec![1.0, 1.0e100, 1.0, -1.0e100];
let neumaier = NeumaierSum::new_sequential(values);
let sum = neumaier.result();
println!("Sum: {}", sum);
assert_eq!(sum, 2.0);Sourcefn new_parallel<I>(values: I) -> Self
fn new_parallel<I>(values: I) -> Self
Builds an accumulator by processing a parallel iterator.
The iterator is partitioned into chunks by rayon; each chunk is folded
into a partial accumulator with push, and the partial
results are merged with combine.
Requires Self: Send.
Any additional Send constraints on iterator items are enforced by the
concrete IntoParallelIterator implementation.
§Example
use num_valid::algorithms::accumulators::*;
use rayon::prelude::*;
let values = vec![1.0_f64, 2.0, 3.0, 4.0];
let sum = NaiveSum::new_parallel(values.into_par_iter()).result();
assert_eq!(sum, 10.0);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".