std: Replace for with do { .. } expr where internal iterators are used
This commit is contained in:
@@ -784,11 +784,12 @@ impl<
|
|||||||
fn encode(&self, e: &mut E) {
|
fn encode(&self, e: &mut E) {
|
||||||
do e.emit_map(self.len()) |e| {
|
do e.emit_map(self.len()) |e| {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
for self.each |key, val| {
|
do self.each |key, val| {
|
||||||
e.emit_map_elt_key(i, |e| key.encode(e));
|
e.emit_map_elt_key(i, |e| key.encode(e));
|
||||||
e.emit_map_elt_val(i, |e| val.encode(e));
|
e.emit_map_elt_val(i, |e| val.encode(e));
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
true
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -814,10 +815,11 @@ impl<S: Encoder> Encodable<S> for TrieSet {
|
|||||||
fn encode(&self, s: &mut S) {
|
fn encode(&self, s: &mut S) {
|
||||||
do s.emit_seq(self.len()) |s| {
|
do s.emit_seq(self.len()) |s| {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
for self.each |e| {
|
do self.each |e| {
|
||||||
s.emit_seq_elt(i, |s| e.encode(s));
|
s.emit_seq_elt(i, |s| e.encode(s));
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
true
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,27 +94,29 @@ pub unsafe fn annihilate() {
|
|||||||
//
|
//
|
||||||
// In this pass, nothing gets freed, so it does not matter whether
|
// In this pass, nothing gets freed, so it does not matter whether
|
||||||
// we read the next field before or after the callback.
|
// we read the next field before or after the callback.
|
||||||
for each_live_alloc(true) |box, uniq| {
|
do each_live_alloc(true) |box, uniq| {
|
||||||
stats.n_total_boxes += 1;
|
stats.n_total_boxes += 1;
|
||||||
if uniq {
|
if uniq {
|
||||||
stats.n_unique_boxes += 1;
|
stats.n_unique_boxes += 1;
|
||||||
} else {
|
} else {
|
||||||
(*box).ref_count = managed::RC_IMMORTAL;
|
(*box).ref_count = managed::RC_IMMORTAL;
|
||||||
}
|
}
|
||||||
}
|
true
|
||||||
|
};
|
||||||
|
|
||||||
// Pass 2: Drop all boxes.
|
// Pass 2: Drop all boxes.
|
||||||
//
|
//
|
||||||
// In this pass, unique-managed boxes may get freed, but not
|
// In this pass, unique-managed boxes may get freed, but not
|
||||||
// managed boxes, so we must read the `next` field *after* the
|
// managed boxes, so we must read the `next` field *after* the
|
||||||
// callback, as the original value may have been freed.
|
// callback, as the original value may have been freed.
|
||||||
for each_live_alloc(false) |box, uniq| {
|
do each_live_alloc(false) |box, uniq| {
|
||||||
if !uniq {
|
if !uniq {
|
||||||
let tydesc = (*box).type_desc;
|
let tydesc = (*box).type_desc;
|
||||||
let data = &(*box).data as *();
|
let data = &(*box).data as *();
|
||||||
((*tydesc).drop_glue)(data as *i8);
|
((*tydesc).drop_glue)(data as *i8);
|
||||||
}
|
}
|
||||||
}
|
true
|
||||||
|
};
|
||||||
|
|
||||||
// Pass 3: Free all boxes.
|
// Pass 3: Free all boxes.
|
||||||
//
|
//
|
||||||
@@ -122,14 +124,15 @@ pub unsafe fn annihilate() {
|
|||||||
// unique-managed boxes, though I think that none of those are
|
// unique-managed boxes, though I think that none of those are
|
||||||
// left), so we must read the `next` field before, since it will
|
// left), so we must read the `next` field before, since it will
|
||||||
// not be valid after.
|
// not be valid after.
|
||||||
for each_live_alloc(true) |box, uniq| {
|
do each_live_alloc(true) |box, uniq| {
|
||||||
if !uniq {
|
if !uniq {
|
||||||
stats.n_bytes_freed +=
|
stats.n_bytes_freed +=
|
||||||
(*((*box).type_desc)).size
|
(*((*box).type_desc)).size
|
||||||
+ sys::size_of::<raw::Box<()>>();
|
+ sys::size_of::<raw::Box<()>>();
|
||||||
local_free(box as *i8);
|
local_free(box as *i8);
|
||||||
}
|
}
|
||||||
}
|
true
|
||||||
|
};
|
||||||
|
|
||||||
if debug_mem() {
|
if debug_mem() {
|
||||||
// We do logging here w/o allocation.
|
// We do logging here w/o allocation.
|
||||||
|
|||||||
@@ -130,15 +130,17 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
|
|||||||
hash: uint,
|
hash: uint,
|
||||||
k: &K)
|
k: &K)
|
||||||
-> SearchResult {
|
-> SearchResult {
|
||||||
for self.bucket_sequence(hash) |i| {
|
let mut ret = TableFull;
|
||||||
|
do self.bucket_sequence(hash) |i| {
|
||||||
match self.buckets[i] {
|
match self.buckets[i] {
|
||||||
Some(ref bkt) => if bkt.hash == hash && *k == bkt.key {
|
Some(ref bkt) if bkt.hash == hash && *k == bkt.key => {
|
||||||
return FoundEntry(i);
|
ret = FoundEntry(i); false
|
||||||
},
|
},
|
||||||
None => return FoundHole(i)
|
None => { ret = FoundHole(i); false }
|
||||||
|
_ => true,
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
TableFull
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -146,17 +148,17 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
|
|||||||
hash: uint,
|
hash: uint,
|
||||||
k: &Q)
|
k: &Q)
|
||||||
-> SearchResult {
|
-> SearchResult {
|
||||||
for self.bucket_sequence(hash) |i| {
|
let mut ret = TableFull;
|
||||||
|
do self.bucket_sequence(hash) |i| {
|
||||||
match self.buckets[i] {
|
match self.buckets[i] {
|
||||||
Some(ref bkt) => {
|
Some(ref bkt) if bkt.hash == hash && k.equiv(&bkt.key) => {
|
||||||
if bkt.hash == hash && k.equiv(&bkt.key) {
|
ret = FoundEntry(i); false
|
||||||
return FoundEntry(i);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
None => return FoundHole(i)
|
None => { ret = FoundHole(i); false }
|
||||||
|
_ => true,
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
TableFull
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expand the capacity of the array to the next power of two
|
/// Expand the capacity of the array to the next power of two
|
||||||
@@ -272,11 +274,6 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
|
|||||||
|
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search(&self, hash: uint,
|
|
||||||
op: &fn(x: &Option<Bucket<K, V>>) -> bool) {
|
|
||||||
let _ = self.bucket_sequence(hash, |i| op(&self.buckets[i]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K:Hash + Eq,V> Container for HashMap<K, V> {
|
impl<K:Hash + Eq,V> Container for HashMap<K, V> {
|
||||||
|
|||||||
@@ -770,9 +770,10 @@ impl<T:Reader> ReaderUtil for T {
|
|||||||
|
|
||||||
fn read_lines(&self) -> ~[~str] {
|
fn read_lines(&self) -> ~[~str] {
|
||||||
do vec::build |push| {
|
do vec::build |push| {
|
||||||
for self.each_line |line| {
|
do self.each_line |line| {
|
||||||
push(line.to_owned());
|
push(line.to_owned());
|
||||||
}
|
true
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1880,16 +1881,16 @@ mod tests {
|
|||||||
|
|
||||||
{
|
{
|
||||||
let file = io::file_reader(&path).unwrap();
|
let file = io::file_reader(&path).unwrap();
|
||||||
for file.each_byte() |_| {
|
do file.each_byte() |_| {
|
||||||
fail!("must be empty");
|
fail!("must be empty")
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let file = io::file_reader(&path).unwrap();
|
let file = io::file_reader(&path).unwrap();
|
||||||
for file.each_char() |_| {
|
do file.each_char() |_| {
|
||||||
fail!("must be empty");
|
fail!("must be empty")
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -790,7 +790,7 @@ pub fn list_dir_path(p: &Path) -> ~[Path] {
|
|||||||
/// all its contents. Use carefully!
|
/// all its contents. Use carefully!
|
||||||
pub fn remove_dir_recursive(p: &Path) -> bool {
|
pub fn remove_dir_recursive(p: &Path) -> bool {
|
||||||
let mut error_happened = false;
|
let mut error_happened = false;
|
||||||
for walk_dir(p) |inner| {
|
do walk_dir(p) |inner| {
|
||||||
if !error_happened {
|
if !error_happened {
|
||||||
if path_is_dir(inner) {
|
if path_is_dir(inner) {
|
||||||
if !remove_dir_recursive(inner) {
|
if !remove_dir_recursive(inner) {
|
||||||
@@ -803,6 +803,7 @@ pub fn remove_dir_recursive(p: &Path) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
true
|
||||||
};
|
};
|
||||||
// Directory should now be empty
|
// Directory should now be empty
|
||||||
!error_happened && remove_dir(p)
|
!error_happened && remove_dir(p)
|
||||||
|
|||||||
@@ -372,8 +372,9 @@ impl Drop for Taskgroup {
|
|||||||
// with our own taskgroup, so long as both happen before we die.
|
// with our own taskgroup, so long as both happen before we die.
|
||||||
// We remove ourself from every ancestor we can, so no cleanup; no
|
// We remove ourself from every ancestor we can, so no cleanup; no
|
||||||
// break.
|
// break.
|
||||||
for each_ancestor(&mut this.ancestors, |_| {}) |ancestor_group| {
|
do each_ancestor(&mut this.ancestors, |_| {}) |ancestor_group| {
|
||||||
leave_taskgroup(ancestor_group, &me, false);
|
leave_taskgroup(ancestor_group, &me, false);
|
||||||
|
true
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -459,11 +459,12 @@ mod test_map {
|
|||||||
assert!(m.insert(1, 2));
|
assert!(m.insert(1, 2));
|
||||||
|
|
||||||
let mut n = 0;
|
let mut n = 0;
|
||||||
for m.each |k, v| {
|
do m.each |k, v| {
|
||||||
assert_eq!(*k, n);
|
assert_eq!(*k, n);
|
||||||
assert_eq!(*v, n * 2);
|
assert_eq!(*v, n * 2);
|
||||||
n += 1;
|
n += 1;
|
||||||
}
|
true
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -475,14 +476,16 @@ mod test_map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut n = uint::max_value - 10000;
|
let mut n = uint::max_value - 10000;
|
||||||
for m.each |k, v| {
|
do m.each |k, v| {
|
||||||
if n == uint::max_value - 5000 { break }
|
if n == uint::max_value - 5000 { false } else {
|
||||||
assert!(n < uint::max_value - 5000);
|
assert!(n < uint::max_value - 5000);
|
||||||
|
|
||||||
assert_eq!(*k, n);
|
assert_eq!(*k, n);
|
||||||
assert_eq!(*v, n / 2);
|
assert_eq!(*v, n / 2);
|
||||||
n += 1;
|
n += 1;
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -496,11 +499,12 @@ mod test_map {
|
|||||||
assert!(m.insert(1, 2));
|
assert!(m.insert(1, 2));
|
||||||
|
|
||||||
let mut n = 4;
|
let mut n = 4;
|
||||||
for m.each_reverse |k, v| {
|
do m.each_reverse |k, v| {
|
||||||
assert_eq!(*k, n);
|
assert_eq!(*k, n);
|
||||||
assert_eq!(*v, n * 2);
|
assert_eq!(*v, n * 2);
|
||||||
n -= 1;
|
n -= 1;
|
||||||
}
|
true
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -512,14 +516,16 @@ mod test_map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut n = uint::max_value - 1;
|
let mut n = uint::max_value - 1;
|
||||||
for m.each_reverse |k, v| {
|
do m.each_reverse |k, v| {
|
||||||
if n == uint::max_value - 5000 { break }
|
if n == uint::max_value - 5000 { false } else {
|
||||||
assert!(n > uint::max_value - 5000);
|
assert!(n > uint::max_value - 5000);
|
||||||
|
|
||||||
assert_eq!(*k, n);
|
assert_eq!(*k, n);
|
||||||
assert_eq!(*v, n / 2);
|
assert_eq!(*v, n / 2);
|
||||||
n -= 1;
|
n -= 1;
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -572,10 +578,11 @@ mod test_set {
|
|||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
|
||||||
for trie.each |x| {
|
do trie.each |x| {
|
||||||
assert_eq!(expected[i], *x);
|
assert_eq!(expected[i], *x);
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
true
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -2767,19 +2767,19 @@ mod tests {
|
|||||||
let mut results: ~[~[int]];
|
let mut results: ~[~[int]];
|
||||||
|
|
||||||
results = ~[];
|
results = ~[];
|
||||||
for each_permutation([]) |v| { results.push(v.to_owned()); }
|
do each_permutation([]) |v| { results.push(v.to_owned()); true };
|
||||||
assert_eq!(results, ~[~[]]);
|
assert_eq!(results, ~[~[]]);
|
||||||
|
|
||||||
results = ~[];
|
results = ~[];
|
||||||
for each_permutation([7]) |v| { results.push(v.to_owned()); }
|
do each_permutation([7]) |v| { results.push(v.to_owned()); true };
|
||||||
assert_eq!(results, ~[~[7]]);
|
assert_eq!(results, ~[~[7]]);
|
||||||
|
|
||||||
results = ~[];
|
results = ~[];
|
||||||
for each_permutation([1,1]) |v| { results.push(v.to_owned()); }
|
do each_permutation([1,1]) |v| { results.push(v.to_owned()); true };
|
||||||
assert_eq!(results, ~[~[1,1],~[1,1]]);
|
assert_eq!(results, ~[~[1,1],~[1,1]]);
|
||||||
|
|
||||||
results = ~[];
|
results = ~[];
|
||||||
for each_permutation([5,2,0]) |v| { results.push(v.to_owned()); }
|
do each_permutation([5,2,0]) |v| { results.push(v.to_owned()); true };
|
||||||
assert!(results ==
|
assert!(results ==
|
||||||
~[~[5,2,0],~[5,0,2],~[2,5,0],~[2,0,5],~[0,5,2],~[0,2,5]]);
|
~[~[5,2,0],~[5,0,2],~[2,5,0],~[2,0,5],~[0,5,2],~[0,2,5]]);
|
||||||
}
|
}
|
||||||
@@ -3107,12 +3107,13 @@ mod tests {
|
|||||||
fn test_permute_fail() {
|
fn test_permute_fail() {
|
||||||
let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
|
let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
for each_permutation(v) |_elt| {
|
do each_permutation(v) |_elt| {
|
||||||
if i == 2 {
|
if i == 2 {
|
||||||
fail!()
|
fail!()
|
||||||
}
|
}
|
||||||
i += 0;
|
i += 0;
|
||||||
}
|
true
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -3425,9 +3426,10 @@ mod tests {
|
|||||||
fn test_permutations0() {
|
fn test_permutations0() {
|
||||||
let values = [];
|
let values = [];
|
||||||
let mut v : ~[~[int]] = ~[];
|
let mut v : ~[~[int]] = ~[];
|
||||||
for each_permutation(values) |p| {
|
do each_permutation(values) |p| {
|
||||||
v.push(p.to_owned());
|
v.push(p.to_owned());
|
||||||
}
|
true
|
||||||
|
};
|
||||||
assert_eq!(v, ~[~[]]);
|
assert_eq!(v, ~[~[]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3435,9 +3437,10 @@ mod tests {
|
|||||||
fn test_permutations1() {
|
fn test_permutations1() {
|
||||||
let values = [1];
|
let values = [1];
|
||||||
let mut v : ~[~[int]] = ~[];
|
let mut v : ~[~[int]] = ~[];
|
||||||
for each_permutation(values) |p| {
|
do each_permutation(values) |p| {
|
||||||
v.push(p.to_owned());
|
v.push(p.to_owned());
|
||||||
}
|
true
|
||||||
|
};
|
||||||
assert_eq!(v, ~[~[1]]);
|
assert_eq!(v, ~[~[1]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3445,9 +3448,10 @@ mod tests {
|
|||||||
fn test_permutations2() {
|
fn test_permutations2() {
|
||||||
let values = [1,2];
|
let values = [1,2];
|
||||||
let mut v : ~[~[int]] = ~[];
|
let mut v : ~[~[int]] = ~[];
|
||||||
for each_permutation(values) |p| {
|
do each_permutation(values) |p| {
|
||||||
v.push(p.to_owned());
|
v.push(p.to_owned());
|
||||||
}
|
true
|
||||||
|
};
|
||||||
assert_eq!(v, ~[~[1,2],~[2,1]]);
|
assert_eq!(v, ~[~[1,2],~[2,1]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3455,9 +3459,10 @@ mod tests {
|
|||||||
fn test_permutations3() {
|
fn test_permutations3() {
|
||||||
let values = [1,2,3];
|
let values = [1,2,3];
|
||||||
let mut v : ~[~[int]] = ~[];
|
let mut v : ~[~[int]] = ~[];
|
||||||
for each_permutation(values) |p| {
|
do each_permutation(values) |p| {
|
||||||
v.push(p.to_owned());
|
v.push(p.to_owned());
|
||||||
}
|
true
|
||||||
|
};
|
||||||
assert_eq!(v, ~[~[1,2,3],~[1,3,2],~[2,1,3],~[2,3,1],~[3,1,2],~[3,2,1]]);
|
assert_eq!(v, ~[~[1,2,3],~[1,3,2],~[2,1,3],~[2,3,1],~[3,1,2],~[3,2,1]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,13 +65,14 @@ fn square_from_char(c: char) -> square {
|
|||||||
fn read_board_grid<rdr:'static + io::Reader>(input: rdr) -> ~[~[square]] {
|
fn read_board_grid<rdr:'static + io::Reader>(input: rdr) -> ~[~[square]] {
|
||||||
let input = @input as @io::Reader;
|
let input = @input as @io::Reader;
|
||||||
let mut grid = ~[];
|
let mut grid = ~[];
|
||||||
for input.each_line |line| {
|
do input.each_line |line| {
|
||||||
let mut row = ~[];
|
let mut row = ~[];
|
||||||
foreach c in line.iter() {
|
foreach c in line.iter() {
|
||||||
row.push(square_from_char(c))
|
row.push(square_from_char(c))
|
||||||
}
|
}
|
||||||
grid.push(row)
|
grid.push(row);
|
||||||
}
|
true
|
||||||
|
};
|
||||||
let width = grid[0].len();
|
let width = grid[0].len();
|
||||||
foreach row in grid.iter() { assert!(row.len() == width) }
|
foreach row in grid.iter() { assert!(row.len() == width) }
|
||||||
grid
|
grid
|
||||||
|
|||||||
Reference in New Issue
Block a user