Skip to content

Commit 0c7aedd

Browse files
committed
auto merge of #5365 : thestinger/rust/map, r=catamorphism
2 parents 2293b07 + becad9b commit 0c7aedd

File tree

6 files changed

+61
-12
lines changed

6 files changed

+61
-12
lines changed

src/libcore/container.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ pub trait Map<K, V>: Mutable {
3535
/// Visit all values
3636
pure fn each_value(&self, f: &fn(&V) -> bool);
3737

38+
/// Iterate over the map and mutate the contained values
39+
fn mutate_values(&mut self, f: &fn(&K, &mut V) -> bool);
40+
3841
/// Return the value corresponding to the key in the map
3942
pure fn find(&self, key: &K) -> Option<&self/V>;
4043

src/libcore/hashmap.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,19 @@ pub mod linear {
325325
self.each(|&(_, v)| blk(v))
326326
}
327327
328+
/// Iterate over the map and mutate the contained values
329+
fn mutate_values(&mut self, blk: &fn(&'self K,
330+
&'self mut V) -> bool) {
331+
for uint::range(0, self.buckets.len()) |i| {
332+
match self.buckets[i] {
333+
Some(Bucket{key: ref key, value: ref mut value, _}) => {
334+
if !blk(key, value) { return }
335+
}
336+
None => ()
337+
}
338+
}
339+
}
340+
328341
/// Return the value corresponding to the key in the map
329342
pure fn find(&self, k: &K) -> Option<&self/V> {
330343
match self.bucket_for_key(k) {

src/libcore/trie.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,16 @@ impl<T> Map<uint, T> for TrieMap<T> {
8181

8282
/// Visit all values in order
8383
#[inline(always)]
84-
pure fn each_value(&self,
85-
f: &fn(&T) -> bool) {
84+
pure fn each_value(&self, f: &fn(&T) -> bool) {
8685
self.each(|&(_, v)| f(v))
8786
}
8887

88+
/// Iterate over the map and mutate the contained values
89+
#[inline(always)]
90+
fn mutate_values(&mut self, f: &fn(&uint, &mut T) -> bool) {
91+
self.root.mutate_values(f);
92+
}
93+
8994
/// Return the value corresponding to the key in the map
9095
#[inline(hint)]
9196
pure fn find(&self, key: &uint) -> Option<&self/T> {
@@ -150,11 +155,6 @@ impl<T> TrieMap<T> {
150155
pure fn each_value_reverse(&self, f: &fn(&T) -> bool) {
151156
self.each_reverse(|&(_, v)| f(v))
152157
}
153-
154-
/// Iterate over the map and mutate the contained values
155-
fn mutate_values(&mut self, f: &fn(uint, &mut T) -> bool) {
156-
self.root.mutate_values(f);
157-
}
158158
}
159159

160160
pub struct TrieSet {
@@ -248,13 +248,13 @@ impl<T> TrieNode<T> {
248248
true
249249
}
250250

251-
fn mutate_values(&mut self, f: &fn(uint, &mut T) -> bool) -> bool {
251+
fn mutate_values(&mut self, f: &fn(&uint, &mut T) -> bool) -> bool {
252252
for vec::each_mut(self.children) |child| {
253253
match *child {
254254
Internal(ref mut x) => if !x.mutate_values(f) {
255255
return false
256256
},
257-
External(k, ref mut v) => if !f(k, v) { return false },
257+
External(k, ref mut v) => if !f(&k, v) { return false },
258258
Nothing => ()
259259
}
260260
}
@@ -269,8 +269,8 @@ pure fn chunk(n: uint, idx: uint) -> uint {
269269
(n >> (SHIFT * real_idx)) & MASK
270270
}
271271

272-
fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint,
273-
value: T, idx: uint) -> bool {
272+
fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint, value: T,
273+
idx: uint) -> bool {
274274
let mut tmp = Nothing;
275275
tmp <-> *child;
276276
let mut added = false;

src/libstd/smallintmap.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,17 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
8585
self.each(|&(_, v)| blk(v))
8686
}
8787

88-
/// Return the value corresponding to the key in the map
88+
/// Visit all key-value pairs in order
89+
fn mutate_values(&mut self, it: &fn(&uint, &'self mut V) -> bool) {
90+
for uint::range(0, self.v.len()) |i| {
91+
match self.v[i] {
92+
Some(ref mut elt) => if !it(&i, elt) { break },
93+
None => ()
94+
}
95+
}
96+
}
97+
98+
/// Iterate over the map and mutate the contained values
8999
pure fn find(&self, key: &uint) -> Option<&self/V> {
90100
if *key < self.v.len() {
91101
match self.v[*key] {

src/libstd/treemap.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ impl<K: TotalOrd, V> Map<K, V> for TreeMap<K, V> {
134134
self.each(|&(_, v)| f(v))
135135
}
136136

137+
/// Iterate over the map and mutate the contained values
138+
fn mutate_values(&mut self, f: &fn(&'self K, &'self mut V) -> bool) {
139+
mutate_values(&mut self.root, f);
140+
}
141+
137142
/// Return the value corresponding to the key in the map
138143
pure fn find(&self, key: &K) -> Option<&self/V> {
139144
let mut current: &self/Option<~TreeNode<K, V>> = &self.root;
@@ -558,6 +563,20 @@ pure fn each_reverse<K: TotalOrd, V>(node: &r/Option<~TreeNode<K, V>>,
558563
}
559564
}
560565

566+
fn mutate_values<K: TotalOrd, V>(node: &'r mut Option<~TreeNode<K, V>>,
567+
f: &fn(&'r K, &'r mut V) -> bool) -> bool {
568+
match *node {
569+
Some(~TreeNode{key: ref key, value: ref mut value, left: ref mut left,
570+
right: ref mut right, _}) => {
571+
if !mutate_values(left, f) { return false }
572+
if !f(key, value) { return false }
573+
if !mutate_values(right, f) { return false }
574+
}
575+
None => return false
576+
}
577+
true
578+
}
579+
561580
// Remove left horizontal link by rotating right
562581
fn skew<K: TotalOrd, V>(node: &mut ~TreeNode<K, V>) {
563582
if node.left.map_default(false, |x| x.level == node.level) {

src/test/run-pass/class-impl-very-parameterized-trait.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ impl<T> Map<int, T> for cat<T> {
8181
for self.each |&(_, v)| { if !f(v) { break; } loop;};
8282
}
8383

84+
fn mutate_values(&mut self, f: &fn(&int, &mut T) -> bool) {
85+
fail!(~"nope")
86+
}
87+
8488
fn insert(&mut self, k: int, _: T) -> bool {
8589
self.meows += k;
8690
true

0 commit comments

Comments
 (0)