Skip to content

Commit 9053f54

Browse files
committed
Move map iface over to more for-friendly iteration methods
1 parent a872a99 commit 9053f54

File tree

18 files changed

+74
-83
lines changed

18 files changed

+74
-83
lines changed

src/cargo/cargo.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ fn try_parse_sources(filename: str, sources: map::hashmap<str, source>) {
222222
let c = io::read_whole_file_str(filename);
223223
alt json::from_str(result::get(c)) {
224224
ok(json::dict(j)) {
225-
j.items { |k, v|
225+
for j.each { |k, v|
226226
sources.insert(k, parse_source(k, v));
227227
#debug("source: %s", k);
228228
}
@@ -404,11 +404,11 @@ fn configure(opts: options) -> cargo {
404404
need_dir(c.libdir);
405405
need_dir(c.bindir);
406406

407-
sources.keys { |k|
407+
for sources.each_key { |k|
408408
let mut s = sources.get(k);
409409
load_source_packages(c, s);
410410
sources.insert(k, s);
411-
};
411+
}
412412

413413
if c.pgp {
414414
pgp::init(c.root);
@@ -422,11 +422,11 @@ fn configure(opts: options) -> cargo {
422422
}
423423

424424
fn for_each_package(c: cargo, b: fn(source, package)) {
425-
c.sources.values({ |v|
425+
for c.sources.each_value {|v|
426426
for vec::each(copy v.packages) {|p|
427427
b(v, p);
428428
}
429-
})
429+
}
430430
}
431431

432432
// Runs all programs in directory <buildpath>
@@ -592,7 +592,7 @@ fn cargo_suggestion(c: cargo, syncing: bool, fallback: fn())
592592
}
593593
if !syncing {
594594
let mut npkg = 0u;
595-
c.sources.values({ |v| npkg += vec::len(v.packages) });
595+
for c.sources.each_value { |v| npkg += vec::len(v.packages) }
596596
if npkg == 0u {
597597
error("No packages known. You may wish to run " +
598598
"\"cargo sync\".");
@@ -776,7 +776,7 @@ fn cmd_sync(c: cargo) {
776776
sync_one(c, c.opts.free[2], c.sources.get(c.opts.free[2]));
777777
} else {
778778
cargo_suggestion(c, true, { || } );
779-
c.sources.items { |k, v|
779+
for c.sources.each { |k, v|
780780
sync_one(c, k, v);
781781
}
782782
}

src/librustsyntax/ext/simplext.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn use_selectors_to_bind(b: binders, e: @expr) -> option<bindings> {
167167
alt sel(match_expr(e)) { none { ret none; } _ { } }
168168
}
169169
let mut never_mind: bool = false;
170-
b.real_binders.items {|key, val|
170+
for b.real_binders.each {|key, val|
171171
alt val(match_expr(e)) {
172172
none { never_mind = true; }
173173
some(mtc) { res.insert(key, mtc); }
@@ -251,7 +251,7 @@ fn free_vars(b: bindings, e: @expr, it: fn(ident)) {
251251
with *default_ast_fold()};
252252
let f = make_fold(f_pre);
253253
f.fold_expr(e); // ignore result
254-
idents.keys {|x| it(x); };
254+
for idents.each_key {|x| it(x); };
255255
}
256256

257257

src/librustsyntax/parse/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ nonetheless valid as identifiers becasue they are unambiguous.
217217
"]
218218
fn keyword_table() -> hashmap<str, ()> {
219219
let keywords = str_hash();
220-
bad_expr_word_table().keys() {|word|
220+
for bad_expr_word_table().each_key {|word|
221221
keywords.insert(word, ());
222222
}
223223
let other_keywords = [

src/libstd/json.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn to_writer(wr: io::writer, j: json) {
8585

8686
wr.write_str("{ ");
8787
let mut first = true;
88-
d.items { |key, value|
88+
for d.each { |key, value|
8989
if !first {
9090
wr.write_str(", ");
9191
}
@@ -481,7 +481,7 @@ fn eq(value0: json, value1: json) -> bool {
481481
(dict(d0), dict(d1)) {
482482
if d0.size() == d1.size() {
483483
let mut equal = true;
484-
d0.items { |k, v0|
484+
for d0.each { |k, v0|
485485
alt d1.find(k) {
486486
some(v1) {
487487
if !eq(v0, v1) { equal = false; } }

src/libstd/map.rs

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ iface map<K: copy, V: copy> {
5656
fn remove(K) -> option<V>;
5757

5858
#[doc = "Iterate over all the key/value pairs in the map"]
59-
fn items(fn(K, V));
59+
fn each(fn(K, V) -> bool);
6060

6161
#[doc = "Iterate over all the keys in the map"]
62-
fn keys(fn(K));
62+
fn each_key(fn(K) -> bool);
6363

6464
#[doc = "Iterate over all the values in the map"]
65-
fn values(fn(V));
65+
fn each_value(fn(V) -> bool);
6666
}
6767

6868
// FIXME: package this up and export it as a datatype usable for
@@ -207,49 +207,40 @@ mod chained {
207207
ret vec::to_mut(vec::from_elem(nchains, absent));
208208
}
209209

210-
fn foreach_entry<K: copy, V: copy>(chain0: chain<K,V>,
211-
blk: fn(@entry<K,V>)) {
212-
let mut chain = chain0;
213-
loop {
214-
alt chain {
215-
absent { ret; }
216-
present(entry) {
217-
let next = entry.next;
218-
blk(entry); // may modify entry.next!
219-
chain = next;
220-
}
221-
}
222-
}
223-
}
224-
225-
fn foreach_chain<K: copy, V: copy>(chains: [const chain<K,V>],
226-
blk: fn(@entry<K,V>)) {
227-
let mut i = 0u;
228-
let n = vec::len(chains);
210+
fn each_entry<K: copy, V: copy>(tbl: t<K, V>,
211+
blk: fn(@entry<K,V>) -> bool) {
212+
let mut i = 0u, n = vec::len(tbl.chains);
229213
while i < n {
230-
foreach_entry(chains[i], blk);
214+
let mut chain = tbl.chains[i];
215+
loop {
216+
alt chain {
217+
absent { break; }
218+
present(entry) {
219+
let next = entry.next;
220+
if !blk(entry) { ret; }
221+
chain = next;
222+
}
223+
}
224+
}
231225
i += 1u;
232226
}
233227
}
234228

235229
fn rehash<K: copy, V: copy>(tbl: t<K,V>) {
236-
let old_chains = tbl.chains;
237-
let n_old_chains = vec::len(old_chains);
230+
let n_old_chains = vec::len(tbl.chains);
238231
let n_new_chains: uint = uint::next_power_of_two(n_old_chains + 1u);
239-
tbl.chains = chains(n_new_chains);
240-
foreach_chain(old_chains) { |entry|
232+
let new_chains = chains(n_new_chains);
233+
for each_entry(tbl) {|entry|
241234
let idx = entry.hash % n_new_chains;
242-
entry.next = tbl.chains[idx];
243-
tbl.chains[idx] = present(entry);
235+
entry.next = new_chains[idx];
236+
new_chains[idx] = present(entry);
244237
}
238+
tbl.chains = new_chains;
245239
}
246240

247-
fn items<K: copy, V: copy>(tbl: t<K,V>, blk: fn(K,V)) {
248-
let tbl_chains = tbl.chains; // Satisfy alias checker.
249-
foreach_chain(tbl_chains) { |entry|
250-
let key = entry.key;
251-
let value = entry.value;
252-
blk(key, value);
241+
fn each<K: copy, V: copy>(tbl: t<K,V>, blk: fn(K,V) -> bool) {
242+
for each_entry(tbl) {|entry|
243+
if !blk(copy entry.key, copy entry.value) { break; }
253244
}
254245
}
255246

@@ -277,11 +268,11 @@ mod chained {
277268

278269
fn remove(k: K) -> option<V> { remove(self, k) }
279270

280-
fn items(blk: fn(K, V)) { items(self, blk); }
271+
fn each(blk: fn(K, V) -> bool) { each(self, blk); }
281272

282-
fn keys(blk: fn(K)) { items(self) { |k, _v| blk(k) } }
273+
fn each_key(blk: fn(K) -> bool) { each(self) { |k, _v| blk(k)} }
283274

284-
fn values(blk: fn(V)) { items(self) { |_k, v| blk(v) } }
275+
fn each_value(blk: fn(V) -> bool) { each(self) { |_k, v| blk(v)} }
285276
}
286277

287278
fn mk<K: copy, V: copy>(hasher: hashfn<K>, eqer: eqfn<K>) -> t<K,V> {

src/libstd/smallintmap.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,27 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
8989
fn get(&&key: uint) -> V { get(self, key) }
9090
fn find(&&key: uint) -> option<V> { find(self, key) }
9191
fn rehash() { fail }
92-
fn items(it: fn(&&uint, V)) {
92+
fn each(it: fn(&&uint, V) -> bool) {
9393
let mut idx = 0u, l = self.v.len();
9494
while idx < l {
9595
alt self.v[idx] {
9696
some(elt) {
97-
it(idx, copy elt);
97+
if !it(idx, copy elt) { break; }
9898
}
9999
none { }
100100
}
101101
idx += 1u;
102102
}
103103
}
104-
fn keys(it: fn(&&uint)) {
104+
fn each_key(it: fn(&&uint) -> bool) {
105105
let mut idx = 0u, l = self.v.len();
106106
while idx < l {
107-
if self.v[idx] != none { it(idx); }
107+
if self.v[idx] != none && !it(idx) { ret; }
108108
idx += 1u;
109109
}
110110
}
111-
fn values(it: fn(V)) {
112-
self.items({|_i, v| it(v)});
111+
fn each_value(it: fn(V) -> bool) {
112+
self.each {|_i, v| it(v)}
113113
}
114114
}
115115

src/rustc/metadata/cstore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn have_crate_data(cstore: cstore, cnum: ast::crate_num) -> bool {
106106
}
107107

108108
fn iter_crate_data(cstore: cstore, i: fn(ast::crate_num, crate_metadata)) {
109-
p(cstore).metas.items {|k,v| i(k, v);};
109+
for p(cstore).metas.each {|k,v| i(k, v);};
110110
}
111111

112112
fn add_used_crate_file(cstore: cstore, lib: str) {
@@ -155,7 +155,7 @@ fn get_dep_hashes(cstore: cstore) -> [str] {
155155
type crate_hash = {name: str, hash: str};
156156
let mut result = [];
157157

158-
p(cstore).use_crate_map.values {|cnum|
158+
for p(cstore).use_crate_map.each_value {|cnum|
159159
let cdata = cstore::get_crate_data(cstore, cnum);
160160
let hash = decoder::get_crate_hash(cdata.data);
161161
#debug("Add hash[%s]: %s", cdata.name, hash);

src/rustc/metadata/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ fn encode_item_paths(ebml_w: ebml::writer, ecx: @encode_ctxt, crate: @crate)
206206
fn encode_reexport_paths(ebml_w: ebml::writer,
207207
ecx: @encode_ctxt, &index: [entry<str>]) {
208208
let tcx = ecx.ccx.tcx;
209-
ecx.ccx.exp_map.items {|exp_id, defs|
209+
for ecx.ccx.exp_map.each {|exp_id, defs|
210210
for defs.each {|def|
211211
if !def.reexp { cont; }
212212
let path = alt check tcx.items.get(exp_id) {

src/rustc/middle/capture.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,6 @@ fn compute_capture_vars(tcx: ty::ctxt,
129129
}
130130

131131
let mut result = [];
132-
cap_map.values { |cap_var| result += [cap_var]; }
132+
for cap_map.each_value { |cap_var| result += [cap_var]; }
133133
ret result;
134134
}

src/rustc/middle/last_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn find_last_uses(c: @crate, def_map: resolve::def_map,
7070
mut blocks: nil};
7171
visit::visit_crate(*c, cx, v);
7272
let mini_table = std::map::int_hash();
73-
cx.last_uses.items {|key, val|
73+
for cx.last_uses.each {|key, val|
7474
if val {
7575
alt key {
7676
path(id) {

src/rustc/middle/lint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn time(do_it: bool, what: str, thunk: fn()) {
172172

173173
fn check_item(cx: ctxt, i: @ast::item) {
174174
cx.with_warn_attrs(i.attrs) {|cx|
175-
cx.curr.items {|lint, level|
175+
for cx.curr.each {|lint, level|
176176
alt lint {
177177
ctypes { check_item_ctypes(cx, level, i); }
178178
unused_imports { check_item_unused_imports(cx, level, i); }
@@ -265,7 +265,7 @@ fn check_crate(tcx: ty::ctxt, crate: @ast::crate,
265265
tcx: tcx};
266266

267267
// Install defaults.
268-
cx.dict.items {|_k, spec| cx.set_level(spec.lint, spec.default); }
268+
for cx.dict.each {|_k, spec| cx.set_level(spec.lint, spec.default); }
269269

270270
// Install command-line options, overriding defaults.
271271
for lint_opts.each {|pair|

src/rustc/middle/resolve.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ fn map_crate(e: @env, c: @ast::crate) {
352352

353353
fn resolve_imports(e: env) {
354354
e.used_imports.track = true;
355-
e.imports.items {|id, v|
355+
for e.imports.each {|id, v|
356356
alt check v {
357357
todo(name, path, span, scopes) {
358358
resolve_import(e, id, name, *path, span, scopes);
@@ -368,7 +368,7 @@ fn resolve_imports(e: env) {
368368
// using lint-specific control flags presently but resolve-specific data
369369
// structures. Should use the general lint framework (with scopes, attrs).
370370
fn check_unused_imports(e: @env, level: lint::level) {
371-
e.imports.items {|k, v|
371+
for e.imports.each {|k, v|
372372
alt v {
373373
resolved(_, _, _, _, name, sp) {
374374
if !vec::contains(e.used_imports.data, k) {
@@ -1673,8 +1673,8 @@ fn lookup_external(e: env, cnum: int, ids: [ident], ns: namespace) ->
16731673
fn check_for_collisions(e: @env, c: ast::crate) {
16741674
// Module indices make checking those relatively simple -- just check each
16751675
// name for multiple entities in the same namespace.
1676-
e.mod_map.values {|val|
1677-
val.index.items {|k, v| check_mod_name(*e, k, v); };
1676+
for e.mod_map.each_value {|val|
1677+
for val.index.each {|k, v| check_mod_name(*e, k, v); };
16781678
};
16791679
// Other scopes have to be checked the hard way.
16801680
let v =
@@ -1912,7 +1912,7 @@ fn check_exports(e: @env) {
19121912
assert mid.crate == ast::local_crate;
19131913
let ixm = e.mod_map.get(mid.node);
19141914

1915-
ixm.index.items() {|ident, mies|
1915+
for ixm.index.each {|ident, mies|
19161916
list::iter(mies) {|mie|
19171917
alt mie {
19181918
mie_item(item) {
@@ -2055,7 +2055,7 @@ fn check_exports(e: @env) {
20552055
}
20562056
}
20572057

2058-
e.mod_map.values {|_mod|
2058+
for e.mod_map.each_value {|_mod|
20592059
alt _mod.m {
20602060
some(m) {
20612061
let glob_is_re_exported = int_hash();

src/rustc/middle/trans/alt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ fn compile_submatch(bcx: block, m: match, vals: [ValueRef],
365365
some(e) {
366366
// Temporarily set bindings. They'll be rewritten to PHI nodes
367367
// for the actual arm block.
368-
data.id_map.items {|key, val|
368+
for data.id_map.each {|key, val|
369369
let loc = local_mem(option::get(assoc(key, m[0].bound)));
370370
bcx.fcx.lllocals.insert(val, loc);
371371
};
@@ -565,7 +565,7 @@ fn make_phi_bindings(bcx: block, map: [exit_node],
565565
let _icx = bcx.insn_ctxt("alt::make_phi_bindings");
566566
let our_block = bcx.llbb as uint;
567567
let mut success = true, bcx = bcx;
568-
ids.items {|name, node_id|
568+
for ids.each {|name, node_id|
569569
let mut llbbs = [];
570570
let mut vals = [];
571571
for vec::each(map) {|ex|
@@ -583,7 +583,7 @@ fn make_phi_bindings(bcx: block, map: [exit_node],
583583
};
584584
if success {
585585
// Copy references that the alias analysis considered unsafe
586-
ids.values {|node_id|
586+
for ids.each_value {|node_id|
587587
if bcx.ccx().maps.copy_map.contains_key(node_id) {
588588
let local = alt bcx.fcx.lllocals.find(node_id) {
589589
some(local_mem(x)) { x }

0 commit comments

Comments
 (0)