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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! Module of operations that can be performed in a segment tree.
//!
//! A segment tree needs some operation, and this module contains the main [`Operation`]
//! trait, together with the marker trait [`Commutative`]. This module also contains
//! implementations for simple operations.
//!
//! [`Operation`]: trait.Operation.html
//! [`Commutative`]: trait.Commutative.html

use std::num::Wrapping;

#[cfg(feature = "num-bigint")]
mod num;

/// A trait that specifies which associative operator to use in a segment tree.
pub trait Operation<N> {
    /// The operation that is performed to combine two intervals in the segment tree.
    ///
    /// This function must be [associative][1], that is `combine(combine(a, b), c) =
    /// combine(a, combine(b, c))`.
    ///
    /// [1]: https://en.wikipedia.org/wiki/Associative_property
    fn combine(&self, a: &N, b: &N) -> N;
    /// Replace the value in `a` with `combine(a, b)`. This function exists to allow
    /// certain optimizations and by default simply calls `combine`.
    #[inline]
    fn combine_mut(&self, a: &mut N, b: &N) {
        let res = self.combine(&*a, b);
        *a = res;
    }
    /// Replace the value in `b` with `combine(a, b)`. This function exists to allow
    /// certain optimizations and by default simply calls `combine`.
    #[inline]
    fn combine_mut2(&self, a: &N, b: &mut N) {
        let res = self.combine(a, &*b);
        *b = res;
    }
    /// Must return the same as `combine`. This function exists to allow certain
    /// optimizations and by default simply calls `combine_mut`.
    #[inline]
    fn combine_left(&self, mut a: N, b: &N) -> N {
        self.combine_mut(&mut a, b); a
    }
    /// Must return the same as `combine`. This function exists to allow certain
    /// optimizations and by default simply calls `combine_mut2`.
    #[inline]
    fn combine_right(&self, a: &N, mut b: N) -> N {
        self.combine_mut2(a, &mut b); b
    }
    /// Must return the same as `combine`. This function exists to allow certain
    /// optimizations and by default simply calls `combine_left`.
    #[inline]
    fn combine_both(&self, a: N, b: N) -> N {
        self.combine_left(a, &b)
    }
}

/// A marker trait that specifies that an [`Operation`] is [commutative][1], that is:
/// `combine(a, b) = combine(b, a)`.
///
/// [`Operation`]: trait.Operation.html
/// [1]: https://en.wikipedia.org/wiki/Commutative_property
pub trait Commutative<N>: Operation<N> {}

/// A trait that specifies that this [`Operation`] has an [identity element][1].
///
/// An identity must satisfy `combine(a, id) = a` and `combine(id, a) = a`, where `id` is
/// something returned by [`identity`].
///
/// [`Operation`]: trait.Operation.html
/// [`identity`]: #tymethod.identity
/// [1]: https://en.wikipedia.org/wiki/Identity_element
pub trait Identity<N> {
    /// Returns an element such that if [combined][1] with any element `a` the result must
    /// be `a`.
    ///
    /// [1]: trait.Operation.html#tymethod.combine
    fn identity(&self) -> N;
}

/// A trait for invertible operations.
pub trait Invertible<N> {
    /// A method such that the following code will leave `a` in the same state as it
    /// started.
    ///
    ///```rust
    ///# use segment_tree::ops::{Add, Operation, Invertible};
    ///# let op = Add;
    ///# let mut a = 1i32;
    ///# let mut original_value_of_a = 1i32;
    ///# let mut b = 2i32;
    /// // after running these two methods, a should be unchanged:
    ///op.combine_mut(&mut a, &b);
    ///op.uncombine(&mut a, &b);
    ///assert_eq!(a, original_value_of_a);
    /// // a should also be unchanged after running them in the reverse order
    ///op.uncombine(&mut a, &b);
    ///op.combine_mut(&mut a, &b);
    ///assert_eq!(a, original_value_of_a);
    ///```
    fn uncombine(&self, a: &mut N, b: &N);
}

/// Each node contains the sum of the interval it represents.
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct Add;
/// Each node contains the product of the interval it represents.
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct Mul;


/// Each node contains the bitwise and of the interval it represents.
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct And;
/// Each node contains the bitwise or of the interval it represents.
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct Or;
/// Each node contains the bitwise xor of the interval it represents.
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct Xor;

/// Each node contains the minimum of the interval it represents.
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct Min;
/// Each node contains the maximum of the interval it represents.
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct Max;

