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
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::gaussian_integer::{ComparableGaussianInteger, ComparableGaussianIntegerRef};
use core::cmp::Ordering;
impl Ord for ComparableGaussianIntegerRef<'_> {
/// Compares two [`ComparableGaussianIntegerRef`]s.
///
/// The order is lexicographic: real parts are compared first, and imaginary parts break ties.
/// This is a total order, and its equality agrees with
/// [`GaussianInteger`](crate::gaussian_integer::GaussianInteger) equality, but it is not
/// compatible with arithmetic: no total order on the complex numbers is. It is intended for
/// canonically sorting Gaussian integers and for using them as keys in ordered collections.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
/// bits of the real and imaginary parts of `self` and `other`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::{I, NegativeI, One, Zero};
/// use malachite_nz::gaussian_integer::{ComparableGaussianIntegerRef, GaussianInteger};
///
/// let zero = GaussianInteger::ZERO;
/// let one = GaussianInteger::ONE;
/// let i = GaussianInteger::I;
/// let negative_i = GaussianInteger::NEGATIVE_I;
///
/// // 0 < i, since the real parts are equal and 0 < 1
/// assert!(ComparableGaussianIntegerRef(&zero) < ComparableGaussianIntegerRef(&i));
/// // -i < i
/// assert!(ComparableGaussianIntegerRef(&negative_i) < ComparableGaussianIntegerRef(&i));
/// // i < 1, since 0 < 1 and the real parts are compared first
/// assert!(ComparableGaussianIntegerRef(&i) < ComparableGaussianIntegerRef(&one));
/// ```
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.0
.real
.cmp(&other.0.real)
.then_with(|| self.0.imaginary.cmp(&other.0.imaginary))
}
}
impl PartialOrd for ComparableGaussianIntegerRef<'_> {
/// Compares two [`ComparableGaussianIntegerRef`]s.
///
/// See the documentation for the [`Ord`] implementation.
#[inline]
fn partial_cmp(&self, other: &ComparableGaussianIntegerRef) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ComparableGaussianInteger {
/// Compares two [`ComparableGaussianInteger`]s.
///
/// The order is lexicographic: real parts are compared first, and imaginary parts break ties.
/// This is a total order, and its equality agrees with
/// [`GaussianInteger`](crate::gaussian_integer::GaussianInteger) equality, but it is not
/// compatible with arithmetic: no total order on the complex numbers is. It is intended for
/// canonically sorting Gaussian integers and for using them as keys in ordered collections.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
/// bits of the real and imaginary parts of `self` and `other`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::{I, NegativeI, One, Zero};
/// use malachite_nz::gaussian_integer::{ComparableGaussianInteger, GaussianInteger};
///
/// // 0 < i, since the real parts are equal and 0 < 1
/// assert!(
/// ComparableGaussianInteger(GaussianInteger::ZERO)
/// < ComparableGaussianInteger(GaussianInteger::I)
/// );
/// // -i < i
/// assert!(
/// ComparableGaussianInteger(GaussianInteger::NEGATIVE_I)
/// < ComparableGaussianInteger(GaussianInteger::I)
/// );
/// // i < 1, since 0 < 1 and the real parts are compared first
/// assert!(
/// ComparableGaussianInteger(GaussianInteger::I)
/// < ComparableGaussianInteger(GaussianInteger::ONE)
/// );
/// ```
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.as_ref().cmp(&other.as_ref())
}
}
#[allow(clippy::non_canonical_partial_ord_impl)]
impl PartialOrd for ComparableGaussianInteger {
/// Compares two [`ComparableGaussianInteger`]s.
///
/// See the documentation for the [`Ord`] implementation.
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.as_ref().cmp(&other.as_ref()))
}
}