vec: remove BaseIter implementation

I removed the `static-method-test.rs` test because it was heavily based
on `BaseIter` and there are plenty of other more complex uses of static
methods anyway.
This commit is contained in:
Daniel Micay 2013-06-21 08:29:53 -04:00
parent c9342663df
commit d2e9912aea
181 changed files with 796 additions and 876 deletions

View file

@ -24,7 +24,7 @@ pub fn alist_add<A:Copy,B:Copy>(lst: &alist<A,B>, k: A, v: B) {
pub fn alist_get<A:Copy,B:Copy>(lst: &alist<A,B>, k: A) -> B {
let eq_fn = lst.eq_fn;
for lst.data.each |entry| {
for lst.data.iter().advance |entry| {
if eq_fn(copy entry.key, copy k) { return copy entry.value; }
}
fail!();

View file

@ -141,7 +141,7 @@ fn bfs(graph: graph, key: node_id) -> bfs_result {
while !q.is_empty() {
let t = q.pop_front();
do graph[t].each() |k| {
do graph[t].iter().advance |k| {
if marks[*k] == -1i64 {
marks[*k] = t;
q.add_back(*k);
@ -201,7 +201,7 @@ fn bfs2(graph: graph, key: node_id) -> bfs_result {
let mut color = white;
do neighbors.each() |k| {
do neighbors.iter().advance |k| {
if is_gray(&colors[*k]) {
color = gray(*k);
false
@ -286,7 +286,7 @@ fn pbfs(graph: &arc::ARC<graph>, key: node_id) -> bfs_result {
let mut color = white;
do neighbors.each() |k| {
do neighbors.iter().advance |k| {
if is_gray(&colors[*k]) {
color = gray(*k);
false

View file

@ -59,7 +59,7 @@ static HOMO_SAPIENS: [AminoAcid, ..4] = [
fn sum_and_scale(a: &'static [AminoAcid]) -> ~[AminoAcid] {
let mut result = ~[];
let mut p = 0f32;
for a.each |a_i| {
for a.iter().advance |a_i| {
let mut a_i = *a_i;
p += a_i.p;
a_i.p = p * LOOKUP_SCALE;
@ -151,7 +151,7 @@ impl RandomFasta {
fn nextc(&mut self) -> u8 {
let r = self.rng(1.0);
for self.lookup.each |a| {
for self.lookup.iter().advance |a| {
if a.p >= r {
return a.c;
}

View file

@ -47,11 +47,11 @@ struct AminoAcids {
fn make_cumulative(aa: ~[AminoAcids]) -> ~[AminoAcids] {
let mut cp: u32 = 0u32;
let mut ans: ~[AminoAcids] = ~[];
for aa.each |a| {
for aa.iter().advance |a| {
cp += a.prob;
ans += [AminoAcids {ch: a.ch, prob: cp}];
}
return ans;
ans
}
fn select_random(r: u32, genelist: ~[AminoAcids]) -> char {
@ -64,7 +64,7 @@ fn select_random(r: u32, genelist: ~[AminoAcids]) -> char {
} else { return bisect(v, mid, hi, target); }
} else { return v[hi].ch; }
}
return bisect(copy genelist, 0, genelist.len() - 1, r);
bisect(copy genelist, 0, genelist.len() - 1, r)
}
fn make_random_fasta(wr: @io::Writer,
@ -106,7 +106,7 @@ fn make_repeat_fasta(wr: @io::Writer, id: ~str, desc: ~str, s: ~str, n: int) {
}
fn acid(ch: char, prob: u32) -> AminoAcids {
return AminoAcids {ch: ch, prob: prob};
AminoAcids {ch: ch, prob: prob}
}
fn main() {

View file

@ -64,7 +64,7 @@ fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str {
let mut buffer = ~"";
for pairs_sorted.each |kv| {
for pairs_sorted.iter().advance |kv| {
let (k,v) = copy *kv;
unsafe {
let b = str::raw::from_bytes(k);

View file

@ -91,7 +91,7 @@ fn stress(num_tasks: int) {
stress_task(i);
}
}
for results.each |r| {
for results.iter().advance |r| {
r.recv();
}
}

View file

@ -10,7 +10,7 @@
fn main() {
let v = @mut [ 1, 2, 3 ];
for v.each |_x| {
for v.iter().advance |_x| {
v[1] = 4; //~ ERROR cannot assign
}
}

View file

@ -15,7 +15,7 @@ trait vec_monad<A> {
impl<A> vec_monad<A> for ~[A] {
fn bind<B>(&self, f: &fn(A) -> ~[B]) {
let mut r = fail!();
for self.each |elt| { r += f(*elt); }
for self.iter().advance |elt| { r += f(*elt); }
//~^ WARNING unreachable expression
//~^^ ERROR the type of this value must be known
}

View file

@ -14,7 +14,7 @@
fn fail_len(v: ~[int]) -> uint {
let mut i = 3;
fail!();
for v.each |x| { i += 1u; }
for v.iter().advance |x| { i += 1u; }
//~^ ERROR: unreachable statement
return i;
}

View file

@ -37,7 +37,7 @@ fn main() {
('c', 'd'),
('e', 'f')];
for v.each |&(x,y)| {} // should be OK
for v.iter().advance |&(x,y)| {} // should be OK
// Make sure none of the errors above were fatal
let x: char = true; //~ ERROR expected `char` but found `bool`

View file

@ -109,7 +109,7 @@ fn main() {
'\u2004', '\u2005', '\u2006', '\u2007', '\u2008', '\u2009', '\u200A',
'\u2028', '\u2029', '\u202F', '\u205F', '\u3000'];
// <= bugs in pretty-printer?
for chars.each |c| {
for chars.iter().advance |c| {
let ws = c.is_whitespace();
println(fmt!("%? %?" , c , ws));
}

View file

@ -102,7 +102,7 @@ fn main() {
'\xA0', '\u1680', '\u180E', '\u2000', '\u2001', '\u2002', '\u2003',
'\u2004', '\u2005', '\u2006', '\u2007', '\u2008', '\u2009', '\u200A',
'\u2028', '\u2029', '\u202F', '\u205F', '\u3000'];
for chars.each |c| {
for chars.iter().advance |c| {
let ws = c.is_whitespace();
println(fmt!("%? %?", c , ws)); // <= bugs in pretty-printer?
}

View file

@ -10,7 +10,7 @@
// xfail-fast
fn iter_vec<T>(v: ~[T], f: &fn(&T)) { for v.each |x| { f(x); } }
fn iter_vec<T>(v: ~[T], f: &fn(&T)) { for v.iter().advance |x| { f(x); } }
pub fn main() {
let v = ~[1, 2, 3, 4, 5, 6, 7];

View file

@ -10,7 +10,7 @@
// xfail-fast
fn iter_vec<T>(v: ~[T], f: &fn(&T)) { for v.each |x| { f(x); } }
fn iter_vec<T>(v: ~[T], f: &fn(&T)) { for v.iter().advance |x| { f(x); } }
pub fn main() {
let v = ~[1, 2, 3, 4, 5];

View file

@ -10,7 +10,7 @@
fn sum_slice(x: &[int]) -> int {
let mut sum = 0;
for x.each |i| { sum += *i; }
for x.iter().advance |i| { sum += *i; }
return sum;
}

View file

@ -4,7 +4,7 @@ struct Wizard {
impl Wizard {
pub fn cast(&mut self) {
for self.spells.each |&spell| {
for self.spells.iter().advance |&spell| {
println(spell);
}
}

View file

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// xfail-fast
/*!
* Try to double-check that static fns have the right size (with or
* without dummy env ptr, as appropriate) by iterating a size-2 array.
@ -21,6 +23,6 @@ struct S<'self>(&'self fn());
static closures: &'static [S<'static>] = &[S(f), S(f)];
pub fn main() {
for bare_fns.each |&bare_fn| { bare_fn() }
for closures.each |&closure| { (*closure)() }
for std::vec::each(bare_fns) |&bare_fn| { bare_fn() }
for std::vec::each(closures) |&closure| { (*closure)() }
}

View file

@ -16,10 +16,10 @@ struct Box {
impl Box {
pub fn set_many(&mut self, xs: &[uint]) {
for xs.each |x| { self.x = *x; }
for xs.iter().advance |x| { self.x = *x; }
}
pub fn set_many2(@mut self, xs: &[uint]) {
for xs.each |x| { self.x = *x; }
for xs.iter().advance |x| { self.x = *x; }
}
}

View file

@ -10,7 +10,7 @@
pub fn main() {
let v : &[(int,int)] = &[ (1, 2), (3, 4), (5, 6) ];
for v.each |&(x, y)| {
for v.iter().advance |&(x, y)| {
println(y.to_str());
println(x.to_str());
}

View file

@ -8,4 +8,4 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
pub fn main() { let x: ~[int] = ~[]; for x.each |_i| { fail!("moop"); } }
pub fn main() { let x: ~[int] = ~[]; for x.iter().advance |_| { fail!("moop"); } }

View file

@ -31,7 +31,7 @@ mod map_reduce {
enum ctrl_proto { find_reducer(~[u8], Chan<int>), mapper_done, }
fn start_mappers(ctrl: SharedChan<ctrl_proto>, inputs: ~[~str]) {
for inputs.each |i| {
for inputs.iter().advance |i| {
let ctrl = ctrl.clone();
let i = i.clone();
task::spawn(|| map_task(ctrl.clone(), i.clone()) );

View file

@ -73,7 +73,7 @@ fn read_board_grid<rdr:'static + io::Reader>(in: rdr) -> ~[~[square]] {
grid.push(row)
}
let width = grid[0].len();
for grid.each |row| { assert!(row.len() == width) }
for grid.iter().advance |row| { assert!(row.len() == width) }
grid
}

View file

@ -14,7 +14,7 @@ struct trie_node {
}
fn print_str_vector(vector: ~[~str]) {
for vector.each() |string| {
for vector.iter().advance |string| {
println(*string);
}
}

View file

@ -12,7 +12,7 @@
trait Canvas {
fn add_point(&self, point: &int);
fn add_points(&self, shapes: &[int]) {
for shapes.each |pt| {
for shapes.iter().advance |pt| {
self.add_point(pt)
}
}

View file

@ -34,14 +34,12 @@ struct Point {
// Represents an offset on a canvas. (This has the same structure as a Point.
// but different semantics).
struct Size
{
struct Size {
width: int,
height: int,
}
struct Rect
{
struct Rect {
top_left: Point,
size: Size,
}
@ -49,8 +47,7 @@ struct Rect
// TODO: operators
// Contains the information needed to do shape rendering via ASCII art.
struct AsciiArt
{
struct AsciiArt {
width: uint,
height: uint,
priv fill: char,
@ -67,15 +64,11 @@ impl Drop for AsciiArt {
// It's common to define a constructor sort of function to create struct instances.
// If there is a canonical constructor it is typically named the same as the type.
// Other constructor sort of functions are typically named from_foo, from_bar, etc.
fn AsciiArt(width: uint, height: uint, fill: char) -> AsciiArt
{
fn AsciiArt(width: uint, height: uint, fill: char) -> AsciiArt {
// Use an anonymous function to build a vector of vectors containing
// blank characters for each position in our canvas.
let mut lines = do vec::build_sized(height)
|push|
{
for height.times
{
let mut lines = do vec::build_sized(height) |push| {
for height.times {
let mut line = ~[];
vec::grow_set(&mut line, width-1, &'.', '.');
push(line);
@ -88,14 +81,10 @@ fn AsciiArt(width: uint, height: uint, fill: char) -> AsciiArt
}
// Methods particular to the AsciiArt struct.
impl AsciiArt
{
fn add_pt(&mut self, x: int, y: int)
{
if x >= 0 && x < self.width as int
{
if y >= 0 && y < self.height as int
{
impl AsciiArt {
fn add_pt(&mut self, x: int, y: int) {
if x >= 0 && x < self.width as int {
if y >= 0 && y < self.height as int {
// Note that numeric types don't implicitly convert to each other.
let v = y as uint;
let h = x as uint;
@ -127,16 +116,14 @@ impl ToStr for AsciiArt {
// This is similar to an interface in other languages: it defines a protocol which
// developers can implement for arbitrary concrete types.
#[allow(default_methods)]
trait Canvas
{
trait Canvas {
fn add_point(&mut self, shape: Point);
fn add_rect(&mut self, shape: Rect);
// Unlike interfaces traits support default implementations.
// Got an ICE as soon as I added this method.
fn add_points(&mut self, shapes: &[Point])
{
for shapes.each |pt| {self.add_point(*pt)};
fn add_points(&mut self, shapes: &[Point]) {
for shapes.iter().advance |pt| {self.add_point(*pt)};
}
}
@ -144,25 +131,19 @@ trait Canvas
// Other implementations could also be provided (e.g. for PDF or Apple's Quartz)
// and code can use them polymorphically via the Canvas trait.
impl Canvas for AsciiArt {
fn add_point(&mut self, shape: Point)
{
fn add_point(&mut self, shape: Point) {
self.add_pt(shape.x, shape.y);
}
fn add_rect(&mut self, shape: Rect)
{
fn add_rect(&mut self, shape: Rect) {
// Add the top and bottom lines.
for int::range(shape.top_left.x, shape.top_left.x + shape.size.width)
|x|
{
for int::range(shape.top_left.x, shape.top_left.x + shape.size.width) |x| {
self.add_pt(x, shape.top_left.y);
self.add_pt(x, shape.top_left.y + shape.size.height - 1);
}
// Add the left and right lines.
for int::range(shape.top_left.y, shape.top_left.y + shape.size.height)
|y|
{
for int::range(shape.top_left.y, shape.top_left.y + shape.size.height) |y|{
self.add_pt(shape.top_left.x, y);
self.add_pt(shape.top_left.x + shape.size.width - 1, y);
}
@ -171,10 +152,8 @@ impl Canvas for AsciiArt {
// Rust's unit testing framework is currently a bit under-developed so we'll use
// this little helper.
pub fn check_strs(actual: &str, expected: &str) -> bool
{
if actual != expected
{
pub fn check_strs(actual: &str, expected: &str) -> bool {
if actual != expected {
io::stderr().write_line(fmt!("Found:\n%s\nbut expected\n%s", actual, expected));
return false;
}
@ -182,15 +161,13 @@ pub fn check_strs(actual: &str, expected: &str) -> bool
}
fn test_ascii_art_ctor()
{
fn test_ascii_art_ctor() {
let art = AsciiArt(3, 3, '*');
assert!(check_strs(art.to_str(), "...\n...\n..."));
}
fn test_add_pt()
{
fn test_add_pt() {
let mut art = AsciiArt(3, 3, '*');
art.add_pt(0, 0);
art.add_pt(0, -10);
@ -199,8 +176,7 @@ fn test_add_pt()
}
fn test_shapes()
{
fn test_shapes() {
let mut art = AsciiArt(4, 4, '*');
art.add_rect(Rect {top_left: Point {x: 0, y: 0}, size: Size {width: 4, height: 4}});
art.add_point(Point {x: 2, y: 2});

View file

@ -11,7 +11,7 @@
pub fn main() {
let x = ~[1, 2, 3];
let mut y = 0;
for x.each |i| { debug!(*i); y += *i; }
for x.iter().advance |i| { debug!(*i); y += *i; }
debug!(y);
assert_eq!(y, 6);
let s = ~"hello there";

View file

@ -11,6 +11,6 @@
pub fn main() {
let x = ~[10, 20, 30];
let mut sum = 0;
for x.each |x| { sum += *x; }
for x.iter().advance |x| { sum += *x; }
assert_eq!(sum, 60);
}

View file

@ -19,7 +19,7 @@ trait vec_monad<A> {
impl<A> vec_monad<A> for ~[A] {
fn bind<B:Copy>(&self, f: &fn(&A) -> ~[B]) -> ~[B] {
let mut r = ~[];
for self.each |elt| { r += f(elt); }
for self.iter().advance |elt| { r += f(elt); }
r
}
}

View file

@ -68,7 +68,7 @@ pub fn main() {
calllink10
];
let mut rng = rand::rng();
for fns.each |f| {
for fns.iter().advance |f| {
let f = *f;
let sz = rng.next() % 256u32 + 256u32;
let frame_backoff = rng.next() % 10u32 + 1u32;

View file

@ -17,7 +17,7 @@ fn test1() {
fn test2() {
let mut ints = [0, ..32];
for ints.mut_iter().advance |i| { *i += 22; }
for ints.each |i| { assert!(*i == 22); }
for ints.iter().advance |i| { assert!(*i == 22); }
}
pub fn main() {

View file

@ -30,7 +30,7 @@ impl<K,V> AssociationList<K,V> {
impl<K:Eq,V:Copy> Index<K,V> for AssociationList<K,V> {
fn index(&self, index: &K) -> V {
for self.pairs.each |pair| {
for self.pairs.iter().advance |pair| {
if pair.key == *index {
return copy pair.value;
}

View file

@ -27,7 +27,7 @@ fn main() {
assert_eq!(foos[i], Foo { bar: 1, baz: 2});
}
for foos.each |&foo| {
for foos.iter().advance |&foo| {
assert_eq!(foo, Foo { bar: 1, baz: 2 });
}
}

View file

@ -656,7 +656,8 @@ pub fn main() {
let v = @v as @TyVisitor;
visit_tydesc(td, v);
for (u.vals.clone()).each |s| {
let r = u.vals.clone();
for r.iter().advance |s| {
println(fmt!("val: %s", *s));
}
error!("%?", u.vals.clone());

View file

@ -15,14 +15,14 @@ fn foo(c: ~[int]) {
match none::<int> {
some::<int>(_) => {
for c.each |i| {
debug!(a);
let a = 17;
b += ~[a];
some::<int>(_) => {
for c.iter().advance |i| {
debug!(a);
let a = 17;
b += ~[a];
}
}
}
_ => { }
_ => { }
}
}

View file

@ -48,10 +48,10 @@ trait vec_utils<T> {
impl<T> vec_utils<T> for ~[T] {
fn length_(&self) -> uint { self.len() }
fn iter_(&self, f: &fn(&T)) { for self.each |x| { f(x); } }
fn iter_(&self, f: &fn(&T)) { for self.iter().advance |x| { f(x); } }
fn map_<U:Copy>(&self, f: &fn(&T) -> U) -> ~[U] {
let mut r = ~[];
for self.each |elt| { r += ~[f(elt)]; }
for self.iter().advance |elt| { r += ~[f(elt)]; }
r
}
}

View file

@ -1,94 +0,0 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// xfail-fast
use std::at_vec;
use std::uint;
use std::vec;
// A trait for objects that can be used to do an if-then-else
// (No actual need for this to be static, but it is a simple test.)
trait bool_like {
fn select<A>(b: Self, x1: A, x2: A) -> A;
}
fn andand<T:bool_like + Copy>(x1: T, x2: T) -> T {
bool_like::select(copy x1, x2, x1)
}
impl bool_like for bool {
fn select<A>(b: bool, x1: A, x2: A) -> A {
if b { x1 } else { x2 }
}
}
impl bool_like for int {
fn select<A>(b: int, x1: A, x2: A) -> A {
if b != 0 { x1 } else { x2 }
}
}
// A trait for sequences that can be constructed imperatively.
trait buildable<A> {
fn build_sized(size: uint, builder: &fn(push: &fn(v: A))) -> Self;
}
impl<A> buildable<A> for @[A] {
#[inline(always)]
fn build_sized(size: uint, builder: &fn(push: &fn(v: A))) -> @[A] {
at_vec::build_sized(size, builder)
}
}
impl<A> buildable<A> for ~[A] {
#[inline(always)]
fn build_sized(size: uint, builder: &fn(push: &fn(v: A))) -> ~[A] {
vec::build_sized(size, builder)
}
}
#[inline(always)]
fn build<A, B: buildable<A>>(builder: &fn(push: &fn(v: A))) -> B {
buildable::build_sized(4, builder)
}
/// Apply a function to each element of an iterable and return the results
fn map<T, IT: BaseIter<T>, U, BU: buildable<U>>
(v: IT, f: &fn(&T) -> U) -> BU {
do build |push| {
for v.each() |elem| {
push(f(elem));
}
}
}
fn seq_range<BT:buildable<int>>(lo: uint, hi: uint) -> BT {
do buildable::build_sized(hi-lo) |push| {
for uint::range(lo, hi) |i| {
push(i as int);
}
}
}
pub fn main() {
let v: @[int] = seq_range(0, 10);
assert_eq!(v, @[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
let v: @[int] = map(&[1,2,3], |&x| 1+x);
assert_eq!(v, @[2, 3, 4]);
let v: ~[int] = map(&[1,2,3], |&x| 1+x);
assert_eq!(v, ~[2, 3, 4]);
assert_eq!(bool_like::select(true, 9, 14), 9);
assert!(!andand(true, false));
assert_eq!(andand(7, 12), 12);
assert_eq!(andand(0, 12), 0);
}

View file

@ -54,7 +54,7 @@ fn test00() {
// Read from spawned tasks...
let mut sum = 0;
for results.each |r| {
for results.iter().advance |r| {
i = 0;
while i < number_of_messages {
let value = po.recv();
@ -64,7 +64,7 @@ fn test00() {
}
// Join spawned tasks...
for results.each |r| { r.recv(); }
for results.iter().advance |r| { r.recv(); }
debug!("Completed: Final number is: ");
error!(sum);

View file

@ -31,7 +31,7 @@ trait map<T> {
impl<T> map<T> for ~[T] {
fn map<U:Copy>(&self, f: &fn(&T) -> U) -> ~[U] {
let mut r = ~[];
for self.each |x| { r += ~[f(x)]; }
for std::vec::each(*self) |x| { r += ~[f(x)]; }
r
}
}