Implement index operator trait

This commit is contained in:
Brendan Zabarauskas 2012-09-29 20:37:46 +10:00
parent 96713c75b3
commit 782b3aaca1
3 changed files with 52 additions and 42 deletions

View file

@ -1,6 +1,6 @@
use std::cmp::FuzzyEq;
use cmp::Eq;
use ops::Neg;
use ops::{Neg, Index};
// use to_str::ToStr;
use math::Sqrt;
use quat::Quat;
@ -14,7 +14,6 @@ pub trait Matrix<T, V> {
pure fn cols() -> uint;
pure fn is_col_major() -> bool;
pure fn index(&&index:uint) -> V;
pure fn row(&&i:uint) -> V;
pure fn col(&&i:uint) -> V;
@ -98,11 +97,6 @@ pub impl Mat2: Matrix<float, Vec2> {
#[inline]
pure fn is_col_major() -> bool { true }
#[inline]
pure fn index(&&i: uint) -> Vec2 {
self.data[i]
}
#[inline]
pure fn row(&&i:uint) -> Vec2 {
Vec2(self[0][i],
@ -186,6 +180,13 @@ pub impl Mat2: Matrix<float, Vec2> {
}
}
pub impl Mat2: Index<uint, Vec2> {
#[inline]
pure fn index(+i: uint) -> Vec2 {
self.data[i]
}
}
pub impl Mat2: Neg<Mat2> {
#[inline]
pure fn neg() -> Mat2 {
@ -263,11 +264,6 @@ pub impl Mat3: Matrix<float, Vec3> {
#[inline]
pure fn is_col_major() -> bool { true }
#[inline]
pure fn index(&&i: uint) -> Vec3 {
self.data[i]
}
#[inline]
pure fn row(&&i:uint) -> Vec3 {
Vec3(self[0][i],
@ -433,6 +429,13 @@ pub impl Mat3: Matrix3<Vec3> {
}
}
pub impl Mat3: Index<uint, Vec3> {
#[inline]
pure fn index(+i: uint) -> Vec3 {
self.data[i]
}
}
pub impl Mat3: Neg<Mat3> {
#[inline]
pure fn neg() -> Mat3 {
@ -515,11 +518,6 @@ pub impl Mat4: Matrix<float, Vec4> {
#[inline]
pure fn is_col_major() -> bool { true }
#[inline]
pure fn index(&&i: uint) -> Vec4 {
self.data[i]
}
#[inline]
pure fn row(&&i:uint) -> Vec4 {
Vec4(self[0][i],
@ -678,6 +676,13 @@ pub impl Mat4: Matrix4<Vec3, Vec4> {
}
}
pub impl Mat4: Index<uint, Vec4> {
#[inline]
pure fn index(+i: uint) -> Vec4 {
self.data[i]
}
}
pub impl Mat4: Neg<Mat4> {
#[inline]
pure fn neg() -> Mat4 {

View file

@ -1,6 +1,6 @@
use std::cmp::FuzzyEq;
use cmp::Eq;
use ops::Neg;
use ops::{Neg, Index};
use to_str::ToStr;
use math::Sqrt;
use mat::{Mat3, Mat4};
@ -14,7 +14,6 @@ use vec::Vec3;
pub trait Quaternion<T> {
pure fn dim() -> uint;
pure fn index(&&index:uint) -> T;
pure fn w() -> T;
pure fn x() -> T;
pure fn y() -> T;
@ -72,11 +71,6 @@ pub impl Quat: Quaternion<float> {
#[inline]
pure fn dim() -> uint { 4 }
#[inline]
pure fn index(&&i: uint) -> float {
self.data[i]
}
#[inline] pure fn w() -> float { self.data[0] }
#[inline] pure fn x() -> float { self.data[1] }
#[inline] pure fn y() -> float { self.data[2] }
@ -182,6 +176,13 @@ pub impl Quat: Quaternion<float> {
}
}
pub impl Quat: Index<uint, float> {
#[inline]
pure fn index(+i: uint) -> float {
self.data[i]
}
}
pub impl Quat: Neg<Quat> {
#[inline]
pure fn neg() -> Quat {

View file

@ -1,6 +1,6 @@
use std::cmp::FuzzyEq;
use cmp::Eq;
use ops::Neg;
use ops::{Neg, Index};
use math::{Abs, min, max, Sqrt};
use to_str::ToStr;
@ -10,8 +10,6 @@ use to_str::ToStr;
pub trait Vector<T> {
static pure fn dim() -> uint;
pure fn index(&&index:uint) -> T;
pure fn add_f(&&value:T) -> self;
pure fn sub_f(&&value:T) -> self;
pure fn mul_f(&&value:T) -> self;
@ -125,11 +123,6 @@ pub impl Vec2: Vector<float> {
#[inline]
static pure fn dim() -> uint { 2 }
#[inline]
pure fn index(&&i: uint) -> float {
self.data[i]
}
#[inline]
pure fn add_f(&&value:float) -> Vec2 {
Vec2(self[0] + value,
@ -222,6 +215,13 @@ pub impl Vec2: Vector<float> {
#[inline] static pure fn identity() -> Vec2 { Vec2(1f, 1f) }
}
pub impl Vec2: Index<uint, float> {
#[inline]
pure fn index(+i: uint) -> float {
self.data[i]
}
}
pub impl Vec2: Neg<Vec2> {
#[inline]
pure fn neg() -> Vec2 {
@ -305,11 +305,6 @@ pub impl Vec3: Vector<float> {
#[inline]
static pure fn dim() -> uint { 3 }
#[inline]
pure fn index(&&i: uint) -> float {
self.data[i]
}
#[inline]
pure fn add_f(&&value:float) -> Vec3 {
Vec3(self[0] + value,
@ -414,6 +409,13 @@ pub impl Vec3: Vector<float> {
#[inline] static pure fn identity() -> Vec3 { Vec3(1f, 1f, 1f) }
}
pub impl Vec3: Index<uint, float> {
#[inline]
pure fn index(+i: uint) -> float {
self.data[i]
}
}
pub impl Vec3: Neg<Vec3> {
#[inline]
pure fn neg() -> Vec3 {
@ -494,11 +496,6 @@ pub impl Vec4: Vector<float> {
#[inline]
static pure fn dim() -> uint { 4 }
#[inline]
pure fn index(&&i: uint) -> float {
self.data[i]
}
#[inline]
pure fn add_f(&&value:float) -> Vec4 {
Vec4(self[0] + value,
@ -615,6 +612,13 @@ pub impl Vec4: Vector<float> {
#[inline] static pure fn identity() -> Vec4 { Vec4(1f, 1f, 1f, 1f) }
}
pub impl Vec4: Index<uint, float> {
#[inline]
pure fn index(+i: uint) -> float {
self.data[i]
}
}
pub impl Vec4: Neg<Vec4> {
#[inline]
pure fn neg() -> Vec4 {