diff options
| author | Kevin Ballard <kevin@sb.org> | 2013-07-03 11:30:12 -0700 |
|---|---|---|
| committer | Kevin Ballard <kevin@sb.org> | 2013-07-06 14:14:45 -0700 |
| commit | e6f9b08610050f8e98903829056cf6ff83e95ef3 (patch) | |
| tree | a121cac32001d1bb6223fa6a8949ce93dce10f73 /src | |
| parent | 20016b92c8c03e33ad9b965fba32ac851fe9f6bf (diff) | |
| download | rust-e6f9b08610050f8e98903829056cf6ff83e95ef3.tar.gz rust-e6f9b08610050f8e98903829056cf6ff83e95ef3.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libextra/priority_queue.rs | 3 | ||||
| -rw-r--r-- | src/libextra/treemap.rs | 11 | ||||
| -rw-r--r-- | src/libsyntax/opt_vec.rs | 8 |
3 files changed, 20 insertions, 2 deletions
diff --git a/src/libextra/priority_queue.rs b/src/libextra/priority_queue.rs index 3d1ca4a9818..1f7ba9f6530 100644 --- a/src/libextra/priority_queue.rs +++ b/src/libextra/priority_queue.rs @@ -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)] diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs index 5e898f8e59d..b7df27c0e49 100644 --- a/src/libextra/treemap.rs +++ b/src/libextra/treemap.rs @@ -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> { diff --git a/src/libsyntax/opt_vec.rs b/src/libsyntax/opt_vec.rs index bf8c5ae462b..8e2da3d6eb1 100644 --- a/src/libsyntax/opt_vec.rs +++ b/src/libsyntax/opt_vec.rs @@ -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)) + } + } } |
