/rust/registry/src/index.crates.io-6f17d22bba15001f/num-bigint-0.4.6/src/bigint/division.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::CheckedUnsignedAbs::{Negative, Positive}; |
2 | | use super::Sign::NoSign; |
3 | | use super::{BigInt, UnsignedAbs}; |
4 | | |
5 | | use crate::{IsizePromotion, UsizePromotion}; |
6 | | |
7 | | use core::ops::{Div, DivAssign, Rem, RemAssign}; |
8 | | use num_integer::Integer; |
9 | | use num_traits::{CheckedDiv, CheckedEuclid, Euclid, Signed, ToPrimitive, Zero}; |
10 | | |
11 | | forward_all_binop_to_ref_ref!(impl Div for BigInt, div); |
12 | | |
13 | | impl Div<&BigInt> for &BigInt { |
14 | | type Output = BigInt; |
15 | | |
16 | | #[inline] |
17 | 0 | fn div(self, other: &BigInt) -> BigInt { |
18 | 0 | let (q, _) = self.div_rem(other); |
19 | 0 | q |
20 | 0 | } Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Div>::div Unexecuted instantiation: <&num_bigint::bigint::BigInt as core::ops::arith::Div>::div |
21 | | } |
22 | | |
23 | | impl DivAssign<&BigInt> for BigInt { |
24 | | #[inline] |
25 | 0 | fn div_assign(&mut self, other: &BigInt) { |
26 | 0 | *self = &*self / other; |
27 | 0 | } |
28 | | } |
29 | | forward_val_assign!(impl DivAssign for BigInt, div_assign); |
30 | | |
31 | | promote_all_scalars!(impl Div for BigInt, div); |
32 | | promote_all_scalars_assign!(impl DivAssign for BigInt, div_assign); |
33 | | forward_all_scalar_binop_to_val_val!(impl Div<u32> for BigInt, div); |
34 | | forward_all_scalar_binop_to_val_val!(impl Div<u64> for BigInt, div); |
35 | | forward_all_scalar_binop_to_val_val!(impl Div<u128> for BigInt, div); |
36 | | |
37 | | impl Div<u32> for BigInt { |
38 | | type Output = BigInt; |
39 | | |
40 | | #[inline] |
41 | 0 | fn div(self, other: u32) -> BigInt { |
42 | 0 | BigInt::from_biguint(self.sign, self.data / other) |
43 | 0 | } |
44 | | } |
45 | | |
46 | | impl DivAssign<u32> for BigInt { |
47 | | #[inline] |
48 | 0 | fn div_assign(&mut self, other: u32) { |
49 | 0 | self.data /= other; |
50 | 0 | if self.data.is_zero() { |
51 | 0 | self.sign = NoSign; |
52 | 0 | } |
53 | 0 | } |
54 | | } |
55 | | |
56 | | impl Div<BigInt> for u32 { |
57 | | type Output = BigInt; |
58 | | |
59 | | #[inline] |
60 | 0 | fn div(self, other: BigInt) -> BigInt { |
61 | 0 | BigInt::from_biguint(other.sign, self / other.data) |
62 | 0 | } |
63 | | } |
64 | | |
65 | | impl Div<u64> for BigInt { |
66 | | type Output = BigInt; |
67 | | |
68 | | #[inline] |
69 | 0 | fn div(self, other: u64) -> BigInt { |
70 | 0 | BigInt::from_biguint(self.sign, self.data / other) |
71 | 0 | } |
72 | | } |
73 | | |
74 | | impl DivAssign<u64> for BigInt { |
75 | | #[inline] |
76 | 0 | fn div_assign(&mut self, other: u64) { |
77 | 0 | self.data /= other; |
78 | 0 | if self.data.is_zero() { |
79 | 0 | self.sign = NoSign; |
80 | 0 | } |
81 | 0 | } |
82 | | } |
83 | | |
84 | | impl Div<BigInt> for u64 { |
85 | | type Output = BigInt; |
86 | | |
87 | | #[inline] |
88 | 0 | fn div(self, other: BigInt) -> BigInt { |
89 | 0 | BigInt::from_biguint(other.sign, self / other.data) |
90 | 0 | } |
91 | | } |
92 | | |
93 | | impl Div<u128> for BigInt { |
94 | | type Output = BigInt; |
95 | | |
96 | | #[inline] |
97 | 0 | fn div(self, other: u128) -> BigInt { |
98 | 0 | BigInt::from_biguint(self.sign, self.data / other) |
99 | 0 | } |
100 | | } |
101 | | |
102 | | impl DivAssign<u128> for BigInt { |
103 | | #[inline] |
104 | 0 | fn div_assign(&mut self, other: u128) { |
105 | 0 | self.data /= other; |
106 | 0 | if self.data.is_zero() { |
107 | 0 | self.sign = NoSign; |
108 | 0 | } |
109 | 0 | } |
110 | | } |
111 | | |
112 | | impl Div<BigInt> for u128 { |
113 | | type Output = BigInt; |
114 | | |
115 | | #[inline] |
116 | 0 | fn div(self, other: BigInt) -> BigInt { |
117 | 0 | BigInt::from_biguint(other.sign, self / other.data) |
118 | 0 | } |
119 | | } |
120 | | |
121 | | forward_all_scalar_binop_to_val_val!(impl Div<i32> for BigInt, div); |
122 | | forward_all_scalar_binop_to_val_val!(impl Div<i64> for BigInt, div); |
123 | | forward_all_scalar_binop_to_val_val!(impl Div<i128> for BigInt, div); |
124 | | |
125 | | impl Div<i32> for BigInt { |
126 | | type Output = BigInt; |
127 | | |
128 | | #[inline] |
129 | 0 | fn div(self, other: i32) -> BigInt { |
130 | 0 | match other.checked_uabs() { |
131 | 0 | Positive(u) => self / u, |
132 | 0 | Negative(u) => -self / u, |
133 | | } |
134 | 0 | } |
135 | | } |
136 | | |
137 | | impl DivAssign<i32> for BigInt { |
138 | | #[inline] |
139 | 0 | fn div_assign(&mut self, other: i32) { |
140 | 0 | match other.checked_uabs() { |
141 | 0 | Positive(u) => *self /= u, |
142 | 0 | Negative(u) => { |
143 | 0 | self.sign = -self.sign; |
144 | 0 | *self /= u; |
145 | 0 | } |
146 | | } |
147 | 0 | } |
148 | | } |
149 | | |
150 | | impl Div<BigInt> for i32 { |
151 | | type Output = BigInt; |
152 | | |
153 | | #[inline] |
154 | 0 | fn div(self, other: BigInt) -> BigInt { |
155 | 0 | match self.checked_uabs() { |
156 | 0 | Positive(u) => u / other, |
157 | 0 | Negative(u) => u / -other, |
158 | | } |
159 | 0 | } |
160 | | } |
161 | | |
162 | | impl Div<i64> for BigInt { |
163 | | type Output = BigInt; |
164 | | |
165 | | #[inline] |
166 | 0 | fn div(self, other: i64) -> BigInt { |
167 | 0 | match other.checked_uabs() { |
168 | 0 | Positive(u) => self / u, |
169 | 0 | Negative(u) => -self / u, |
170 | | } |
171 | 0 | } |
172 | | } |
173 | | |
174 | | impl DivAssign<i64> for BigInt { |
175 | | #[inline] |
176 | 0 | fn div_assign(&mut self, other: i64) { |
177 | 0 | match other.checked_uabs() { |
178 | 0 | Positive(u) => *self /= u, |
179 | 0 | Negative(u) => { |
180 | 0 | self.sign = -self.sign; |
181 | 0 | *self /= u; |
182 | 0 | } |
183 | | } |
184 | 0 | } |
185 | | } |
186 | | |
187 | | impl Div<BigInt> for i64 { |
188 | | type Output = BigInt; |
189 | | |
190 | | #[inline] |
191 | 0 | fn div(self, other: BigInt) -> BigInt { |
192 | 0 | match self.checked_uabs() { |
193 | 0 | Positive(u) => u / other, |
194 | 0 | Negative(u) => u / -other, |
195 | | } |
196 | 0 | } |
197 | | } |
198 | | |
199 | | impl Div<i128> for BigInt { |
200 | | type Output = BigInt; |
201 | | |
202 | | #[inline] |
203 | 0 | fn div(self, other: i128) -> BigInt { |
204 | 0 | match other.checked_uabs() { |
205 | 0 | Positive(u) => self / u, |
206 | 0 | Negative(u) => -self / u, |
207 | | } |
208 | 0 | } |
209 | | } |
210 | | |
211 | | impl DivAssign<i128> for BigInt { |
212 | | #[inline] |
213 | 0 | fn div_assign(&mut self, other: i128) { |
214 | 0 | match other.checked_uabs() { |
215 | 0 | Positive(u) => *self /= u, |
216 | 0 | Negative(u) => { |
217 | 0 | self.sign = -self.sign; |
218 | 0 | *self /= u; |
219 | 0 | } |
220 | | } |
221 | 0 | } |
222 | | } |
223 | | |
224 | | impl Div<BigInt> for i128 { |
225 | | type Output = BigInt; |
226 | | |
227 | | #[inline] |
228 | 0 | fn div(self, other: BigInt) -> BigInt { |
229 | 0 | match self.checked_uabs() { |
230 | 0 | Positive(u) => u / other, |
231 | 0 | Negative(u) => u / -other, |
232 | | } |
233 | 0 | } |
234 | | } |
235 | | |
236 | | forward_all_binop_to_ref_ref!(impl Rem for BigInt, rem); |
237 | | |
238 | | impl Rem<&BigInt> for &BigInt { |
239 | | type Output = BigInt; |
240 | | |
241 | | #[inline] |
242 | 0 | fn rem(self, other: &BigInt) -> BigInt { |
243 | 0 | if let Some(other) = other.to_u32() { |
244 | 0 | self % other |
245 | 0 | } else if let Some(other) = other.to_i32() { |
246 | 0 | self % other |
247 | | } else { |
248 | 0 | let (_, r) = self.div_rem(other); |
249 | 0 | r |
250 | | } |
251 | 0 | } |
252 | | } |
253 | | |
254 | | impl RemAssign<&BigInt> for BigInt { |
255 | | #[inline] |
256 | 0 | fn rem_assign(&mut self, other: &BigInt) { |
257 | 0 | *self = &*self % other; |
258 | 0 | } |
259 | | } |
260 | | forward_val_assign!(impl RemAssign for BigInt, rem_assign); |
261 | | |
262 | | promote_all_scalars!(impl Rem for BigInt, rem); |
263 | | promote_all_scalars_assign!(impl RemAssign for BigInt, rem_assign); |
264 | | forward_all_scalar_binop_to_val_val!(impl Rem<u32> for BigInt, rem); |
265 | | forward_all_scalar_binop_to_val_val!(impl Rem<u64> for BigInt, rem); |
266 | | forward_all_scalar_binop_to_val_val!(impl Rem<u128> for BigInt, rem); |
267 | | |
268 | | impl Rem<u32> for BigInt { |
269 | | type Output = BigInt; |
270 | | |
271 | | #[inline] |
272 | 0 | fn rem(self, other: u32) -> BigInt { |
273 | 0 | BigInt::from_biguint(self.sign, self.data % other) |
274 | 0 | } |
275 | | } |
276 | | |
277 | | impl RemAssign<u32> for BigInt { |
278 | | #[inline] |
279 | 0 | fn rem_assign(&mut self, other: u32) { |
280 | 0 | self.data %= other; |
281 | 0 | if self.data.is_zero() { |
282 | 0 | self.sign = NoSign; |
283 | 0 | } |
284 | 0 | } |
285 | | } |
286 | | |
287 | | impl Rem<BigInt> for u32 { |
288 | | type Output = BigInt; |
289 | | |
290 | | #[inline] |
291 | 0 | fn rem(self, other: BigInt) -> BigInt { |
292 | 0 | BigInt::from(self % other.data) |
293 | 0 | } |
294 | | } |
295 | | |
296 | | impl Rem<u64> for BigInt { |
297 | | type Output = BigInt; |
298 | | |
299 | | #[inline] |
300 | 0 | fn rem(self, other: u64) -> BigInt { |
301 | 0 | BigInt::from_biguint(self.sign, self.data % other) |
302 | 0 | } |
303 | | } |
304 | | |
305 | | impl RemAssign<u64> for BigInt { |
306 | | #[inline] |
307 | 0 | fn rem_assign(&mut self, other: u64) { |
308 | 0 | self.data %= other; |
309 | 0 | if self.data.is_zero() { |
310 | 0 | self.sign = NoSign; |
311 | 0 | } |
312 | 0 | } |
313 | | } |
314 | | |
315 | | impl Rem<BigInt> for u64 { |
316 | | type Output = BigInt; |
317 | | |
318 | | #[inline] |
319 | 0 | fn rem(self, other: BigInt) -> BigInt { |
320 | 0 | BigInt::from(self % other.data) |
321 | 0 | } |
322 | | } |
323 | | |
324 | | impl Rem<u128> for BigInt { |
325 | | type Output = BigInt; |
326 | | |
327 | | #[inline] |
328 | 0 | fn rem(self, other: u128) -> BigInt { |
329 | 0 | BigInt::from_biguint(self.sign, self.data % other) |
330 | 0 | } |
331 | | } |
332 | | |
333 | | impl RemAssign<u128> for BigInt { |
334 | | #[inline] |
335 | 0 | fn rem_assign(&mut self, other: u128) { |
336 | 0 | self.data %= other; |
337 | 0 | if self.data.is_zero() { |
338 | 0 | self.sign = NoSign; |
339 | 0 | } |
340 | 0 | } |
341 | | } |
342 | | |
343 | | impl Rem<BigInt> for u128 { |
344 | | type Output = BigInt; |
345 | | |
346 | | #[inline] |
347 | 0 | fn rem(self, other: BigInt) -> BigInt { |
348 | 0 | BigInt::from(self % other.data) |
349 | 0 | } |
350 | | } |
351 | | |
352 | | forward_all_scalar_binop_to_val_val!(impl Rem<i32> for BigInt, rem); |
353 | | forward_all_scalar_binop_to_val_val!(impl Rem<i64> for BigInt, rem); |
354 | | forward_all_scalar_binop_to_val_val!(impl Rem<i128> for BigInt, rem); |
355 | | |
356 | | impl Rem<i32> for BigInt { |
357 | | type Output = BigInt; |
358 | | |
359 | | #[inline] |
360 | 0 | fn rem(self, other: i32) -> BigInt { |
361 | 0 | self % other.unsigned_abs() |
362 | 0 | } |
363 | | } |
364 | | |
365 | | impl RemAssign<i32> for BigInt { |
366 | | #[inline] |
367 | 0 | fn rem_assign(&mut self, other: i32) { |
368 | 0 | *self %= other.unsigned_abs(); |
369 | 0 | } |
370 | | } |
371 | | |
372 | | impl Rem<BigInt> for i32 { |
373 | | type Output = BigInt; |
374 | | |
375 | | #[inline] |
376 | 0 | fn rem(self, other: BigInt) -> BigInt { |
377 | 0 | match self.checked_uabs() { |
378 | 0 | Positive(u) => u % other, |
379 | 0 | Negative(u) => -(u % other), |
380 | | } |
381 | 0 | } |
382 | | } |
383 | | |
384 | | impl Rem<i64> for BigInt { |
385 | | type Output = BigInt; |
386 | | |
387 | | #[inline] |
388 | 0 | fn rem(self, other: i64) -> BigInt { |
389 | 0 | self % other.unsigned_abs() |
390 | 0 | } |
391 | | } |
392 | | |
393 | | impl RemAssign<i64> for BigInt { |
394 | | #[inline] |
395 | 0 | fn rem_assign(&mut self, other: i64) { |
396 | 0 | *self %= other.unsigned_abs(); |
397 | 0 | } |
398 | | } |
399 | | |
400 | | impl Rem<BigInt> for i64 { |
401 | | type Output = BigInt; |
402 | | |
403 | | #[inline] |
404 | 0 | fn rem(self, other: BigInt) -> BigInt { |
405 | 0 | match self.checked_uabs() { |
406 | 0 | Positive(u) => u % other, |
407 | 0 | Negative(u) => -(u % other), |
408 | | } |
409 | 0 | } |
410 | | } |
411 | | |
412 | | impl Rem<i128> for BigInt { |
413 | | type Output = BigInt; |
414 | | |
415 | | #[inline] |
416 | 0 | fn rem(self, other: i128) -> BigInt { |
417 | 0 | self % other.unsigned_abs() |
418 | 0 | } |
419 | | } |
420 | | |
421 | | impl RemAssign<i128> for BigInt { |
422 | | #[inline] |
423 | 0 | fn rem_assign(&mut self, other: i128) { |
424 | 0 | *self %= other.unsigned_abs(); |
425 | 0 | } |
426 | | } |
427 | | |
428 | | impl Rem<BigInt> for i128 { |
429 | | type Output = BigInt; |
430 | | |
431 | | #[inline] |
432 | 0 | fn rem(self, other: BigInt) -> BigInt { |
433 | 0 | match self.checked_uabs() { |
434 | 0 | Positive(u) => u % other, |
435 | 0 | Negative(u) => -(u % other), |
436 | | } |
437 | 0 | } |
438 | | } |
439 | | |
440 | | impl CheckedDiv for BigInt { |
441 | | #[inline] |
442 | 0 | fn checked_div(&self, v: &BigInt) -> Option<BigInt> { |
443 | 0 | if v.is_zero() { |
444 | 0 | return None; |
445 | 0 | } |
446 | 0 | Some(self.div(v)) |
447 | 0 | } |
448 | | } |
449 | | |
450 | | impl CheckedEuclid for BigInt { |
451 | | #[inline] |
452 | 0 | fn checked_div_euclid(&self, v: &BigInt) -> Option<BigInt> { |
453 | 0 | if v.is_zero() { |
454 | 0 | return None; |
455 | 0 | } |
456 | 0 | Some(self.div_euclid(v)) |
457 | 0 | } |
458 | | |
459 | | #[inline] |
460 | 0 | fn checked_rem_euclid(&self, v: &BigInt) -> Option<BigInt> { |
461 | 0 | if v.is_zero() { |
462 | 0 | return None; |
463 | 0 | } |
464 | 0 | Some(self.rem_euclid(v)) |
465 | 0 | } |
466 | | |
467 | 0 | fn checked_div_rem_euclid(&self, v: &Self) -> Option<(Self, Self)> { |
468 | 0 | Some(self.div_rem_euclid(v)) |
469 | 0 | } |
470 | | } |
471 | | |
472 | | impl Euclid for BigInt { |
473 | | #[inline] |
474 | 0 | fn div_euclid(&self, v: &BigInt) -> BigInt { |
475 | 0 | let (q, r) = self.div_rem(v); |
476 | 0 | if r.is_negative() { |
477 | 0 | if v.is_positive() { |
478 | 0 | q - 1 |
479 | | } else { |
480 | 0 | q + 1 |
481 | | } |
482 | | } else { |
483 | 0 | q |
484 | | } |
485 | 0 | } |
486 | | |
487 | | #[inline] |
488 | 0 | fn rem_euclid(&self, v: &BigInt) -> BigInt { |
489 | 0 | let r = self % v; |
490 | 0 | if r.is_negative() { |
491 | 0 | if v.is_positive() { |
492 | 0 | r + v |
493 | | } else { |
494 | 0 | r - v |
495 | | } |
496 | | } else { |
497 | 0 | r |
498 | | } |
499 | 0 | } |
500 | | |
501 | 0 | fn div_rem_euclid(&self, v: &Self) -> (Self, Self) { |
502 | 0 | let (q, r) = self.div_rem(v); |
503 | 0 | if r.is_negative() { |
504 | 0 | if v.is_positive() { |
505 | 0 | (q - 1, r + v) |
506 | | } else { |
507 | 0 | (q + 1, r - v) |
508 | | } |
509 | | } else { |
510 | 0 | (q, r) |
511 | | } |
512 | 0 | } |
513 | | } |