From 6ff177efeadea57797f51bb2c7400746f8b0a3af Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Tue, 26 Jan 2016 13:54:03 -0800 Subject: [PATCH] Add size hints for BTreeSet iterators --- src/libcollections/btree/set.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/libcollections/btree/set.rs b/src/libcollections/btree/set.rs index 91fc8d8217f..1cd50c2dcbe 100644 --- a/src/libcollections/btree/set.rs +++ b/src/libcollections/btree/set.rs @@ -12,6 +12,7 @@ // to TreeMap use core::cmp::Ordering::{self, Less, Greater, Equal}; +use core::cmp::{min, max}; use core::fmt::Debug; use core::fmt; use core::iter::{Peekable, FromIterator}; @@ -703,7 +704,9 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> { } } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T> ExactSizeIterator for Iter<'a, T> {} +impl<'a, T> ExactSizeIterator for Iter<'a, T> { + fn len(&self) -> usize { self.iter.len() } +} #[stable(feature = "rust1", since = "1.0.0")] @@ -724,7 +727,9 @@ impl DoubleEndedIterator for IntoIter { } } #[stable(feature = "rust1", since = "1.0.0")] -impl ExactSizeIterator for IntoIter {} +impl ExactSizeIterator for IntoIter { + fn len(&self) -> usize { self.iter.len() } +} impl<'a, T> Clone for Range<'a, T> { @@ -780,6 +785,12 @@ impl<'a, T: Ord> Iterator for Difference<'a, T> { } } } + + fn size_hint(&self) -> (usize, Option) { + let a_len = self.a.len(); + let b_len = self.b.len(); + (a_len.saturating_sub(b_len), Some(a_len)) + } } impl<'a, T> Clone for SymmetricDifference<'a, T> { @@ -806,6 +817,10 @@ impl<'a, T: Ord> Iterator for SymmetricDifference<'a, T> { } } } + + fn size_hint(&self) -> (usize, Option) { + (0, Some(self.a.len() + self.b.len())) + } } impl<'a, T> Clone for Intersection<'a, T> { @@ -842,6 +857,10 @@ impl<'a, T: Ord> Iterator for Intersection<'a, T> { } } } + + fn size_hint(&self) -> (usize, Option) { + (0, Some(min(self.a.len(), self.b.len()))) + } } impl<'a, T> Clone for Union<'a, T> { @@ -868,4 +887,10 @@ impl<'a, T: Ord> Iterator for Union<'a, T> { } } } + + fn size_hint(&self) -> (usize, Option) { + let a_len = self.a.len(); + let b_len = self.b.len(); + (max(a_len, b_len), Some(a_len + b_len)) + } }