Skip to main content

malachite_nz/gaussian_integer/comparison/
partial_eq_integer.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::gaussian_integer::GaussianInteger;
10use crate::integer::Integer;
11
12impl PartialEq<Integer> for GaussianInteger {
13    /// Determines whether a [`GaussianInteger`] is equal to an [`Integer`].
14    ///
15    /// # Worst-case complexity
16    /// $T(n) = O(n)$
17    ///
18    /// $M(n) = O(1)$
19    ///
20    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.real.significant_bits(),
21    /// other.significant_bits())`.
22    ///
23    /// # Examples
24    /// ```
25    /// use malachite_nz::gaussian_integer::GaussianInteger;
26    /// use malachite_nz::integer::Integer;
27    /// use std::str::FromStr;
28    ///
29    /// assert!(GaussianInteger::from(123) == Integer::from(123));
30    /// assert!(GaussianInteger::from_str("123+i").unwrap() != Integer::from(123));
31    /// ```
32    fn eq(&self, other: &Integer) -> bool {
33        self.imaginary == 0u32 && self.real == *other
34    }
35}
36
37impl PartialEq<GaussianInteger> for Integer {
38    /// Determines whether an [`Integer`] is equal to a [`GaussianInteger`].
39    ///
40    /// # Worst-case complexity
41    /// $T(n) = O(n)$
42    ///
43    /// $M(n) = O(1)$
44    ///
45    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
46    /// other.real.significant_bits())`.
47    ///
48    /// # Examples
49    /// ```
50    /// use malachite_nz::gaussian_integer::GaussianInteger;
51    /// use malachite_nz::integer::Integer;
52    /// use std::str::FromStr;
53    ///
54    /// assert!(Integer::from(123) == GaussianInteger::from(123));
55    /// assert!(Integer::from(123) != GaussianInteger::from_str("123+i").unwrap());
56    /// ```
57    fn eq(&self, other: &GaussianInteger) -> bool {
58        other.imaginary == 0u32 && *self == other.real
59    }
60}