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 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
use malachite_base::num::arithmetic::traits::{
DivisibleBy, DivisibleByPowerOf2, EqMod, EqModPowerOf2, Parity, PowerOf2, WrappingAddAssign,
};
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::conversion::traits::SplitInHalf;
use malachite_base::num::logic::traits::TrailingZeros;
use malachite_base::slices::slice_trailing_zeros;
use natural::arithmetic::div_exact::limbs_modular_invert_limb;
use natural::arithmetic::divisible_by::{
limbs_divisible_by, limbs_divisible_by_limb, limbs_divisible_by_val_ref,
};
use natural::arithmetic::mod_op::limbs_mod_limb;
use natural::arithmetic::sub::{
limbs_sub_greater_in_place_left, limbs_sub_greater_to_out, limbs_sub_limb_in_place,
limbs_sub_limb_to_out, limbs_sub_same_length_in_place_left,
limbs_sub_same_length_in_place_right, limbs_sub_same_length_to_out,
};
use natural::comparison::cmp::limbs_cmp;
use natural::InnerNatural::{Large, Small};
use natural::Natural;
use platform::{DoubleLimb, Limb, BMOD_1_TO_MOD_1_THRESHOLD};
use std::cmp::Ordering;
// See the description for `limbs_mod_exact_odd_limb`. divisor must be odd.
//
// # Worst-case complexity
// Constant time and additional memory.
//
// This is equivalent to `mpn_modexact_1c_odd`, from `mpn/generic/mode1o.c`, GMP 6.2.1, where
// `size == 1`. Pass `carry = 0` to get `mpn_modexact_1_odd` from `gmp-impl.c`, GMP 6.2.1, where
// `size == 1`.
pub_const_test! {limbs_limb_mod_exact_odd_limb(n: Limb, d: Limb, carry: Limb) -> Limb {
if n > carry {
let result = (n - carry) % d;
if result == 0 {
0
} else {
d - result
}
} else {
(carry - n) % d
}
}}
// Calculates an r satisfying
//
// r * B ^ k + n - c == q * d
//
// where B = 2<sup>`Limb::WIDTH`</sup>, k is either `ns.len()` or `ns.len()` - 1 (the caller won't
// know which), c is `carry`, and q is the quotient (discarded). `d` must be odd and `carry` can be
// any limb value.
//
// If c < d then r will be in the range 0 <= r < d, or if c >= d then 0 <= r <= d.
//
// This slightly strange function suits the initial N x 1 reduction for GCDs or Jacobi symbols
// since the factors of 2 in B ^ k can be ignored, leaving -r == a mod d (by passing c = 0). For a
// GCD the factor of -1 on r can be ignored, or for the Jacobi symbol it can be accounted for. The
// function also suits divisibility and congruence testing, since if r = 0 (or r = d) is obtained,
// then a ≡ c mod d.
//
// ns must be nonempty and divisor must be odd.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// This is equivalent to `mpn_modexact_1c_odd` from mpn/generic/mode1o.c, GMP 6.2.1. Pass
// `carry = 0` to get `mpn_modexact_1_odd` from `gmp-impl.c`, GMP 6.2.1.
pub_crate_test! {limbs_mod_exact_odd_limb(ns: &[Limb], d: Limb, mut carry: Limb) -> Limb {
let len = ns.len();
if len == 1 {
return limbs_limb_mod_exact_odd_limb(ns[0], d, carry);
}
let d_inv = limbs_modular_invert_limb(d);
let d_double = DoubleLimb::from(d);
let (xs_last, xs_init) = ns.split_last().unwrap();
let xs_last = *xs_last;
for x in xs_init {
let (diff, small_carry) = x.overflowing_sub(carry);
carry = (DoubleLimb::from(diff.wrapping_mul(d_inv)) * d_double).upper_half();
if small_carry {
carry.wrapping_add_assign(1);
}
}
if xs_last <= d {
if carry >= xs_last {
carry - xs_last
} else {
carry.wrapping_add(d - xs_last)
}
} else {
let (diff, small_carry) = xs_last.overflowing_sub(carry);
carry = (DoubleLimb::from(diff.wrapping_mul(d_inv)) * d_double).upper_half();
if small_carry {
carry.wrapping_add_assign(1);
}
carry
}
}}
// Interpreting a slice of `Limb`s as the limbs of a `Natural` in ascending order, determines
// whether that `Natural` is equal to a limb mod a given `Limb` `m`.
//
// This function assumes that `m` is nonzero, `xs` has at least two elements, and the last element
// of `xs` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// # Panics
// Panics if the length of `xs` is less than 2 or `m` is zero.
//
// This is equivalent to `mpz_congruent_ui_p` from `mpz/cong_ui.c`, GMP 6.2.1, where `a` is
// positive and the `ABOVE_THRESHOLD` branch is excluded.
pub_crate_test! {limbs_eq_limb_mod_limb(xs: &[Limb], y: Limb, m: Limb) -> bool {
assert_ne!(m, 0);
assert!(xs.len() > 1);
let r = if m.even() {
let twos = TrailingZeros::trailing_zeros(m);
if !xs[0].wrapping_sub(y).divisible_by_power_of_2(twos) {
return false;
}
limbs_mod_exact_odd_limb(xs, m >> twos, y)
} else {
limbs_mod_exact_odd_limb(xs, m, y)
};
r == 0 || r == m
}}
#[allow(clippy::absurd_extreme_comparisons)]
fn limbs_eq_limb_mod_helper(xs: &[Limb], y: Limb, ms: &[Limb]) -> Option<bool> {
let m_len = ms.len();
assert!(m_len > 1);
let x_len = xs.len();
assert!(x_len > 1);
assert_ne!(*xs.last().unwrap(), 0);
assert_ne!(y, 0);
assert_ne!(*ms.last().unwrap(), 0);
if m_len > x_len {
// x < m, y < m, and x != y, so x != y mod m
return Some(false);
}
let m_0 = ms[0];
// Check xs == ys mod low zero bits of m_0.
let m_0_trailing_zeros = TrailingZeros::trailing_zeros(m_0);
if !xs[0].eq_mod_power_of_2(y, m_0_trailing_zeros) {
return Some(false);
}
if m_len == 2 && m_0 != 0 {
let m_1 = ms[1];
if m_1 < Limb::power_of_2(m_0_trailing_zeros) {
let m_0 = (m_0 >> m_0_trailing_zeros) | (m_1 << (Limb::WIDTH - m_0_trailing_zeros));
return Some(if x_len >= BMOD_1_TO_MOD_1_THRESHOLD {
let r = limbs_mod_limb(xs, m_0);
if y < m_0 {
r == y
} else {
r == y % m_0
}
} else {
let r = limbs_mod_exact_odd_limb(xs, m_0, y);
r == 0 || r == m_0
});
}
}
None
}
// Interpreting a slice of `Limb`s `xs`, a Limb `y`, and another slice of `Limb`s `ms` as three
// numbers x, y, and m, determines whether x ≡ y mod m. Both input slices are immutable.
//
// This function assumes that each of the two input slices have at least two elements, their last
// elements are nonzero, and `y` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// # Panics
// Panics if the length of `xs` or `ms` is less than 2, if the last element of either of the slices
// is zero, or if `y` is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a`, `c`, and `d`
// are positive, `a` and `d` are longer than one limb, and `c` is one limb long.
pub_test! {limbs_eq_limb_mod_ref_ref(xs: &[Limb], y: Limb, ms: &[Limb]) -> bool {
if let Some(equal) = limbs_eq_limb_mod_helper(xs, y, ms) {
return equal;
}
let mut scratch = vec![0; xs.len()];
// calculate |xs - y|
assert!(!limbs_sub_limb_to_out(&mut scratch, xs, y));
scratch.truncate(scratch.len() - slice_trailing_zeros(&scratch));
scratch.len() >= ms.len() && limbs_divisible_by_val_ref(&mut scratch, ms)
}}
// Interpreting a slice of `Limb`s `xs`, a Limb `y`, and another slice of `Limb`s `ms` as three
// numbers x, y, and m, determines whether x ≡ y mod m. The first input slice is immutable and the
// second is mutable.
//
// This function assumes that each of the two input slices have at least two elements, their last
// elements are nonzero, and `y` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// # Panics
// Panics if the length of `xs` or `ms` is less than 2, if the last element of either of the slices
// is zero, or if `y` is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a`, `c`, and `d`
// are positive, `a` and `d` are longer than one limb, and `c` is one limb long.
pub_test! {limbs_eq_limb_mod_ref_val(xs: &[Limb], y: Limb, ms: &mut [Limb]) -> bool {
if let Some(equal) = limbs_eq_limb_mod_helper(xs, y, ms) {
return equal;
}
let mut scratch = vec![0; xs.len()];
// calculate |xs - y|
assert!(!limbs_sub_limb_to_out(&mut scratch, xs, y));
scratch.truncate(scratch.len() - slice_trailing_zeros(&scratch));
scratch.len() >= ms.len() && limbs_divisible_by(&mut scratch, ms)
}}
// Interpreting a slice of `Limb`s `xs`, a Limb `y`, and another slice of `Limb`s `ms` as three
// numbers x, y, and m, determines whether x ≡ y mod m. The first input slice is mutable and the
// second is immutable.
//
// This function assumes that each of the two input slices have at least two elements, their last
// elements are nonzero, and `y` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// # Panics
// Panics if the length of `xs` or `m` is less than 2, if the last element of either of the slices
// is zero, or if `y` is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a`, `c`, and `d`
// are positive, `a` and `d` are longer than one limb, and `c` is one limb long.
pub_test! {limbs_eq_limb_mod_val_ref(xs: &mut [Limb], y: Limb, ms: &[Limb]) -> bool {
if let Some(equal) = limbs_eq_limb_mod_helper(xs, y, ms) {
return equal;
}
// calculate |xs - y|
assert!(!limbs_sub_limb_in_place(xs, y));
let new_len = xs.len() - slice_trailing_zeros(xs);
new_len >= ms.len() && limbs_divisible_by_val_ref(&mut xs[..new_len], ms)
}}
// Interpreting a slice of `Limb`s `xs`, a Limb `y`, and another slice of `Limb`s `ms` as three
// numbers x, y, and m, determines whether x ≡ y mod m. Both input slices are mutable.
//
// This function assumes that each of the two input slices have at least two elements, their last
// elements are nonzero, and `y` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// # Panics
// Panics if the length of `xs` or `ms` is less than 2, if the last element of either of the slices
// is zero, or if `y` is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a`, `c`, and `d`
// are positive, `a` and `d` are longer than one limb, and `c` is one limb long.
pub_test! {limbs_eq_limb_mod(xs: &mut [Limb], y: Limb, ms: &mut [Limb]) -> bool {
if let Some(equal) = limbs_eq_limb_mod_helper(xs, y, ms) {
return equal;
}
// calculate |xs - y|
assert!(!limbs_sub_limb_in_place(xs, y));
let new_len = xs.len() - slice_trailing_zeros(xs);
new_len >= ms.len() && limbs_divisible_by(&mut xs[..new_len], ms)
}}
// xs.len() >= ys.len()
fn limbs_eq_mod_limb_helper(xs: &[Limb], ys: &[Limb], m: Limb) -> Option<bool> {
let x_len = xs.len();
let y_len = ys.len();
assert!(y_len > 1);
assert!(x_len >= y_len);
assert_ne!(*xs.last().unwrap(), 0);
assert_ne!(*ys.last().unwrap(), 0);
assert_ne!(m, 0);
if xs == ys {
Some(true)
} else if !xs[0].eq_mod_power_of_2(ys[0], u64::from(m.trailing_zeros())) {
// Check xs == ys mod low zero bits of m.
Some(false)
} else {
None
}
}
// Interpreting two slices of `Limb`s `xs` and `ys` and a Limb `m` as three numbers x, y, and m,
// determines whether x ≡ y mod m. Both input slices are immutable.
//
// This function assumes that each of the two input slices have at least two elements, their last
// elements are nonzero, and `m` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
//
// # Panics
// Panics if the length of `xs` or `ys` is less than 2, if the last element of either of the slices
// is zero, or if `m` is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a`, `c`, and `d`
// are positive, `a` and `c` are longer than one limb, and `m` is one limb long.
pub_test! {limbs_eq_mod_limb_ref_ref(xs: &[Limb], ys: &[Limb], m: Limb) -> bool {
if xs.len() >= ys.len() {
limbs_eq_mod_limb_ref_ref_greater(xs, ys, m)
} else {
limbs_eq_mod_limb_ref_ref_greater(ys, xs, m)
}
}}
// xs.len() >= ys.len()
fn limbs_eq_mod_limb_ref_ref_greater(xs: &[Limb], ys: &[Limb], m: Limb) -> bool {
if let Some(equal) = limbs_eq_mod_limb_helper(xs, ys, m) {
return equal;
}
let mut scratch = vec![0; xs.len()];
// calculate |xs - ys|
if limbs_cmp(xs, ys) >= Ordering::Equal {
assert!(!limbs_sub_greater_to_out(&mut scratch, xs, ys));
} else {
assert!(!limbs_sub_same_length_to_out(&mut scratch, ys, xs));
}
scratch.truncate(scratch.len() - slice_trailing_zeros(&scratch));
// scratch is non-empty here because xs != ys
if scratch.len() == 1 {
scratch[0].divisible_by(m)
} else {
limbs_divisible_by_limb(&scratch, m)
}
}
// Interpreting two slices of `Limb`s `xs` and `ys` and a Limb `m` as three numbers x, y, and m,
// determines whether x ≡ y mod m. The first input slice is immutable and the second is mutable.
//
// This function assumes that each of the two input slices have at least two elements, their last
// elements are nonzero, and `m` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
//
// # Panics
// Panics if the length of `xs` or `ys` is less than 2, if the last element of either of the slices
// is zero, or if `m` is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a`, `c`, and `d`
// are positive, `a` and `c` are longer than one limb, and `m` is one limb long.
pub_test! {limbs_eq_mod_limb_ref_val(xs: &[Limb], ys: &mut [Limb], m: Limb) -> bool {
if xs.len() >= ys.len() {
limbs_eq_mod_limb_ref_val_greater(xs, ys, m)
} else {
limbs_eq_mod_limb_val_ref_greater(ys, xs, m)
}
}}
// xs.len() >= ys.len()
fn limbs_eq_mod_limb_ref_val_greater(xs: &[Limb], ys: &mut [Limb], m: Limb) -> bool {
if let Some(equal) = limbs_eq_mod_limb_helper(xs, ys, m) {
return equal;
}
let mut scratch;
// calculate |xs - ys|
let scratch = if limbs_cmp(xs, ys) >= Ordering::Equal {
scratch = vec![0; xs.len()];
assert!(!limbs_sub_greater_to_out(&mut scratch, xs, ys));
&mut scratch
} else {
assert!(!limbs_sub_same_length_in_place_left(ys, xs));
ys
};
let new_len = scratch.len() - slice_trailing_zeros(scratch);
// scratch is non-empty here because xs != ys
if new_len == 1 {
scratch[0].divisible_by(m)
} else {
limbs_divisible_by_limb(&scratch[..new_len], m)
}
}
// Interpreting two slices of `Limb`s `xs` and `ys` and a Limb `m` as three numbers x, y, and m,
// determines whether x ≡ y mod m. The first input slice is mutable and the second is immutable.
//
// This function assumes that each of the two input slices have at least two elements, their last
// elements are nonzero, and `m` is nonzero. Both input slices are immutable.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
//
// # Panics
// Panics if the length of `xs` or `ys` is less than 2, if the last element of either of the slices
// is zero, or if `m` is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a`, `c`, and `d`
// are positive, `a` and `c` are longer than one limb, and `m` is one limb long.
pub_test! {limbs_eq_mod_limb_val_ref(xs: &mut [Limb], ys: &[Limb], m: Limb) -> bool {
if xs.len() >= ys.len() {
limbs_eq_mod_limb_val_ref_greater(xs, ys, m)
} else {
limbs_eq_mod_limb_ref_val_greater(ys, xs, m)
}
}}
// xs.len() >= ys.len()
fn limbs_eq_mod_limb_val_ref_greater(xs: &mut [Limb], ys: &[Limb], m: Limb) -> bool {
if let Some(equal) = limbs_eq_mod_limb_helper(xs, ys, m) {
return equal;
}
// calculate |xs - ys|
if limbs_cmp(xs, ys) >= Ordering::Equal {
assert!(!limbs_sub_greater_in_place_left(xs, ys));
} else {
assert!(!limbs_sub_same_length_in_place_right(ys, xs));
}
let new_len = xs.len() - slice_trailing_zeros(xs);
// xs is non-empty here because xs != ys
if new_len == 1 {
xs[0].divisible_by(m)
} else {
limbs_divisible_by_limb(&xs[..new_len], m)
}
}
// xs.len() >= ys.len()
fn limbs_eq_mod_helper(xs: &[Limb], ys: &[Limb], m: &[Limb]) -> Option<bool> {
let m_len = m.len();
assert!(m_len > 1);
let x_len = xs.len();
let y_len = ys.len();
assert!(y_len > 1);
assert!(x_len >= y_len);
assert_ne!(*xs.last().unwrap(), 0);
assert_ne!(*ys.last().unwrap(), 0);
assert_ne!(*m.last().unwrap(), 0);
if xs == ys {
Some(true)
} else if m_len > x_len || !xs[0].eq_mod_power_of_2(ys[0], TrailingZeros::trailing_zeros(m[0]))
{
// Either: x < m, y < m, and x != y, so x != y mod m
// Or: xs != ys mod low zero bits of m_0
Some(false)
} else {
None
}
}
// Interpreting three slice of `Limb`s as the limbs of three `Natural`s, determines whether the
// first `Natural` is equal to the second `Natural` mod the third `Natural`. All input slices are
// immutable.
//
// This function assumes that each of the three input slices have at least two elements, and their
// last elements are nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
//
// # Panics
// Panics if the length of `xs`, `ys`, or `ms` is less than 2, or if the last element of any of the
// slices is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a`, `c`, and `d`
// are positive and each is longer than one limb.
pub_test! {limbs_eq_mod_ref_ref_ref(xs: &[Limb], ys: &[Limb], ms: &[Limb]) -> bool {
if xs.len() >= ys.len() {
limbs_eq_mod_greater_ref_ref_ref(xs, ys, ms)
} else {
limbs_eq_mod_greater_ref_ref_ref(ys, xs, ms)
}
}}
// xs.len() >= ys.len()
fn limbs_eq_mod_greater_ref_ref_ref(xs: &[Limb], ys: &[Limb], ms: &[Limb]) -> bool {
if let Some(equal) = limbs_eq_mod_helper(xs, ys, ms) {
return equal;
}
let mut scratch = vec![0; xs.len()];
// calculate |xs - ys|
if limbs_cmp(xs, ys) >= Ordering::Equal {
assert!(!limbs_sub_greater_to_out(&mut scratch, xs, ys));
} else {
assert!(!limbs_sub_same_length_to_out(&mut scratch, ys, xs));
}
scratch.truncate(scratch.len() - slice_trailing_zeros(&scratch));
scratch.len() >= ms.len() && limbs_divisible_by_val_ref(&mut scratch, ms)
}
// Interpreting three slice of `Limb`s as the limbs of three `Natural`s, determines whether the
// first `Natural` is equal to the second `Natural` mod the third `Natural`. The first two input
// slices are immutable, and the third is mutable.
//
// This function assumes that each of the three input slices have at least two elements, and their
// last elements are nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
//
// # Panics
// Panics if the length of `xs`, `ys`, or `ms` is less than 2, or if the last element of any
// of the slices is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a`, `c`, and `d`
// are positive and each is longer than one limb.
pub_test! {limbs_eq_mod_ref_ref_val(xs: &[Limb], ys: &[Limb], ms: &mut [Limb]) -> bool {
if xs.len() >= ys.len() {
limbs_eq_mod_greater_ref_ref_val(xs, ys, ms)
} else {
limbs_eq_mod_greater_ref_ref_val(ys, xs, ms)
}
}}
// xs.len() >= ys.len()
fn limbs_eq_mod_greater_ref_ref_val(xs: &[Limb], ys: &[Limb], ms: &mut [Limb]) -> bool {
if let Some(equal) = limbs_eq_mod_helper(xs, ys, ms) {
return equal;
}
let mut scratch = vec![0; xs.len()];
// calculate |xs - ys|
if limbs_cmp(xs, ys) >= Ordering::Equal {
assert!(!limbs_sub_greater_to_out(&mut scratch, xs, ys));
} else {
assert!(!limbs_sub_same_length_to_out(&mut scratch, ys, xs));
}
scratch.truncate(scratch.len() - slice_trailing_zeros(&scratch));
scratch.len() >= ms.len() && limbs_divisible_by(&mut scratch, ms)
}
// Interpreting three slice of `Limb`s as the limbs of three `Natural`s, determines whether the
// first `Natural` is equal to the second `Natural` mod the third `Natural`. The first and third
// input slices are immutable, and the second is mutable.
//
// This function assumes that each of the three input slices have at least two elements, and their
// last elements are nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
//
// # Panics
// Panics if the length of `xs`, `ys`, or `ms` is less than 2, or if the last element of any
// of the slices is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a`, `c`, and `d`
// are positive and each is longer than one limb.
pub_test! {limbs_eq_mod_ref_val_ref(xs: &[Limb], ys: &mut [Limb], ms: &[Limb]) -> bool {
if xs.len() >= ys.len() {
limbs_eq_mod_greater_ref_val_ref(xs, ys, ms)
} else {
limbs_eq_mod_greater_val_ref_ref(ys, xs, ms)
}
}}
// xs.len() >= ys.len()
fn limbs_eq_mod_greater_ref_val_ref(xs: &[Limb], ys: &mut [Limb], ms: &[Limb]) -> bool {
if let Some(equal) = limbs_eq_mod_helper(xs, ys, ms) {
return equal;
}
let mut scratch;
// calculate |xs - ys|
let scratch = if limbs_cmp(xs, ys) >= Ordering::Equal {
scratch = vec![0; xs.len()];
assert!(!limbs_sub_greater_to_out(&mut scratch, xs, ys));
&mut scratch
} else {
assert!(!limbs_sub_same_length_in_place_left(ys, xs));
ys
};
let new_len = scratch.len() - slice_trailing_zeros(scratch);
new_len >= ms.len() && limbs_divisible_by_val_ref(&mut scratch[..new_len], ms)
}
// xs.len() >= ys.len()
fn limbs_eq_mod_greater_val_ref_ref(xs: &mut [Limb], ys: &[Limb], ms: &[Limb]) -> bool {
if let Some(equal) = limbs_eq_mod_helper(xs, ys, ms) {
return equal;
}
// calculate |xs - ys|
if limbs_cmp(xs, ys) >= Ordering::Equal {
assert!(!limbs_sub_greater_in_place_left(xs, ys));
} else {
assert!(!limbs_sub_same_length_in_place_right(ys, xs));
}
let new_len = xs.len() - slice_trailing_zeros(xs);
new_len >= ms.len() && limbs_divisible_by_val_ref(&mut xs[..new_len], ms)
}
// Interpreting three slice of `Limb`s as the limbs of three `Natural`s, determines whether the
// first `Natural` is equal to the second `Natural` mod the third `Natural`. The first input slice
// is immutable, and the second and third are mutable.
//
// This function assumes that each of the three input slices have at least two elements, and their
// last elements are nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `max(xs.len(), ys.len())`.
//
// # Panics
// Panics if the length of `xs`, `ys`, or `ms` is less than 2, or if the last element of any
// of the slices is zero.
//
// This is equivalent to `mpz_congruent_p` from `mpz/cong.c`, GMP 6.2.1, where `a`, `c`, and `d`
// are positive and each is longer than one limb.
pub_test! {limbs_eq_mod_ref_val_val(xs: &[Limb], ys: &mut [Limb], ms: &mut [Limb]) -> bool {
if xs.len() >= ys.len() {
limbs_eq_mod_greater_ref_val_val(xs, ys, ms)
} else {
limbs_eq_mod_greater_val_ref_val(ys, xs, ms)
}
}}
// xs.len() >= ys.len()
fn limbs_eq_mod_greater_ref_val_val(xs: &[Limb], ys: &mut [Limb], ms: &mut [Limb]) -> bool {
if let Some(equal) = limbs_eq_mod_helper(xs, ys, ms) {
return equal;
}
let mut scratch;
// calculate |xs - ys|
let scratch = if limbs_cmp(xs, ys) >= Ordering::Equal {
scratch = vec![0; xs.len()];
assert!(!limbs_sub_greater_to_out(&mut scratch, xs, ys));
&mut scratch
} else {
assert!(!limbs_sub_same_length_in_place_left(ys, xs));
ys
};
let new_len = scratch.len() - slice_trailing_zeros(scratch);
new_len >= ms.len() && limbs_divisible_by(&mut scratch[..new_len], ms)
}
// xs.len() >= ys.len()
fn limbs_eq_mod_greater_val_ref_val(xs: &mut [Limb], ys: &[Limb], ms: &mut [Limb]) -> bool {
if let Some(equal) = limbs_eq_mod_helper(xs, ys, ms) {
return equal;
}
// calculate |xs - ys|
if limbs_cmp(xs, ys) >= Ordering::Equal {
assert!(!limbs_sub_greater_in_place_left(xs, ys));
} else {
assert!(!limbs_sub_same_length_in_place_right(ys, xs));
}
let new_len = xs.len() - slice_trailing_zeros(xs);
new_len >= ms.len() && limbs_divisible_by(&mut xs[..new_len], ms)
}
impl Natural {
fn eq_mod_limb(&self, other: Limb, m: Limb) -> bool {
match *self {
Natural(Small(small)) => small.eq_mod(other, m),
Natural(Large(_)) if m == 0 => false,
Natural(Large(ref limbs)) => limbs_eq_limb_mod_limb(limbs, other, m),
}
}
}
impl EqMod<Natural, Natural> for Natural {
/// Returns whether a [`Natural`] is equivalent to another [`Natural`] modulo a third; that is,
/// whether the difference between the first two is a multiple of the third. All three are
/// taken by value.
///
/// Two [`Natural`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(self.significant_bits(), other.significant_bits())`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::natural::Natural;
/// use std::str::FromStr;
///
/// assert_eq!(
/// Natural::from(123u32).eq_mod(Natural::from(223u32), Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// Natural::from_str("1000000987654").unwrap().eq_mod(
/// Natural::from_str("2000000987654").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// Natural::from_str("1000000987654").unwrap().eq_mod(
/// Natural::from_str("2000000987655").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: Natural, m: Natural) -> bool {
match (self, other, m) {
(x, y, natural_zero!()) => x == y,
(x, natural_zero!(), m) => x.divisible_by(m),
(natural_zero!(), y, m) => y.divisible_by(m),
(ref x, Natural(Small(y)), Natural(Small(m))) => x.eq_mod_limb(y, m),
(Natural(Small(x)), ref y, Natural(Small(m))) => y.eq_mod_limb(x, m),
(Natural(Small(x)), Natural(Small(y)), _) => x == y,
(Natural(Large(ref mut xs)), Natural(Large(ref ys)), Natural(Small(m))) => {
limbs_eq_mod_limb_val_ref(xs, ys, m)
}
(Natural(Large(ref mut xs)), Natural(Small(y)), Natural(Large(ref mut m))) => {
limbs_eq_limb_mod(xs, y, m)
}
(Natural(Small(x)), Natural(Large(ref mut ys)), Natural(Large(ref mut m))) => {
limbs_eq_limb_mod(ys, x, m)
}
(Natural(Large(ref mut xs)), Natural(Large(ref ys)), Natural(Large(ref mut m))) => {
limbs_eq_mod_ref_val_val(ys, xs, m)
}
}
}
}
impl<'a> EqMod<Natural, &'a Natural> for Natural {
/// Returns whether a [`Natural`] is equivalent to another [`Natural`] modulo a third; that is,
/// whether the difference between the first two is a multiple of the third. The first two are
/// taken by value and the third by reference.
///
/// Two [`Natural`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(self.significant_bits(), other.significant_bits())`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::natural::Natural;
/// use std::str::FromStr;
///
/// assert_eq!(
/// Natural::from(123u32).eq_mod(Natural::from(223u32), &Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// Natural::from_str("1000000987654").unwrap().eq_mod(
/// Natural::from_str("2000000987654").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// Natural::from_str("1000000987654").unwrap().eq_mod(
/// Natural::from_str("2000000987655").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: Natural, m: &'a Natural) -> bool {
match (self, other, m) {
(x, y, &natural_zero!()) => x == y,
(x, natural_zero!(), m) => x.divisible_by(m),
(natural_zero!(), y, m) => y.divisible_by(m),
(ref x, Natural(Small(y)), &Natural(Small(m))) => x.eq_mod_limb(y, m),
(Natural(Small(x)), ref y, &Natural(Small(m))) => y.eq_mod_limb(x, m),
(Natural(Small(x)), Natural(Small(y)), _) => x == y,
(Natural(Large(ref mut xs)), Natural(Large(ref ys)), &Natural(Small(m))) => {
limbs_eq_mod_limb_val_ref(xs, ys, m)
}
(Natural(Large(ref mut xs)), Natural(Small(y)), &Natural(Large(ref m))) => {
limbs_eq_limb_mod_val_ref(xs, y, m)
}
(Natural(Small(x)), Natural(Large(ref mut ys)), &Natural(Large(ref m))) => {
limbs_eq_limb_mod_val_ref(ys, x, m)
}
(Natural(Large(ref mut xs)), Natural(Large(ref ys)), &Natural(Large(ref m))) => {
limbs_eq_mod_ref_val_ref(ys, xs, m)
}
}
}
}
impl<'a> EqMod<&'a Natural, Natural> for Natural {
/// Returns whether a [`Natural`] is equivalent to another [`Natural`] modulo a third; that is,
/// whether the difference between the first two is a multiple of the third. The first and
/// third are taken by value and the second by reference.
///
/// Two [`Natural`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(self.significant_bits(), other.significant_bits())`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::natural::Natural;
/// use std::str::FromStr;
///
/// assert_eq!(
/// Natural::from(123u32).eq_mod(&Natural::from(223u32), Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// Natural::from_str("1000000987654").unwrap().eq_mod(
/// &Natural::from_str("2000000987654").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// Natural::from_str("1000000987654").unwrap().eq_mod(
/// &Natural::from_str("2000000987655").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: &'a Natural, m: Natural) -> bool {
match (self, other, m) {
(x, y, natural_zero!()) => x == *y,
(x, &natural_zero!(), m) => x.divisible_by(m),
(natural_zero!(), y, m) => y.divisible_by(m),
(ref x, &Natural(Small(y)), Natural(Small(m))) => x.eq_mod_limb(y, m),
(Natural(Small(x)), y, Natural(Small(m))) => y.eq_mod_limb(x, m),
(Natural(Small(x)), &Natural(Small(y)), _) => x == y,
(Natural(Large(ref mut xs)), &Natural(Large(ref ys)), Natural(Small(m))) => {
limbs_eq_mod_limb_val_ref(xs, ys, m)
}
(Natural(Large(ref mut xs)), &Natural(Small(y)), Natural(Large(ref mut m))) => {
limbs_eq_limb_mod(xs, y, m)
}
(Natural(Small(x)), &Natural(Large(ref ys)), Natural(Large(ref mut m))) => {
limbs_eq_limb_mod_ref_val(ys, x, m)
}
(Natural(Large(ref mut xs)), &Natural(Large(ref ys)), Natural(Large(ref mut m))) => {
limbs_eq_mod_ref_val_val(ys, xs, m)
}
}
}
}
impl<'a, 'b> EqMod<&'a Natural, &'b Natural> for Natural {
/// Returns whether a [`Natural`] is equivalent to another [`Natural`] modulo a third; that is,
/// whether the difference between the first two is a multiple of the third. The first is taken
/// by value and the second and third by reference.
///
/// Two [`Natural`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(self.significant_bits(), other.significant_bits())`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::natural::Natural;
/// use std::str::FromStr;
///
/// assert_eq!(
/// Natural::from(123u32).eq_mod(&Natural::from(223u32), &Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// Natural::from_str("1000000987654").unwrap().eq_mod(
/// &Natural::from_str("2000000987654").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// Natural::from_str("1000000987654").unwrap().eq_mod(
/// &Natural::from_str("2000000987655").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: &'a Natural, m: &'b Natural) -> bool {
match (self, other, m) {
(x, y, &natural_zero!()) => x == *y,
(x, &natural_zero!(), m) => x.divisible_by(m),
(natural_zero!(), y, m) => y.divisible_by(m),
(ref x, &Natural(Small(y)), &Natural(Small(m))) => x.eq_mod_limb(y, m),
(Natural(Small(x)), y, &Natural(Small(m))) => y.eq_mod_limb(x, m),
(Natural(Small(x)), &Natural(Small(y)), _) => x == y,
(Natural(Large(ref mut xs)), &Natural(Large(ref ys)), &Natural(Small(m))) => {
limbs_eq_mod_limb_val_ref(xs, ys, m)
}
(Natural(Large(ref mut xs)), &Natural(Small(y)), &Natural(Large(ref m))) => {
limbs_eq_limb_mod_val_ref(xs, y, m)
}
(Natural(Small(x)), &Natural(Large(ref ys)), &Natural(Large(ref m))) => {
limbs_eq_limb_mod_ref_ref(ys, x, m)
}
(Natural(Large(ref mut xs)), &Natural(Large(ref ys)), &Natural(Large(ref m))) => {
limbs_eq_mod_ref_val_ref(ys, xs, m)
}
}
}
}
impl<'a> EqMod<Natural, Natural> for &'a Natural {
/// Returns whether a [`Natural`] is equivalent to another [`Natural`] modulo a third; that is,
/// whether the difference between the first two is a multiple of the third. The first is taken
/// by reference and the second and third by value.
///
/// Two [`Natural`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(self.significant_bits(), other.significant_bits())`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::natural::Natural;
/// use std::str::FromStr;
///
/// assert_eq!(
/// (&Natural::from(123u32)).eq_mod(Natural::from(223u32), Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// (&Natural::from_str("1000000987654").unwrap()).eq_mod(
/// Natural::from_str("2000000987654").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// (&Natural::from_str("1000000987654").unwrap()).eq_mod(
/// Natural::from_str("2000000987655").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: Natural, m: Natural) -> bool {
match (self, other, m) {
(x, y, natural_zero!()) => *x == y,
(x, natural_zero!(), m) => x.divisible_by(m),
(&natural_zero!(), y, m) => y.divisible_by(m),
(x, Natural(Small(y)), Natural(Small(m))) => x.eq_mod_limb(y, m),
(&Natural(Small(x)), ref y, Natural(Small(m))) => y.eq_mod_limb(x, m),
(&Natural(Small(x)), Natural(Small(y)), _) => x == y,
(&Natural(Large(ref xs)), Natural(Large(ref mut ys)), Natural(Small(m))) => {
limbs_eq_mod_limb_ref_val(xs, ys, m)
}
(&Natural(Large(ref xs)), Natural(Small(y)), Natural(Large(ref mut m))) => {
limbs_eq_limb_mod_ref_val(xs, y, m)
}
(&Natural(Small(x)), Natural(Large(ref mut ys)), Natural(Large(ref mut m))) => {
limbs_eq_limb_mod(ys, x, m)
}
(&Natural(Large(ref xs)), Natural(Large(ref mut ys)), Natural(Large(ref mut m))) => {
limbs_eq_mod_ref_val_val(xs, ys, m)
}
}
}
}
impl<'a, 'b> EqMod<Natural, &'b Natural> for &'a Natural {
/// Returns whether a [`Natural`] is equivalent to another [`Natural`] modulo a third; that is,
/// whether the difference between the first two is a multiple of the third. The first and
/// third are taken by reference and the second by value.
///
/// Two [`Natural`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(self.significant_bits(), other.significant_bits())`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::natural::Natural;
/// use std::str::FromStr;
///
/// assert_eq!(
/// (&Natural::from(123u32)).eq_mod(Natural::from(223u32), &Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// (&Natural::from_str("1000000987654").unwrap()).eq_mod(
/// Natural::from_str("2000000987654").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// (&Natural::from_str("1000000987654").unwrap()).eq_mod(
/// Natural::from_str("2000000987655").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: Natural, m: &'b Natural) -> bool {
match (self, other, m) {
(x, y, &natural_zero!()) => *x == y,
(x, natural_zero!(), m) => x.divisible_by(m),
(&natural_zero!(), y, m) => y.divisible_by(m),
(x, Natural(Small(y)), &Natural(Small(m))) => x.eq_mod_limb(y, m),
(&Natural(Small(x)), ref y, &Natural(Small(m))) => y.eq_mod_limb(x, m),
(&Natural(Small(x)), Natural(Small(y)), _) => x == y,
(&Natural(Large(ref xs)), Natural(Large(ref mut ys)), &Natural(Small(m))) => {
limbs_eq_mod_limb_ref_val(xs, ys, m)
}
(&Natural(Large(ref xs)), Natural(Small(y)), &Natural(Large(ref m))) => {
limbs_eq_limb_mod_ref_ref(xs, y, m)
}
(&Natural(Small(x)), Natural(Large(ref mut ys)), &Natural(Large(ref m))) => {
limbs_eq_limb_mod_val_ref(ys, x, m)
}
(&Natural(Large(ref xs)), Natural(Large(ref mut ys)), &Natural(Large(ref m))) => {
limbs_eq_mod_ref_val_ref(xs, ys, m)
}
}
}
}
impl<'a, 'b> EqMod<&'b Natural, Natural> for &'a Natural {
/// Returns whether a [`Natural`] is equivalent to another [`Natural`] modulo a third; that is,
/// whether the difference between the first two is a multiple of the third. The first and
/// second are taken by reference and the third by value.
///
/// Two [`Natural`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(self.significant_bits(), other.significant_bits())`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::natural::Natural;
/// use std::str::FromStr;
///
/// assert_eq!(
/// (&Natural::from(123u32)).eq_mod(&Natural::from(223u32), Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// (&Natural::from_str("1000000987654").unwrap()).eq_mod(
/// &Natural::from_str("2000000987654").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// (&Natural::from_str("1000000987654").unwrap()).eq_mod(
/// &Natural::from_str("2000000987655").unwrap(),
/// Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: &'b Natural, m: Natural) -> bool {
match (self, other, m) {
(x, y, natural_zero!()) => x == y,
(x, &natural_zero!(), m) => x.divisible_by(m),
(&natural_zero!(), y, m) => y.divisible_by(m),
(x, &Natural(Small(y)), Natural(Small(m))) => x.eq_mod_limb(y, m),
(&Natural(Small(x)), y, Natural(Small(m))) => y.eq_mod_limb(x, m),
(&Natural(Small(x)), &Natural(Small(y)), _) => x == y,
(&Natural(Large(ref xs)), &Natural(Large(ref ys)), Natural(Small(m))) => {
limbs_eq_mod_limb_ref_ref(xs, ys, m)
}
(&Natural(Large(ref xs)), &Natural(Small(y)), Natural(Large(ref mut m))) => {
limbs_eq_limb_mod_ref_val(xs, y, m)
}
(&Natural(Small(x)), &Natural(Large(ref ys)), Natural(Large(ref mut m))) => {
limbs_eq_limb_mod_ref_val(ys, x, m)
}
(&Natural(Large(ref xs)), &Natural(Large(ref ys)), Natural(Large(ref mut m))) => {
limbs_eq_mod_ref_ref_val(xs, ys, m)
}
}
}
}
impl<'a, 'b, 'c> EqMod<&'b Natural, &'c Natural> for &'a Natural {
/// Returns whether a [`Natural`] is equivalent to another [`Natural`] modulo a third; that is,
/// whether the difference between the first two is a multiple of the third. All three are
/// taken by reference.
///
/// Two [`Natural`]s are equal to each other modulo 0 iff they are equal.
///
/// $f(x, y, m) = (x \equiv y \mod m)$.
///
/// $f(x, y, m) = (\exists k \in \Z : x - y = km)$.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(self.significant_bits(), other.significant_bits())`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::EqMod;
/// use malachite_nz::natural::Natural;
/// use std::str::FromStr;
///
/// assert_eq!(
/// (&Natural::from(123u32)).eq_mod(&Natural::from(223u32), &Natural::from(100u32)),
/// true
/// );
/// assert_eq!(
/// (&Natural::from_str("1000000987654").unwrap()).eq_mod(
/// &Natural::from_str("2000000987654").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// true
/// );
/// assert_eq!(
/// (&Natural::from_str("1000000987654").unwrap()).eq_mod(
/// &Natural::from_str("2000000987655").unwrap(),
/// &Natural::from_str("1000000000000").unwrap()
/// ),
/// false
/// );
/// ```
fn eq_mod(self, other: &'b Natural, m: &'c Natural) -> bool {
match (self, other, m) {
(x, y, &natural_zero!()) => x == y,
(x, &natural_zero!(), m) => x.divisible_by(m),
(&natural_zero!(), y, m) => y.divisible_by(m),
(x, &Natural(Small(y)), &Natural(Small(m))) => x.eq_mod_limb(y, m),
(&Natural(Small(x)), y, &Natural(Small(m))) => y.eq_mod_limb(x, m),
(&Natural(Small(x)), &Natural(Small(y)), _) => x == y,
(&Natural(Large(ref xs)), &Natural(Large(ref ys)), &Natural(Small(m))) => {
limbs_eq_mod_limb_ref_ref(xs, ys, m)
}
(&Natural(Large(ref xs)), &Natural(Small(y)), &Natural(Large(ref m))) => {
limbs_eq_limb_mod_ref_ref(xs, y, m)
}
(&Natural(Small(x)), &Natural(Large(ref ys)), &Natural(Large(ref m))) => {
limbs_eq_limb_mod_ref_ref(ys, x, m)
}
(&Natural(Large(ref xs)), &Natural(Large(ref ys)), &Natural(Large(ref m))) => {
limbs_eq_mod_ref_ref_ref(xs, ys, m)
}
}
}
}