From 3b814b462f15d07ef487d72e9ce97463b5bf224d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 13 Sep 2013 11:11:04 +0200 Subject: [PATCH] Add unsafe methods to initialize a DVec or DMat without initialization. --- src/dmat.rs | 23 +++++++++++++++++++---- src/dvec.rs | 13 +++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/dmat.rs b/src/dmat.rs index 58dfdeb1..e595d195 100644 --- a/src/dmat.rs +++ b/src/dmat.rs @@ -17,6 +17,21 @@ pub struct DMat { priv mij: ~[N] } +impl DMat { + /// Creates an uninitialized matrix. + #[inline] + pub unsafe fn new_uninitialized(nrows: uint, ncols: uint) -> DMat { + let mut vec = vec::with_capacity(nrows * ncols); + vec::raw::set_len(&mut vec, nrows * ncols); + + DMat { + nrows: nrows, + ncols: ncols, + mij: vec + } + } +} + impl DMat { /// Builds a matrix filled with zeros. /// @@ -95,7 +110,7 @@ impl DMat { /// components. #[inline] pub fn new_identity(dim: uint) -> DMat { - let mut res = DMat::new_zeros(dim, dim); + let mut res = DMat::new_zeros(dim, dim); for i in range(0u, dim) { let _1: N = One::one(); @@ -142,7 +157,7 @@ impl + Add + Zero> Mul, DMat> for DMat fn mul(&self, other: &DMat) -> DMat { assert!(self.ncols == other.nrows); - let mut res = DMat::new_zeros(self.nrows, other.ncols); + let mut res = unsafe { DMat::new_uninitialized(self.nrows, other.ncols) }; for i in range(0u, self.nrows) { for j in range(0u, other.ncols) { @@ -165,7 +180,7 @@ RMul> for DMat { fn rmul(&self, other: &DVec) -> DVec { assert!(self.ncols == other.at.len()); - let mut res : DVec = DVec::new_zeros(self.nrows); + let mut res : DVec = unsafe { DVec::new_uninitialized(self.nrows) }; for i in range(0u, self.nrows) { let mut acc: N = Zero::zero(); @@ -186,7 +201,7 @@ LMul> for DMat { fn lmul(&self, other: &DVec) -> DVec { assert!(self.nrows == other.at.len()); - let mut res : DVec = DVec::new_zeros(self.ncols); + let mut res : DVec = unsafe { DVec::new_uninitialized(self.ncols) }; for i in range(0u, self.ncols) { let mut acc: N = Zero::zero(); diff --git a/src/dvec.rs b/src/dvec.rs index 7d00da44..87d3f817 100644 --- a/src/dvec.rs +++ b/src/dvec.rs @@ -54,6 +54,19 @@ impl DVec { } } +impl DVec { + /// Creates an uninitialized vec. + #[inline] + pub unsafe fn new_uninitialized(dim: uint) -> DVec { + let mut vec = vec::with_capacity(dim); + vec::raw::set_len(&mut vec, dim); + + DVec { + at: vec + } + } +} + impl DVec { /// Builds a vector filled with a constant. #[inline]