macro_rules! impl_operation_infix {
    ($op:ty, $ty:ty, $combineop:tt, $doc:expr) => {
        impl Operation<$ty> for $op {
            #[doc = $doc]
            #[inline]
            fn combine(&self, a: &$ty, b: &$ty) -> $ty {
                *a $combineop *b
            }
        }
    }
}
macro_rules! impl_operation_prefix {
    ($op:ty, $ty:ty, $combinef:expr, $doc:expr) => {
        impl Operation<$ty> for $op {
            #[doc = $doc]
            #[inline]
            fn combine(&self, a: &$ty, b: &$ty) -> $ty {
                $combinef(*a, *b)
            }
        }
    }
}
macro_rules! impl_identity {
    ($op:ty, $ty:ty, $iden:expr, $doc:expr) => {
        impl Identity<$ty> for $op {
            #[doc = $doc]
            #[inline]
            fn identity(&self) -> $ty {
                $iden
            }
        }
    }
}
macro_rules! impl_inverse {
    ($op:ty, $ty:ty, $uncombineop:tt, $doc:expr) => {
        impl Invertible<$ty> for $op {
            #[doc = $doc]
            #[inline]
            fn uncombine(&self, a: &mut $ty, b: &$ty) {
                *a = *a $uncombineop *b;
            }
        }
    }
}
macro_rules! impl_unsigned_primitive {
    ($ty:tt) => {
        impl_operation_infix!(Add, $ty, +, "Returns the sum.");
        impl_identity!(Add, $ty, 0, "Returns zero.");
        impl Commutative<$ty> for Add {}

        impl_operation_infix!(Add, Wrapping<$ty>, +, "Returns the sum.");
        impl_identity!(Add, Wrapping<$ty>, Wrapping(0), "Returns zero.");
        impl_inverse!(Add, Wrapping<$ty>, -, "Returns the difference.");
        impl Commutative<Wrapping<$ty>> for Add {}

        impl_operation_infix!(Xor, $ty, ^, "Returns the bitwise exclusive or.");
        impl_identity!(Xor, $ty, 0, "Returns zero.");
        impl_inverse!(Xor, $ty, ^, "Returns the bitwise exclusive or.");
        impl Commutative<$ty> for Xor {}

        impl_operation_infix!(Mul, $ty, *, "Returns the product.");
        impl_identity!(Mul, $ty, 1, "Returns one.");
        impl Commutative<$ty> for Mul {}

        impl_operation_infix!(Mul, Wrapping<$ty>, *, "Returns the product.");
        impl_identity!(Mul, Wrapping<$ty>, Wrapping(1), "Returns one.");
        impl Commutative<Wrapping<$ty>> for Mul {}

        impl_operation_infix!(And, $ty, &, "Returns the bitwise and.");
        impl_identity!(And, $ty, std::$ty::MAX, "Returns the largest possible value.");
        impl Commutative<$ty> for And {}

        impl_operation_infix!(Or, $ty, &, "Returns the bitwise or.");
        impl_identity!(Or, $ty, 0, "Returns zero.");
        impl Commutative<$ty> for Or {}

        impl_operation_prefix!(Min, $ty, std::cmp::min, "Returns the minimum.");
        impl_identity!(Min, $ty, std::$ty::MAX, "Returns the largest possible value.");
        impl Commutative<$ty> for Min {}

        impl_operation_prefix!(Max, $ty, std::cmp::max, "Returns the maximum.");
        impl_identity!(Max, $ty, 0, "Returns zero.");
        impl Commutative<$ty> for Max {}
    }
}
impl_unsigned_primitive!(u8);
impl_unsigned_primitive!(u16);
impl_unsigned_primitive!(u32);
impl_unsigned_primitive!(u64);
impl_unsigned_primitive!(u128);
impl_unsigned_primitive!(usize);

