Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing redundant internal iterators with external ones, part 1 #6995

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/libextra/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use core::prelude::*;

use core::iterator::IteratorUtil;
use core::str;
use core::uint;
use core::vec;
Expand Down Expand Up @@ -173,7 +174,7 @@ pub fn sha1() -> @Sha1 {
fn mk_result(st: &mut Sha1State) -> ~[u8] {
if !(*st).computed { pad_msg(st); (*st).computed = true; }
let mut rs: ~[u8] = ~[];
for vec::each_mut((*st).h) |ptr_hpart| {
for st.h.mut_iter().advance |ptr_hpart| {
let hpart = *ptr_hpart;
let a = (hpart >> 24u32 & 0xFFu32) as u8;
let b = (hpart >> 16u32 & 0xFFu32) as u8;
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use core::prelude::*;

use core::iterator::IteratorUtil;
use core::cast;
use core::io;
use core::str;
Expand Down Expand Up @@ -895,7 +896,7 @@ impl<'self, O:DataFlowOperator> PropagationContext<'self, O> {

fn reset(&mut self, bits: &mut [uint]) {
let e = if self.dfcx.oper.initial_value() {uint::max_value} else {0};
for vec::each_mut(bits) |b| { *b = e; }
for bits.mut_iter().advance |b| { *b = e; }
}

fn add_to_entry_set(&mut self, id: ast::node_id, pred_bits: &[uint]) {
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use middle::typeck;
use util::ppaux::{Repr, ty_to_str};
use util::ppaux::UserString;

use core::iterator::IteratorUtil;
use core::vec;
use syntax::ast::*;
use syntax::attr::attrs_contains_name;
Expand Down Expand Up @@ -268,7 +269,7 @@ pub fn check_expr(e: @expr, cx: Context, v: visit::vt<Context>) {
ts.repr(cx.tcx),
type_param_defs.repr(cx.tcx));
}
for vec::each2(**ts, *type_param_defs) |&ty, type_param_def| {
for ts.iter().zip(type_param_defs.iter()).advance |(&ty, type_param_def)| {
check_bounds(cx, type_parameter_id, e.span, ty, type_param_def)
}
}
Expand Down Expand Up @@ -309,7 +310,7 @@ fn check_ty(aty: @Ty, cx: Context, v: visit::vt<Context>) {
let did = ast_util::def_id_of_def(cx.tcx.def_map.get_copy(&id));
let type_param_defs =
ty::lookup_item_type(cx.tcx, did).generics.type_param_defs;
for vec::each2(**ts, *type_param_defs) |&ty, type_param_def| {
for ts.iter().zip(type_param_defs.iter()).advance |(&ty, type_param_def)| {
check_bounds(cx, aty.id, aty.span, ty, type_param_def)
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/trans/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use middle::trans::common::*;
use middle::ty;
use util::ppaux::ty_to_str;

use core::iterator::IteratorUtil;
use core::vec;
use syntax::ast;
use syntax::ast_map::path_name;
Expand Down Expand Up @@ -75,7 +76,7 @@ pub fn maybe_instantiate_inline(ccx: @CrateContext, fn_id: ast::def_id,
ast::item_enum(_, _) => {
let vs_here = ty::enum_variants(ccx.tcx, local_def(item.id));
let vs_there = ty::enum_variants(ccx.tcx, parent_id);
for vec::each2(*vs_here, *vs_there) |here, there| {
for vs_here.iter().zip(vs_there.iter()).advance |(here, there)| {
if there.id == fn_id { my_id = here.id.node; }
ccx.external.insert(there.id, Some(here.id.node));
}
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/middle/trans/type_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use middle::trans::inline;
use middle::ty;
use middle::typeck;

use core::iterator::IteratorUtil;
use core::option::{Some, None};
use core::uint;
use core::vec;
Expand Down Expand Up @@ -264,7 +265,7 @@ pub fn mark_for_method_call(cx: Context, e_id: node_id, callee_id: node_id) {
for opt_static_did.each |&did| {
for cx.ccx.tcx.node_type_substs.find_copy(&callee_id).each |ts| {
let type_uses = type_uses_for(cx.ccx, did, ts.len());
for vec::each2(*type_uses, *ts) |uses, subst| {
for type_uses.iter().zip(ts.iter()).advance |(uses, subst)| {
type_needs(cx, *uses, *subst)
}
}
Expand Down Expand Up @@ -302,7 +303,7 @@ pub fn mark_for_expr(cx: Context, e: @expr) {
for opt_ts.each |ts| {
let id = ast_util::def_id_of_def(cx.ccx.tcx.def_map.get_copy(&e.id));
let uses_for_ts = type_uses_for(cx.ccx, id, ts.len());
for vec::each2(*uses_for_ts, *ts) |uses, subst| {
for uses_for_ts.iter().zip(ts.iter()).advance |(uses, subst)| {
type_needs(cx, *uses, *subst)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use middle::typeck::check::{instantiate_path, lookup_def};
use middle::typeck::check::{structure_of, valid_range_bounds};
use middle::typeck::require_same_types;

use core::iterator::IteratorUtil;
use core::hashmap::{HashMap, HashSet};
use core::vec;
use syntax::ast;
use syntax::ast_util;
use syntax::codemap::span;
Expand Down Expand Up @@ -232,7 +232,7 @@ pub fn check_pat_variant(pcx: &pat_ctxt, pat: @ast::pat, path: @ast::Path,

if !error_happened {
for subpats.each |pats| {
for vec::each2(*pats, arg_types) |subpat, arg_ty| {
for pats.iter().zip(arg_types.iter()).advance |(subpat, arg_ty)| {
check_pat(pcx, *subpat, *arg_ty);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ use util::common::{block_query, indenter, loop_query};
use util::ppaux::{bound_region_to_str};
use util::ppaux;


use core::iterator::IteratorUtil;
use core::cast::transmute;
use core::hashmap::HashMap;
use core::result;
Expand Down Expand Up @@ -412,7 +414,7 @@ pub fn check_fn(ccx: @mut CrateCtxt,
for opt_self_info.each |self_info| {
fcx.write_ty(self_info.self_id, self_info.self_ty);
}
for vec::each2(decl.inputs, arg_tys) |input, arg| {
for decl.inputs.iter().zip(arg_tys.iter()).advance |(input, arg)| {
fcx.write_ty(input.id, *arg);
}

Expand Down Expand Up @@ -449,7 +451,7 @@ pub fn check_fn(ccx: @mut CrateCtxt,
}

// Add formal parameters.
for vec::each2(arg_tys, decl.inputs) |arg_ty, input| {
for arg_tys.iter().zip(decl.inputs.iter()).advance |(arg_ty, input)| {
// Create type variables for each argument.
do pat_util::pat_bindings(tcx.def_map, input.pat)
|_bm, pat_id, _sp, _path| {
Expand Down
7 changes: 4 additions & 3 deletions src/librustc/middle/typeck/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use syntax::visit::{Visitor, SimpleVisitor};
use syntax::visit::{visit_mod};
use util::ppaux::ty_to_str;

use core::iterator::IteratorUtil;
use core::hashmap::{HashMap, HashSet};
use core::old_iter;
use core::result::Ok;
Expand Down Expand Up @@ -617,9 +618,9 @@ impl CoherenceChecker {
// Check to ensure that each parameter binding respected its
// kind bounds.
for [ a, b ].each |result| {
for vec::each2(result.type_variables,
*result.type_param_defs)
|ty_var, type_param_def|
for result.type_variables.iter()
.zip(result.type_param_defs.iter())
.advance |(ty_var, type_param_def)|
{
if type_param_def.bounds.builtin_bounds.contains_elem(
ty::BoundCopy)
Expand Down
39 changes: 21 additions & 18 deletions src/libstd/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub trait IteratorUtil<A> {
/// assert_eq!(it.next().get(), &1);
/// assert!(it.next().is_none());
/// ~~~
fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<Self, U>;
fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<A, Self, U>;

/// Creates an iterator which iterates over both this and the specified
/// iterators simultaneously, yielding the two elements as pairs. When
Expand All @@ -73,7 +73,7 @@ pub trait IteratorUtil<A> {
/// assert_eq!(it.next().get(), (&0, &1));
/// assert!(it.next().is_none());
/// ~~~
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<Self, U>;
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<A, Self, B, U>;

// FIXME: #5898: should be called map
/// Creates a new iterator which will apply the specified function to each
Expand Down Expand Up @@ -139,7 +139,7 @@ pub trait IteratorUtil<A> {
/// assert_eq!(it.next().get(), (1, &200));
/// assert!(it.next().is_none());
/// ~~~
fn enumerate(self) -> EnumerateIterator<Self>;
fn enumerate(self) -> EnumerateIterator<A, Self>;

/// Creates an iterator which invokes the predicate on elements until it
/// returns true. Once the predicate returns true, all further elements are
Expand Down Expand Up @@ -349,12 +349,12 @@ pub trait IteratorUtil<A> {
/// In the future these will be default methods instead of a utility trait.
impl<A, T: Iterator<A>> IteratorUtil<A> for T {
#[inline(always)]
fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<T, U> {
fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<A, T, U> {
ChainIterator{a: self, b: other, flag: false}
}

#[inline(always)]
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<T, U> {
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<A, T, B, U> {
ZipIterator{a: self, b: other}
}

Expand All @@ -375,7 +375,7 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
}

#[inline(always)]
fn enumerate(self) -> EnumerateIterator<T> {
fn enumerate(self) -> EnumerateIterator<A, T> {
EnumerateIterator{iter: self, count: 0}
}

Expand Down Expand Up @@ -570,13 +570,14 @@ impl<A: Ord, T: Iterator<A>> OrdIterator<A> for T {
}

/// An iterator which strings two iterators together
pub struct ChainIterator<T, U> {
// FIXME #6967: Dummy A parameter to get around type inference bug
pub struct ChainIterator<A, T, U> {
priv a: T,
priv b: U,
priv flag: bool
}

impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<T, U> {
impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<A, T, U> {
#[inline]
fn next(&mut self) -> Option<A> {
if self.flag {
Expand All @@ -593,12 +594,13 @@ impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<T, U> {
}

/// An iterator which iterates two other iterators simultaneously
pub struct ZipIterator<T, U> {
// FIXME #6967: Dummy A & B parameters to get around type inference bug
pub struct ZipIterator<A, T, B, U> {
priv a: T,
priv b: U
}

impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<T, U> {
impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<A, T, B, U> {
#[inline]
fn next(&mut self) -> Option<(A, B)> {
match (self.a.next(), self.b.next()) {
Expand Down Expand Up @@ -664,12 +666,13 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for FilterMapIterator<'self, A, B,
}

/// An iterator which yields the current count and the element during iteration
pub struct EnumerateIterator<T> {
// FIXME #6967: Dummy A parameter to get around type inference bug
pub struct EnumerateIterator<A, T> {
priv iter: T,
priv count: uint
}

impl<A, T: Iterator<A>> Iterator<(uint, A)> for EnumerateIterator<T> {
impl<A, T: Iterator<A>> Iterator<(uint, A)> for EnumerateIterator<A, T> {
#[inline]
fn next(&mut self) -> Option<(uint, A)> {
match self.iter.next() {
Expand Down Expand Up @@ -887,7 +890,7 @@ mod tests {
let expected = [0, 1, 2, 3, 4, 5, 30, 40, 50, 60];
let mut it = xs.iter().chain(ys.iter());
let mut i = 0;
for it.advance |&x: &uint| {
for it.advance |&x| {
assert_eq!(x, expected[i]);
i += 1;
}
Expand All @@ -896,7 +899,7 @@ mod tests {
let ys = Counter::new(30u, 10).take(4);
let mut it = xs.iter().transform(|&x| x).chain(ys);
let mut i = 0;
for it.advance |x: uint| {
for it.advance |x| {
assert_eq!(x, expected[i]);
i += 1;
}
Expand All @@ -906,15 +909,15 @@ mod tests {
#[test]
fn test_filter_map() {
let mut it = Counter::new(0u, 1u).take(10)
.filter_map(|x: uint| if x.is_even() { Some(x*x) } else { None });
.filter_map(|x| if x.is_even() { Some(x*x) } else { None });
assert_eq!(it.collect::<~[uint]>(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
}

#[test]
fn test_iterator_enumerate() {
let xs = [0u, 1, 2, 3, 4, 5];
let mut it = xs.iter().enumerate();
for it.advance |(i, &x): (uint, &uint)| {
for it.advance |(i, &x)| {
assert_eq!(i, x);
}
}
Expand All @@ -925,7 +928,7 @@ mod tests {
let ys = [0u, 1, 2, 3, 5, 13];
let mut it = xs.iter().take_while(|&x| *x < 15u);
let mut i = 0;
for it.advance |&x: &uint| {
for it.advance |&x| {
assert_eq!(x, ys[i]);
i += 1;
}
Expand All @@ -938,7 +941,7 @@ mod tests {
let ys = [15, 16, 17, 19];
let mut it = xs.iter().skip_while(|&x| *x < 15u);
let mut i = 0;
for it.advance |&x: &uint| {
for it.advance |&x| {
assert_eq!(x, ys[i]);
i += 1;
}
Expand Down
12 changes: 6 additions & 6 deletions src/libstd/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ bounded and unbounded protocols allows for less code duplication.
use container::Container;
use cast::{forget, transmute, transmute_copy};
use either::{Either, Left, Right};
use iterator::IteratorUtil;
use kinds::Owned;
use libc;
use ops::Drop;
Expand All @@ -96,8 +97,7 @@ use unstable::intrinsics;
use ptr;
use ptr::RawPtr;
use task;
use vec;
use vec::OwnedVector;
use vec::{OwnedVector, MutableVector};
use util::replace;

static SPIN_COUNT: uint = 0;
Expand Down Expand Up @@ -600,7 +600,7 @@ pub fn wait_many<T: Selectable>(pkts: &mut [T]) -> uint {

let mut data_avail = false;
let mut ready_packet = pkts.len();
for vec::eachi_mut(pkts) |i, p| {
for pkts.mut_iter().enumerate().advance |(i, p)| {
unsafe {
let p = &mut *p.header();
let old = p.mark_blocked(this);
Expand All @@ -622,7 +622,7 @@ pub fn wait_many<T: Selectable>(pkts: &mut [T]) -> uint {
let event = wait_event(this) as *PacketHeader;

let mut pos = None;
for vec::eachi_mut(pkts) |i, p| {
for pkts.mut_iter().enumerate().advance |(i, p)| {
if p.header() == event {
pos = Some(i);
break;
Expand All @@ -640,7 +640,7 @@ pub fn wait_many<T: Selectable>(pkts: &mut [T]) -> uint {

debug!("%?", &mut pkts[ready_packet]);

for vec::each_mut(pkts) |p| {
for pkts.mut_iter().advance |p| {
unsafe {
(*p.header()).unblock()
}
Expand Down Expand Up @@ -853,7 +853,7 @@ pub fn select<T:Owned,Tb:Owned>(mut endpoints: ~[RecvPacketBuffered<T, Tb>])
Option<T>,
~[RecvPacketBuffered<T, Tb>]) {
let mut endpoint_headers = ~[];
for vec::each_mut(endpoints) |endpoint| {
for endpoints.mut_iter().advance |endpoint| {
endpoint_headers.push(endpoint.header());
}

Expand Down
1 change: 0 additions & 1 deletion src/libstd/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,3 @@ pub use io::{Reader, ReaderUtil, Writer, WriterUtil};
// Reexported runtime types
pub use comm::{stream, Port, Chan, GenericChan, GenericSmartChan, GenericPort, Peekable};
pub use task::spawn;

3 changes: 2 additions & 1 deletion src/libstd/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! An ordered map and set for integer keys implemented as a radix trie

use prelude::*;
use iterator::IteratorUtil;
use uint;
use util::{swap, replace};
use vec;
Expand Down Expand Up @@ -277,7 +278,7 @@ impl<T> TrieNode<T> {
}

fn mutate_values<'a>(&'a mut self, f: &fn(&uint, &mut T) -> bool) -> bool {
for vec::each_mut(self.children) |child| {
for self.children.mut_iter().advance |child| {
match *child {
Internal(ref mut x) => if !x.mutate_values(f) {
return false
Expand Down
Loading