Merge pull request #242 from bjz/index-operators

Clean up index operator usages
This commit is contained in:
Brendan Zabarauskas 2015-10-04 13:02:05 +11:00
commit c8b7db0a73
2 changed files with 112 additions and 112 deletions

View file

@ -21,25 +21,25 @@ use std::ops::*;
pub trait Array1<Element: Copy>: Index<usize, Output=Element> + IndexMut<usize, Output=Element> { pub trait Array1<Element: Copy>: Index<usize, Output=Element> + IndexMut<usize, Output=Element> {
/// Get the pointer to the first element of the array. /// Get the pointer to the first element of the array.
fn ptr<'a>(&'a self) -> &'a Element { fn ptr<'a>(&'a self) -> &'a Element {
&(*self)[0] &self[0]
} }
/// Get a mutable pointer to the first element of the array. /// Get a mutable pointer to the first element of the array.
fn mut_ptr<'a>(&'a mut self) -> &'a mut Element { fn mut_ptr<'a>(&'a mut self) -> &'a mut Element {
&mut (*self)[0] &mut self[0]
} }
/// Swap the elements at indices `i` and `j` in-place. /// Swap the elements at indices `i` and `j` in-place.
#[inline] #[inline]
fn swap_elems(&mut self, i: usize, j: usize) { fn swap_elems(&mut self, i: usize, j: usize) {
// Yeah, ok borrow checker I know what I'm doing here // Yeah, ok borrow checker I know what I'm doing here
unsafe { ptr::swap(&mut (*self)[i], &mut (*self)[j]) }; unsafe { ptr::swap(&mut self[i], &mut self[j]) };
} }
/// Replace an element in the array. /// Replace an element in the array.
#[inline] #[inline]
fn replace_elem(&mut self, i: usize, src: Element) -> Element { fn replace_elem(&mut self, i: usize, src: Element) -> Element {
mem::replace(&mut (*self)[i], src) mem::replace(&mut self[i], src)
} }
} }
@ -48,24 +48,24 @@ pub trait Array2<Column: Array1<Element>+'static, Row: Array1<Element>, Element:
Index<usize, Output=Column> + IndexMut<usize, Output=Column> { Index<usize, Output=Column> + IndexMut<usize, Output=Column> {
/// Get the pointer to the first element of the array. /// Get the pointer to the first element of the array.
fn ptr<'a>(&'a self) -> &'a Element { fn ptr<'a>(&'a self) -> &'a Element {
&(*self)[0][0] &self[0][0]
} }
/// Get a mutable pointer to the first element of the array. /// Get a mutable pointer to the first element of the array.
fn mut_ptr<'a>(&'a mut self) -> &'a mut Element { fn mut_ptr<'a>(&'a mut self) -> &'a mut Element {
&mut (*self)[0][0] &mut self[0][0]
} }
/// Swap two columns of this array. /// Swap two columns of this array.
#[inline] #[inline]
fn swap_cols(&mut self, a: usize, b: usize) { fn swap_cols(&mut self, a: usize, b: usize) {
unsafe { ptr::swap(&mut (*self)[a], &mut (*self)[b]) }; unsafe { ptr::swap(&mut self[a], &mut self[b]) };
} }
/// Replace a column in the array. /// Replace a column in the array.
#[inline] #[inline]
fn replace_col(&mut self, c: usize, src: Column) -> Column { fn replace_col(&mut self, c: usize, src: Column) -> Column {
mem::replace(&mut (*self)[c], src) mem::replace(&mut self[c], src)
} }
/// Get a row from this array by-value. /// Get a row from this array by-value.
@ -79,6 +79,6 @@ pub trait Array2<Column: Array1<Element>+'static, Row: Array1<Element>, Element:
fn swap_elems(&mut self, a: (usize, usize), b: (usize, usize)) { fn swap_elems(&mut self, a: (usize, usize), b: (usize, usize)) {
let (ac, ar) = a; let (ac, ar) = a;
let (bc, br) = b; let (bc, br) = b;
unsafe { ptr::swap(&mut (*self)[ac][ar], &mut (*self)[bc][br]) }; unsafe { ptr::swap(&mut self[ac][ar], &mut self[bc][br]) };
} }
} }

