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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//! Traits for types that can be used in a `PrefixSum`.
//!
//! This crate has the features `num-bigint`, `num-rational` and `num-complex`, which can
//! be enabled to add implementations of `Summable` to those types. There is also a `num`
//! feature, which enables all the numeric features.

/// Trait for types that can be used in a `PrefixSum`.
///
/// The distinction between `add_assign` and `add_assign_ref` exists to allow types such
/// as [`BigInt`] to make proper use of memory allocations as appropriate.
///
/// See also the [`SummableSigned`] marker trait.
///
/// # Laws
///
///  1. Any piece of code using [`add_assign`] should be equivalent to the code using
///     [`add_assign_ref`] instead, excluding any performance differences. The same must
///     be true for [`sub_assign`] and [`sub_assign_ref`].
///  2. Addition should be [associative][1], that is if `a`, `b` and `c` are values of a
///     type implementing `Summable`, then
///     ```
///     # use prefix_sum::summable::Summable;
///     # let mut a: i32 = 80;
///     # let b: i32 = 91;
///     # let c: i32 = 31;
///     a.add_assign(b);
///     a.add_assign(c);
///     # assert_eq!(a, 80+b+c);
///     ```
///     must leave `a` in the same state as
///     ```
///     # use prefix_sum::summable::Summable;
///     # let mut a: i32 = 80;
///     # let mut b: i32 = 91;
///     # let c: i32 = 31;
///     b.add_assign(c);
///     a.add_assign(b);
///     # assert_eq!(a, 80+91+c);
///     ```
///  3. Addition should be [commutative][2], that is if `a` and `b` are values of a type
///     implementing `Summable`, then
///     ```
///     # use prefix_sum::summable::Summable;
///     # let mut a: i32 = 80;
///     # let b: i32 = 91;
///     a.add_assign(b);
///     # assert_eq!(a, 80+91);
///     ```
///     should leave `a` in the same state as what the following code leaves `b` in:
///     ```
///     # use prefix_sum::summable::Summable;
///     # let a: i32 = 80;
///     # let mut b: i32 = 91;
///     b.add_assign(a);
///     # assert_eq!(b, 80+91);
///     ```
///  4. The additive identity of [`add_assign`] should be the value returned by [`zero`],
///     that is, if `a` is a value of a type implementing `Summable`, then
///     ```
///     # use prefix_sum::summable::Summable;
///     # let mut a: i32 = 80;
///     a.add_assign(Summable::zero());
///     # assert_eq!(a, 80);
///     ```
///     must not change the value of `a`.
///  5. Any value substracted from itself must be zero, that is, if `a` and `b` are two
///     values of a type implementing `Summable`, then if `a` is equal to `b`, then
///     ```
///     # use prefix_sum::summable::Summable;
///     # let mut a: i32 = 80;
///     # let b: i32 = a;
///     a.sub_assign(b);
///     # assert_eq!(a, 0);
///     ```
///     must leave `a` in the same state as
///     ```
///     # use prefix_sum::summable::Summable;
///     # let mut a: i32 = 80;
///     a = Summable::zero();
///     # assert_eq!(a, 0);
///     ```
///  6. Subtraction should be equivalent to addition of inverses, that is if `a` and `b`
///     are values of any type implementing `Summable`, then
///     ```
///     # use prefix_sum::summable::Summable;
///     # let mut a: i32 = 80;
///     # let mut b: i32 = 91;
///     a.sub_assign(b);
///     # assert_eq!(a, 80 - 91);
///     ```
///     must leave `a` in the same state as
///     ```
///     # use prefix_sum::summable::Summable;
///     # let mut a: i32 = 80;
///     # let mut b: i32 = 91;
///     let b_tmp = b;
///     b = Summable::zero();
///     b.sub_assign(b_tmp);
///     a.add_assign(b);
///     # assert_eq!(a, 80 - 91);
///     ```
///
/// Note that usage of a `PrefixSum` will always result in negative values somewhere, so
/// unsigned integers must implement addition and subtraction in a wrapping manner to be
/// usable.
///
/// [`BigInt`]: https://docs.rs/num-bigint/0.2/num_bigint/struct.BigInt.html
/// [`SummableSigned`]: trait.SummableSigned.html
/// [`zero`]: trait.Summable.html#tymethod.zero
/// [`add_assign`]: trait.Summable.html#tymethod.add_assign
/// [`add_assign_ref`]: trait.Summable.html#tymethod.add_assign_ref
/// [`sub_assign`]: trait.Summable.html#tymethod.sub_assign
/// [`sub_assign_ref`]: trait.Summable.html#tymethod.sub_assign_ref
/// [1]: https://en.wikipedia.org/wiki/Associative_property
/// [2]: https://en.wikipedia.org/wiki/Commutative_property
pub trait Summable {
    /// Returns the identity for the given type. This should always return the same value.
    fn zero() -> Self;
    /// Add the given value to self.
    fn add_assign_ref(&mut self, rhs: &Self);
    /// Subtract the given value from self.
    fn sub_assign_ref(&mut self, rhs: &Self);
    /// Add the given value to self while reusing resources in `rhs`.
    fn add_assign(&mut self, rhs: Self);
    /// Subtract the given value from self while reusing resources in `rhs`.
    fn sub_assign(&mut self, rhs: Self);
}

