1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::ops::{Add, Mul};

use crate::nodes::{LazyNode, Node, PersistentNode};

/// Implementation of range sum for generic type T, it implements [Node], [LazyNode] and [PersistentNode], as such it can be used as a node in every segment tree type.
#[derive(Clone, Debug)]
pub struct Sum<T>
where
    T: Add<Output = T> + Clone,
{
    value: T,
    lazy_value: Option<T>,
    left: usize,
    right: usize,
}

impl<T> Node for Sum<T>
where
    T: Add<Output = T> + Clone,
{
    type Value = T;
    /// The node is initialized with the value given.
    fn initialize(v: &Self::Value) -> Self {
        Sum {
            value: v.clone(),
            lazy_value: None,
            left: 0,
            right: 0,
        }
    }
    /// As this is a range sum node, the operation which is used to 'merge' two nodes is `+`.
    fn combine(a: &Self, b: &Self) -> Self {
        Sum {
            value: a.value.clone() + b.value.clone(),
            lazy_value: None,
            left: 0,
            right: 0,
        }
    }
    fn value(&self) -> &Self::Value {
        &self.value
    }
}

/// Implementation for sum range query node, the update adds the value to each item in the range.
/// It assumes that `a*n`, where a: T and n: usize is well defined and `a*n = a+...+a` with 'n' a.
/// For non-commutative operations, two things will be true `lazy_value = lazy_value + new_value`.
impl<T> LazyNode for Sum<T>
where
    T: Add<Output = T> + Mul<usize, Output = T> + Clone,
{
    fn lazy_update(&mut self, i: usize, j: usize) {
        if let Some(value) = self.lazy_value.take() {
            let temp = self.value.clone() + value * (j - i + 1);
            self.value = temp;
        }
    }

    fn update_lazy_value(&mut self, new_value: &<Self as Node>::Value) {
        if let Some(value) = self.lazy_value.take() {
            self.lazy_value = Some(value + new_value.clone());
        } else {
            self.lazy_value = Some(new_value.clone());
        }
    }

    fn lazy_value(&self) -> Option<&<Self as Node>::Value> {
        self.lazy_value.as_ref()
    }
}
/// This is a pretty generic implementation of [PersistentNode] for a struct.
impl<T> PersistentNode for Sum<T>
where
    T: Add<Output = T> + Clone,
{
    fn left_child(&self) -> usize {
        self.left
    }

    fn right_child(&self) -> usize {
        self.right
    }

    fn set_children(&mut self, left: usize, right: usize) {
        self.left = left;
        self.right = right;
    }
}

#[cfg(test)]
mod tests {
    use std::ops::{Add, Mul};

    use crate::{
        default::Sum,
        nodes::{LazyNode, Node},
    };

    #[derive(Clone, Copy, Debug, PartialEq)]
    struct NonCommutativeTest(u64);
    /// It satisfies a+b==b
    impl Add for NonCommutativeTest {
        type Output = Self;

        fn add(self, rhs: Self) -> Self::Output {
            rhs
        }
    }

    impl Mul<usize> for NonCommutativeTest {
        type Output = Self;

        fn mul(self, _rhs: usize) -> Self::Output {
            self
        }
    }

    #[test]
    fn sum_works() {
        let nodes: Vec<Sum<usize>> = (0..=1000000).map(|x| Sum::initialize(&x)).collect();
        let result = nodes
            .iter()
            .fold(Sum::initialize(&0), |acc, new| Sum::combine(&acc, new));
        assert_eq!(result.value(), &500000500000);
    }
    #[test]
    fn non_commutative_sum_works() {
        let nodes: Vec<Sum<NonCommutativeTest>> = (0..=1000000)
            .map(|x| Sum::initialize(&NonCommutativeTest(x)))
            .collect();
        let result = nodes
            .iter()
            .fold(Sum::initialize(&NonCommutativeTest(0)), |acc, new| {
                Sum::combine(&acc, new)
            });
        assert_eq!(result.value(), &NonCommutativeTest(1000000));
    }
    #[test]
    fn update_lazy_value_works() {
        let mut node = Sum::initialize(&1);
        node.update_lazy_value(&2);
        assert_eq!(node.lazy_value(), Some(&2));
    }

    #[test]
    fn lazy_update_works() {
        // Node represents the range [0,10] with sum 1.
        let mut node = Sum::initialize(&1);
        node.update_lazy_value(&2);
        node.lazy_update(0, 10);
        assert_eq!(node.value(), &23);
    }

    #[test]
    fn non_commutative_update_lazy_value_works() {
        let mut node = Sum::initialize(&NonCommutativeTest(1));
        node.update_lazy_value(&NonCommutativeTest(2));
        assert_eq!(node.lazy_value(), Some(&NonCommutativeTest(2)));
    }
    #[test]
    fn non_commutative_lazy_update_works() {
        // Node represents the range [0,10] with sum 1.
        let mut node = Sum::initialize(&NonCommutativeTest(1));
        node.update_lazy_value(&NonCommutativeTest(2));
        node.lazy_update(0, 10);
        assert_eq!(node.value(), &NonCommutativeTest(2));
    }
}