Skip to content

Commit

Permalink
Add size hints for BTreeSet iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Jan 26, 2016
1 parent 5d6e8fc commit 6ff177e
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/libcollections/btree/set.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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")]
Expand All @@ -724,7 +727,9 @@ impl<T> DoubleEndedIterator for IntoIter<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> {
Expand Down Expand Up @@ -780,6 +785,12 @@ impl<'a, T: Ord> Iterator for Difference<'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> {
Expand All @@ -806,6 +817,10 @@ impl<'a, T: Ord> Iterator for SymmetricDifference<'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> {
Expand Down Expand Up @@ -842,6 +857,10 @@ impl<'a, T: Ord> Iterator for Intersection<'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> {
Expand All @@ -868,4 +887,10 @@ impl<'a, T: Ord> Iterator for Union<'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))
}
}

0 comments on commit 6ff177e

Please sign in to comment.