malachite_nz/gaussian_integer/comparison/cmp.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::{ComparableGaussianInteger, ComparableGaussianIntegerRef};
10use core::cmp::Ordering;
11
12impl Ord for ComparableGaussianIntegerRef<'_> {
13 /// Compares two [`ComparableGaussianIntegerRef`]s.
14 ///
15 /// The order is lexicographic: real parts are compared first, and imaginary parts break ties.
16 /// This is a total order, and its equality agrees with
17 /// [`GaussianInteger`](crate::gaussian_integer::GaussianInteger) equality, but it is not
18 /// compatible with arithmetic: no total order on the complex numbers is. It is intended for
19 /// canonically sorting Gaussian integers and for using them as keys in ordered collections.
20 ///
21 /// # Worst-case complexity
22 /// $T(n) = O(n)$
23 ///
24 /// $M(n) = O(1)$
25 ///
26 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
27 /// bits of the real and imaginary parts of `self` and `other`.
28 ///
29 /// # Examples
30 /// ```
31 /// use malachite_base::num::basic::traits::{I, NegativeI, One, Zero};
32 /// use malachite_nz::gaussian_integer::{ComparableGaussianIntegerRef, GaussianInteger};
33 ///
34 /// let zero = GaussianInteger::ZERO;
35 /// let one = GaussianInteger::ONE;
36 /// let i = GaussianInteger::I;
37 /// let negative_i = GaussianInteger::NEGATIVE_I;
38 ///
39 /// // 0 < i, since the real parts are equal and 0 < 1
40 /// assert!(ComparableGaussianIntegerRef(&zero) < ComparableGaussianIntegerRef(&i));
41 /// // -i < i
42 /// assert!(ComparableGaussianIntegerRef(&negative_i) < ComparableGaussianIntegerRef(&i));
43 /// // i < 1, since 0 < 1 and the real parts are compared first
44 /// assert!(ComparableGaussianIntegerRef(&i) < ComparableGaussianIntegerRef(&one));
45 /// ```
46 #[inline]
47 fn cmp(&self, other: &Self) -> Ordering {
48 self.0
49 .real
50 .cmp(&other.0.real)
51 .then_with(|| self.0.imaginary.cmp(&other.0.imaginary))
52 }
53}
54
55impl PartialOrd for ComparableGaussianIntegerRef<'_> {
56 /// Compares two [`ComparableGaussianIntegerRef`]s.
57 ///
58 /// See the documentation for the [`Ord`] implementation.
59 #[inline]
60 fn partial_cmp(&self, other: &ComparableGaussianIntegerRef) -> Option<Ordering> {
61 Some(self.cmp(other))
62 }
63}
64
65impl Ord for ComparableGaussianInteger {
66 /// Compares two [`ComparableGaussianInteger`]s.
67 ///
68 /// The order is lexicographic: real parts are compared first, and imaginary parts break ties.
69 /// This is a total order, and its equality agrees with
70 /// [`GaussianInteger`](crate::gaussian_integer::GaussianInteger) equality, but it is not
71 /// compatible with arithmetic: no total order on the complex numbers is. It is intended for
72 /// canonically sorting Gaussian integers and for using them as keys in ordered collections.
73 ///
74 /// # Worst-case complexity
75 /// $T(n) = O(n)$
76 ///
77 /// $M(n) = O(1)$
78 ///
79 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
80 /// bits of the real and imaginary parts of `self` and `other`.
81 ///
82 /// # Examples
83 /// ```
84 /// use malachite_base::num::basic::traits::{I, NegativeI, One, Zero};
85 /// use malachite_nz::gaussian_integer::{ComparableGaussianInteger, GaussianInteger};
86 ///
87 /// // 0 < i, since the real parts are equal and 0 < 1
88 /// assert!(
89 /// ComparableGaussianInteger(GaussianInteger::ZERO)
90 /// < ComparableGaussianInteger(GaussianInteger::I)
91 /// );
92 /// // -i < i
93 /// assert!(
94 /// ComparableGaussianInteger(GaussianInteger::NEGATIVE_I)
95 /// < ComparableGaussianInteger(GaussianInteger::I)
96 /// );
97 /// // i < 1, since 0 < 1 and the real parts are compared first
98 /// assert!(
99 /// ComparableGaussianInteger(GaussianInteger::I)
100 /// < ComparableGaussianInteger(GaussianInteger::ONE)
101 /// );
102 /// ```
103 #[inline]
104 fn cmp(&self, other: &Self) -> Ordering {
105 self.as_ref().cmp(&other.as_ref())
106 }
107}
108
109#[allow(clippy::non_canonical_partial_ord_impl)]
110impl PartialOrd for ComparableGaussianInteger {
111 /// Compares two [`ComparableGaussianInteger`]s.
112 ///
113 /// See the documentation for the [`Ord`] implementation.
114 #[inline]
115 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
116 Some(self.as_ref().cmp(&other.as_ref()))
117 }
118}