Skip to content

Commit fe74a1c

Browse files
committed
core: rename vec::rev_each{,i} to vec::each{,i}_reverse
I'm making this change because the _reverse suffix is more commonly used in libcore/libstd.
1 parent 4cb9ca9 commit fe74a1c

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

src/libcore/vec.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ pub pure fn foldl<T, U>(z: T, v: &[U], p: &fn(t: T, u: &U) -> T) -> T {
10051005
*/
10061006
pub pure fn foldr<T, U: Copy>(v: &[T], z: U, p: &fn(t: &T, u: U) -> U) -> U {
10071007
let mut accum = z;
1008-
for rev_each(v) |elt| {
1008+
for v.each_reverse |elt| {
10091009
accum = p(elt, accum);
10101010
}
10111011
accum
@@ -1411,8 +1411,8 @@ pub pure fn eachi<T>(v: &'r [T], f: &fn(uint, v: &'r T) -> bool) {
14111411
* Return true to continue, false to break.
14121412
*/
14131413
#[inline(always)]
1414-
pub pure fn rev_each<T>(v: &'r [T], blk: &fn(v: &'r T) -> bool) {
1415-
rev_eachi(v, |_i, v| blk(v))
1414+
pub pure fn each_reverse<T>(v: &'r [T], blk: &fn(v: &'r T) -> bool) {
1415+
eachi_reverse(v, |_i, v| blk(v))
14161416
}
14171417

14181418
/**
@@ -1421,7 +1421,7 @@ pub pure fn rev_each<T>(v: &'r [T], blk: &fn(v: &'r T) -> bool) {
14211421
* Return true to continue, false to break.
14221422
*/
14231423
#[inline(always)]
1424-
pub pure fn rev_eachi<T>(v: &'r [T], blk: &fn(i: uint, v: &'r T) -> bool) {
1424+
pub pure fn eachi_reverse<T>(v: &'r [T], blk: &fn(i: uint, v: &'r T) -> bool) {
14251425
let mut i = v.len();
14261426
while i > 0 {
14271427
i -= 1;
@@ -1736,6 +1736,8 @@ pub trait ImmutableVector<T> {
17361736
pure fn initn(&self, n: uint) -> &'self [T];
17371737
pure fn last(&self) -> &'self T;
17381738
pure fn last_opt(&self) -> Option<&'self T>;
1739+
pure fn each_reverse(&self, blk: &fn(&T) -> bool);
1740+
pure fn eachi_reverse(&self, blk: &fn(uint, &T) -> bool);
17391741
pure fn foldr<U: Copy>(&self, z: U, p: &fn(t: &T, u: U) -> U) -> U;
17401742
pure fn map<U>(&self, f: &fn(t: &T) -> U) -> ~[U];
17411743
pure fn mapi<U>(&self, f: &fn(uint, t: &T) -> U) -> ~[U];
@@ -1785,6 +1787,18 @@ impl<T> ImmutableVector<T> for &'self [T] {
17851787
#[inline]
17861788
pure fn last_opt(&self) -> Option<&'self T> { last_opt(*self) }
17871789

1790+
/// Iterates over a vector's elements in reverse.
1791+
#[inline]
1792+
pure fn each_reverse(&self, blk: &fn(&T) -> bool) {
1793+
each_reverse(*self, blk)
1794+
}
1795+
1796+
/// Iterates over a vector's elements and indices in reverse.
1797+
#[inline]
1798+
pure fn eachi_reverse(&self, blk: &fn(uint, &T) -> bool) {
1799+
eachi_reverse(*self, blk)
1800+
}
1801+
17881802
/// Reduce a vector from right to left
17891803
#[inline]
17901804
pure fn foldr<U:Copy>(&self, z: U, p: &fn(t: &T, u: U) -> U) -> U {
@@ -3131,33 +3145,42 @@ mod tests {
31313145
}
31323146

31333147
#[test]
3134-
fn test_reach_empty() {
3135-
for rev_each::<int>(~[]) |_v| {
3148+
fn test_each_reverse_empty() {
3149+
let v: ~[int] = ~[];
3150+
for v.each_reverse |_v| {
31363151
fail!(); // should never execute
31373152
}
31383153
}
31393154

31403155
#[test]
3141-
fn test_reach_nonempty() {
3156+
fn test_each_reverse_nonempty() {
31423157
let mut i = 0;
3143-
for rev_each(~[1, 2, 3]) |v| {
3158+
for each_reverse(~[1, 2, 3]) |v| {
31443159
if i == 0 { fail_unless!(*v == 3); }
31453160
i += *v
31463161
}
31473162
fail_unless!(i == 6);
31483163
}
31493164

31503165
#[test]
3151-
fn test_reachi() {
3166+
fn test_eachi_reverse() {
31523167
let mut i = 0;
3153-
for rev_eachi(~[0, 1, 2]) |j, v| {
3168+
for eachi_reverse(~[0, 1, 2]) |j, v| {
31543169
if i == 0 { fail_unless!(*v == 2); }
31553170
fail_unless!(j == *v as uint);
31563171
i += *v;
31573172
}
31583173
fail_unless!(i == 3);
31593174
}
31603175

3176+
#[test]
3177+
fn test_eachi_reverse_empty() {
3178+
let v: ~[int] = ~[];
3179+
for v.eachi_reverse |_i, _v| {
3180+
fail!(); // should never execute
3181+
}
3182+
}
3183+
31613184
#[test]
31623185
fn test_each_permutation() {
31633186
let mut results: ~[~[int]];

src/librustc/middle/trans/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ pub fn trans_block_cleanups_(bcx: block,
12641264
bcx.ccx().sess.opts.debugging_opts & session::no_landing_pads != 0;
12651265
if bcx.unreachable && !no_lpads { return bcx; }
12661266
let mut bcx = bcx;
1267-
for vec::rev_each(cleanups) |cu| {
1267+
for cleanups.each_reverse |cu| {
12681268
match *cu {
12691269
clean(cfn, cleanup_type) | clean_temp(_, cfn, cleanup_type) => {
12701270
// Some types don't need to be cleaned up during

src/libstd/bigint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ pub impl BigUint {
324324
if s_len < o_len { return -1; }
325325
if s_len > o_len { return 1; }
326326

327-
for vec::rev_eachi(self.data) |i, elm| {
327+
for self.data.eachi_reverse |i, elm| {
328328
match (*elm, other.data[i]) {
329329
(l, r) if l < r => return -1,
330330
(l, r) if l > r => return 1,
@@ -387,7 +387,7 @@ pub impl BigUint {
387387
let bn = *b.data.last();
388388
let mut d = ~[];
389389
let mut carry = 0;
390-
for vec::rev_each(an) |elt| {
390+
for an.each_reverse |elt| {
391391
let ai = BigDigit::to_uint(carry, *elt);
392392
let di = ai / (bn as uint);
393393
fail_unless!(di < BigDigit::base);
@@ -499,7 +499,7 @@ pub impl BigUint {
499499

500500
let mut borrow = 0;
501501
let mut shifted = ~[];
502-
for vec::rev_each(self.data) |elem| {
502+
for self.data.each_reverse |elem| {
503503
shifted = ~[(*elem >> n_bits) | borrow] + shifted;
504504
borrow = *elem << (uint::bits - n_bits);
505505
}

0 commit comments

Comments
 (0)