remove each
from vec, HashMap and HashSet
This commit is contained in:
parent
64ee9668a2
commit
e67c48a591
43 changed files with 139 additions and 223 deletions
|
@ -1552,13 +1552,6 @@ fn each(v: &[int], op: &fn(v: &int)) {
|
|||
}
|
||||
~~~~
|
||||
|
||||
As an aside, the reason we pass in a *pointer* to an integer rather
|
||||
than the integer itself is that this is how the actual `each()`
|
||||
function for vectors works. `vec::each` though is a
|
||||
[generic](#generics) function, so must be efficient to use for all
|
||||
types. Passing the elements by pointer avoids copying potentially
|
||||
large objects.
|
||||
|
||||
As a caller, if we use a closure to provide the final operator
|
||||
argument, we can write it in a way that has a pleasant, block-like
|
||||
structure.
|
||||
|
@ -1616,6 +1609,9 @@ To enable `debug!` logging, set the RUST_LOG environment variable to the name of
|
|||
|
||||
## For loops
|
||||
|
||||
> ***Note:*** The closure-based protocol used `for` loop is on the way out. The `for` loop will
|
||||
> use iterator objects in the future instead.
|
||||
|
||||
The most common way to express iteration in Rust is with a `for`
|
||||
loop. Like `do`, `for` is a nice syntax for describing control flow
|
||||
with closures. Additionally, within a `for` loop, `break`, `loop`,
|
||||
|
@ -1640,7 +1636,16 @@ fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
|
|||
And using this function to iterate over a vector:
|
||||
|
||||
~~~~
|
||||
# use each = std::vec::each;
|
||||
# fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
|
||||
# let mut n = 0;
|
||||
# while n < v.len() {
|
||||
# if !op(&v[n]) {
|
||||
# return false;
|
||||
# }
|
||||
# n += 1;
|
||||
# }
|
||||
# return true;
|
||||
# }
|
||||
each([2, 4, 8, 5, 16], |n| {
|
||||
if *n % 2 != 0 {
|
||||
println("found odd number!");
|
||||
|
@ -1656,7 +1661,16 @@ out of the loop, you just write `break`. To skip ahead
|
|||
to the next iteration, write `loop`.
|
||||
|
||||
~~~~
|
||||
# use each = std::vec::each;
|
||||
# fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
|
||||
# let mut n = 0;
|
||||
# while n < v.len() {
|
||||
# if !op(&v[n]) {
|
||||
# return false;
|
||||
# }
|
||||
# n += 1;
|
||||
# }
|
||||
# return true;
|
||||
# }
|
||||
for each([2, 4, 8, 5, 16]) |n| {
|
||||
if *n % 2 != 0 {
|
||||
println("found odd number!");
|
||||
|
@ -1671,7 +1685,16 @@ normally allowed in closures, in a block that appears as the body of a
|
|||
the enclosing function, not just the loop body.
|
||||
|
||||
~~~~
|
||||
# use each = std::vec::each;
|
||||
# fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
|
||||
# let mut n = 0;
|
||||
# while n < v.len() {
|
||||
# if !op(&v[n]) {
|
||||
# return false;
|
||||
# }
|
||||
# n += 1;
|
||||
# }
|
||||
# return true;
|
||||
# }
|
||||
fn contains(v: &[int], elt: int) -> bool {
|
||||
for each(v) |x| {
|
||||
if (*x == elt) { return true; }
|
||||
|
@ -1686,7 +1709,16 @@ In these situations it can be convenient to lean on Rust's
|
|||
argument patterns to bind `x` to the actual value, not the pointer.
|
||||
|
||||
~~~~
|
||||
# use each = std::vec::each;
|
||||
# fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
|
||||
# let mut n = 0;
|
||||
# while n < v.len() {
|
||||
# if !op(&v[n]) {
|
||||
# return false;
|
||||
# }
|
||||
# n += 1;
|
||||
# }
|
||||
# return true;
|
||||
# }
|
||||
# fn contains(v: &[int], elt: int) -> bool {
|
||||
for each(v) |&x| {
|
||||
if (x == elt) { return true; }
|
||||
|
@ -1841,10 +1873,9 @@ vector consisting of the result of applying `function` to each element
|
|||
of `vector`:
|
||||
|
||||
~~~~
|
||||
# use std::vec;
|
||||
fn map<T, U>(vector: &[T], function: &fn(v: &T) -> U) -> ~[U] {
|
||||
let mut accumulator = ~[];
|
||||
for vec::each(vector) |element| {
|
||||
for vector.iter().advance |element| {
|
||||
accumulator.push(function(element));
|
||||
}
|
||||
return accumulator;
|
||||
|
|
|
@ -529,7 +529,7 @@ fn compose_and_run_compiler(
|
|||
let extra_link_args = ~[~"-L",
|
||||
aux_output_dir_name(config, testfile).to_str()];
|
||||
|
||||
for vec::each(props.aux_builds) |rel_ab| {
|
||||
for props.aux_builds.iter().advance |rel_ab| {
|
||||
let abs_ab = config.aux_base.push_rel(&Path(*rel_ab));
|
||||
let aux_args =
|
||||
make_compile_args(config, props, ~[~"--lib"] + extra_link_args,
|
||||
|
|
|
@ -521,6 +521,7 @@ mod tests {
|
|||
use core::cell::Cell;
|
||||
use core::comm;
|
||||
use core::task;
|
||||
use core::uint;
|
||||
|
||||
#[test]
|
||||
fn manually_share_arc() {
|
||||
|
@ -790,18 +791,20 @@ mod tests {
|
|||
}
|
||||
assert_eq!(*state, 42);
|
||||
*state = 31337;
|
||||
// FIXME: #7372: hits type inference bug with iterators
|
||||
// send to other readers
|
||||
for vec::each(reader_convos) |x| {
|
||||
match *x {
|
||||
for uint::range(0, reader_convos.len()) |i| {
|
||||
match reader_convos[i] {
|
||||
(ref rc, _) => rc.send(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
let read_mode = arc.downgrade(write_mode);
|
||||
do (&read_mode).read |state| {
|
||||
// FIXME: #7372: hits type inference bug with iterators
|
||||
// complete handshake with other readers
|
||||
for vec::each(reader_convos) |x| {
|
||||
match *x {
|
||||
for uint::range(0, reader_convos.len()) |i| {
|
||||
match reader_convos[i] {
|
||||
(_, ref rp) => rp.recv(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -418,10 +418,11 @@ pub fn opts_str(mm: &Matches, names: &[~str]) -> ~str {
|
|||
*/
|
||||
pub fn opt_strs(mm: &Matches, nm: &str) -> ~[~str] {
|
||||
let mut acc: ~[~str] = ~[];
|
||||
for vec::each(opt_vals(mm, nm)) |v| {
|
||||
let r = opt_vals(mm, nm);
|
||||
for r.iter().advance |v| {
|
||||
match *v { Val(ref s) => acc.push(copy *s), _ => () }
|
||||
}
|
||||
return acc;
|
||||
acc
|
||||
}
|
||||
|
||||
/// Returns the string argument supplied to a matching option or none
|
||||
|
|
|
@ -1123,7 +1123,7 @@ impl Eq for Json {
|
|||
&Object(ref d1) => {
|
||||
if d0.len() == d1.len() {
|
||||
let mut equal = true;
|
||||
for d0.each |k, v0| {
|
||||
for d0.iter().advance |(k, v0)| {
|
||||
match d1.find(k) {
|
||||
Some(v1) if v0 == v1 => { },
|
||||
_ => { equal = false; break }
|
||||
|
@ -1186,12 +1186,12 @@ impl Ord for Json {
|
|||
let mut d1_flat = ~[];
|
||||
|
||||
// FIXME #4430: this is horribly inefficient...
|
||||
for d0.each |k, v| {
|
||||
for d0.iter().advance |(k, v)| {
|
||||
d0_flat.push((@copy *k, @copy *v));
|
||||
}
|
||||
d0_flat.qsort();
|
||||
|
||||
for d1.each |k, v| {
|
||||
for d1.iter().advance |(k, v)| {
|
||||
d1_flat.push((@copy *k, @copy *v));
|
||||
}
|
||||
d1_flat.qsort();
|
||||
|
@ -1326,7 +1326,7 @@ impl<A:ToJson> ToJson for ~[A] {
|
|||
impl<A:ToJson + Copy> ToJson for HashMap<~str, A> {
|
||||
fn to_json(&self) -> Json {
|
||||
let mut d = HashMap::new();
|
||||
for self.each |key, value| {
|
||||
for self.iter().advance |(key, value)| {
|
||||
d.insert(copy *key, value.to_json());
|
||||
}
|
||||
Object(~d)
|
||||
|
|
|
@ -207,7 +207,7 @@ pub fn encode_form_urlencoded(m: &HashMap<~str, ~[~str]>) -> ~str {
|
|||
let mut out = ~"";
|
||||
let mut first = true;
|
||||
|
||||
for m.each |key, values| {
|
||||
for m.iter().advance |(key, values)| {
|
||||
let key = encode_plus(*key);
|
||||
|
||||
for values.iter().advance |value| {
|
||||
|
|
|
@ -710,7 +710,7 @@ impl<
|
|||
fn encode(&self, e: &mut E) {
|
||||
do e.emit_map(self.len()) |e| {
|
||||
let mut i = 0;
|
||||
for self.each |key, val| {
|
||||
for self.iter().advance |(key, val)| {
|
||||
e.emit_map_elt_key(i, |e| key.encode(e));
|
||||
e.emit_map_elt_val(i, |e| val.encode(e));
|
||||
i += 1;
|
||||
|
@ -744,7 +744,7 @@ impl<
|
|||
fn encode(&self, s: &mut S) {
|
||||
do s.emit_seq(self.len()) |s| {
|
||||
let mut i = 0;
|
||||
for self.each |e| {
|
||||
for self.iter().advance |e| {
|
||||
s.emit_seq_elt(i, |s| e.encode(s));
|
||||
i += 1;
|
||||
}
|
||||
|
|
|
@ -1094,7 +1094,8 @@ mod tests {
|
|||
};
|
||||
assert!(result.is_err());
|
||||
// child task must have finished by the time try returns
|
||||
for vec::each(p.recv()) |p| { p.recv(); } // wait on all its siblings
|
||||
let r = p.recv();
|
||||
for r.iter().advance |p| { p.recv(); } // wait on all its siblings
|
||||
do m.lock_cond |cond| {
|
||||
let woken = cond.broadcast();
|
||||
assert_eq!(woken, 0);
|
||||
|
|
|
@ -146,7 +146,7 @@ impl WorkMap {
|
|||
impl<S:Encoder> Encodable<S> for WorkMap {
|
||||
fn encode(&self, s: &mut S) {
|
||||
let mut d = ~[];
|
||||
for self.each |k, v| {
|
||||
for self.iter().advance |(k, v)| {
|
||||
d.push((copy *k, copy *v))
|
||||
}
|
||||
sort::tim_sort(d);
|
||||
|
@ -320,7 +320,7 @@ impl TPrep for Prep {
|
|||
}
|
||||
|
||||
fn all_fresh(&self, cat: &str, map: &WorkMap) -> bool {
|
||||
for map.each |k, v| {
|
||||
for map.iter().advance |(k, v)| {
|
||||
if ! self.is_fresh(cat, k.kind, k.name, *v) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -86,7 +86,7 @@ pub fn have_crate_data(cstore: &CStore, cnum: ast::crate_num) -> bool {
|
|||
|
||||
pub fn iter_crate_data(cstore: &CStore,
|
||||
i: &fn(ast::crate_num, @crate_metadata)) {
|
||||
for cstore.metas.each |&k, &v| {
|
||||
for cstore.metas.iter().advance |(&k, &v)| {
|
||||
i(k, v);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -240,7 +240,8 @@ fn check_fn(
|
|||
|
||||
// Check kinds on free variables:
|
||||
do with_appropriate_checker(cx, fn_id) |chk| {
|
||||
for vec::each(*freevars::get_freevars(cx.tcx, fn_id)) |fv| {
|
||||
let r = freevars::get_freevars(cx.tcx, fn_id);
|
||||
for r.iter().advance |fv| {
|
||||
chk(cx, *fv);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -436,7 +436,7 @@ impl LanguageItemCollector {
|
|||
}
|
||||
|
||||
pub fn check_completeness(&self) {
|
||||
for self.item_refs.each |&key, &item_ref| {
|
||||
for self.item_refs.iter().advance |(&key, &item_ref)| {
|
||||
match self.items.items[item_ref] {
|
||||
None => {
|
||||
self.session.err(fmt!("no item found for `%s`", key));
|
||||
|
|
|
@ -361,7 +361,7 @@ impl Context {
|
|||
}
|
||||
|
||||
fn lint_to_str(&self, lint: lint) -> &'static str {
|
||||
for self.dict.each |k, v| {
|
||||
for self.dict.iter().advance |(k, v)| {
|
||||
if v.lint == lint {
|
||||
return *k;
|
||||
}
|
||||
|
@ -742,7 +742,8 @@ fn check_item_ctypes(cx: &Context, it: @ast::item) {
|
|||
|
||||
fn check_foreign_fn(cx: &Context, decl: &ast::fn_decl) {
|
||||
let tys = vec::map(decl.inputs, |a| a.ty );
|
||||
for vec::each(vec::append_one(tys, decl.output)) |ty| {
|
||||
let r = vec::append_one(tys, decl.output);
|
||||
for r.iter().advance |ty| {
|
||||
check_ty(cx, *ty);
|
||||
}
|
||||
}
|
||||
|
@ -1171,7 +1172,7 @@ pub fn check_crate(tcx: ty::ctxt, crate: @ast::crate) {
|
|||
|
||||
// If we missed any lints added to the session, then there's a bug somewhere
|
||||
// in the iteration code.
|
||||
for tcx.sess.lints.each |_, v| {
|
||||
for tcx.sess.lints.iter().advance |(_, v)| {
|
||||
for v.iter().advance |t| {
|
||||
match *t {
|
||||
(lint, span, ref msg) =>
|
||||
|
|
|
@ -948,7 +948,7 @@ pub fn determine_rp_in_crate(sess: Session,
|
|||
debug!("%s", {
|
||||
debug!("Region variance results:");
|
||||
let region_paramd_items = cx.region_paramd_items;
|
||||
for region_paramd_items.each |&key, &value| {
|
||||
for region_paramd_items.iter().advance |(&key, &value)| {
|
||||
debug!("item %? (%s) is parameterized with variance %?",
|
||||
key,
|
||||
ast_map::node_id_to_str(ast_map, key,
|
||||
|
|
|
@ -1386,7 +1386,7 @@ impl Resolver {
|
|||
}
|
||||
|
||||
let def_id = local_def(item.id);
|
||||
for method_names.each |name, _| {
|
||||
for method_names.iter().advance |(name, _)| {
|
||||
if !self.method_map.contains_key(name) {
|
||||
self.method_map.insert(*name, HashSet::new());
|
||||
}
|
||||
|
@ -1704,7 +1704,7 @@ impl Resolver {
|
|||
interned_method_names.insert(method_name);
|
||||
}
|
||||
}
|
||||
for interned_method_names.each |name| {
|
||||
for interned_method_names.iter().advance |name| {
|
||||
if !self.method_map.contains_key(name) {
|
||||
self.method_map.insert(*name, HashSet::new());
|
||||
}
|
||||
|
@ -2470,8 +2470,8 @@ impl Resolver {
|
|||
assert_eq!(containing_module.glob_count, 0);
|
||||
|
||||
// Add all resolved imports from the containing module.
|
||||
for containing_module.import_resolutions.each
|
||||
|ident, target_import_resolution| {
|
||||
for containing_module.import_resolutions.iter().advance
|
||||
|(ident, target_import_resolution)| {
|
||||
|
||||
debug!("(resolving glob import) writing module resolution \
|
||||
%? into `%s`",
|
||||
|
@ -2555,13 +2555,13 @@ impl Resolver {
|
|||
};
|
||||
|
||||
// Add all children from the containing module.
|
||||
for containing_module.children.each |&ident, name_bindings| {
|
||||
for containing_module.children.iter().advance |(&ident, name_bindings)| {
|
||||
merge_import_resolution(ident, *name_bindings);
|
||||
}
|
||||
|
||||
// Add external module children from the containing module.
|
||||
for containing_module.external_module_children.each
|
||||
|&ident, module| {
|
||||
for containing_module.external_module_children.iter().advance
|
||||
|(&ident, module)| {
|
||||
let name_bindings =
|
||||
@mut Resolver::create_name_bindings_from_module(*module);
|
||||
merge_import_resolution(ident, name_bindings);
|
||||
|
@ -3251,7 +3251,7 @@ impl Resolver {
|
|||
pub fn add_exports_for_module(@mut self,
|
||||
exports2: &mut ~[Export2],
|
||||
module_: @mut Module) {
|
||||
for module_.children.each |ident, namebindings| {
|
||||
for module_.children.iter().advance |(ident, namebindings)| {
|
||||
debug!("(computing exports) maybe export '%s'",
|
||||
self.session.str_of(*ident));
|
||||
self.add_exports_of_namebindings(&mut *exports2,
|
||||
|
@ -3266,7 +3266,7 @@ impl Resolver {
|
|||
false);
|
||||
}
|
||||
|
||||
for module_.import_resolutions.each |ident, importresolution| {
|
||||
for module_.import_resolutions.iter().advance |(ident, importresolution)| {
|
||||
if importresolution.privacy != Public {
|
||||
debug!("(computing exports) not reexporting private `%s`",
|
||||
self.session.str_of(*ident));
|
||||
|
@ -4039,7 +4039,7 @@ impl Resolver {
|
|||
for arm.pats.iter().enumerate().advance |(i, p)| {
|
||||
let map_i = self.binding_mode_map(*p);
|
||||
|
||||
for map_0.each |&key, &binding_0| {
|
||||
for map_0.iter().advance |(&key, &binding_0)| {
|
||||
match map_i.find(&key) {
|
||||
None => {
|
||||
self.session.span_err(
|
||||
|
@ -4060,7 +4060,7 @@ impl Resolver {
|
|||
}
|
||||
}
|
||||
|
||||
for map_i.each |&key, &binding| {
|
||||
for map_i.iter().advance |(&key, &binding)| {
|
||||
if !map_0.contains_key(&key) {
|
||||
self.session.span_err(
|
||||
binding.span,
|
||||
|
@ -5355,7 +5355,7 @@ impl Resolver {
|
|||
}
|
||||
|
||||
debug!("Import resolutions:");
|
||||
for module_.import_resolutions.each |name, import_resolution| {
|
||||
for module_.import_resolutions.iter().advance |(name, import_resolution)| {
|
||||
let value_repr;
|
||||
match import_resolution.target_for_namespace(ValueNS) {
|
||||
None => { value_repr = ~""; }
|
||||
|
|
|
@ -1673,7 +1673,7 @@ pub fn trans_match_inner(scope_cx: block,
|
|||
|
||||
let mut arm_datas = ~[];
|
||||
let mut matches = ~[];
|
||||
for vec::each(arms) |arm| {
|
||||
for arms.iter().advance |arm| {
|
||||
let body = scope_block(bcx, arm.body.info(), "case_body");
|
||||
let bindings_map = create_bindings_map(bcx, arm.pats[0]);
|
||||
let arm_data = @ArmData {bodycx: body,
|
||||
|
|
|
@ -2945,7 +2945,7 @@ pub fn trans_crate(sess: session::Session,
|
|||
}
|
||||
|
||||
if ccx.sess.count_llvm_insns() {
|
||||
for ccx.stats.llvm_insns.each |&k, &v| {
|
||||
for ccx.stats.llvm_insns.iter().advance |(&k, &v)| {
|
||||
io::println(fmt!("%-7u %s", v, k));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -704,11 +704,11 @@ pub fn trans_args(cx: block,
|
|||
// now that all arguments have been successfully built, we can revoke any
|
||||
// temporary cleanups, as they are only needed if argument construction
|
||||
// should fail (for example, cleanup of copy mode args).
|
||||
for vec::each(temp_cleanups) |c| {
|
||||
for temp_cleanups.iter().advance |c| {
|
||||
revoke_clean(bcx, *c)
|
||||
}
|
||||
|
||||
return bcx;
|
||||
bcx
|
||||
}
|
||||
|
||||
pub enum AutorefArg {
|
||||
|
|
|
@ -213,7 +213,8 @@ pub fn type_needs_inner(cx: Context,
|
|||
ty::ty_enum(did, ref substs) => {
|
||||
if list::find(enums_seen, |id| *id == did).is_none() {
|
||||
let seen = @Cons(did, enums_seen);
|
||||
for vec::each(*ty::enum_variants(cx.ccx.tcx, did)) |v| {
|
||||
let r = ty::enum_variants(cx.ccx.tcx, did);
|
||||
for r.iter().advance |v| {
|
||||
for v.args.iter().advance |aty| {
|
||||
let t = ty::subst(cx.ccx.tcx, &(*substs), *aty);
|
||||
type_needs_inner(cx, use_, t, seen);
|
||||
|
|
|
@ -1285,7 +1285,7 @@ impl RegionVarBindings {
|
|||
|
||||
// It would be nice to write this using map():
|
||||
let mut edges = vec::with_capacity(num_edges);
|
||||
for self.constraints.each |constraint, span| {
|
||||
for self.constraints.iter().advance |(constraint, span)| {
|
||||
edges.push(GraphEdge {
|
||||
next_edge: [uint::max_value, uint::max_value],
|
||||
constraint: *constraint,
|
||||
|
|
|
@ -166,7 +166,7 @@ Available lint options:
|
|||
padded(max_key, "name"), "default", "meaning"));
|
||||
io::println(fmt!(" %s %7.7s %s\n",
|
||||
padded(max_key, "----"), "-------", "-------"));
|
||||
for lint_dict.each |k, v| {
|
||||
for lint_dict.iter().advance |(k, v)| {
|
||||
let k = k.replace("_", "-");
|
||||
io::println(fmt!(" %s %7.7s %s",
|
||||
padded(max_key, k),
|
||||
|
|
|
@ -96,7 +96,7 @@ impl Program {
|
|||
|
||||
code.push_str("fn main() {\n");
|
||||
// It's easy to initialize things if we don't run things...
|
||||
for self.local_vars.each |name, var| {
|
||||
for self.local_vars.iter().advance |(name, var)| {
|
||||
let mt = var.mt();
|
||||
code.push_str(fmt!("let%s %s: %s = fail!();\n", mt, *name, var.ty));
|
||||
var.alter(*name, &mut code);
|
||||
|
@ -149,7 +149,7 @@ impl Program {
|
|||
|
||||
// Using this __tls_map handle, deserialize each variable binding that
|
||||
// we know about
|
||||
for self.local_vars.each |name, var| {
|
||||
for self.local_vars.iter().advance |(name, var)| {
|
||||
let mt = var.mt();
|
||||
code.push_str(fmt!("let%s %s: %s = {
|
||||
let data = __tls_map.get_copy(&~\"%s\");
|
||||
|
@ -175,7 +175,7 @@ impl Program {
|
|||
|
||||
// After the input code is run, we can re-serialize everything back out
|
||||
// into tls map (to be read later on by this task)
|
||||
for self.local_vars.each |name, var| {
|
||||
for self.local_vars.iter().advance |(name, var)| {
|
||||
code.push_str(fmt!("{
|
||||
let local: %s = %s;
|
||||
let bytes = do ::std::io::with_bytes_writer |io| {
|
||||
|
@ -237,7 +237,7 @@ impl Program {
|
|||
/// program starts
|
||||
pub fn set_cache(&self) {
|
||||
let map = @mut HashMap::new();
|
||||
for self.local_vars.each |name, value| {
|
||||
for self.local_vars.iter().advance |(name, value)| {
|
||||
map.insert(copy *name, @copy value.data);
|
||||
}
|
||||
unsafe {
|
||||
|
|
|
@ -488,11 +488,6 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Visit all key-value pairs
|
||||
pub fn each<'a>(&'a self, blk: &fn(&K, &'a V) -> bool) -> bool {
|
||||
self.iter().advance(|(k, v)| blk(k, v))
|
||||
}
|
||||
|
||||
/// Visit all keys
|
||||
pub fn each_key(&self, blk: &fn(k: &K) -> bool) -> bool {
|
||||
self.iter().advance(|(k, _)| blk(k))
|
||||
|
@ -718,12 +713,6 @@ impl<T:Hash + Eq> HashSet<T> {
|
|||
self.map.contains_key_equiv(value)
|
||||
}
|
||||
|
||||
/// Visit all elements in arbitrary order
|
||||
/// FIXME: #6978: Remove when all callers are converted
|
||||
pub fn each(&self, f: &fn(&T) -> bool) -> bool {
|
||||
self.iter().advance(f)
|
||||
}
|
||||
|
||||
/// An iterator visiting all elements in arbitrary order.
|
||||
/// Iterator element type is &'a T.
|
||||
pub fn iter<'a>(&'a self) -> HashSetIterator<'a, T> {
|
||||
|
|
|
@ -111,7 +111,7 @@ fn taskset_remove(tasks: &mut TaskSet, task: *rust_task) {
|
|||
assert!(was_present);
|
||||
}
|
||||
pub fn taskset_each(tasks: &TaskSet, blk: &fn(v: *rust_task) -> bool) -> bool {
|
||||
tasks.each(|k| blk(*k))
|
||||
tasks.iter().advance(|k| blk(*k))
|
||||
}
|
||||
|
||||
// One of these per group of linked-failure tasks.
|
||||
|
|
|
@ -444,7 +444,7 @@ pub fn partitioned<T:Copy>(v: &[T], f: &fn(&T) -> bool) -> (~[T], ~[T]) {
|
|||
let mut lefts = ~[];
|
||||
let mut rights = ~[];
|
||||
|
||||
for each(v) |elt| {
|
||||
for v.iter().advance |elt| {
|
||||
if f(elt) {
|
||||
lefts.push(copy *elt);
|
||||
} else {
|
||||
|
@ -850,7 +850,7 @@ pub fn grow_set<T:Copy>(v: &mut ~[T], index: uint, initval: &T, val: T) {
|
|||
/// Apply a function to each element of a vector and return the results
|
||||
pub fn map<T, U>(v: &[T], f: &fn(t: &T) -> U) -> ~[U] {
|
||||
let mut result = with_capacity(v.len());
|
||||
for each(v) |elem| {
|
||||
for v.iter().advance |elem| {
|
||||
result.push(f(elem));
|
||||
}
|
||||
result
|
||||
|
@ -886,7 +886,7 @@ pub fn mapi<T, U>(v: &[T], f: &fn(uint, t: &T) -> U) -> ~[U] {
|
|||
*/
|
||||
pub fn flat_map<T, U>(v: &[T], f: &fn(t: &T) -> ~[U]) -> ~[U] {
|
||||
let mut result = ~[];
|
||||
for each(v) |elem| { result.push_all_move(f(elem)); }
|
||||
for v.iter().advance |elem| { result.push_all_move(f(elem)); }
|
||||
result
|
||||
}
|
||||
|
||||
|
@ -939,7 +939,7 @@ pub fn filter_mapped<T, U: Copy>(
|
|||
*/
|
||||
|
||||
let mut result = ~[];
|
||||
for each(v) |elem| {
|
||||
for v.iter().advance |elem| {
|
||||
match f(elem) {
|
||||
None => {/* no-op */ }
|
||||
Some(result_elem) => { result.push(result_elem); }
|
||||
|
@ -974,7 +974,7 @@ pub fn filter<T>(v: ~[T], f: &fn(t: &T) -> bool) -> ~[T] {
|
|||
*/
|
||||
pub fn filtered<T:Copy>(v: &[T], f: &fn(t: &T) -> bool) -> ~[T] {
|
||||
let mut result = ~[];
|
||||
for each(v) |elem| {
|
||||
for v.iter().advance |elem| {
|
||||
if f(elem) { result.push(copy *elem); }
|
||||
}
|
||||
result
|
||||
|
@ -1058,7 +1058,7 @@ impl<'self, T:Copy> VectorVector<T> for &'self [&'self [T]] {
|
|||
|
||||
/// Return true if a vector contains an element with the given value
|
||||
pub fn contains<T:Eq>(v: &[T], x: &T) -> bool {
|
||||
for each(v) |elt| { if *x == *elt { return true; } }
|
||||
for v.iter().advance |elt| { if *x == *elt { return true; } }
|
||||
false
|
||||
}
|
||||
|
||||
|
@ -1209,7 +1209,7 @@ pub fn bsearch_elem<T:TotalOrd>(v: &[T], x: &T) -> Option<uint> {
|
|||
*/
|
||||
pub fn unzip_slice<T:Copy,U:Copy>(v: &[(T, U)]) -> (~[T], ~[U]) {
|
||||
let mut (ts, us) = (~[], ~[]);
|
||||
for each(v) |p| {
|
||||
for v.iter().advance |p| {
|
||||
let (t, u) = copy *p;
|
||||
ts.push(t);
|
||||
us.push(u);
|
||||
|
@ -1347,69 +1347,6 @@ pub fn reversed<T:Copy>(v: &const [T]) -> ~[T] {
|
|||
rs
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates over a vector, yielding each element to a closure.
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `v` - A vector, to be iterated over
|
||||
* * `f` - A closure to do the iterating. Within this closure, return true to
|
||||
* * continue iterating, false to break.
|
||||
*
|
||||
* # Examples
|
||||
*
|
||||
* ~~~ {.rust}
|
||||
* [1,2,3].each(|&i| {
|
||||
* io::println(int::str(i));
|
||||
* true
|
||||
* });
|
||||
* ~~~
|
||||
*
|
||||
* ~~~ {.rust}
|
||||
* [1,2,3,4,5].each(|&i| {
|
||||
* if i < 4 {
|
||||
* io::println(int::str(i));
|
||||
* true
|
||||
* }
|
||||
* else {
|
||||
* false
|
||||
* }
|
||||
* });
|
||||
* ~~~
|
||||
*
|
||||
* You probably will want to use each with a `for`/`do` expression, depending
|
||||
* on your iteration needs:
|
||||
*
|
||||
* ~~~ {.rust}
|
||||
* for [1,2,3].each |&i| {
|
||||
* io::println(int::str(i));
|
||||
* }
|
||||
* ~~~
|
||||
*/
|
||||
#[inline]
|
||||
pub fn each<'r,T>(v: &'r [T], f: &fn(&'r T) -> bool) -> bool {
|
||||
// ^^^^
|
||||
// NB---this CANNOT be &const [T]! The reason
|
||||
// is that you are passing it to `f()` using
|
||||
// an immutable.
|
||||
|
||||
let mut broke = false;
|
||||
do as_imm_buf(v) |p, n| {
|
||||
let mut n = n;
|
||||
let mut p = p;
|
||||
while n > 0u {
|
||||
unsafe {
|
||||
let q = cast::copy_lifetime_vec(v, &*p);
|
||||
if !f(q) { break; }
|
||||
p = ptr::offset(p, 1u);
|
||||
}
|
||||
n -= 1u;
|
||||
}
|
||||
broke = n > 0;
|
||||
}
|
||||
return !broke;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over all permutations of vector `v`.
|
||||
*
|
||||
|
@ -3069,36 +3006,6 @@ mod tests {
|
|||
assert_eq!(v, ~[1, 3, 5]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_each_empty() {
|
||||
for each::<int>([]) |_v| {
|
||||
fail!(); // should never be executed
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_each_nonempty() {
|
||||
let mut i = 0;
|
||||
for each([1, 2, 3]) |v| {
|
||||
i += *v;
|
||||
}
|
||||
assert_eq!(i, 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_each_ret_len0() {
|
||||
let a0 : [int, .. 0] = [];
|
||||
assert_eq!(each(a0, |_p| fail!()), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_each_ret_len1() {
|
||||
let a1 = [17];
|
||||
assert_eq!(each(a1, |_p| true), true);
|
||||
assert_eq!(each(a1, |_p| false), false);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_each_permutation() {
|
||||
let mut results: ~[~[int]];
|
||||
|
@ -3854,21 +3761,6 @@ mod tests {
|
|||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore(windows)]
|
||||
#[should_fail]
|
||||
fn test_each_fail() {
|
||||
let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
|
||||
let mut i = 0;
|
||||
do each(v) |_elt| {
|
||||
if i == 2 {
|
||||
fail!()
|
||||
}
|
||||
i += 0;
|
||||
false
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore(windows)]
|
||||
#[should_fail]
|
||||
|
|
|
@ -86,7 +86,7 @@ fn make_graph(N: uint, edges: ~[(node_id, node_id)]) -> graph {
|
|||
HashSet::new()
|
||||
};
|
||||
|
||||
for vec::each(edges) |e| {
|
||||
for edges.iter().advance |e| {
|
||||
match *e {
|
||||
(i, j) => {
|
||||
graph[i].insert(j);
|
||||
|
@ -441,7 +441,7 @@ fn main() {
|
|||
let stop = time::precise_time_s();
|
||||
|
||||
let mut total_edges = 0;
|
||||
vec::each(graph, |edges| { total_edges += edges.len(); true });
|
||||
for graph.iter().advance |edges| { total_edges += edges.len(); }
|
||||
|
||||
io::stdout().write_line(fmt!("Generated graph with %? edges in %? seconds.",
|
||||
total_edges / 2,
|
||||
|
|
|
@ -83,7 +83,7 @@ fn run(args: &[~str]) {
|
|||
server(&from_parent, &to_parent);
|
||||
}
|
||||
|
||||
for vec::each(worker_results) |r| {
|
||||
for worker_results.iter().advance |r| {
|
||||
r.recv();
|
||||
}
|
||||
|
||||
|
|
|
@ -79,7 +79,7 @@ fn run(args: &[~str]) {
|
|||
server(&from_parent, &to_parent);
|
||||
}
|
||||
|
||||
for vec::each(worker_results) |r| {
|
||||
for worker_results.iter().advance |r| {
|
||||
r.recv();
|
||||
}
|
||||
|
||||
|
|
|
@ -188,7 +188,7 @@ fn rendezvous(nn: uint, set: ~[color]) {
|
|||
|
||||
// save each creature's meeting stats
|
||||
let mut report = ~[];
|
||||
for vec::each(to_creature) |_to_one| {
|
||||
for to_creature.iter().advance |_to_one| {
|
||||
report.push(from_creatures_log.recv());
|
||||
}
|
||||
|
||||
|
@ -196,7 +196,7 @@ fn rendezvous(nn: uint, set: ~[color]) {
|
|||
io::println(show_color_list(set));
|
||||
|
||||
// print each creature's stats
|
||||
for vec::each(report) |rep| {
|
||||
for report.iter().advance |rep| {
|
||||
io::println(*rep);
|
||||
}
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str {
|
|||
let mut pairs = ~[];
|
||||
|
||||
// map -> [(k,%)]
|
||||
for mm.each |&key, &val| {
|
||||
for mm.iter().advance |(&key, &val)| {
|
||||
pairs.push((key, pct(val, total)));
|
||||
}
|
||||
|
||||
|
|
|
@ -8,10 +8,8 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use std::vec;
|
||||
|
||||
fn main() {
|
||||
for vec::each(~[0]) |_i| { //~ ERROR A for-loop body must return (), but
|
||||
for 2.times { //~ ERROR A for-loop body must return (), but
|
||||
true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ struct Foo {
|
|||
|
||||
impl Foo {
|
||||
pub fn foo(&mut self, fun: &fn(&int)) {
|
||||
for self.n.each |f| {
|
||||
for self.n.iter().advance |f| {
|
||||
fun(f);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,10 +8,7 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use std::vec;
|
||||
|
||||
fn main() {
|
||||
for vec::each(fail!()) |i| {
|
||||
let _ = i * 2; //~ ERROR the type of this value must be known
|
||||
};
|
||||
let x = fail!();
|
||||
x.clone(); //~ ERROR the type of this value must be known in this context
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ use std::vec;
|
|||
|
||||
fn main() {
|
||||
let a: ~[int] = ~[];
|
||||
vec::each(a, |_| -> bool {
|
||||
a.iter().advance(|_| -> bool {
|
||||
//~^ ERROR mismatched types
|
||||
});
|
||||
}
|
||||
|
|
|
@ -12,21 +12,19 @@
|
|||
// making method calls, but only if there aren't any matches without
|
||||
// it.
|
||||
|
||||
use std::vec;
|
||||
|
||||
trait iterable<A> {
|
||||
fn iterate(&self, blk: &fn(x: &A) -> bool) -> bool;
|
||||
}
|
||||
|
||||
impl<'self,A> iterable<A> for &'self [A] {
|
||||
fn iterate(&self, f: &fn(x: &A) -> bool) -> bool {
|
||||
vec::each(*self, f)
|
||||
self.iter().advance(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> iterable<A> for ~[A] {
|
||||
fn iterate(&self, f: &fn(x: &A) -> bool) -> bool {
|
||||
vec::each(*self, f)
|
||||
self.iter().advance(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,11 +8,10 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use std::vec;
|
||||
|
||||
pub fn main() {
|
||||
let mut sum = 0;
|
||||
for vec::each(~[1, 2, 3, 4, 5]) |x| {
|
||||
let xs = ~[1, 2, 3, 4, 5];
|
||||
for xs.iter().advance |x| {
|
||||
sum += *x;
|
||||
}
|
||||
assert_eq!(sum, 15);
|
||||
|
|
|
@ -15,7 +15,7 @@ pub fn main() {
|
|||
let v = ~[-1f, 0f, 1f, 2f, 3f];
|
||||
|
||||
// Statement form does not require parentheses:
|
||||
for vec::each(v) |i| {
|
||||
for v.iter().advance |i| {
|
||||
info!("%?", *i);
|
||||
}
|
||||
|
||||
|
|
|
@ -8,12 +8,10 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use std::vec;
|
||||
|
||||
fn want_slice(v: &[int]) -> int {
|
||||
let mut sum = 0;
|
||||
for vec::each(v) |i| { sum += *i; }
|
||||
return sum;
|
||||
for v.iter().advance |i| { sum += *i; }
|
||||
sum
|
||||
}
|
||||
|
||||
fn has_mut_vec(v: ~[int]) -> int {
|
||||
|
|
|
@ -16,7 +16,8 @@ pub fn main() {
|
|||
assert_eq!(i, 10);
|
||||
loop { i += 1; if i == 20 { break; } }
|
||||
assert_eq!(i, 20);
|
||||
for vec::each(~[1, 2, 3, 4, 5, 6]) |x| {
|
||||
let xs = [1, 2, 3, 4, 5, 6];
|
||||
for xs.iter().advance |x| {
|
||||
if *x == 3 { break; } assert!((*x <= 3));
|
||||
}
|
||||
i = 0;
|
||||
|
@ -26,7 +27,8 @@ pub fn main() {
|
|||
i += 1; if i % 2 == 0 { loop; } assert!((i % 2 != 0));
|
||||
if i >= 10 { break; }
|
||||
}
|
||||
for vec::each(~[1, 2, 3, 4, 5, 6]) |x| {
|
||||
let ys = ~[1, 2, 3, 4, 5, 6];
|
||||
for ys.iter().advance |x| {
|
||||
if *x % 2 == 0 { loop; }
|
||||
assert!((*x % 2 != 0));
|
||||
}
|
||||
|
|
|
@ -23,6 +23,6 @@ struct S<'self>(&'self fn());
|
|||
static closures: &'static [S<'static>] = &[S(f), S(f)];
|
||||
|
||||
pub fn main() {
|
||||
for std::vec::each(bare_fns) |&bare_fn| { bare_fn() }
|
||||
for std::vec::each(closures) |&closure| { (*closure)() }
|
||||
for bare_fns.iter().advance |&bare_fn| { bare_fn() }
|
||||
for closures.iter().advance |&closure| { (*closure)() }
|
||||
}
|
||||
|
|
|
@ -8,10 +8,11 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// xfail-test: #3511: does not currently compile, due to rvalue issues
|
||||
|
||||
use std::vec;
|
||||
|
||||
struct Pair { x: int, y: int }
|
||||
|
||||
pub fn main() {
|
||||
for vec::each(~[Pair {x: 10, y: 20}, Pair {x: 30, y: 0}]) |elt| {
|
||||
assert_eq!(elt.x + elt.y, 30);
|
||||
|
|
|
@ -18,7 +18,7 @@ trait sum {
|
|||
impl<'self> sum for &'self [int] {
|
||||
fn sum(self) -> int {
|
||||
let mut sum = 0;
|
||||
for vec::each(self) |e| { sum += *e; }
|
||||
for self.iter().advance |e| { sum += *e; }
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,10 @@ trait map<T> {
|
|||
impl<T> map<T> for ~[T] {
|
||||
fn map<U:Copy>(&self, f: &fn(&T) -> U) -> ~[U] {
|
||||
let mut r = ~[];
|
||||
for std::vec::each(*self) |x| { r += ~[f(x)]; }
|
||||
// FIXME: #7355 generates bad code with Iterator
|
||||
for std::uint::range(0, self.len()) |i| {
|
||||
r += ~[f(&self[i])];
|
||||
}
|
||||
r
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue