Skip to content

Commit

Permalink
Rollup merge of rust-lang#47162 - stjepang:cleanup-btreeset, r=alexcr…
Browse files Browse the repository at this point in the history
…ichton

Remove `T: Ord` bound from `BTreeSet::{is_empty, len}`

This change makes the API for `BTreeSet` more consistent with `BTreeMap`, where `BTreeMap::{is_empty, len}` don't require `T: Ord` either.

Also, it reduces the number of `impl`s for `BTreeSet`, making the generated documentation look much cleaner. Closes rust-lang#47138.

cc @rust-lang/libs
  • Loading branch information
kennytm authored Jan 5, 2018
2 parents a5dd5fe + 6076cf6 commit ff2f328
Showing 1 changed file with 68 additions and 72 deletions.
140 changes: 68 additions & 72 deletions src/liballoc/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,43 +228,7 @@ impl<T: Ord> BTreeSet<T> {
pub fn new() -> BTreeSet<T> {
BTreeSet { map: BTreeMap::new() }
}
}

impl<T> BTreeSet<T> {
/// Gets an iterator that visits the values in the `BTreeSet` in ascending order.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let set: BTreeSet<usize> = [1, 2, 3].iter().cloned().collect();
/// let mut set_iter = set.iter();
/// assert_eq!(set_iter.next(), Some(&1));
/// assert_eq!(set_iter.next(), Some(&2));
/// assert_eq!(set_iter.next(), Some(&3));
/// assert_eq!(set_iter.next(), None);
/// ```
///
/// Values returned by the iterator are returned in ascending order:
///
/// ```
/// use std::collections::BTreeSet;
///
/// let set: BTreeSet<usize> = [3, 1, 2].iter().cloned().collect();
/// let mut set_iter = set.iter();
/// assert_eq!(set_iter.next(), Some(&1));
/// assert_eq!(set_iter.next(), Some(&2));
/// assert_eq!(set_iter.next(), Some(&3));
/// assert_eq!(set_iter.next(), None);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn iter(&self) -> Iter<T> {
Iter { iter: self.map.keys() }
}
}

impl<T: Ord> BTreeSet<T> {
/// Constructs a double-ended iterator over a sub-range of elements in the set.
/// The simplest way is to use the range syntax `min..max`, thus `range(min..max)` will
/// yield elements from min (inclusive) to max (exclusive).
Expand Down Expand Up @@ -293,9 +257,7 @@ impl<T: Ord> BTreeSet<T> {
{
Range { iter: self.map.range(range) }
}
}

impl<T: Ord> BTreeSet<T> {
/// Visits the values representing the difference,
/// i.e. the values that are in `self` but not in `other`,
/// in ascending order.
Expand Down Expand Up @@ -408,40 +370,6 @@ impl<T: Ord> BTreeSet<T> {
}
}

/// Returns the number of elements in the set.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let mut v = BTreeSet::new();
/// assert_eq!(v.len(), 0);
/// v.insert(1);
/// assert_eq!(v.len(), 1);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> usize {
self.map.len()
}

/// Returns `true` if the set contains no elements.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let mut v = BTreeSet::new();
/// assert!(v.is_empty());
/// v.insert(1);
/// assert!(!v.is_empty());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Clears the set, removing all values.
///
/// # Examples
Expand Down Expand Up @@ -724,6 +652,74 @@ impl<T: Ord> BTreeSet<T> {
}
}

impl<T> BTreeSet<T> {
/// Gets an iterator that visits the values in the `BTreeSet` in ascending order.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let set: BTreeSet<usize> = [1, 2, 3].iter().cloned().collect();
/// let mut set_iter = set.iter();
/// assert_eq!(set_iter.next(), Some(&1));
/// assert_eq!(set_iter.next(), Some(&2));
/// assert_eq!(set_iter.next(), Some(&3));
/// assert_eq!(set_iter.next(), None);
/// ```
///
/// Values returned by the iterator are returned in ascending order:
///
/// ```
/// use std::collections::BTreeSet;
///
/// let set: BTreeSet<usize> = [3, 1, 2].iter().cloned().collect();
/// let mut set_iter = set.iter();
/// assert_eq!(set_iter.next(), Some(&1));
/// assert_eq!(set_iter.next(), Some(&2));
/// assert_eq!(set_iter.next(), Some(&3));
/// assert_eq!(set_iter.next(), None);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn iter(&self) -> Iter<T> {
Iter { iter: self.map.keys() }
}

/// Returns the number of elements in the set.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let mut v = BTreeSet::new();
/// assert_eq!(v.len(), 0);
/// v.insert(1);
/// assert_eq!(v.len(), 1);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> usize {
self.map.len()
}

/// Returns `true` if the set contains no elements.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let mut v = BTreeSet::new();
/// assert!(v.is_empty());
/// v.insert(1);
/// assert!(!v.is_empty());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Ord> FromIterator<T> for BTreeSet<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> BTreeSet<T> {
Expand Down

0 comments on commit ff2f328

Please sign in to comment.