osiris_set_std/
range_applications.rs

1pub(crate) fn reduce<T>(range: &[T], func: fn(acc: T, word: &T) -> T, first: T) -> T {
2    range.iter().fold(first, func)
3}
4
5#[cfg(feature = "unsigned")]
6pub mod unsigned {
7    use osiris_data::data::atomic::Word;
8    use crate::range_applications::reduce;
9
10    pub fn sum(range: &[Word]) -> Word {
11        reduce(range, |acc, &word| Word::new(acc.to_u64() + word.to_u64()), Word::default())
12    }
13
14    pub fn product(range: &[Word]) -> Word {
15        reduce(range, |acc, &word| Word::new(acc.to_u64() * word.to_u64()), Word::new(1))
16    }
17
18    pub fn quotient(range: &[Word]) -> Word {
19        let first = range[0];
20        let rest = &range[1..];
21        reduce(rest, |acc, &word| Word::new(acc.to_u64() / word.to_u64()), first)
22    }
23
24    pub fn difference(range: &[Word]) -> Word {
25        reduce(range, |acc, &word| Word::new(acc.to_u64() - word.to_u64()), Word::default())
26    }
27}
28
29#[cfg(feature = "floating-point")]
30pub mod float {
31    use osiris_process::register::floating_point::Number;
32    use crate::range_applications::reduce;
33
34    pub fn sum(range: &[Number]) -> Number {
35        reduce(range, |acc, &word| Number::new(acc.to_f64() + word.to_f64()), Number::default())
36    }
37
38    pub fn product(range: &[Number]) -> Number {
39        reduce(range, |acc, &word| Number::new(acc.to_f64() * word.to_f64()), Number::new(1.0))
40    }
41
42    pub fn quotient(range: &[Number]) -> Number {
43        reduce(range, |acc, &word| Number::new(acc.to_f64() + word.to_f64()), Number::new(1.0))
44    }
45
46    pub fn difference(range: &[Number]) -> Number {
47        reduce(range, |acc, &word| Number::new(acc.to_f64() * word.to_f64()), Number::default())
48    }
49}