Skip to content

Commit 11b8f9c

Browse files
committed
Implement Index for SmallIntMap
This also deprecates SmallIntMap::get. Use indexing instead.
1 parent 2dd6bc6 commit 11b8f9c

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

src/libcollections/smallintmap.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,15 @@ impl<V> SmallIntMap<V> {
209209
/// # Example
210210
///
211211
/// ```
212+
/// #![allow(deprecated)]
213+
///
212214
/// use std::collections::SmallIntMap;
213215
///
214216
/// let mut map = SmallIntMap::new();
215217
/// map.insert(1, "a");
216218
/// assert_eq!(map.get(&1), &"a");
217219
/// ```
220+
#[deprecated = "prefer using indexing, e.g., map[0]"]
218221
pub fn get<'a>(&'a self, key: &uint) -> &'a V {
219222
self.find(key).expect("key not present")
220223
}
@@ -330,11 +333,11 @@ impl<V:Clone> SmallIntMap<V> {
330333
///
331334
/// // Key does not exist, will do a simple insert
332335
/// assert!(map.update(1, vec![1i, 2], |old, new| old.append(new.as_slice())));
333-
/// assert_eq!(map.get(&1), &vec![1i, 2]);
336+
/// assert_eq!(map[1], vec![1i, 2]);
334337
///
335338
/// // Key exists, update the value
336339
/// assert!(!map.update(1, vec![3i, 4], |old, new| old.append(new.as_slice())));
337-
/// assert_eq!(map.get(&1), &vec![1i, 2, 3, 4]);
340+
/// assert_eq!(map[1], vec![1i, 2, 3, 4]);
338341
/// ```
339342
pub fn update(&mut self, key: uint, newval: V, ff: |V, V| -> V) -> bool {
340343
self.update_with_key(key, newval, |_k, v, v1| ff(v,v1))
@@ -354,11 +357,11 @@ impl<V:Clone> SmallIntMap<V> {
354357
///
355358
/// // Key does not exist, will do a simple insert
356359
/// assert!(map.update_with_key(7, 10, |key, old, new| (old + new) % key));
357-
/// assert_eq!(map.get(&7), &10);
360+
/// assert_eq!(map[7], 10);
358361
///
359362
/// // Key exists, update the value
360363
/// assert!(!map.update_with_key(7, 20, |key, old, new| (old + new) % key));
361-
/// assert_eq!(map.get(&7), &2);
364+
/// assert_eq!(map[7], 2);
362365
/// ```
363366
pub fn update_with_key(&mut self,
364367
key: uint,
@@ -416,6 +419,21 @@ impl<V> Extendable<(uint, V)> for SmallIntMap<V> {
416419
}
417420
}
418421

422+
impl<V> Index<uint, V> for SmallIntMap<V> {
423+
#[inline]
424+
fn index<'a>(&'a self, i: &uint) -> &'a V {
425+
self.get(i)
426+
}
427+
}
428+
429+
// FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
430+
/*impl<V> IndexMut<uint, V> for SmallIntMap<V> {
431+
#[inline]
432+
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V {
433+
self.find_mut(i).expect("key not present")
434+
}
435+
}*/
436+
419437
macro_rules! iterator {
420438
(impl $name:ident -> $elem:ty, $getter:ident) => {
421439
impl<'a, T> Iterator<$elem> for $name<'a, T> {
@@ -843,6 +861,29 @@ mod test_map {
843861
assert_eq!(map.find(&k), Some(&v));
844862
}
845863
}
864+
865+
#[test]
866+
fn test_index() {
867+
let mut map: SmallIntMap<int> = SmallIntMap::new();
868+
869+
map.insert(1, 2);
870+
map.insert(2, 1);
871+
map.insert(3, 4);
872+
873+
assert_eq!(map[3], 4);
874+
}
875+
876+
#[test]
877+
#[should_fail]
878+
fn test_index_nonexistent() {
879+
let mut map: SmallIntMap<int> = SmallIntMap::new();
880+
881+
map.insert(1, 2);
882+
map.insert(2, 1);
883+
map.insert(3, 4);
884+
885+
map[4];
886+
}
846887
}
847888

848889
#[cfg(test)]

0 commit comments

Comments
 (0)