Add size hints for BTreeSet iterators

This commit is contained in:
Matt Brubeck 2016-01-26 13:54:03 -08:00
parent 5d6e8fceda
commit 6ff177efea

View File

@ -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 @@ fn next_back(&mut self) -> Option<&'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 @@ fn next_back(&mut self) -> Option<T> {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ExactSizeIterator for IntoIter<T> {}
impl<T> ExactSizeIterator for IntoIter<T> {
fn len(&self) -> usize { self.iter.len() }
}
impl<'a, T> Clone for Range<'a, T> {
@ -780,6 +785,12 @@ fn next(&mut self) -> Option<&'a T> {
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
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 @@ fn next(&mut self) -> Option<&'a T> {
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.a.len() + self.b.len()))
}
}
impl<'a, T> Clone for Intersection<'a, T> {
@ -842,6 +857,10 @@ fn next(&mut self) -> Option<&'a T> {
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(min(self.a.len(), self.b.len())))
}
}
impl<'a, T> Clone for Union<'a, T> {
@ -868,4 +887,10 @@ fn next(&mut self) -> Option<&'a T> {
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let a_len = self.a.len();
let b_len = self.b.len();
(max(a_len, b_len), Some(a_len + b_len))
}
}