implement ReverseIter for SmallIntMap

This commit is contained in:
Daniel Micay 2013-02-07 20:20:39 -05:00
parent a2f922f282
commit fe841f3e56

View File

@ -14,7 +14,7 @@
*/ */
use core::container::{Container, Mutable, Map, Set}; use core::container::{Container, Mutable, Map, Set};
use core::iter::BaseIter; use core::iter::{BaseIter, ReverseIter};
use core::option::{Some, None}; use core::option::{Some, None};
use core::prelude::*; use core::prelude::*;
@ -36,6 +36,18 @@ impl<V> SmallIntMap<V>: BaseIter<(uint, &V)> {
pure fn size_hint(&self) -> Option<uint> { Some(self.len()) } pure fn size_hint(&self) -> Option<uint> { Some(self.len()) }
} }
impl<V> SmallIntMap<V>: ReverseIter<(uint, &V)> {
/// Visit all key-value pairs in reverse order
pure fn each_reverse(&self, it: fn(&(uint, &self/V)) -> bool) {
for uint::range_rev(self.v.len(), 0) |i| {
match self.v[i - 1] {
Some(ref elt) => if !it(&(i - 1, elt)) { break },
None => ()
}
}
}
}
impl<V> SmallIntMap<V>: Container { impl<V> SmallIntMap<V>: Container {
/// Return the number of elements in the map /// Return the number of elements in the map
pure fn len(&self) -> uint { pure fn len(&self) -> uint {