/// A marker trait on types that allow negative values.
///
/// Types without this marker cannot use `sum[a..b] -= value;`.
pub trait SummableSigned: Summable {}

#[cfg(feature = "num-bigint")]
impl Summable for num_bigint::BigInt {
    fn zero() -> Self {
        0u32.into()
    }
    fn add_assign_ref(&mut self, rhs: &Self) {
        *self += rhs;
    }
    fn sub_assign_ref(&mut self, rhs: &Self) {
        *self -= rhs;
    }
    fn add_assign(&mut self, rhs: Self) {
        *self += rhs;
    }
    fn sub_assign(&mut self, rhs: Self) {
        *self -= rhs;
    }
}
macro_rules! impl_summable_ratio {
    ( $ty:ty, $zero:expr ) => {
        #[cfg(feature = "num-rational")]
        impl SummableSigned for num_rational::Ratio<$ty> {}
        #[cfg(feature = "num-rational")]
        impl Summable for num_rational::Ratio<$ty> {
            #[inline]
            fn zero() -> Self {
                $zero
            }
            #[inline]
            fn add_assign_ref(&mut self, rhs: &Self) {
                *self += rhs;
            }
            #[inline]
            fn sub_assign_ref(&mut self, rhs: &Self) {
                *self -= rhs;
            }
            #[inline]
            fn add_assign(&mut self, rhs: Self) {
                *self += rhs;
            }
            #[inline]
            fn sub_assign(&mut self, rhs: Self) {
                *self -= rhs;
            }
        }
    };
}
impl_summable_ratio!(i8, 0.into());
impl_summable_ratio!(i16, 0.into());
impl_summable_ratio!(i32, 0.into());
impl_summable_ratio!(i64, 0.into());
impl_summable_ratio!(i128, 0.into());
impl_summable_ratio!(isize, 0.into());
#[cfg(feature = "num-bigint")]
impl_summable_ratio!(num_bigint::BigInt, num_bigint::BigInt::from(0).into());

#[cfg(feature = "num-complex")]
impl<T: SummableSigned> SummableSigned for num_complex::Complex<T> {}
#[cfg(feature = "num-complex")]
impl<T: SummableSigned> Summable for num_complex::Complex<T> {
    #[inline]
    fn zero() -> Self {
        num_complex::Complex {
            re: T::zero(),
            im: T::zero(),
        }
    }
    #[inline]
    fn add_assign_ref(&mut self, rhs: &Self) {
        T::add_assign_ref(&mut self.re, &rhs.re);
        T::add_assign_ref(&mut self.im, &rhs.im);
    }
    #[inline]
    fn sub_assign_ref(&mut self, rhs: &Self) {
        T::sub_assign_ref(&mut self.re, &rhs.re);
        T::sub_assign_ref(&mut self.im, &rhs.im);
    }
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        T::add_assign(&mut self.re, rhs.re);
        T::add_assign(&mut self.im, rhs.im);
    }
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        T::sub_assign(&mut self.re, rhs.re);
        T::sub_assign(&mut self.im, rhs.im);
    }
}