macro_rules! impl_signed_primitive {
    ($ty:tt) => {
        impl_operation_infix!(Add, $ty, +, "Returns the sum.");
        impl_identity!(Add, $ty, 0, "Returns zero.");
        impl_inverse!(Add, $ty, -, "Returns the difference.");
        impl Commutative<$ty> for Add {}

        impl_operation_infix!(Add, Wrapping<$ty>, +, "Returns the sum.");
        impl_identity!(Add, Wrapping<$ty>, Wrapping(0), "Returns zero.");
        impl_inverse!(Add, Wrapping<$ty>, -, "Returns the difference.");
        impl Commutative<Wrapping<$ty>> for Add {}

        impl_operation_infix!(Xor, $ty, ^, "Returns the bitwise exclusive or.");
        impl_identity!(Xor, $ty, 0, "Returns zero.");
        impl_inverse!(Xor, $ty, ^, "Returns the bitwise exclusive or.");
        impl Commutative<$ty> for Xor {}

        impl_operation_infix!(Mul, $ty, *, "Returns the product.");
        impl_identity!(Mul, $ty, 1, "Returns one.");
        impl Commutative<$ty> for Mul {}

        impl_operation_infix!(Mul, Wrapping<$ty>, *, "Returns the product.");
        impl_identity!(Mul, Wrapping<$ty>, Wrapping(1), "Returns one.");
        impl Commutative<Wrapping<$ty>> for Mul {}

        impl_operation_infix!(And, $ty, &, "Returns the bitwise and.");
        impl_identity!(And, $ty, -1, "Returns negative one.");
        impl Commutative<$ty> for And {}

        impl_operation_infix!(Or, $ty, &, "Returns the bitwise or.");
        impl_identity!(Or, $ty, 0, "Returns zero.");
        impl Commutative<$ty> for Or {}

        impl_operation_prefix!(Min, $ty, std::cmp::min, "Returns the minimum.");
        impl_identity!(Min, $ty, std::$ty::MAX, "Returns the largest possible value.");
        impl Commutative<$ty> for Min {}

        impl_operation_prefix!(Max, $ty, std::cmp::max, "Returns the maximum.");
        impl_identity!(Max, $ty, std::$ty::MIN, "Returns the smallest possible value.");
        impl Commutative<$ty> for Max {}
    }
}
impl_signed_primitive!(i8);
impl_signed_primitive!(i16);
impl_signed_primitive!(i32);
impl_signed_primitive!(i64);
impl_signed_primitive!(i128);
impl_signed_primitive!(isize);

impl_operation_infix!(Add, f32, +, "Returns the sum.");
impl_inverse!(Add, f32, -, "Returns the difference.");
impl_identity!(Add, f32, 0.0, "Returns zero.");
impl Commutative<f32> for Add {}

impl_operation_infix!(Mul, f32, *, "Returns the product.");
impl_inverse!(Mul, f32, /, "Returns the ratio.");
impl_identity!(Mul, f32, 1.0, "Returns one.");
impl Commutative<f32> for Mul {}

impl_operation_infix!(Add, f64, +, "Returns the sum.");
impl_inverse!(Add, f64, -, "Returns the difference.");
impl_identity!(Add, f64, 0.0, "Returns zero.");
impl Commutative<f64> for Add {}

impl_operation_infix!(Mul, f64, *, "Returns the product.");
impl_inverse!(Mul, f64, /, "Returns the ratio.");
impl_identity!(Mul, f64, 1.0, "Returns one.");
impl Commutative<f64> for Mul {}


/// Variant of [`Min`] that considers NaN the largest value.
///
/// [`Min`]: struct.Min.html
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct MinIgnoreNaN;
impl_identity!(MinIgnoreNaN, f32, std::f32::NAN, "Returns NaN.");
impl_identity!(MinIgnoreNaN, f64, std::f64::NAN, "Returns NaN.");
impl Commutative<f32> for MinIgnoreNaN {}
impl Commutative<f64> for MinIgnoreNaN {}
impl Operation<f32> for MinIgnoreNaN {
    /// Returns the smallest of the two values.
    ///
    /// If either argument is NaN, the other is returned.
    fn combine(&self, a: &f32, b: &f32) -> f32 {
        if b > a || b.is_nan() { *a } else { *b }
    }
}
impl Operation<f64> for MinIgnoreNaN {
    /// Returns the smallest of the two values.
    ///
    /// If either argument is NaN, the other is returned.
    fn combine(&self, a: &f64, b: &f64) -> f64 {
        if b > a || b.is_nan() { *a } else { *b }
    }
}

/// Variant of [`Min`] that considers NaN the smallest value.
///
/// [`Min`]: struct.Min.html
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct MinTakeNaN;
impl_identity!(MinTakeNaN, f32, std::f32::INFINITY, "Returns infinity.");
impl_identity!(MinTakeNaN, f64, std::f64::INFINITY, "Returns infinity.");
impl Commutative<f32> for MinTakeNaN {}
impl Commutative<f64> for MinTakeNaN {}
impl Operation<f32> for MinTakeNaN {
    /// Returns the smallest of the two values.
    ///
    /// If either argument is NaN, this method returns NaN.
    fn combine(&self, a: &f32, b: &f32) -> f32 {
        if b > a || a.is_nan() { *a } else { *b }
    }
}
impl Operation<f64> for MinTakeNaN {
    /// Returns the smallest of the two values.
    ///
    /// If either argument is NaN, this method returns NaN.
    fn combine(&self, a: &f64, b: &f64) -> f64 {
        if b > a || a.is_nan() { *a } else { *b }
    }
}

/// Variant of [`Max`] that considers NaN the smallest value.
///
/// [`Max`]: struct.Max.html
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct MaxIgnoreNaN;
impl_identity!(MaxIgnoreNaN, f32, std::f32::NAN, "Returns NaN.");
impl_identity!(MaxIgnoreNaN, f64, std::f64::NAN, "Returns NaN.");
impl Commutative<f32> for MaxIgnoreNaN {}
impl Commutative<f64> for MaxIgnoreNaN {}
impl Operation<f32> for MaxIgnoreNaN {
    /// Returns the largest of the two values.
    ///
    /// If either argument is NaN, the other is returned.
    fn combine(&self, a: &f32, b: &f32) -> f32 {
        if b < a || b.is_nan() { *a } else { *b }
    }
}
impl Operation<f64> for MaxIgnoreNaN {
    /// Returns the largest of the two values.
    ///
    /// If either argument is NaN, the other is returned.
    fn combine(&self, a: &f64, b: &f64) -> f64 {
        if b < a || b.is_nan() { *a } else { *b }
    }
}

/// Variant of [`Max`] that considers NaN the largest value.
///
/// [`Max`]: struct.Max.html
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct MaxTakeNaN;
impl_identity!(MaxTakeNaN, f32, std::f32::NEG_INFINITY, "Returns negative infinity.");
impl_identity!(MaxTakeNaN, f64, std::f64::NEG_INFINITY, "Returns negative infinity.");
impl Commutative<f32> for MaxTakeNaN {}
impl Commutative<f64> for MaxTakeNaN {}
impl Operation<f32> for MaxTakeNaN {
    /// Returns the largest of the two values.
    ///
    /// If either argument is NaN, this method returns NaN.
    fn combine(&self, a: &f32, b: &f32) -> f32 {
        if b < a || a.is_nan() { *a } else { *b }
    }
}
impl Operation<f64> for MaxTakeNaN {
    /// Returns the largest of the two values.
    ///
    /// If either argument is NaN, this method returns NaN.
    fn combine(&self, a: &f64, b: &f64) -> f64 {
        if b < a || a.is_nan() { *a } else { *b }
    }
}

impl_operation_infix!(And, bool, &&, "Returns the boolean and.");
impl_identity!(And, bool, true, "Returns `true`.");

impl_operation_infix!(Or, bool, ||, "Returns the boolean or.");
impl_identity!(Or, bool, false, "Returns `false`.");

impl_operation_infix!(Xor, bool, ^, "Returns the boolean xor.");
impl_inverse!(Xor, bool, ^, "Returns the boolean xor.");
impl_identity!(Xor, bool, false, "Returns `false`.");

#[cfg(test)]
mod tests {
    use std::{f32, i32, u32};
    use crate::ops::*;
    #[test]
    fn ops_nan() {
        assert_eq!(MaxIgnoreNaN.combine_both(0.0, 1.0), 1.0);
        assert_eq!(MaxIgnoreNaN.combine_both(1.0, 0.0), 1.0);
        assert_eq!(MaxIgnoreNaN.combine_both(f32::NAN, 1.0), 1.0);
        assert_eq!(MaxIgnoreNaN.combine_both(1.0, f32::NAN), 1.0);
        assert_eq!(MaxIgnoreNaN.combine_both(f32::NAN, f32::NEG_INFINITY),
            f32::NEG_INFINITY);
        assert_eq!(MaxIgnoreNaN.combine_both(f32::NEG_INFINITY, f32::NAN),
            f32::NEG_INFINITY);
        assert!(MaxIgnoreNaN.combine_both(f32::NAN, f32::NAN).is_nan());

        assert_eq!(MinIgnoreNaN.combine_both(0.0, 1.0), 0.0);
        assert_eq!(MinIgnoreNaN.combine_both(1.0, 0.0), 0.0);
        assert_eq!(MinIgnoreNaN.combine_both(f32::NAN, 1.0), 1.0);
        assert_eq!(MinIgnoreNaN.combine_both(1.0, f32::NAN), 1.0);
        assert_eq!(MinIgnoreNaN.combine_both(f32::NAN, f32::INFINITY), f32::INFINITY);
        assert_eq!(MinIgnoreNaN.combine_both(f32::INFINITY, f32::NAN), f32::INFINITY);
        assert!(MinIgnoreNaN.combine_both(f32::NAN, f32::NAN).is_nan());

        assert_eq!(MaxTakeNaN.combine_both(0.0, 1.0), 1.0);
        assert_eq!(MaxTakeNaN.combine_both(1.0, 0.0), 1.0);
        assert!(MaxTakeNaN.combine_both(f32::NAN, f32::INFINITY).is_nan());
        assert!(MaxTakeNaN.combine_both(f32::INFINITY, f32::NAN).is_nan());
        assert!(MaxTakeNaN.combine_both(f32::NAN, f32::NEG_INFINITY).is_nan());
        assert!(MaxTakeNaN.combine_both(f32::NEG_INFINITY, f32::NAN).is_nan());
        assert!(MaxTakeNaN.combine_both(f32::NAN, f32::NAN).is_nan());

        assert_eq!(MinTakeNaN.combine_both(0.0, 1.0), 0.0);
        assert_eq!(MinTakeNaN.combine_both(1.0, 0.0), 0.0);
        assert!(MinTakeNaN.combine_both(f32::NAN, f32::INFINITY).is_nan());
        assert!(MinTakeNaN.combine_both(f32::INFINITY, f32::NAN).is_nan());
        assert!(MinTakeNaN.combine_both(f32::NAN, f32::NEG_INFINITY).is_nan());
        assert!(MinTakeNaN.combine_both(f32::NEG_INFINITY, f32::NAN).is_nan());
        assert!(MinTakeNaN.combine_both(f32::NAN, f32::NAN).is_nan());
    }
    #[test]
    fn ops_and_identity() {
        for i in -200i32 ..= 200i32 {
            assert_eq!(And.combine_both(i, And.identity()), i);
        }
        assert_eq!(And.combine_both(i32::MAX, And.identity()), i32::MAX);
        assert_eq!(And.combine_both(i32::MIN, And.identity()), i32::MIN);
        assert_eq!(And.combine_both(0i32, And.identity()), 0i32);

        assert_eq!(And.combine_both(0u32, And.identity()), 0u32);
        assert_eq!(And.combine_both(u32::MAX, And.identity()), u32::MAX);
    }
}