View file

@ -84,8 +84,8 @@ impl<S: Copy + Neg<Output = S>> Matrix2<S> {
/// Negate this `Matrix2` in-place. /// Negate this `Matrix2` in-place.
#[inline] #[inline]
pub fn neg_self(&mut self) { pub fn neg_self(&mut self) {
(&mut self[0]).neg_self(); self[0].neg_self();
(&mut self[1]).neg_self(); self[1].neg_self();
} }
} }
@ -186,9 +186,9 @@ impl<S: Copy + Neg<Output = S>> Matrix3<S> {
/// Negate this `Matrix3` in-place. /// Negate this `Matrix3` in-place.
#[inline] #[inline]
pub fn neg_self(&mut self) { pub fn neg_self(&mut self) {
(&mut self[0]).neg_self(); self[0].neg_self();
(&mut self[1]).neg_self(); self[1].neg_self();
(&mut self[2]).neg_self(); self[2].neg_self();
} }
} }
@ -242,10 +242,10 @@ impl<S: Copy + Neg<Output = S>> Matrix4<S> {
/// Negate this `Matrix4` in-place. /// Negate this `Matrix4` in-place.
#[inline] #[inline]
pub fn neg_self(&mut self) { pub fn neg_self(&mut self) {
(&mut self[0]).neg_self(); self[0].neg_self();
(&mut self[1]).neg_self(); self[1].neg_self();
(&mut self[2]).neg_self(); self[2].neg_self();
(&mut self[3]).neg_self(); self[3].neg_self();
} }
} }
@ -368,8 +368,8 @@ impl<S: Copy + 'static> Array2<Vector2<S>, Vector2<S>, S> for Matrix2<S> {
#[inline] #[inline]
fn swap_rows(&mut self, a: usize, b: usize) { fn swap_rows(&mut self, a: usize, b: usize) {
(&mut self[0]).swap_elems(a, b); self[0].swap_elems(a, b);
(&mut self[1]).swap_elems(a, b); self[1].swap_elems(a, b);
} }
} }
@ -383,9 +383,9 @@ impl<S: Copy + 'static> Array2<Vector3<S>, Vector3<S>, S> for Matrix3<S> {
#[inline] #[inline]
fn swap_rows(&mut self, a: usize, b: usize) { fn swap_rows(&mut self, a: usize, b: usize) {
(&mut self[0]).swap_elems(a, b); self[0].swap_elems(a, b);
(&mut self[1]).swap_elems(a, b); self[1].swap_elems(a, b);
(&mut self[2]).swap_elems(a, b); self[2].swap_elems(a, b);
} }
} }
@ -400,10 +400,10 @@ impl<S: Copy + 'static> Array2<Vector4<S>, Vector4<S>, S> for Matrix4<S> {
#[inline] #[inline]
fn swap_rows(&mut self, a: usize, b: usize) { fn swap_rows(&mut self, a: usize, b: usize) {
(&mut self[0]).swap_elems(a, b); self[0].swap_elems(a, b);
(&mut self[1]).swap_elems(a, b); self[1].swap_elems(a, b);
(&mut self[2]).swap_elems(a, b); self[2].swap_elems(a, b);
(&mut self[3]).swap_elems(a, b); self[3].swap_elems(a, b);
} }
} }
@ -430,32 +430,32 @@ impl<S: BaseFloat> Matrix<S, Vector2<S>> for Matrix2<S> {
#[inline] #[inline]
fn mul_self_s(&mut self, s: S) { fn mul_self_s(&mut self, s: S) {
(&mut self[0]).mul_self_s(s); self[0].mul_self_s(s);
(&mut self[1]).mul_self_s(s); self[1].mul_self_s(s);
} }
#[inline] #[inline]
fn div_self_s(&mut self, s: S) { fn div_self_s(&mut self, s: S) {
(&mut self[0]).div_self_s(s); self[0].div_self_s(s);
(&mut self[1]).div_self_s(s); self[1].div_self_s(s);
} }
#[inline] #[inline]
fn rem_self_s(&mut self, s: S) { fn rem_self_s(&mut self, s: S) {
(&mut self[0]).rem_self_s(s); self[0].rem_self_s(s);
(&mut self[1]).rem_self_s(s); self[1].rem_self_s(s);
} }
#[inline] #[inline]
fn add_self_m(&mut self, m: &Matrix2<S>) { fn add_self_m(&mut self, m: &Matrix2<S>) {
(&mut self[0]).add_self_v(&m[0]); self[0].add_self_v(&m[0]);
(&mut self[1]).add_self_v(&m[1]); self[1].add_self_v(&m[1]);
} }
#[inline] #[inline]
fn sub_self_m(&mut self, m: &Matrix2<S>) { fn sub_self_m(&mut self, m: &Matrix2<S>) {
(&mut self[0]).sub_self_v(&m[0]); self[0].sub_self_v(&m[0]);
(&mut self[1]).sub_self_v(&m[1]); self[1].sub_self_v(&m[1]);
} }
fn transpose(&self) -> Matrix2<S> { fn transpose(&self) -> Matrix2<S> {
@ -492,15 +492,15 @@ impl<S: BaseFloat> Matrix<S, Vector2<S>> for Matrix2<S> {
#[inline] #[inline]
fn is_diagonal(&self) -> bool { fn is_diagonal(&self) -> bool {
(&self[0][1]).approx_eq(&S::zero()) && self[0][1].approx_eq(&S::zero()) &&
(&self[1][0]).approx_eq(&S::zero()) self[1][0].approx_eq(&S::zero())
} }
#[inline] #[inline]
fn is_symmetric(&self) -> bool { fn is_symmetric(&self) -> bool {
(&self[0][1]).approx_eq(&self[1][0]) && self[0][1].approx_eq(&self[1][0]) &&
(&self[1][0]).approx_eq(&self[0][1]) self[1][0].approx_eq(&self[0][1])
} }
} }
@ -529,37 +529,37 @@ impl<S: BaseFloat> Matrix<S, Vector3<S>> for Matrix3<S> {
#[inline] #[inline]
fn mul_self_s(&mut self, s: S) { fn mul_self_s(&mut self, s: S) {
(&mut self[0]).mul_self_s(s); self[0].mul_self_s(s);
(&mut self[1]).mul_self_s(s); self[1].mul_self_s(s);
(&mut self[2]).mul_self_s(s); self[2].mul_self_s(s);
} }
#[inline] #[inline]
fn div_self_s(&mut self, s: S) { fn div_self_s(&mut self, s: S) {
(&mut self[0]).div_self_s(s); self[0].div_self_s(s);
(&mut self[1]).div_self_s(s); self[1].div_self_s(s);
(&mut self[2]).div_self_s(s); self[2].div_self_s(s);
} }
#[inline] #[inline]
fn rem_self_s(&mut self, s: S) { fn rem_self_s(&mut self, s: S) {
(&mut self[0]).rem_self_s(s); self[0].rem_self_s(s);
(&mut self[1]).rem_self_s(s); self[1].rem_self_s(s);
(&mut self[2]).rem_self_s(s); self[2].rem_self_s(s);
} }
#[inline] #[inline]
fn add_self_m(&mut self, m: &Matrix3<S>) { fn add_self_m(&mut self, m: &Matrix3<S>) {
(&mut self[0]).add_self_v(&m[0]); self[0].add_self_v(&m[0]);
(&mut self[1]).add_self_v(&m[1]); self[1].add_self_v(&m[1]);
(&mut self[2]).add_self_v(&m[2]); self[2].add_self_v(&m[2]);
} }
#[inline] #[inline]
fn sub_self_m(&mut self, m: &Matrix3<S>) { fn sub_self_m(&mut self, m: &Matrix3<S>) {
(&mut self[0]).sub_self_v(&m[0]); self[0].sub_self_v(&m[0]);
(&mut self[1]).sub_self_v(&m[1]); self[1].sub_self_v(&m[1]);
(&mut self[2]).sub_self_v(&m[2]); self[2].sub_self_v(&m[2]);
} }
fn transpose(&self) -> Matrix3<S> { fn transpose(&self) -> Matrix3<S> {
@ -598,25 +598,25 @@ impl<S: BaseFloat> Matrix<S, Vector3<S>> for Matrix3<S> {
} }
fn is_diagonal(&self) -> bool { fn is_diagonal(&self) -> bool {
(&self[0][1]).approx_eq(&S::zero()) && self[0][1].approx_eq(&S::zero()) &&
(&self[0][2]).approx_eq(&S::zero()) && self[0][2].approx_eq(&S::zero()) &&
(&self[1][0]).approx_eq(&S::zero()) && self[1][0].approx_eq(&S::zero()) &&
(&self[1][2]).approx_eq(&S::zero()) && self[1][2].approx_eq(&S::zero()) &&
(&self[2][0]).approx_eq(&S::zero()) && self[2][0].approx_eq(&S::zero()) &&
(&self[2][1]).approx_eq(&S::zero()) self[2][1].approx_eq(&S::zero())
} }
fn is_symmetric(&self) -> bool { fn is_symmetric(&self) -> bool {
(&self[0][1]).approx_eq(&self[1][0]) && self[0][1].approx_eq(&self[1][0]) &&
(&self[0][2]).approx_eq(&self[2][0]) && self[0][2].approx_eq(&self[2][0]) &&
(&self[1][0]).approx_eq(&self[0][1]) && self[1][0].approx_eq(&self[0][1]) &&
(&self[1][2]).approx_eq(&self[2][1]) && self[1][2].approx_eq(&self[2][1]) &&
(&self[2][0]).approx_eq(&self[0][2]) && self[2][0].approx_eq(&self[0][2]) &&
(&self[2][1]).approx_eq(&self[1][2]) self[2][1].approx_eq(&self[1][2])
} }
} }
@ -647,42 +647,42 @@ impl<S: BaseFloat> Matrix<S, Vector4<S>> for Matrix4<S> {
#[inline] #[inline]
fn mul_self_s(&mut self, s: S) { fn mul_self_s(&mut self, s: S) {
(&mut self[0]).mul_self_s(s); self[0].mul_self_s(s);
(&mut self[1]).mul_self_s(s); self[1].mul_self_s(s);
(&mut self[2]).mul_self_s(s); self[2].mul_self_s(s);
(&mut self[3]).mul_self_s(s); self[3].mul_self_s(s);
} }
#[inline] #[inline]
fn div_self_s(&mut self, s: S) { fn div_self_s(&mut self, s: S) {
(&mut self[0]).div_self_s(s); self[0].div_self_s(s);
(&mut self[1]).div_self_s(s); self[1].div_self_s(s);
(&mut self[2]).div_self_s(s); self[2].div_self_s(s);
(&mut self[3]).div_self_s(s); self[3].div_self_s(s);
} }
#[inline] #[inline]
fn rem_self_s(&mut self, s: S) { fn rem_self_s(&mut self, s: S) {
(&mut self[0]).rem_self_s(s); self[0].rem_self_s(s);
(&mut self[1]).rem_self_s(s); self[1].rem_self_s(s);
(&mut self[2]).rem_self_s(s); self[2].rem_self_s(s);
(&mut self[3]).rem_self_s(s); self[3].rem_self_s(s);
} }
#[inline] #[inline]
fn add_self_m(&mut self, m: &Matrix4<S>) { fn add_self_m(&mut self, m: &Matrix4<S>) {
(&mut self[0]).add_self_v(&m[0]); self[0].add_self_v(&m[0]);
(&mut self[1]).add_self_v(&m[1]); self[1].add_self_v(&m[1]);
(&mut self[2]).add_self_v(&m[2]); self[2].add_self_v(&m[2]);
(&mut self[3]).add_self_v(&m[3]); self[3].add_self_v(&m[3]);
} }
#[inline] #[inline]
fn sub_self_m(&mut self, m: &Matrix4<S>) { fn sub_self_m(&mut self, m: &Matrix4<S>) {
(&mut self[0]).sub_self_v(&m[0]); self[0].sub_self_v(&m[0]);
(&mut self[1]).sub_self_v(&m[1]); self[1].sub_self_v(&m[1]);
(&mut self[2]).sub_self_v(&m[2]); self[2].sub_self_v(&m[2]);
(&mut self[3]).sub_self_v(&m[3]); self[3].sub_self_v(&m[3]);
} }
fn transpose(&self) -> Matrix4<S> { fn transpose(&self) -> Matrix4<S> {
@ -754,39 +754,39 @@ impl<S: BaseFloat> Matrix<S, Vector4<S>> for Matrix4<S> {
} }
fn is_diagonal(&self) -> bool { fn is_diagonal(&self) -> bool {
(&self[0][1]).approx_eq(&S::zero()) && self[0][1].approx_eq(&S::zero()) &&
(&self[0][2]).approx_eq(&S::zero()) && self[0][2].approx_eq(&S::zero()) &&
(&self[0][3]).approx_eq(&S::zero()) && self[0][3].approx_eq(&S::zero()) &&
(&self[1][0]).approx_eq(&S::zero()) && self[1][0].approx_eq(&S::zero()) &&
(&self[1][2]).approx_eq(&S::zero()) && self[1][2].approx_eq(&S::zero()) &&
(&self[1][3]).approx_eq(&S::zero()) && self[1][3].approx_eq(&S::zero()) &&
(&self[2][0]).approx_eq(&S::zero()) && self[2][0].approx_eq(&S::zero()) &&
(&self[2][1]).approx_eq(&S::zero()) && self[2][1].approx_eq(&S::zero()) &&
(&self[2][3]).approx_eq(&S::zero()) && self[2][3].approx_eq(&S::zero()) &&
(&self[3][0]).approx_eq(&S::zero()) && self[3][0].approx_eq(&S::zero()) &&
(&self[3][1]).approx_eq(&S::zero()) && self[3][1].approx_eq(&S::zero()) &&
(&self[3][2]).approx_eq(&S::zero()) self[3][2].approx_eq(&S::zero())
} }
fn is_symmetric(&self) -> bool { fn is_symmetric(&self) -> bool {
(&self[0][1]).approx_eq(&self[1][0]) && self[0][1].approx_eq(&self[1][0]) &&
(&self[0][2]).approx_eq(&self[2][0]) && self[0][2].approx_eq(&self[2][0]) &&
(&self[0][3]).approx_eq(&self[3][0]) && self[0][3].approx_eq(&self[3][0]) &&
(&self[1][0]).approx_eq(&self[0][1]) && self[1][0].approx_eq(&self[0][1]) &&
(&self[1][2]).approx_eq(&self[2][1]) && self[1][2].approx_eq(&self[2][1]) &&
(&self[1][3]).approx_eq(&self[3][1]) && self[1][3].approx_eq(&self[3][1]) &&
(&self[2][0]).approx_eq(&self[0][2]) && self[2][0].approx_eq(&self[0][2]) &&
(&self[2][1]).approx_eq(&self[1][2]) && self[2][1].approx_eq(&self[1][2]) &&
(&self[2][3]).approx_eq(&self[3][2]) && self[2][3].approx_eq(&self[3][2]) &&
(&self[3][0]).approx_eq(&self[0][3]) && self[3][0].approx_eq(&self[0][3]) &&
(&self[3][1]).approx_eq(&self[1][3]) && self[3][1].approx_eq(&self[1][3]) &&
(&self[3][2]).approx_eq(&self[2][3]) self[3][2].approx_eq(&self[2][3])
} }
} }