1
Fork 0

Implement size_hint() on all remaining Iterators

Add size_hint() to the Iterators in libextra and the Iterator in
libsyntax.

Skip deque for the moment, as it's being worked on elsewhere.
This commit is contained in:
Kevin Ballard 2013-07-03 11:30:12 -07:00
parent 20016b92c8
commit e6f9b08610
3 changed files with 20 additions and 2 deletions

View file

@ -186,6 +186,9 @@ pub struct PriorityQueueIterator <'self, T> {
impl<'self, T> Iterator<&'self T> for PriorityQueueIterator<'self, T> {
#[inline]
fn next(&mut self) -> Option<(&'self T)> { self.iter.next() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
}
#[cfg(test)]

View file

@ -196,14 +196,15 @@ impl<K: TotalOrd, V> TreeMap<K, V> {
/// Get a lazy iterator over the key-value pairs in the map.
/// Requires that it be frozen (immutable).
pub fn iter<'a>(&'a self) -> TreeMapIterator<'a, K, V> {
TreeMapIterator{stack: ~[], node: &self.root}
TreeMapIterator{stack: ~[], node: &self.root, remaining: self.length}
}
}
/// Lazy forward iterator over a map
pub struct TreeMapIterator<'self, K, V> {
priv stack: ~[&'self ~TreeNode<K, V>],
priv node: &'self Option<~TreeNode<K, V>>
priv node: &'self Option<~TreeNode<K, V>>,
priv remaining: uint
}
impl<'self, K, V> Iterator<(&'self K, &'self V)> for TreeMapIterator<'self, K, V> {
@ -220,12 +221,18 @@ impl<'self, K, V> Iterator<(&'self K, &'self V)> for TreeMapIterator<'self, K, V
None => {
let res = self.stack.pop();
self.node = &res.right;
self.remaining -= 1;
return Some((&res.key, &res.value));
}
}
}
None
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
(self.remaining, Some(self.remaining))
}
}
impl<'self, T> Iterator<&'self T> for TreeSetIterator<'self, T> {

View file

@ -146,4 +146,12 @@ impl<'self, T> Iterator<&'self T> for OptVecIterator<'self, T> {
None => None
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
match self.iter {
Some(ref x) => x.size_hint(),
None => (0, Some(0))
}
}
}