Skip to content

Commit 8f71cb0

Browse files
committed
Implement Index for TrieMap
1 parent 11b8f9c commit 8f71cb0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/libcollections/trie.rs

+38
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,21 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
502502
}
503503
}
504504

505+
impl<T> Index<uint, T> for TrieMap<T> {
506+
#[inline]
507+
fn index<'a>(&'a self, i: &uint) -> &'a T {
508+
self.find(i).expect("key not present")
509+
}
510+
}
511+
512+
// FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
513+
/*impl<T> IndexMut<uint, T> for TrieMap<T> {
514+
#[inline]
515+
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut T {
516+
self.find_mut(i).expect("key not present")
517+
}
518+
}*/
519+
505520
/// A set implemented as a radix trie.
506521
///
507522
/// # Example
@@ -1391,6 +1406,29 @@ mod test_map {
13911406
assert!(map_str == "{1: a, 2: b}".to_string());
13921407
assert_eq!(format!("{}", empty), "{}".to_string());
13931408
}
1409+
1410+
#[test]
1411+
fn test_index() {
1412+
let mut map = TrieMap::new();
1413+
1414+
map.insert(1, 2i);
1415+
map.insert(2, 1i);
1416+
map.insert(3, 4i);
1417+
1418+
assert_eq!(map[2], 1);
1419+
}
1420+
1421+
#[test]
1422+
#[should_fail]
1423+
fn test_index_nonexistent() {
1424+
let mut map = TrieMap::new();
1425+
1426+
map.insert(1, 2i);
1427+
map.insert(2, 1i);
1428+
map.insert(3, 4i);
1429+
1430+
map[4];
1431+
}
13941432
}
13951433

13961434
#[cfg(test)]

0 commit comments

Comments
 (0)