macro_rules! impl_summable_signed {
    ( $ty:tt ) => {
        impl SummableSigned for $ty {}
        impl Summable for $ty {
            #[inline]
            fn zero() -> $ty {
                0 as $ty
            }
            #[inline]
            fn add_assign_ref(&mut self, rhs: &$ty) {
                *self += *rhs;
            }
            #[inline]
            fn sub_assign_ref(&mut self, rhs: &$ty) {
                *self -= *rhs;
            }
            #[inline]
            fn add_assign(&mut self, rhs: $ty) {
                *self += rhs;
            }
            #[inline]
            fn sub_assign(&mut self, rhs: $ty) {
                *self -= rhs;
            }
        }
    };
}
macro_rules! impl_summable_signed_wrapping {
    ( $ty:tt ) => {
        impl SummableSigned for std::num::Wrapping<$ty> {}
        impl Summable for std::num::Wrapping<$ty> {
            #[inline]
            fn zero() -> Self {
                std::num::Wrapping(0 as $ty)
            }
            #[inline]
            fn add_assign_ref(&mut self, rhs: &Self) {
                *self += *rhs;
            }
            #[inline]
            fn sub_assign_ref(&mut self, rhs: &Self) {
                *self -= *rhs;
            }
            #[inline]
            fn add_assign(&mut self, rhs: Self) {
                *self += rhs;
            }
            #[inline]
            fn sub_assign(&mut self, rhs: Self) {
                *self -= rhs;
            }
        }
    };
}
macro_rules! impl_summable_unsigned {
    ( $ty:tt ) => {
        impl Summable for $ty {
            #[inline]
            fn zero() -> $ty {
                0 as $ty
            }
            #[inline]
            fn add_assign_ref(&mut self, rhs: &$ty) {
                *self = self.wrapping_add(*rhs);
            }
            #[inline]
            fn sub_assign_ref(&mut self, rhs: &$ty) {
                *self = self.wrapping_sub(*rhs);
            }
            #[inline]
            fn add_assign(&mut self, rhs: $ty) {
                *self = self.wrapping_add(rhs);
            }
            #[inline]
            fn sub_assign(&mut self, rhs: $ty) {
                *self = self.wrapping_sub(rhs);
            }
        }
    };
}
macro_rules! impl_summable_unsigned_wrapping {
    ( $ty:tt ) => {
        impl Summable for std::num::Wrapping<$ty> {
            #[inline]
            fn zero() -> Self {
                std::num::Wrapping(0 as $ty)
            }
            #[inline]
            fn add_assign_ref(&mut self, rhs: &Self) {
                *self += rhs;
            }
            #[inline]
            fn sub_assign_ref(&mut self, rhs: &Self) {
                *self -= *rhs;
            }
            #[inline]
            fn add_assign(&mut self, rhs: Self) {
                *self += rhs;
            }
            #[inline]
            fn sub_assign(&mut self, rhs: Self) {
                *self -= rhs;
            }
        }
        impl SummableSigned for std::num::Wrapping<$ty> {}
    };
}

impl_summable_signed!(i8);
impl_summable_signed!(i16);
impl_summable_signed!(i32);
impl_summable_signed!(i64);
impl_summable_signed!(i128);
impl_summable_signed!(isize);
impl_summable_signed!(f32);
impl_summable_signed!(f64);
impl_summable_unsigned!(u8);
impl_summable_unsigned!(u16);
impl_summable_unsigned!(u32);
impl_summable_unsigned!(u64);
impl_summable_unsigned!(u128);
impl_summable_unsigned!(usize);
impl_summable_signed_wrapping!(i8);
impl_summable_signed_wrapping!(i16);
impl_summable_signed_wrapping!(i32);
impl_summable_signed_wrapping!(i64);
impl_summable_signed_wrapping!(i128);
impl_summable_signed_wrapping!(isize);
impl_summable_unsigned_wrapping!(u8);
impl_summable_unsigned_wrapping!(u16);
impl_summable_unsigned_wrapping!(u32);
impl_summable_unsigned_wrapping!(u64);
impl_summable_unsigned_wrapping!(u128);
impl_summable_unsigned_wrapping!(usize);