2014-09-16 10:49:26 -04:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// This is pretty much entirely stolen from TreeSet, since BTreeMap has an identical interface
|
|
|
|
// to TreeMap
|
|
|
|
|
2015-01-03 22:42:21 -05:00
|
|
|
use core::cmp::Ordering::{self, Less, Greater, Equal};
|
2016-01-26 13:54:03 -08:00
|
|
|
use core::cmp::{min, max};
|
2015-01-20 15:45:07 -08:00
|
|
|
use core::fmt::Debug;
|
2014-12-22 09:04:23 -08:00
|
|
|
use core::fmt;
|
2016-01-18 07:36:39 -05:00
|
|
|
use core::iter::{Peekable, FromIterator};
|
2014-12-22 09:04:23 -08:00
|
|
|
use core::ops::{BitOr, BitAnd, BitXor, Sub};
|
|
|
|
|
2015-02-11 23:16:32 -08:00
|
|
|
use borrow::Borrow;
|
2014-12-22 09:04:23 -08:00
|
|
|
use btree_map::{BTreeMap, Keys};
|
2015-08-27 11:10:28 -04:00
|
|
|
use super::Recover;
|
2015-01-13 21:55:44 +01:00
|
|
|
use Bound;
|
2014-09-16 10:49:26 -04:00
|
|
|
|
2014-11-06 12:24:47 -05:00
|
|
|
// FIXME(conventions): implement bounded iterators
|
|
|
|
|
2014-09-16 10:49:26 -04:00
|
|
|
/// A set based on a B-Tree.
|
2014-10-05 09:48:38 -04:00
|
|
|
///
|
2015-12-14 00:12:17 +02:00
|
|
|
/// See [`BTreeMap`]'s documentation for a detailed discussion of this collection's performance
|
2014-10-05 09:48:38 -04:00
|
|
|
/// benefits and drawbacks.
|
2015-03-13 13:47:55 -04:00
|
|
|
///
|
|
|
|
/// It is a logic error for an item to be modified in such a way that the item's ordering relative
|
2015-12-14 00:12:17 +02:00
|
|
|
/// to any other item, as determined by the [`Ord`] trait, changes while it is in the set. This is
|
|
|
|
/// normally only possible through [`Cell`], [`RefCell`], global state, I/O, or unsafe code.
|
|
|
|
///
|
|
|
|
/// [`BTreeMap`]: ../struct.BTreeMap.html
|
|
|
|
/// [`Ord`]: ../../core/cmp/trait.Ord.html
|
|
|
|
/// [`Cell`]: ../../std/cell/struct.Cell.html
|
|
|
|
/// [`RefCell`]: ../../std/cell/struct.RefCell.html
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub struct BTreeSet<T> {
|
2014-09-16 10:49:26 -04:00
|
|
|
map: BTreeMap<T, ()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over a BTreeSet's items.
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-19 21:52:10 +01:00
|
|
|
pub struct Iter<'a, T: 'a> {
|
2015-11-24 11:23:48 +13:00
|
|
|
iter: Keys<'a, T, ()>,
|
2014-12-12 23:14:57 -06:00
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
|
|
|
|
/// An owning iterator over a BTreeSet's items.
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-19 21:52:10 +01:00
|
|
|
pub struct IntoIter<T> {
|
2016-01-18 07:36:39 -05:00
|
|
|
iter: ::btree_map::IntoIter<T, ()>,
|
2014-12-12 23:14:57 -06:00
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
|
2015-01-13 21:55:44 +01:00
|
|
|
/// An iterator over a sub-range of BTreeSet's items.
|
|
|
|
pub struct Range<'a, T: 'a> {
|
2016-01-18 07:36:39 -05:00
|
|
|
iter: ::btree_map::Range<'a, T, ()>,
|
2015-01-13 21:55:44 +01:00
|
|
|
}
|
|
|
|
|
2014-09-16 10:49:26 -04:00
|
|
|
/// A lazy iterator producing elements in the set difference (in-order).
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub struct Difference<'a, T: 'a> {
|
2015-02-01 12:15:36 -08:00
|
|
|
a: Peekable<Iter<'a, T>>,
|
|
|
|
b: Peekable<Iter<'a, T>>,
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A lazy iterator producing elements in the set symmetric difference (in-order).
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub struct SymmetricDifference<'a, T: 'a> {
|
2015-02-01 12:15:36 -08:00
|
|
|
a: Peekable<Iter<'a, T>>,
|
|
|
|
b: Peekable<Iter<'a, T>>,
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A lazy iterator producing elements in the set intersection (in-order).
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub struct Intersection<'a, T: 'a> {
|
2015-02-01 12:15:36 -08:00
|
|
|
a: Peekable<Iter<'a, T>>,
|
|
|
|
b: Peekable<Iter<'a, T>>,
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A lazy iterator producing elements in the set union (in-order).
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub struct Union<'a, T: 'a> {
|
2015-02-01 12:15:36 -08:00
|
|
|
a: Peekable<Iter<'a, T>>,
|
|
|
|
b: Peekable<Iter<'a, T>>,
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Ord> BTreeSet<T> {
|
|
|
|
/// Makes a new BTreeSet with a reasonable choice of B.
|
2014-12-12 22:09:40 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-11-03 14:49:33 +00:00
|
|
|
/// # #![allow(unused_mut)]
|
2014-12-12 22:09:40 -06:00
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let mut set: BTreeSet<i32> = BTreeSet::new();
|
2014-12-12 22:09:40 -06:00
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-09-16 10:49:26 -04:00
|
|
|
pub fn new() -> BTreeSet<T> {
|
|
|
|
BTreeSet { map: BTreeMap::new() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> BTreeSet<T> {
|
|
|
|
/// Gets an iterator over the BTreeSet's contents.
|
2014-12-12 01:18:10 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
|
2014-12-12 01:18:10 -06:00
|
|
|
///
|
|
|
|
/// for x in set.iter() {
|
|
|
|
/// println!("{}", x);
|
|
|
|
/// }
|
|
|
|
///
|
2015-05-19 03:33:17 +02:00
|
|
|
/// let v: Vec<_> = set.iter().cloned().collect();
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(v, [1, 2, 3, 4]);
|
2014-12-12 01:18:10 -06:00
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-30 19:07:53 -05:00
|
|
|
pub fn iter(&self) -> Iter<T> {
|
2014-12-19 21:52:10 +01:00
|
|
|
Iter { iter: self.map.keys() }
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 21:55:44 +01:00
|
|
|
impl<T: Ord> BTreeSet<T> {
|
|
|
|
/// Constructs a double-ended iterator over a sub-range of elements in the set, starting
|
|
|
|
/// at min, and ending at max. If min is `Unbounded`, then it will be treated as "negative
|
|
|
|
/// infinity", and if max is `Unbounded`, then it will be treated as "positive infinity".
|
|
|
|
/// Thus range(Unbounded, Unbounded) will yield the whole collection.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-07-27 10:50:19 -04:00
|
|
|
/// #![feature(btree_range, collections_bound)]
|
|
|
|
///
|
2015-01-13 21:55:44 +01:00
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
/// use std::collections::Bound::{Included, Unbounded};
|
|
|
|
///
|
|
|
|
/// let mut set = BTreeSet::new();
|
2015-02-06 13:57:13 -05:00
|
|
|
/// set.insert(3);
|
|
|
|
/// set.insert(5);
|
|
|
|
/// set.insert(8);
|
2015-01-13 21:55:44 +01:00
|
|
|
/// for &elem in set.range(Included(&4), Included(&8)) {
|
|
|
|
/// println!("{}", elem);
|
|
|
|
/// }
|
2015-02-06 13:57:13 -05:00
|
|
|
/// assert_eq!(Some(&5), set.range(Included(&4), Unbounded).next());
|
2015-01-13 21:55:44 +01:00
|
|
|
/// ```
|
2015-06-09 14:39:23 -07:00
|
|
|
#[unstable(feature = "btree_range",
|
2015-08-12 22:19:51 -07:00
|
|
|
reason = "matches collection reform specification, waiting for dust to settle",
|
|
|
|
issue = "27787")]
|
2015-12-15 13:02:14 -05:00
|
|
|
pub fn range<'a, Min: ?Sized + Ord, Max: ?Sized + Ord>(&'a self,
|
|
|
|
min: Bound<&Min>,
|
|
|
|
max: Bound<&Max>)
|
|
|
|
-> Range<'a, T>
|
2015-11-24 11:23:48 +13:00
|
|
|
where T: Borrow<Min> + Borrow<Max>
|
2015-08-05 15:12:10 -04:00
|
|
|
{
|
2016-01-18 07:36:39 -05:00
|
|
|
Range { iter: self.map.range(min, max) }
|
2015-01-13 21:55:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 10:49:26 -04:00
|
|
|
impl<T: Ord> BTreeSet<T> {
|
|
|
|
/// Visits the values representing the difference, in ascending order.
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
|
|
|
/// let mut a = BTreeSet::new();
|
2015-02-06 13:57:13 -05:00
|
|
|
/// a.insert(1);
|
|
|
|
/// a.insert(2);
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
|
|
|
/// let mut b = BTreeSet::new();
|
2015-02-06 13:57:13 -05:00
|
|
|
/// b.insert(2);
|
|
|
|
/// b.insert(3);
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
2015-05-19 03:33:17 +02:00
|
|
|
/// let diff: Vec<_> = a.difference(&b).cloned().collect();
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(diff, [1]);
|
2014-12-02 22:59:24 -06:00
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-20 15:28:20 +01:00
|
|
|
pub fn difference<'a>(&'a self, other: &'a BTreeSet<T>) -> Difference<'a, T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
Difference {
|
|
|
|
a: self.iter().peekable(),
|
|
|
|
b: other.iter().peekable(),
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Visits the values representing the symmetric difference, in ascending order.
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
|
|
|
/// let mut a = BTreeSet::new();
|
2015-02-06 13:57:13 -05:00
|
|
|
/// a.insert(1);
|
|
|
|
/// a.insert(2);
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
|
|
|
/// let mut b = BTreeSet::new();
|
2015-02-06 13:57:13 -05:00
|
|
|
/// b.insert(2);
|
|
|
|
/// b.insert(3);
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
2015-05-19 03:33:17 +02:00
|
|
|
/// let sym_diff: Vec<_> = a.symmetric_difference(&b).cloned().collect();
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(sym_diff, [1, 3]);
|
2014-12-02 22:59:24 -06:00
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub fn symmetric_difference<'a>(&'a self,
|
|
|
|
other: &'a BTreeSet<T>)
|
|
|
|
-> SymmetricDifference<'a, T> {
|
|
|
|
SymmetricDifference {
|
|
|
|
a: self.iter().peekable(),
|
|
|
|
b: other.iter().peekable(),
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Visits the values representing the intersection, in ascending order.
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
|
|
|
/// let mut a = BTreeSet::new();
|
2015-02-06 13:57:13 -05:00
|
|
|
/// a.insert(1);
|
|
|
|
/// a.insert(2);
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
|
|
|
/// let mut b = BTreeSet::new();
|
2015-02-06 13:57:13 -05:00
|
|
|
/// b.insert(2);
|
|
|
|
/// b.insert(3);
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
2015-05-19 03:33:17 +02:00
|
|
|
/// let intersection: Vec<_> = a.intersection(&b).cloned().collect();
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(intersection, [2]);
|
2014-12-02 22:59:24 -06:00
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub fn intersection<'a>(&'a self, other: &'a BTreeSet<T>) -> Intersection<'a, T> {
|
|
|
|
Intersection {
|
|
|
|
a: self.iter().peekable(),
|
|
|
|
b: other.iter().peekable(),
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Visits the values representing the union, in ascending order.
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
|
|
|
/// let mut a = BTreeSet::new();
|
2015-02-06 13:57:13 -05:00
|
|
|
/// a.insert(1);
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
|
|
|
/// let mut b = BTreeSet::new();
|
2015-02-06 13:57:13 -05:00
|
|
|
/// b.insert(2);
|
2014-12-02 22:59:24 -06:00
|
|
|
///
|
2015-05-19 03:33:17 +02:00
|
|
|
/// let union: Vec<_> = a.union(&b).cloned().collect();
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(union, [1, 2]);
|
2014-12-02 22:59:24 -06:00
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-20 15:28:20 +01:00
|
|
|
pub fn union<'a>(&'a self, other: &'a BTreeSet<T>) -> Union<'a, T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
Union {
|
|
|
|
a: self.iter().peekable(),
|
|
|
|
b: other.iter().peekable(),
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Returns the number of elements in the set.
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
|
|
|
/// let mut v = BTreeSet::new();
|
|
|
|
/// assert_eq!(v.len(), 0);
|
2015-01-25 22:05:03 +01:00
|
|
|
/// v.insert(1);
|
2014-10-30 13:43:24 -07:00
|
|
|
/// assert_eq!(v.len(), 1);
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.map.len()
|
|
|
|
}
|
2014-10-30 13:43:24 -07:00
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Returns true if the set contains no elements.
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
|
|
|
/// let mut v = BTreeSet::new();
|
|
|
|
/// assert!(v.is_empty());
|
2015-01-25 22:05:03 +01:00
|
|
|
/// v.insert(1);
|
2014-10-30 13:43:24 -07:00
|
|
|
/// assert!(!v.is_empty());
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
2014-10-30 13:43:24 -07:00
|
|
|
|
|
|
|
/// Clears the set, removing all values.
|
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
|
|
|
/// let mut v = BTreeSet::new();
|
2015-01-25 22:05:03 +01:00
|
|
|
/// v.insert(1);
|
2014-10-30 13:43:24 -07:00
|
|
|
/// v.clear();
|
|
|
|
/// assert!(v.is_empty());
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 13:43:24 -07:00
|
|
|
pub fn clear(&mut self) {
|
2014-09-16 10:49:26 -04:00
|
|
|
self.map.clear()
|
|
|
|
}
|
|
|
|
|
2014-10-30 13:43:24 -07:00
|
|
|
/// Returns `true` if the set contains a value.
|
|
|
|
///
|
2014-11-13 15:51:08 -08:00
|
|
|
/// The value may be any borrowed form of the set's value type,
|
|
|
|
/// but the ordering on the borrowed form *must* match the
|
|
|
|
/// ordering on the value type.
|
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
|
2014-10-30 13:43:24 -07:00
|
|
|
/// assert_eq!(set.contains(&1), true);
|
|
|
|
/// assert_eq!(set.contains(&4), false);
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
|
|
|
|
where T: Borrow<Q>,
|
|
|
|
Q: Ord
|
|
|
|
{
|
2014-11-06 12:24:47 -05:00
|
|
|
self.map.contains_key(value)
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
2015-08-27 11:10:28 -04:00
|
|
|
/// Returns a reference to the value in the set, if any, that is equal to the given value.
|
|
|
|
///
|
|
|
|
/// The value may be any borrowed form of the set's value type,
|
|
|
|
/// but the ordering on the borrowed form *must* match the
|
|
|
|
/// ordering on the value type.
|
|
|
|
#[unstable(feature = "set_recovery", issue = "28050")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
|
|
|
|
where T: Borrow<Q>,
|
|
|
|
Q: Ord
|
|
|
|
{
|
2015-08-27 11:10:28 -04:00
|
|
|
Recover::get(&self.map, value)
|
|
|
|
}
|
|
|
|
|
2014-10-30 13:43:24 -07:00
|
|
|
/// Returns `true` if the set has no elements in common with `other`.
|
|
|
|
/// This is equivalent to checking for an empty intersection.
|
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let a: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
|
|
|
|
/// let mut b = BTreeSet::new();
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// assert_eq!(a.is_disjoint(&b), true);
|
|
|
|
/// b.insert(4);
|
|
|
|
/// assert_eq!(a.is_disjoint(&b), true);
|
|
|
|
/// b.insert(1);
|
|
|
|
/// assert_eq!(a.is_disjoint(&b), false);
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 13:43:24 -07:00
|
|
|
pub fn is_disjoint(&self, other: &BTreeSet<T>) -> bool {
|
2014-09-16 10:49:26 -04:00
|
|
|
self.intersection(other).next().is_none()
|
|
|
|
}
|
|
|
|
|
2014-10-30 13:43:24 -07:00
|
|
|
/// Returns `true` if the set is a subset of another.
|
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let sup: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
|
|
|
|
/// let mut set = BTreeSet::new();
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// assert_eq!(set.is_subset(&sup), true);
|
|
|
|
/// set.insert(2);
|
|
|
|
/// assert_eq!(set.is_subset(&sup), true);
|
|
|
|
/// set.insert(4);
|
|
|
|
/// assert_eq!(set.is_subset(&sup), false);
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 13:43:24 -07:00
|
|
|
pub fn is_subset(&self, other: &BTreeSet<T>) -> bool {
|
2014-09-16 10:49:26 -04:00
|
|
|
// Stolen from TreeMap
|
|
|
|
let mut x = self.iter();
|
|
|
|
let mut y = other.iter();
|
|
|
|
let mut a = x.next();
|
|
|
|
let mut b = y.next();
|
|
|
|
while a.is_some() {
|
|
|
|
if b.is_none() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let a1 = a.unwrap();
|
|
|
|
let b1 = b.unwrap();
|
|
|
|
|
|
|
|
match b1.cmp(a1) {
|
|
|
|
Less => (),
|
|
|
|
Greater => return false,
|
|
|
|
Equal => a = x.next(),
|
|
|
|
}
|
|
|
|
|
|
|
|
b = y.next();
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2014-10-30 13:43:24 -07:00
|
|
|
/// Returns `true` if the set is a superset of another.
|
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let sub: BTreeSet<_> = [1, 2].iter().cloned().collect();
|
|
|
|
/// let mut set = BTreeSet::new();
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// assert_eq!(set.is_superset(&sub), false);
|
|
|
|
///
|
|
|
|
/// set.insert(0);
|
|
|
|
/// set.insert(1);
|
|
|
|
/// assert_eq!(set.is_superset(&sub), false);
|
|
|
|
///
|
|
|
|
/// set.insert(2);
|
|
|
|
/// assert_eq!(set.is_superset(&sub), true);
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 13:43:24 -07:00
|
|
|
pub fn is_superset(&self, other: &BTreeSet<T>) -> bool {
|
|
|
|
other.is_subset(self)
|
|
|
|
}
|
|
|
|
|
2015-08-18 18:38:07 -04:00
|
|
|
/// Adds a value to the set.
|
|
|
|
///
|
|
|
|
/// If the set did not have a value present, `true` is returned.
|
|
|
|
///
|
2016-01-15 11:49:43 -05:00
|
|
|
/// If the set did have this key present, `false` is returned, and the
|
2015-08-18 18:38:07 -04:00
|
|
|
/// entry is not updated. See the [module-level documentation] for more.
|
|
|
|
///
|
|
|
|
/// [module-level documentation]: index.html#insert-and-complex-keys
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
|
|
|
/// let mut set = BTreeSet::new();
|
|
|
|
///
|
2015-01-25 22:05:03 +01:00
|
|
|
/// assert_eq!(set.insert(2), true);
|
|
|
|
/// assert_eq!(set.insert(2), false);
|
2014-10-30 13:43:24 -07:00
|
|
|
/// assert_eq!(set.len(), 1);
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 13:43:24 -07:00
|
|
|
pub fn insert(&mut self, value: T) -> bool {
|
2014-11-06 12:24:47 -05:00
|
|
|
self.map.insert(value, ()).is_none()
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
2015-08-27 11:10:28 -04:00
|
|
|
/// Adds a value to the set, replacing the existing value, if any, that is equal to the given
|
|
|
|
/// one. Returns the replaced value.
|
|
|
|
#[unstable(feature = "set_recovery", issue = "28050")]
|
|
|
|
pub fn replace(&mut self, value: T) -> Option<T> {
|
|
|
|
Recover::replace(&mut self.map, value)
|
|
|
|
}
|
|
|
|
|
2014-10-30 13:43:24 -07:00
|
|
|
/// Removes a value from the set. Returns `true` if the value was
|
|
|
|
/// present in the set.
|
|
|
|
///
|
2014-11-13 15:51:08 -08:00
|
|
|
/// The value may be any borrowed form of the set's value type,
|
|
|
|
/// but the ordering on the borrowed form *must* match the
|
|
|
|
/// ordering on the value type.
|
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
|
|
|
/// let mut set = BTreeSet::new();
|
|
|
|
///
|
2015-01-25 22:05:03 +01:00
|
|
|
/// set.insert(2);
|
2014-10-30 13:43:24 -07:00
|
|
|
/// assert_eq!(set.remove(&2), true);
|
|
|
|
/// assert_eq!(set.remove(&2), false);
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
|
|
|
|
where T: Borrow<Q>,
|
|
|
|
Q: Ord
|
|
|
|
{
|
2014-11-06 12:24:47 -05:00
|
|
|
self.map.remove(value).is_some()
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
2015-08-27 11:10:28 -04:00
|
|
|
|
|
|
|
/// Removes and returns the value in the set, if any, that is equal to the given one.
|
|
|
|
///
|
|
|
|
/// The value may be any borrowed form of the set's value type,
|
|
|
|
/// but the ordering on the borrowed form *must* match the
|
|
|
|
/// ordering on the value type.
|
|
|
|
#[unstable(feature = "set_recovery", issue = "28050")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
|
|
|
|
where T: Borrow<Q>,
|
|
|
|
Q: Ord
|
|
|
|
{
|
2015-08-27 11:10:28 -04:00
|
|
|
Recover::take(&mut self.map, value)
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-09-16 10:49:26 -04:00
|
|
|
impl<T: Ord> FromIterator<T> for BTreeSet<T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> BTreeSet<T> {
|
2014-09-16 10:49:26 -04:00
|
|
|
let mut set = BTreeSet::new();
|
|
|
|
set.extend(iter);
|
|
|
|
set
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 10:06:24 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-13 17:55:10 -05:00
|
|
|
impl<T> IntoIterator for BTreeSet<T> {
|
|
|
|
type Item = T;
|
|
|
|
type IntoIter = IntoIter<T>;
|
|
|
|
|
2015-04-17 14:31:30 -07:00
|
|
|
/// Gets an iterator for moving out the BtreeSet's contents.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
|
|
|
/// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
|
|
|
|
///
|
2015-05-19 03:33:17 +02:00
|
|
|
/// let v: Vec<_> = set.into_iter().collect();
|
2015-04-17 14:31:30 -07:00
|
|
|
/// assert_eq!(v, [1, 2, 3, 4]);
|
|
|
|
/// ```
|
2015-02-13 17:55:10 -05:00
|
|
|
fn into_iter(self) -> IntoIter<T> {
|
2016-01-18 07:36:39 -05:00
|
|
|
IntoIter { iter: self.map.into_iter() }
|
2015-02-13 17:55:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 10:06:24 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-07 22:01:05 -05:00
|
|
|
impl<'a, T> IntoIterator for &'a BTreeSet<T> {
|
2015-02-13 17:55:10 -05:00
|
|
|
type Item = &'a T;
|
2015-02-06 14:47:55 -08:00
|
|
|
type IntoIter = Iter<'a, T>;
|
2015-01-07 22:01:05 -05:00
|
|
|
|
|
|
|
fn into_iter(self) -> Iter<'a, T> {
|
|
|
|
self.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-08 01:39:39 +01:00
|
|
|
impl<T: Ord> Extend<T> for BTreeSet<T> {
|
2014-09-16 10:49:26 -04:00
|
|
|
#[inline]
|
2015-11-24 11:23:48 +13:00
|
|
|
fn extend<Iter: IntoIterator<Item = T>>(&mut self, iter: Iter) {
|
2014-09-16 10:49:26 -04:00
|
|
|
for elem in iter {
|
|
|
|
self.insert(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:38:42 +02:00
|
|
|
#[stable(feature = "extend_ref", since = "1.2.0")]
|
|
|
|
impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BTreeSet<T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
|
2015-06-03 12:38:42 +02:00
|
|
|
self.extend(iter.into_iter().cloned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-09-16 10:49:26 -04:00
|
|
|
impl<T: Ord> Default for BTreeSet<T> {
|
|
|
|
fn default() -> BTreeSet<T> {
|
|
|
|
BTreeSet::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 15:45:13 -05:00
|
|
|
impl<'a, 'b, T: Ord + Clone> Sub<&'b BTreeSet<T>> for &'a BTreeSet<T> {
|
|
|
|
type Output = BTreeSet<T>;
|
|
|
|
|
2014-12-13 16:34:36 -05:00
|
|
|
/// Returns the difference of `self` and `rhs` as a new `BTreeSet<T>`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
|
|
|
|
/// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect();
|
2014-12-13 16:34:36 -05:00
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let result = &a - &b;
|
|
|
|
/// let result_vec: Vec<_> = result.into_iter().collect();
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(result_vec, [1, 2]);
|
2014-12-13 16:34:36 -05:00
|
|
|
/// ```
|
|
|
|
fn sub(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
|
|
|
|
self.difference(rhs).cloned().collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 15:45:13 -05:00
|
|
|
impl<'a, 'b, T: Ord + Clone> BitXor<&'b BTreeSet<T>> for &'a BTreeSet<T> {
|
|
|
|
type Output = BTreeSet<T>;
|
|
|
|
|
2014-12-13 16:34:36 -05:00
|
|
|
/// Returns the symmetric difference of `self` and `rhs` as a new `BTreeSet<T>`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
|
|
|
|
/// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();
|
2014-12-13 16:34:36 -05:00
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let result = &a ^ &b;
|
|
|
|
/// let result_vec: Vec<_> = result.into_iter().collect();
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(result_vec, [1, 4]);
|
2014-12-13 16:34:36 -05:00
|
|
|
/// ```
|
|
|
|
fn bitxor(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
|
|
|
|
self.symmetric_difference(rhs).cloned().collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 15:45:13 -05:00
|
|
|
impl<'a, 'b, T: Ord + Clone> BitAnd<&'b BTreeSet<T>> for &'a BTreeSet<T> {
|
|
|
|
type Output = BTreeSet<T>;
|
|
|
|
|
2014-12-13 16:34:36 -05:00
|
|
|
/// Returns the intersection of `self` and `rhs` as a new `BTreeSet<T>`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
|
|
|
|
/// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();
|
2014-12-13 16:34:36 -05:00
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let result = &a & &b;
|
|
|
|
/// let result_vec: Vec<_> = result.into_iter().collect();
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(result_vec, [2, 3]);
|
2014-12-13 16:34:36 -05:00
|
|
|
/// ```
|
|
|
|
fn bitand(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
|
|
|
|
self.intersection(rhs).cloned().collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 15:45:13 -05:00
|
|
|
impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>> for &'a BTreeSet<T> {
|
|
|
|
type Output = BTreeSet<T>;
|
|
|
|
|
2014-12-13 16:34:36 -05:00
|
|
|
/// Returns the union of `self` and `rhs` as a new `BTreeSet<T>`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeSet;
|
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
|
|
|
|
/// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect();
|
2014-12-13 16:34:36 -05:00
|
|
|
///
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let result = &a | &b;
|
|
|
|
/// let result_vec: Vec<_> = result.into_iter().collect();
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(result_vec, [1, 2, 3, 4, 5]);
|
2014-12-13 16:34:36 -05:00
|
|
|
/// ```
|
|
|
|
fn bitor(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
|
|
|
|
self.union(rhs).cloned().collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-20 15:45:07 -08:00
|
|
|
impl<T: Debug> Debug for BTreeSet<T> {
|
2014-09-16 10:49:26 -04:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-05-17 13:17:26 -07:00
|
|
|
f.debug_set().entries(self.iter()).finish()
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 22:55:52 -04:00
|
|
|
impl<'a, T> Clone for Iter<'a, T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
fn clone(&self) -> Iter<'a, T> {
|
|
|
|
Iter { iter: self.iter.clone() }
|
|
|
|
}
|
2015-03-21 22:55:52 -04:00
|
|
|
}
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 23:15:35 -05:00
|
|
|
impl<'a, T> Iterator for Iter<'a, T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
|
2015-11-24 11:23:48 +13:00
|
|
|
fn next(&mut self) -> Option<&'a T> {
|
|
|
|
self.iter.next()
|
|
|
|
}
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
2014-12-12 23:14:57 -06:00
|
|
|
}
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 23:15:35 -05:00
|
|
|
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
fn next_back(&mut self) -> Option<&'a T> {
|
|
|
|
self.iter.next_back()
|
|
|
|
}
|
2014-12-12 23:14:57 -06:00
|
|
|
}
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-01-26 13:54:03 -08:00
|
|
|
impl<'a, T> ExactSizeIterator for Iter<'a, T> {
|
|
|
|
fn len(&self) -> usize { self.iter.len() }
|
|
|
|
}
|
2015-01-01 23:15:35 -05:00
|
|
|
|
2014-12-12 23:14:57 -06:00
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 23:15:35 -05:00
|
|
|
impl<T> Iterator for IntoIter<T> {
|
|
|
|
type Item = T;
|
|
|
|
|
2015-11-24 11:23:48 +13:00
|
|
|
fn next(&mut self) -> Option<T> {
|
2016-01-18 07:36:39 -05:00
|
|
|
self.iter.next().map(|(k, _)| k)
|
2015-11-24 11:23:48 +13:00
|
|
|
}
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
2014-12-12 23:14:57 -06:00
|
|
|
}
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 23:15:35 -05:00
|
|
|
impl<T> DoubleEndedIterator for IntoIter<T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
fn next_back(&mut self) -> Option<T> {
|
2016-01-18 07:36:39 -05:00
|
|
|
self.iter.next_back().map(|(k, _)| k)
|
2015-11-24 11:23:48 +13:00
|
|
|
}
|
2014-12-12 23:14:57 -06:00
|
|
|
}
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-01-26 13:54:03 -08:00
|
|
|
impl<T> ExactSizeIterator for IntoIter<T> {
|
|
|
|
fn len(&self) -> usize { self.iter.len() }
|
|
|
|
}
|
2014-12-12 23:14:57 -06:00
|
|
|
|
2015-01-13 21:55:44 +01:00
|
|
|
|
2015-03-21 22:55:52 -04:00
|
|
|
impl<'a, T> Clone for Range<'a, T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
fn clone(&self) -> Range<'a, T> {
|
|
|
|
Range { iter: self.iter.clone() }
|
|
|
|
}
|
2015-03-21 22:55:52 -04:00
|
|
|
}
|
2015-01-13 21:55:44 +01:00
|
|
|
impl<'a, T> Iterator for Range<'a, T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
|
2015-11-24 11:23:48 +13:00
|
|
|
fn next(&mut self) -> Option<&'a T> {
|
2016-01-18 07:36:39 -05:00
|
|
|
self.iter.next().map(|(k, _)| k)
|
2015-11-24 11:23:48 +13:00
|
|
|
}
|
2015-01-13 21:55:44 +01:00
|
|
|
}
|
|
|
|
impl<'a, T> DoubleEndedIterator for Range<'a, T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
fn next_back(&mut self) -> Option<&'a T> {
|
2016-01-18 07:36:39 -05:00
|
|
|
self.iter.next_back().map(|(k, _)| k)
|
2015-11-24 11:23:48 +13:00
|
|
|
}
|
2015-01-13 21:55:44 +01:00
|
|
|
}
|
|
|
|
|
2014-09-16 10:49:26 -04:00
|
|
|
/// Compare `x` and `y`, but return `short` if x is None and `long` if y is None
|
2015-11-24 11:23:48 +13:00
|
|
|
fn cmp_opt<T: Ord>(x: Option<&T>, y: Option<&T>, short: Ordering, long: Ordering) -> Ordering {
|
2014-09-16 10:49:26 -04:00
|
|
|
match (x, y) {
|
2015-11-24 11:23:48 +13:00
|
|
|
(None, _) => short,
|
|
|
|
(_, None) => long,
|
2014-09-16 10:49:26 -04:00
|
|
|
(Some(x1), Some(y1)) => x1.cmp(y1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 22:55:52 -04:00
|
|
|
impl<'a, T> Clone for Difference<'a, T> {
|
|
|
|
fn clone(&self) -> Difference<'a, T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
Difference {
|
|
|
|
a: self.a.clone(),
|
|
|
|
b: self.b.clone(),
|
|
|
|
}
|
2015-03-21 22:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 23:15:35 -05:00
|
|
|
impl<'a, T: Ord> Iterator for Difference<'a, T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
|
2014-09-16 10:49:26 -04:00
|
|
|
fn next(&mut self) -> Option<&'a T> {
|
|
|
|
loop {
|
|
|
|
match cmp_opt(self.a.peek(), self.b.peek(), Less, Less) {
|
2015-11-24 11:23:48 +13:00
|
|
|
Less => return self.a.next(),
|
|
|
|
Equal => {
|
|
|
|
self.a.next();
|
|
|
|
self.b.next();
|
|
|
|
}
|
|
|
|
Greater => {
|
|
|
|
self.b.next();
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 13:54:03 -08:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
2015-03-21 22:55:52 -04:00
|
|
|
impl<'a, T> Clone for SymmetricDifference<'a, T> {
|
|
|
|
fn clone(&self) -> SymmetricDifference<'a, T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
SymmetricDifference {
|
|
|
|
a: self.a.clone(),
|
|
|
|
b: self.b.clone(),
|
|
|
|
}
|
2015-03-21 22:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 23:15:35 -05:00
|
|
|
impl<'a, T: Ord> Iterator for SymmetricDifference<'a, T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
|
2014-09-16 10:49:26 -04:00
|
|
|
fn next(&mut self) -> Option<&'a T> {
|
|
|
|
loop {
|
|
|
|
match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) {
|
2015-11-24 11:23:48 +13:00
|
|
|
Less => return self.a.next(),
|
|
|
|
Equal => {
|
|
|
|
self.a.next();
|
|
|
|
self.b.next();
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
Greater => return self.b.next(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 13:54:03 -08:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(0, Some(self.a.len() + self.b.len()))
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
2015-03-21 22:55:52 -04:00
|
|
|
impl<'a, T> Clone for Intersection<'a, T> {
|
|
|
|
fn clone(&self) -> Intersection<'a, T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
Intersection {
|
|
|
|
a: self.a.clone(),
|
|
|
|
b: self.b.clone(),
|
|
|
|
}
|
2015-03-21 22:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 23:15:35 -05:00
|
|
|
impl<'a, T: Ord> Iterator for Intersection<'a, T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
|
2014-09-16 10:49:26 -04:00
|
|
|
fn next(&mut self) -> Option<&'a T> {
|
|
|
|
loop {
|
|
|
|
let o_cmp = match (self.a.peek(), self.b.peek()) {
|
2015-11-24 11:23:48 +13:00
|
|
|
(None, _) => None,
|
|
|
|
(_, None) => None,
|
2014-09-16 10:49:26 -04:00
|
|
|
(Some(a1), Some(b1)) => Some(a1.cmp(b1)),
|
|
|
|
};
|
|
|
|
match o_cmp {
|
2015-11-24 11:23:48 +13:00
|
|
|
None => return None,
|
|
|
|
Some(Less) => {
|
|
|
|
self.a.next();
|
|
|
|
}
|
|
|
|
Some(Equal) => {
|
|
|
|
self.b.next();
|
|
|
|
return self.a.next();
|
|
|
|
}
|
|
|
|
Some(Greater) => {
|
|
|
|
self.b.next();
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 13:54:03 -08:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(0, Some(min(self.a.len(), self.b.len())))
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|
|
|
|
|
2015-03-21 22:55:52 -04:00
|
|
|
impl<'a, T> Clone for Union<'a, T> {
|
|
|
|
fn clone(&self) -> Union<'a, T> {
|
2015-11-24 11:23:48 +13:00
|
|
|
Union {
|
|
|
|
a: self.a.clone(),
|
|
|
|
b: self.b.clone(),
|
|
|
|
}
|
2015-03-21 22:55:52 -04:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 23:15:35 -05:00
|
|
|
impl<'a, T: Ord> Iterator for Union<'a, T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
|
2014-09-16 10:49:26 -04:00
|
|
|
fn next(&mut self) -> Option<&'a T> {
|
|
|
|
loop {
|
|
|
|
match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) {
|
2015-11-24 11:23:48 +13:00
|
|
|
Less => return self.a.next(),
|
|
|
|
Equal => {
|
|
|
|
self.b.next();
|
|
|
|
return self.a.next();
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
Greater => return self.b.next(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 13:54:03 -08:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
2014-09-16 10:49:26 -04:00
|
|
|
}
|