Skip to content

Commit 7a1b8a9

Browse files
committed
---
yaml --- r: 54106 b: refs/heads/dist-snap c: f0f4a00 h: refs/heads/master v: v3
1 parent 9ea4058 commit 7a1b8a9

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
99
refs/heads/incoming: 44d4d6de762f3f9aae1fedcf454c66b79b3ad58d
10-
refs/heads/dist-snap: 89e2578a9d1cd18270b7148a5d5d6b8bee051ac5
10+
refs/heads/dist-snap: f0f4a00e88fc374b2b3096789a11bf429d42c3a9
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1313
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/src/libstd/smallintmap.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
8686
self.each(|&(_, v)| blk(v))
8787
}
8888

89-
/// Visit all key-value pairs in order
89+
/// Iterate over the map and mutate the contained values
9090
fn mutate_values(&mut self, it: &fn(&uint, &'self mut V) -> bool) {
9191
for uint::range(0, self.v.len()) |i| {
9292
match self.v[i] {
@@ -96,7 +96,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
9696
}
9797
}
9898

99-
/// Iterate over the map and mutate the contained values
99+
/// Return a reference to the value corresponding to the key
100100
fn find(&self, key: &uint) -> Option<&'self V> {
101101
if *key < self.v.len() {
102102
match self.v[*key] {
@@ -140,6 +140,18 @@ pub impl<V> SmallIntMap<V> {
140140
fn get(&self, key: &uint) -> &'self V {
141141
self.find(key).expect("key not present")
142142
}
143+
144+
/// Return a mutable reference to the value corresponding to the key
145+
fn find_mut(&mut self, key: &uint) -> Option<&'self mut V> {
146+
if *key < self.v.len() {
147+
match self.v[*key] {
148+
Some(ref mut value) => Some(value),
149+
None => None
150+
}
151+
} else {
152+
None
153+
}
154+
}
143155
}
144156

145157
pub impl<V:Copy> SmallIntMap<V> {
@@ -160,6 +172,20 @@ pub impl<V:Copy> SmallIntMap<V> {
160172
#[cfg(test)]
161173
mod tests {
162174
use super::SmallIntMap;
175+
use core::prelude::*;
176+
177+
#[test]
178+
fn test_find_mut() {
179+
let mut m = SmallIntMap::new();
180+
fail_unless!(m.insert(1, 12));
181+
fail_unless!(m.insert(2, 8));
182+
fail_unless!(m.insert(5, 14));
183+
let new = 100;
184+
match m.find_mut(&5) {
185+
None => fail!(), Some(x) => *x = new
186+
}
187+
assert_eq!(m.find(&5), Some(&new));
188+
}
163189

164190
#[test]
165191
fn test_len() {

0 commit comments

Comments
 (0)