Fix `Ord` implementations to be a partial order.
When two elements are not comparable, all comparison operators return `false`.
This commit is contained in:
parent
50b34ca765
commit
9af1cac45d
12
src/mat.rs
12
src/mat.rs
|
@ -30,7 +30,7 @@ pub use traits::transpose::*;
|
|||
mod mat_macros;
|
||||
|
||||
/// Square matrix of dimension 1.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Mat1<N>
|
||||
{
|
||||
m11: N
|
||||
|
@ -55,7 +55,7 @@ to_homogeneous_impl!(Mat1, Mat2, 1, 2)
|
|||
from_homogeneous_impl!(Mat1, Mat2, 1, 2)
|
||||
|
||||
/// Square matrix of dimension 2.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Mat2<N>
|
||||
{
|
||||
m11: N, m12: N,
|
||||
|
@ -84,7 +84,7 @@ to_homogeneous_impl!(Mat2, Mat3, 2, 3)
|
|||
from_homogeneous_impl!(Mat2, Mat3, 2, 3)
|
||||
|
||||
/// Square matrix of dimension 3.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Mat3<N>
|
||||
{
|
||||
m11: N, m12: N, m13: N,
|
||||
|
@ -117,7 +117,7 @@ to_homogeneous_impl!(Mat3, Mat4, 3, 4)
|
|||
from_homogeneous_impl!(Mat3, Mat4, 3, 4)
|
||||
|
||||
/// Square matrix of dimension 4.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Mat4<N>
|
||||
{
|
||||
m11: N, m12: N, m13: N, m14: N,
|
||||
|
@ -158,7 +158,7 @@ to_homogeneous_impl!(Mat4, Mat5, 4, 5)
|
|||
from_homogeneous_impl!(Mat4, Mat5, 4, 5)
|
||||
|
||||
/// Square matrix of dimension 5.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Mat5<N>
|
||||
{
|
||||
m11: N, m12: N, m13: N, m14: N, m15: N,
|
||||
|
@ -205,7 +205,7 @@ to_homogeneous_impl!(Mat5, Mat6, 5, 6)
|
|||
from_homogeneous_impl!(Mat5, Mat6, 5, 6)
|
||||
|
||||
/// Square matrix of dimension 6.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Mat6<N>
|
||||
{
|
||||
m11: N, m12: N, m13: N, m14: N, m15: N, m16: N,
|
||||
|
|
|
@ -264,3 +264,24 @@ fn test_iterator_vec5()
|
|||
#[test]
|
||||
fn test_iterator_vec6()
|
||||
{ test_iterator_impl!(Vec6<f64>, f64); }
|
||||
|
||||
#[test]
|
||||
fn test_ord_vec3()
|
||||
{
|
||||
// equality
|
||||
assert!(Vec3::new(0.5, 0.5, 0.5) == Vec3::new(0.5, 0.5, 0.5));
|
||||
assert!(!(Vec3::new(1.5, 0.5, 0.5) == Vec3::new(0.5, 0.5, 0.5)));
|
||||
assert!(Vec3::new(1.5, 0.5, 0.5) != Vec3::new(0.5, 0.5, 0.5));
|
||||
|
||||
// comparable
|
||||
assert!(Vec3::new(0.5, 0.3, 0.3) < Vec3::new(1.0, 2.0, 1.0));
|
||||
assert!(Vec3::new(0.5, 0.3, 0.3) <= Vec3::new(1.0, 2.0, 1.0));
|
||||
assert!(Vec3::new(2.0, 4.0, 2.0) > Vec3::new(1.0, 2.0, 1.0));
|
||||
assert!(Vec3::new(2.0, 4.0, 2.0) >= Vec3::new(1.0, 2.0, 1.0));
|
||||
|
||||
// not comparable
|
||||
assert!(!(Vec3::new(0.0, 3.0, 0.0) < Vec3::new(1.0, 2.0, 1.0)));
|
||||
assert!(!(Vec3::new(0.0, 3.0, 0.0) > Vec3::new(1.0, 2.0, 1.0)));
|
||||
assert!(!(Vec3::new(0.0, 3.0, 0.0) <= Vec3::new(1.0, 2.0, 1.0)));
|
||||
assert!(!(Vec3::new(0.0, 3.0, 0.0) >= Vec3::new(1.0, 2.0, 1.0)));
|
||||
}
|
||||
|
|
21
src/vec.rs
21
src/vec.rs
|
@ -27,11 +27,11 @@ pub use traits::scalar_op::*;
|
|||
mod vec_macros;
|
||||
|
||||
/// Vector of dimension 0.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, Rand, Zero, ToStr)]
|
||||
pub struct Vec0<N>;
|
||||
|
||||
/// Vector of dimension 1.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Vec1<N>
|
||||
{
|
||||
/// First component of the vector.
|
||||
|
@ -39,6 +39,7 @@ pub struct Vec1<N>
|
|||
}
|
||||
|
||||
new_impl!(Vec1, x)
|
||||
ord_impl!(Vec1, x)
|
||||
vec_axis_impl!(Vec1, x)
|
||||
vec_cast_impl!(Vec1, x)
|
||||
indexable_impl!(Vec1, 1)
|
||||
|
@ -67,7 +68,7 @@ to_homogeneous_impl!(Vec1, Vec2, y, x)
|
|||
from_homogeneous_impl!(Vec1, Vec2, y, x)
|
||||
|
||||
/// Vector of dimension 2.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Vec2<N>
|
||||
{
|
||||
/// First component of the vector.
|
||||
|
@ -77,6 +78,7 @@ pub struct Vec2<N>
|
|||
}
|
||||
|
||||
new_impl!(Vec2, x, y)
|
||||
ord_impl!(Vec2, x, y)
|
||||
vec_axis_impl!(Vec2, x, y)
|
||||
vec_cast_impl!(Vec2, x, y)
|
||||
indexable_impl!(Vec2, 2)
|
||||
|
@ -105,7 +107,7 @@ to_homogeneous_impl!(Vec2, Vec3, z, x, y)
|
|||
from_homogeneous_impl!(Vec2, Vec3, z, x, y)
|
||||
|
||||
/// Vector of dimension 3.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Vec3<N>
|
||||
|
||||
{
|
||||
|
@ -117,8 +119,8 @@ pub struct Vec3<N>
|
|||
z: N
|
||||
}
|
||||
|
||||
|
||||
new_impl!(Vec3, x, y, z)
|
||||
ord_impl!(Vec3, x, y, z)
|
||||
vec_axis_impl!(Vec3, x, y, z)
|
||||
vec_cast_impl!(Vec3, x, y, z)
|
||||
indexable_impl!(Vec3, 3)
|
||||
|
@ -147,7 +149,7 @@ to_homogeneous_impl!(Vec3, Vec4, w, x, y, z)
|
|||
from_homogeneous_impl!(Vec3, Vec4, w, x, y, z)
|
||||
|
||||
/// Vector of dimension 4.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Vec4<N>
|
||||
{
|
||||
/// First component of the vector.
|
||||
|
@ -161,6 +163,7 @@ pub struct Vec4<N>
|
|||
}
|
||||
|
||||
new_impl!(Vec4, x, y, z, w)
|
||||
ord_impl!(Vec4, x, y, z, w)
|
||||
vec_axis_impl!(Vec4, x, y, z, w)
|
||||
vec_cast_impl!(Vec4, x, y, z, w)
|
||||
indexable_impl!(Vec4, 4)
|
||||
|
@ -189,7 +192,7 @@ to_homogeneous_impl!(Vec4, Vec5, a, x, y, z, w)
|
|||
from_homogeneous_impl!(Vec4, Vec5, a, x, y, z, w)
|
||||
|
||||
/// Vector of dimension 5.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Vec5<N>
|
||||
{
|
||||
/// First component of the vector.
|
||||
|
@ -205,6 +208,7 @@ pub struct Vec5<N>
|
|||
}
|
||||
|
||||
new_impl!(Vec5, x, y, z, w, a)
|
||||
ord_impl!(Vec5, x, y, z, w, a)
|
||||
vec_axis_impl!(Vec5, x, y, z, w, a)
|
||||
vec_cast_impl!(Vec5, x, y, z, w, a)
|
||||
indexable_impl!(Vec5, 5)
|
||||
|
@ -233,7 +237,7 @@ to_homogeneous_impl!(Vec5, Vec6, b, x, y, z, w, a)
|
|||
from_homogeneous_impl!(Vec5, Vec6, b, x, y, z, w, a)
|
||||
|
||||
/// Vector of dimension 6.
|
||||
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
|
||||
pub struct Vec6<N>
|
||||
{
|
||||
/// First component of the vector.
|
||||
|
@ -251,6 +255,7 @@ pub struct Vec6<N>
|
|||
}
|
||||
|
||||
new_impl!(Vec6, x, y, z, w, a, b)
|
||||
ord_impl!(Vec6, x, y, z, w, a, b)
|
||||
vec_axis_impl!(Vec6, x, y, z, w, a, b)
|
||||
vec_cast_impl!(Vec6, x, y, z, w, a, b)
|
||||
indexable_impl!(Vec6, 6)
|
||||
|
|
|
@ -17,6 +17,29 @@ macro_rules! new_impl(
|
|||
)
|
||||
)
|
||||
|
||||
macro_rules! ord_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<N: Ord> Ord for $t<N>
|
||||
{
|
||||
#[inline]
|
||||
fn lt(&self, other: &$t<N>) -> bool
|
||||
{ self.$comp0 < other.$comp0 $(&& self.$compN < other.$compN)* }
|
||||
|
||||
#[inline]
|
||||
fn le(&self, other: &$t<N>) -> bool
|
||||
{ self.$comp0 <= other.$comp0 $(&& self.$compN <= other.$compN)* }
|
||||
|
||||
#[inline]
|
||||
fn gt(&self, other: &$t<N>) -> bool
|
||||
{ self.$comp0 > other.$comp0 $(&& self.$compN > other.$compN)* }
|
||||
|
||||
#[inline]
|
||||
fn ge(&self, other: &$t<N>) -> bool
|
||||
{ self.$comp0 >= other.$comp0 $(&& self.$compN >= other.$compN)* }
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
macro_rules! vec_axis_impl(
|
||||
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
||||
impl<N: Zero + One> $t<N>
|
||||
|
|
Loading…
Reference in New Issue