Fix partial_cmp

This commit is contained in:
sebcrozet 2018-10-13 11:24:11 +02:00 committed by Sébastien Crozet
parent 18e9b8998d
commit a390732b97
2 changed files with 24 additions and 12 deletions

View File

@ -894,18 +894,17 @@ where
{ {
#[inline] #[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
assert!( if self.shape() != other.shape() || self.nrows() == 0 || self.ncols() == 0 {
self.shape() == other.shape(), return None;
"Matrix comparison error: dimensions mismatch." }
);
let first_ord = unsafe { let mut first_ord = unsafe {
self.data self.data
.get_unchecked_linear(0) .get_unchecked_linear(0)
.partial_cmp(other.data.get_unchecked_linear(0)) .partial_cmp(other.data.get_unchecked_linear(0))
}; };
if let Some(mut first_ord) = first_ord { if let Some(first_ord) = first_ord.as_mut() {
let mut it = self.iter().zip(other.iter()); let mut it = self.iter().zip(other.iter());
let _ = it.next(); // Drop the first elements (we already tested it). let _ = it.next(); // Drop the first elements (we already tested it).
@ -914,16 +913,16 @@ where
match ord { match ord {
Ordering::Equal => { /* Does not change anything. */ } Ordering::Equal => { /* Does not change anything. */ }
Ordering::Less => { Ordering::Less => {
if first_ord == Ordering::Greater { if *first_ord == Ordering::Greater {
return None; return None;
} }
first_ord = ord *first_ord = ord
} }
Ordering::Greater => { Ordering::Greater => {
if first_ord == Ordering::Less { if *first_ord == Ordering::Less {
return None; return None;
} }
first_ord = ord *first_ord = ord
} }
} }
} else { } else {
@ -976,8 +975,7 @@ impl<N, R: Dim, C: Dim, S> Eq for Matrix<N, R, C, S>
where where
N: Scalar + Eq, N: Scalar + Eq,
S: Storage<N, R, C>, S: Storage<N, R, C>,
{ {}
}
impl<N, R: Dim, C: Dim, S> PartialEq for Matrix<N, R, C, S> impl<N, R: Dim, C: Dim, S> PartialEq for Matrix<N, R, C, S>
where where

View File

@ -1,4 +1,5 @@
use num::{One, Zero}; use num::{One, Zero};
use std::cmp::Ordering;
use na::dimension::{U15, U8}; use na::dimension::{U15, U8};
use na::{ use na::{
@ -723,6 +724,19 @@ fn partial_clamp() {
assert_eq!(*inter.unwrap(), n); assert_eq!(*inter.unwrap(), n);
} }
#[test]
fn partial_cmp() {
// NOTE: from #401.
let a = Vector2::new(1.0, 6.0);
let b = Vector2::new(1.0, 3.0);
let c = Vector2::new(2.0, 7.0);
let d = Vector2::new(0.0, 7.0);
assert_eq!(a.partial_cmp(&a), Some(Ordering::Equal));
assert_eq!(a.partial_cmp(&b), Some(Ordering::Greater));
assert_eq!(a.partial_cmp(&c), Some(Ordering::Less));
assert_eq!(a.partial_cmp(&d), None);
}
#[test] #[test]
fn swizzle() { fn swizzle() {
let a = Vector2::new(1.0f32, 2.0); let a = Vector2::new(1.0f32, 2.0);