Skip to content

Commit 9738c2a

Browse files
committed
test: Rewrite nbody and spectralnorm shootout benchmarks
1 parent 90b65c8 commit 9738c2a

File tree

6 files changed

+206
-306
lines changed

6 files changed

+206
-306
lines changed

src/libcore/core.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pub use vec::{ImmutableEqVector, ImmutableCopyableVector};
9898
pub use vec::{OwnedVector, OwnedCopyableVector};
9999
pub use iter::{BaseIter, ExtendedIter, EqIter, CopyableIter};
100100
pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times};
101+
pub use iter::{ExtendedMutableIter};
101102

102103
pub use num::{Num, NumCast};
103104
pub use ptr::Ptr;

src/libcore/iter.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ pub trait ExtendedIter<A> {
4545
fn flat_map_to_vec<B,IB: BaseIter<B>>(&self, op: &fn(&A) -> IB) -> ~[B];
4646
}
4747

48+
pub trait ExtendedMutableIter<A> {
49+
fn eachi_mut(&mut self, blk: &fn(uint, &mut A) -> bool);
50+
}
51+
4852
pub trait EqIter<A:Eq> {
4953
fn contains(&self, x: &A) -> bool;
5054
fn count(&self, x: &A) -> uint;

src/libcore/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use container::{Container, Mutable, Map, Set};
3333
pub use hash::Hash;
3434
pub use iter::{BaseIter, ReverseIter, MutableIter, ExtendedIter, EqIter};
3535
pub use iter::{CopyableIter, CopyableOrderedIter, CopyableNonstrictIter};
36-
pub use iter::Times;
36+
pub use iter::{Times, ExtendedMutableIter};
3737
pub use num::{Num, NumCast};
3838
pub use path::GenericPath;
3939
pub use path::Path;

src/libcore/vec.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,13 +1388,19 @@ pub fn each<'r,T>(v: &'r [T], f: &fn(&'r T) -> bool) {
13881388
/// to mutate the contents as you iterate.
13891389
#[inline(always)]
13901390
pub fn each_mut<'r,T>(v: &'r mut [T], f: &fn(elem: &'r mut T) -> bool) {
1391-
let mut i = 0;
1392-
let n = v.len();
1393-
while i < n {
1394-
if !f(&mut v[i]) {
1395-
return;
1391+
do vec::as_mut_buf(v) |p, n| {
1392+
let mut n = n;
1393+
let mut p = p;
1394+
while n > 0 {
1395+
unsafe {
1396+
let q: &'r mut T = cast::transmute_mut_region(&mut *p);
1397+
if !f(q) {
1398+
break;
1399+
}
1400+
p = p.offset(1);
1401+
}
1402+
n -= 1;
13961403
}
1397-
i += 1;
13981404
}
13991405
}
14001406
@@ -1426,6 +1432,22 @@ pub fn eachi<'r,T>(v: &'r [T], f: &fn(uint, v: &'r T) -> bool) {
14261432
}
14271433
}
14281434
1435+
/**
1436+
* Iterates over a mutable vector's elements and indices
1437+
*
1438+
* Return true to continue, false to break.
1439+
*/
1440+
#[inline(always)]
1441+
pub fn eachi_mut<'r,T>(v: &'r mut [T], f: &fn(uint, v: &'r mut T) -> bool) {
1442+
let mut i = 0;
1443+
for each_mut(v) |p| {
1444+
if !f(i, p) {
1445+
return;
1446+
}
1447+
i += 1;
1448+
}
1449+
}
1450+
14291451
/**
14301452
* Iterates over a vector's elements in reverse
14311453
*
@@ -2654,6 +2676,13 @@ impl<'self,A> iter::ExtendedIter<A> for &'self [A] {
26542676
}
26552677
}
26562678

2679+
impl<'self,A> iter::ExtendedMutableIter<A> for &'self mut [A] {
2680+
#[inline(always)]
2681+
pub fn eachi_mut(&mut self, blk: &fn(uint, v: &mut A) -> bool) {
2682+
eachi_mut(*self, blk)
2683+
}
2684+
}
2685+
26572686
// FIXME(#4148): This should be redundant
26582687
impl<A> iter::ExtendedIter<A> for ~[A] {
26592688
pub fn eachi(&self, blk: &fn(uint, v: &A) -> bool) {

0 commit comments

Comments
 (0)