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
#[cfg(feature = "num-bigint")]
mod bigint {
use crate::ops::{Operation, Commutative, Identity, Invertible};
use crate::ops::{Add, Mul};
use num_bigint::{BigInt, BigUint};
impl Operation<BigInt> for Add {
/// Returns the sum. This usually allocates memory.
fn combine(&self, a: &BigInt, b: &BigInt) -> BigInt {
a + b
}
/// Computes the sum while reusing memory in `a`.
fn combine_mut(&self, a: &mut BigInt, b: &BigInt) {
*a += b;
}
/// Computes the sum while reusing memory in `b`.
fn combine_mut2(&self, a: &BigInt, b: &mut BigInt) {
*b += a;
}
/// Computes the sum while reusing memory from `a`.
fn combine_left(&self, a: BigInt, b: &BigInt) -> BigInt {
a + b
}
/// Computes the sum while reusing memory from `b`.
fn combine_right(&self, a: &BigInt, b: BigInt) -> BigInt {
a + b
}
/// Computes the sum while reusing memory from the larger of `a` and `b`.
fn combine_both(&self, a: BigInt, b: BigInt) -> BigInt {
a + b
}
}
impl Commutative<BigInt> for Add {}
impl Identity<BigInt> for Add {
/// Returns zero.
fn identity(&self) -> BigInt {
0.into()
}
}
impl Invertible<BigInt> for Add {
/// Computes the difference, while reusing memory in `a`.
fn uncombine(&self, a: &mut BigInt, b: &BigInt) {
*a -= b;
}
}
impl Operation<BigUint> for Add {
/// Returns the sum. This usually allocates memory.
fn combine(&self, a: &BigUint, b: &BigUint) -> BigUint {
a + b
}
/// Computes the sum while reusing memory in `a`.
fn combine_mut(&self, a: &mut BigUint, b: &BigUint) {
*a += b;
}
/// Computes the sum while reusing memory in `b`.
fn combine_mut2(&self, a: &BigUint, b: &mut BigUint) {
*b += a;
}
/// Computes the sum while reusing memory from `a`.
fn combine_left(&self, a: BigUint, b: &BigUint) -> BigUint {
a + b
}
/// Computes the sum while reusing memory from `b`.
fn combine_right(&self, a: &BigUint, b: BigUint) -> BigUint {
a + b
}
/// Computes the sum while reusing memory from the larger of `a` and `b`.
fn combine_both(&self, a: BigUint, b: BigUint) -> BigUint {
a + b
}
}
impl Commutative<BigUint> for Add {}
impl Identity<BigUint> for Add {
/// Returns zero.
fn identity(&self) -> BigUint {
0u32.into()
}
}
impl Operation<BigInt> for Mul {
/// Returns the product. This usually allocates memory.
fn combine(&self, a: &BigInt, b: &BigInt) -> BigInt {
a * b
}
/// Computes the product while reusing memory in `a`.
fn combine_mut(&self, a: &mut BigInt, b: &BigInt) {
*a *= b;
}
/// Computes the product while reusing memory in `b`.
fn combine_mut2(&self, a: &BigInt, b: &mut BigInt) {
*b *= a;
}
/// Computes the product while reusing memory from `a`.
fn combine_left(&self, a: BigInt, b: &BigInt) -> BigInt {
a * b
}
/// Computes the product while reusing memory from `b`.
fn combine_right(&self, a: &BigInt, b: BigInt) -> BigInt {
a * b
}
/// Computes the product while reusing memory from the larger of `a` and `b`.
fn combine_both(&self, a: BigInt, b: BigInt) -> BigInt {
a * b
}
}
impl Commutative<BigInt> for Mul {}
impl Identity<BigInt> for Mul {
/// Returns one.
fn identity(&self) -> BigInt {
1.into()
}
}
impl Operation<BigUint> for Mul {
/// Returns the product. This usually allocates memory.
fn combine(&self, a: &BigUint, b: &BigUint) -> BigUint {
a * b
}
/// Computes the product while reusing memory in `a`.
fn combine_mut(&self, a: &mut BigUint, b: &BigUint) {
*a *= b;
}
/// Computes the product while reusing memory in `b`.
fn combine_mut2(&self, a: &BigUint, b: &mut BigUint) {
*b *= a;
}
/// Computes the product while reusing memory from `a`.
fn combine_left(&self, a: BigUint, b: &BigUint) -> BigUint {
a * b
}
/// Computes the product while reusing memory from `b`.
fn combine_right(&self, a: &BigUint, b: BigUint) -> BigUint {
a * b
}
/// Computes the product while reusing memory from the larger of `a` and `b`.
fn combine_both(&self, a: BigUint, b: BigUint) -> BigUint {
a * b
}
}
impl Commutative<BigUint> for Mul {}
impl Identity<BigUint> for Mul {
/// Returns zero.
fn identity(&self) -> BigUint {
1u32.into()
}
}
}