diff options
| author | bors <bors@rust-lang.org> | 2013-03-14 15:06:49 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-03-14 15:06:49 -0700 |
| commit | 0c7aeddb5fa6ed86f41363b7552ff38aa96a9482 (patch) | |
| tree | ce5d4541063e5cf648fc2b0a0fe80539f2631230 /src/libcore | |
| parent | 2293b075b83d78b8139c0767c8034582112737bc (diff) | |
| parent | becad9bb07423ed4d0d8b192cce83de99b535e86 (diff) | |
| download | rust-0c7aeddb5fa6ed86f41363b7552ff38aa96a9482.tar.gz rust-0c7aeddb5fa6ed86f41363b7552ff38aa96a9482.zip | |
auto merge of #5365 : thestinger/rust/map, r=catamorphism
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/container.rs | 3 | ||||
| -rw-r--r-- | src/libcore/hashmap.rs | 13 | ||||
| -rw-r--r-- | src/libcore/trie.rs | 22 |
3 files changed, 27 insertions, 11 deletions
diff --git a/src/libcore/container.rs b/src/libcore/container.rs index 4f1f6004aad..828678b65f7 100644 --- a/src/libcore/container.rs +++ b/src/libcore/container.rs @@ -35,6 +35,9 @@ pub trait Map<K, V>: Mutable { /// Visit all values pure fn each_value(&self, f: &fn(&V) -> bool); + /// Iterate over the map and mutate the contained values + fn mutate_values(&mut self, f: &fn(&K, &mut V) -> bool); + /// Return the value corresponding to the key in the map pure fn find(&self, key: &K) -> Option<&self/V>; diff --git a/src/libcore/hashmap.rs b/src/libcore/hashmap.rs index 931866999c4..cc7dafad169 100644 --- a/src/libcore/hashmap.rs +++ b/src/libcore/hashmap.rs @@ -325,6 +325,19 @@ pub mod linear { self.each(|&(_, v)| blk(v)) } + /// Iterate over the map and mutate the contained values + fn mutate_values(&mut self, blk: &fn(&'self K, + &'self mut V) -> bool) { + for uint::range(0, self.buckets.len()) |i| { + match self.buckets[i] { + Some(Bucket{key: ref key, value: ref mut value, _}) => { + if !blk(key, value) { return } + } + None => () + } + } + } + /// Return the value corresponding to the key in the map pure fn find(&self, k: &K) -> Option<&self/V> { match self.bucket_for_key(k) { diff --git a/src/libcore/trie.rs b/src/libcore/trie.rs index 7dc85cba297..966db4ec662 100644 --- a/src/libcore/trie.rs +++ b/src/libcore/trie.rs @@ -81,11 +81,16 @@ impl<T> Map<uint, T> for TrieMap<T> { /// Visit all values in order #[inline(always)] - pure fn each_value(&self, - f: &fn(&T) -> bool) { + pure fn each_value(&self, f: &fn(&T) -> bool) { self.each(|&(_, v)| f(v)) } + /// Iterate over the map and mutate the contained values + #[inline(always)] + fn mutate_values(&mut self, f: &fn(&uint, &mut T) -> bool) { + self.root.mutate_values(f); + } + /// Return the value corresponding to the key in the map #[inline(hint)] pure fn find(&self, key: &uint) -> Option<&self/T> { @@ -150,11 +155,6 @@ impl<T> TrieMap<T> { pure fn each_value_reverse(&self, f: &fn(&T) -> bool) { self.each_reverse(|&(_, v)| f(v)) } - - /// Iterate over the map and mutate the contained values - fn mutate_values(&mut self, f: &fn(uint, &mut T) -> bool) { - self.root.mutate_values(f); - } } pub struct TrieSet { @@ -248,13 +248,13 @@ impl<T> TrieNode<T> { true } - fn mutate_values(&mut self, f: &fn(uint, &mut T) -> bool) -> bool { + fn mutate_values(&mut self, f: &fn(&uint, &mut T) -> bool) -> bool { for vec::each_mut(self.children) |child| { match *child { Internal(ref mut x) => if !x.mutate_values(f) { return false }, - External(k, ref mut v) => if !f(k, v) { return false }, + External(k, ref mut v) => if !f(&k, v) { return false }, Nothing => () } } @@ -269,8 +269,8 @@ pure fn chunk(n: uint, idx: uint) -> uint { (n >> (SHIFT * real_idx)) & MASK } -fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint, - value: T, idx: uint) -> bool { +fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint, value: T, + idx: uint) -> bool { let mut tmp = Nothing; tmp <-> *child; let mut added = false; |