/// Store several pieces of information in each node.
#[derive(Clone,Copy,Eq,PartialEq,Ord,PartialOrd,Debug,Default,Hash)]
pub struct Pair<A, B> {
    a: A, b: B
}
impl<A, B> Pair<A, B> {
    /// Create a pair operation.
    pub fn wrap(a: A, b: B) -> Pair<A, B> {
        Pair { a: a, b: b }
    }
    /// Returns the inner operations.
    pub fn into_inner(self) -> (A, B) {
        (self.a, self.b)
    }
}
impl<TA, TB, A: Operation<TA>, B: Operation<TB>> Operation<(TA, TB)> for Pair<A, B> {
    #[inline]
    fn combine(&self, a: &(TA, TB), b: &(TA, TB)) -> (TA, TB) {
        (self.a.combine(&a.0, &b.0), self.b.combine(&a.1, &b.1))
    }
    #[inline]
    fn combine_mut(&self, a: &mut (TA, TB), b: &(TA, TB)) {
        self.a.combine_mut(&mut a.0, &b.0);
        self.b.combine_mut(&mut a.1, &b.1);
    }
    #[inline]
    fn combine_mut2(&self, a: &(TA, TB), b: &mut (TA, TB)) {
        self.a.combine_mut2(&a.0, &mut b.0);
        self.b.combine_mut2(&a.1, &mut b.1);
    }
    #[inline]
    fn combine_left(&self, a: (TA, TB), b: &(TA, TB)) -> (TA, TB) {
        (self.a.combine_left(a.0, &b.0), self.b.combine_left(a.1, &b.1))
    }
    #[inline]
    fn combine_right(&self, a: &(TA, TB), b: (TA, TB)) -> (TA, TB) {
        (self.a.combine_right(&a.0, b.0), self.b.combine_right(&a.1, b.1))
    }
    #[inline]
    fn combine_both(&self, a: (TA, TB), b: (TA, TB)) -> (TA, TB) {
        (self.a.combine_both(a.0, b.0), self.b.combine_both(a.1, b.1))
    }
}
impl<TA, TB, A: Invertible<TA>, B: Invertible<TB>> Invertible<(TA, TB)> for Pair<A, B> {
    #[inline(always)]
    fn uncombine(&self, a: &mut (TA, TB), b: &(TA, TB)) {
        self.a.uncombine(&mut a.0, &b.0);
        self.b.uncombine(&mut a.1, &b.1);
    }
}
impl<TA, TB, A: Commutative<TA>, B: Commutative<TB>> Commutative<(TA, TB)> for Pair<A, B> {}
impl<TA, TB, A: Identity<TA>, B: Identity<TB>> Identity<(TA,TB)> for Pair<A, B> {
    fn identity(&self) -> (TA, TB) {
        (self.a.identity(), self.b.identity())
    }
}