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
// Copyright © 2017 University of Malta // This program is free software: you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public License // as published by the Free Software Foundation, either version 3 of // the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License and a copy of the GNU General Public License along with // this program. If not, see <http://www.gnu.org/licenses/>. use Complex; use std::cmp::Ordering; use std::hash::{Hash, Hasher}; /// A complex number that supports ordering and hashing. /// /// For ordering, the real part has precedence over the imaginary /// part. Negative zero is ordered as less than positive zero. All /// NaNs are ordered as equal and as less than negative infinity, with /// the NaN sign ignored. /// /// # Examples /// /// ```rust /// use rug::Complex; /// use rug::complex::OrdComplex; /// use rug::float::Special; /// use std::cmp::Ordering; /// /// let nan_c = Complex::with_val(53, (Special::Nan, Special::Nan)); /// let nan = OrdComplex::from(nan_c); /// assert_eq!(nan.cmp(&nan), Ordering::Equal); /// /// let one_neg0_c = Complex::with_val(53, (1, Special::NegZero)); /// let one_neg0 = OrdComplex::from(one_neg0_c); /// let one_pos0_c = Complex::with_val(53, (1, Special::Zero)); /// let one_pos0 = OrdComplex::from(one_pos0_c); /// assert_eq!(one_neg0.cmp(&one_pos0), Ordering::Less); /// /// let zero_inf_s = (Special::Zero, Special::Infinity); /// let zero_inf_c = Complex::with_val(53, zero_inf_s); /// let zero_inf = OrdComplex::from(zero_inf_c); /// assert_eq!(one_pos0.cmp(&zero_inf), Ordering::Greater); /// ``` #[derive(Clone, Debug, Default)] pub struct OrdComplex { inner: Complex, } impl OrdComplex { /// Extracts the underlying [`Complex`](../struct.Complex.html). /// /// # Examples /// /// ```rust /// use rug::Complex; /// use rug::complex::OrdComplex; /// let c = Complex::with_val(53, (1.5, 2.5)); /// let ord = OrdComplex::from(c); /// let c_ref = ord.as_complex(); /// let (re, im) = c_ref.as_real_imag(); /// assert_eq!(*re, 1.5); /// assert_eq!(*im, 2.5); pub fn as_complex(&self) -> &Complex { &self.inner } /// Extracts the underlying [`Complex`](../struct.Complex.html). /// /// # Examples /// /// ```rust /// use rug::Complex; /// use rug::complex::OrdComplex; /// let c = Complex::with_val(53, (1.5, -2.5)); /// let mut ord = OrdComplex::from(c); /// ord.as_complex_mut().conj_mut(); /// let (re, im) = ord.as_complex().as_real_imag(); /// assert_eq!(*re, 1.5); /// assert_eq!(*im, 2.5); pub fn as_complex_mut(&mut self) -> &mut Complex { &mut self.inner } } impl Hash for OrdComplex { #[inline] fn hash<H: Hasher>(&self, state: &mut H) { let (re, im) = self.inner.as_real_imag(); let (re, im) = (re.as_ord(), im.as_ord()); re.hash(state); im.hash(state); } } impl PartialEq for OrdComplex { #[inline] fn eq(&self, other: &OrdComplex) -> bool { let (re, im) = self.inner.as_real_imag(); let (re, im) = (re.as_ord(), im.as_ord()); let (other_re, other_im) = other.inner.as_real_imag(); let (other_re, other_im) = (other_re.as_ord(), other_im.as_ord()); re.eq(other_re) && im.eq(other_im) } } impl Eq for OrdComplex {} impl PartialOrd for OrdComplex { #[inline] fn partial_cmp(&self, other: &OrdComplex) -> Option<Ordering> { Some(self.cmp(other)) } } impl Ord for OrdComplex { #[inline] fn cmp(&self, other: &OrdComplex) -> Ordering { let (re, im) = self.inner.as_real_imag(); let (re, im) = (re.as_ord(), im.as_ord()); let (other_re, other_im) = other.inner.as_real_imag(); let (other_re, other_im) = (other_re.as_ord(), other_im.as_ord()); re.cmp(other_re).then(im.cmp(other_im)) } } impl From<Complex> for OrdComplex { #[inline] fn from(c: Complex) -> OrdComplex { OrdComplex { inner: c } } }