Use iterators instead of Array::{map_mut, bimap_mut, fold}
This commit is contained in:
parent
00bd313b87
commit
c404092879
3 changed files with 24 additions and 43 deletions
|
@ -15,10 +15,9 @@
|
||||||
|
|
||||||
#[macro_escape];
|
#[macro_escape];
|
||||||
|
|
||||||
use std::vec::VecIterator;
|
use std::vec::{VecIterator, VecMutIterator};
|
||||||
|
|
||||||
pub trait Array<T, Slice> {
|
pub trait Array<T, Slice> {
|
||||||
fn len(&self) -> uint;
|
|
||||||
fn i<'a>(&'a self, i: uint) -> &'a T;
|
fn i<'a>(&'a self, i: uint) -> &'a T;
|
||||||
fn mut_i<'a>(&'a mut self, i: uint) -> &'a mut T;
|
fn mut_i<'a>(&'a mut self, i: uint) -> &'a mut T;
|
||||||
fn as_slice<'a>(&'a self) -> &'a Slice;
|
fn as_slice<'a>(&'a self) -> &'a Slice;
|
||||||
|
@ -26,48 +25,23 @@ pub trait Array<T, Slice> {
|
||||||
fn from_slice(slice: Slice) -> Self;
|
fn from_slice(slice: Slice) -> Self;
|
||||||
fn build(builder: &fn(i: uint) -> T) -> Self;
|
fn build(builder: &fn(i: uint) -> T) -> Self;
|
||||||
fn iter<'a>(&'a self) -> VecIterator<'a, T>;
|
fn iter<'a>(&'a self) -> VecIterator<'a, T>;
|
||||||
|
fn mut_iter<'a>(&'a mut self) -> VecMutIterator<'a, T>;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn map<U, SliceU, UU: Array<U, SliceU>>(&self, f: &fn(&T) -> U) -> UU {
|
fn map<U, SliceU, UU: Array<U, SliceU>>(&self, f: &fn(&T) -> U) -> UU {
|
||||||
Array::build(|i| f(self.i(i)))
|
Array::build(|i| f(self.i(i)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn map_mut(&mut self, f: &fn(&mut T)) {
|
|
||||||
for i in range(0, self.len()) {
|
|
||||||
f(self.mut_i(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn bimap<U, SliceU, UU: Array<U, SliceU>,
|
fn bimap<U, SliceU, UU: Array<U, SliceU>,
|
||||||
V, SliceV, VV: Array<V, SliceV>>(&self, other: &UU, f: &fn(&T, &U) -> V) -> VV {
|
V, SliceV, VV: Array<V, SliceV>>(&self, other: &UU, f: &fn(&T, &U) -> V) -> VV {
|
||||||
Array::build(|i| f(self.i(i), other.i(i)))
|
Array::build(|i| f(self.i(i), other.i(i)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn bimap_mut<U, SliceU, UU: Array<U, Slice>>(&mut self, other: &UU, f: &fn(&mut T, &U)) {
|
|
||||||
for i in range(0, self.len()) {
|
|
||||||
f(self.mut_i(i), other.i(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn fold<U>(&self, init: U, f: &fn(acc: &U, x: &T) -> U) -> U {
|
|
||||||
let mut acc = init;
|
|
||||||
for i in range(0, self.len()) {
|
|
||||||
acc = f(&acc, self.i(i));
|
|
||||||
}
|
|
||||||
acc
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! array(
|
macro_rules! array(
|
||||||
(impl<$S:ident> $Self:ty -> [$T:ty, ..$n:expr]) => (
|
(impl<$S:ident> $Self:ty -> [$T:ty, ..$n:expr]) => (
|
||||||
impl<$S> Array<$T, [$T,..$n]> for $Self {
|
impl<$S> Array<$T, [$T,..$n]> for $Self {
|
||||||
#[inline]
|
|
||||||
fn len(&self) -> uint { $n }
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn i<'a>(&'a self, i: uint) -> &'a $T {
|
fn i<'a>(&'a self, i: uint) -> &'a $T {
|
||||||
&'a self.as_slice()[i]
|
&'a self.as_slice()[i]
|
||||||
|
@ -97,7 +71,7 @@ macro_rules! array(
|
||||||
fn build(builder: &fn(i: uint) -> $T) -> $Self {
|
fn build(builder: &fn(i: uint) -> $T) -> $Self {
|
||||||
use std::unstable::intrinsics;
|
use std::unstable::intrinsics;
|
||||||
let mut s: [$T,..$n] = unsafe { intrinsics::uninit() };
|
let mut s: [$T,..$n] = unsafe { intrinsics::uninit() };
|
||||||
for i in range::<uint>(0, $n) {
|
for i in range(0u, $n) {
|
||||||
s[i] = builder(i);
|
s[i] = builder(i);
|
||||||
}
|
}
|
||||||
Array::from_slice(s)
|
Array::from_slice(s)
|
||||||
|
@ -107,6 +81,11 @@ macro_rules! array(
|
||||||
fn iter<'a>(&'a self) -> ::std::vec::VecIterator<'a, $T> {
|
fn iter<'a>(&'a self) -> ::std::vec::VecIterator<'a, $T> {
|
||||||
self.as_slice().iter()
|
self.as_slice().iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn mut_iter<'a>(&'a mut self) -> ::std::vec::VecMutIterator<'a, $T> {
|
||||||
|
self.as_mut_slice().mut_iter()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
@ -46,7 +46,7 @@ pub trait Matrix
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn swap_r(&mut self, a: uint, b: uint) {
|
fn swap_r(&mut self, a: uint, b: uint) {
|
||||||
self.map_mut(|c| c.swap(a, b));
|
for c in self.mut_iter() { c.swap(a, b) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
@ -13,6 +13,8 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
use std::num::{zero, one};
|
||||||
|
|
||||||
use traits::alg::Field;
|
use traits::alg::Field;
|
||||||
use traits::alg::VectorSpace;
|
use traits::alg::VectorSpace;
|
||||||
use traits::alg::Array;
|
use traits::alg::Array;
|
||||||
|
@ -37,19 +39,19 @@ pub trait VectorExt
|
||||||
#[inline] fn div_v(&self, other: &Self) -> Self { self.bimap(other, |a, b| a.div(b) ) }
|
#[inline] fn div_v(&self, other: &Self) -> Self { self.bimap(other, |a, b| a.div(b) ) }
|
||||||
#[inline] fn rem_v(&self, other: &Self) -> Self { self.bimap(other, |a, b| a.rem(b) ) }
|
#[inline] fn rem_v(&self, other: &Self) -> Self { self.bimap(other, |a, b| a.rem(b) ) }
|
||||||
|
|
||||||
#[inline] fn neg_self(&mut self) { self.map_mut(|x| *x = -*x); }
|
#[inline] fn neg_self(&mut self) { for x in self.mut_iter() { *x = x.neg() } }
|
||||||
#[inline] fn add_self_s(&mut self, s: S) { self.map_mut(|x| *x = x.add(&s)); }
|
#[inline] fn add_self_s(&mut self, s: S) { for x in self.mut_iter() { *x = x.add(&s) } }
|
||||||
#[inline] fn sub_self_s(&mut self, s: S) { self.map_mut(|x| *x = x.sub(&s)); }
|
#[inline] fn sub_self_s(&mut self, s: S) { for x in self.mut_iter() { *x = x.sub(&s) } }
|
||||||
#[inline] fn mul_self_s(&mut self, s: S) { self.map_mut(|x| *x = x.mul(&s)); }
|
#[inline] fn mul_self_s(&mut self, s: S) { for x in self.mut_iter() { *x = x.mul(&s) } }
|
||||||
#[inline] fn div_self_s(&mut self, s: S) { self.map_mut(|x| *x = x.div(&s)); }
|
#[inline] fn div_self_s(&mut self, s: S) { for x in self.mut_iter() { *x = x.div(&s) } }
|
||||||
#[inline] fn rem_self_s(&mut self, s: S) { self.map_mut(|x| *x = x.rem(&s)); }
|
#[inline] fn rem_self_s(&mut self, s: S) { for x in self.mut_iter() { *x = x.rem(&s) } }
|
||||||
|
|
||||||
#[inline] fn add_self_v(&mut self, other: &Self) { self.bimap_mut::<S, Slice, Self>(other, |a, b| *a = a.add(b)) }
|
#[inline] fn add_self_v(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.add(b) } }
|
||||||
#[inline] fn sub_self_v(&mut self, other: &Self) { self.bimap_mut::<S, Slice, Self>(other, |a, b| *a = a.sub(b)) }
|
#[inline] fn sub_self_v(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.sub(b) } }
|
||||||
#[inline] fn mul_self_v(&mut self, other: &Self) { self.bimap_mut::<S, Slice, Self>(other, |a, b| *a = a.mul(b)) }
|
#[inline] fn mul_self_v(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.mul(b) } }
|
||||||
#[inline] fn div_self_v(&mut self, other: &Self) { self.bimap_mut::<S, Slice, Self>(other, |a, b| *a = a.div(b)) }
|
#[inline] fn div_self_v(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.div(b) } }
|
||||||
#[inline] fn rem_self_v(&mut self, other: &Self) { self.bimap_mut::<S, Slice, Self>(other, |a, b| *a = a.rem(b)) }
|
#[inline] fn rem_self_v(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.rem(b) } }
|
||||||
|
|
||||||
#[inline] fn comp_add(&self) -> S { fail!() }
|
#[inline] fn comp_add(&self) -> S { self.iter().fold(zero::<S>(), |a, b| a.add(b)) }
|
||||||
#[inline] fn comp_mul(&self) -> S { fail!() }
|
#[inline] fn comp_mul(&self) -> S { self.iter().fold(one::<S>(), |a, b| a.mul(b)) }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue