1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // This is pretty much entirely stolen from TreeSet, since BTreeMap has an identical interface
14 use core::cmp::Ordering::{self, Less, Greater, Equal};
15 use core::cmp::{min, max};
18 use core::iter::{Peekable, FromIterator, FusedIterator};
19 use core::ops::{BitOr, BitAnd, BitXor, Sub};
22 use btree_map::{BTreeMap, Keys};
24 use range::RangeArgument;
26 // FIXME(conventions): implement bounded iterators
28 /// A set based on a B-Tree.
30 /// See [`BTreeMap`]'s documentation for a detailed discussion of this collection's performance
31 /// benefits and drawbacks.
33 /// It is a logic error for an item to be modified in such a way that the item's ordering relative
34 /// to any other item, as determined by the [`Ord`] trait, changes while it is in the set. This is
35 /// normally only possible through [`Cell`], [`RefCell`], global state, I/O, or unsafe code.
37 /// [`BTreeMap`]: struct.BTreeMap.html
38 /// [`Ord`]: ../../std/cmp/trait.Ord.html
39 /// [`Cell`]: ../../std/cell/struct.Cell.html
40 /// [`RefCell`]: ../../std/cell/struct.RefCell.html
45 /// use std::collections::BTreeSet;
47 /// // Type inference lets us omit an explicit type signature (which
48 /// // would be `BTreeSet<&str>` in this example).
49 /// let mut books = BTreeSet::new();
51 /// // Add some books.
52 /// books.insert("A Dance With Dragons");
53 /// books.insert("To Kill a Mockingbird");
54 /// books.insert("The Odyssey");
55 /// books.insert("The Great Gatsby");
57 /// // Check for a specific one.
58 /// if !books.contains("The Winds of Winter") {
59 /// println!("We have {} books, but The Winds of Winter ain't one.",
64 /// books.remove("The Odyssey");
66 /// // Iterate over everything.
67 /// for book in &books {
68 /// println!("{}", book);
71 #[derive(Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
72 #[stable(feature = "rust1", since = "1.0.0")]
73 pub struct BTreeSet<T> {
77 /// An iterator over a `BTreeSet`'s items.
79 /// This structure is created by the [`iter`] method on [`BTreeSet`].
81 /// [`BTreeSet`]: struct.BTreeSet.html
82 /// [`iter`]: struct.BTreeSet.html#method.iter
83 #[stable(feature = "rust1", since = "1.0.0")]
84 pub struct Iter<'a, T: 'a> {
85 iter: Keys<'a, T, ()>,
88 /// An owning iterator over a `BTreeSet`'s items.
90 /// This structure is created by the `into_iter` method on [`BTreeSet`]
91 /// [`BTreeSet`] (provided by the `IntoIterator` trait).
93 /// [`BTreeSet`]: struct.BTreeSet.html
94 #[stable(feature = "rust1", since = "1.0.0")]
95 pub struct IntoIter<T> {
96 iter: ::btree_map::IntoIter<T, ()>,
99 /// An iterator over a sub-range of `BTreeSet`'s items.
101 /// This structure is created by the [`range`] method on [`BTreeSet`].
103 /// [`BTreeSet`]: struct.BTreeSet.html
104 /// [`range`]: struct.BTreeSet.html#method.range
105 pub struct Range<'a, T: 'a> {
106 iter: ::btree_map::Range<'a, T, ()>,
109 /// A lazy iterator producing elements in the set difference (in-order).
111 /// This structure is created by the [`difference`] method on [`BTreeSet`].
113 /// [`BTreeSet`]: struct.BTreeSet.html
114 /// [`difference`]: struct.BTreeSet.html#method.difference
115 #[stable(feature = "rust1", since = "1.0.0")]
116 pub struct Difference<'a, T: 'a> {
117 a: Peekable<Iter<'a, T>>,
118 b: Peekable<Iter<'a, T>>,
121 /// A lazy iterator producing elements in the set symmetric difference (in-order).
123 /// This structure is created by the [`symmetric_difference`] method on
126 /// [`BTreeSet`]: struct.BTreeSet.html
127 /// [`symmetric_difference`]: struct.BTreeSet.html#method.symmetric_difference
128 #[stable(feature = "rust1", since = "1.0.0")]
129 pub struct SymmetricDifference<'a, T: 'a> {
130 a: Peekable<Iter<'a, T>>,
131 b: Peekable<Iter<'a, T>>,
134 /// A lazy iterator producing elements in the set intersection (in-order).
136 /// This structure is created by the [`intersection`] method on [`BTreeSet`].
138 /// [`BTreeSet`]: struct.BTreeSet.html
139 /// [`intersection`]: struct.BTreeSet.html#method.intersection
140 #[stable(feature = "rust1", since = "1.0.0")]
141 pub struct Intersection<'a, T: 'a> {
142 a: Peekable<Iter<'a, T>>,
143 b: Peekable<Iter<'a, T>>,
146 /// A lazy iterator producing elements in the set union (in-order).
148 /// This structure is created by the [`union`] method on [`BTreeSet`].
150 /// [`BTreeSet`]: struct.BTreeSet.html
151 /// [`union`]: struct.BTreeSet.html#method.union
152 #[stable(feature = "rust1", since = "1.0.0")]
153 pub struct Union<'a, T: 'a> {
154 a: Peekable<Iter<'a, T>>,
155 b: Peekable<Iter<'a, T>>,
158 impl<T: Ord> BTreeSet<T> {
159 /// Makes a new `BTreeSet` with a reasonable choice of B.
164 /// # #![allow(unused_mut)]
165 /// use std::collections::BTreeSet;
167 /// let mut set: BTreeSet<i32> = BTreeSet::new();
169 #[stable(feature = "rust1", since = "1.0.0")]
170 pub fn new() -> BTreeSet<T> {
171 BTreeSet { map: BTreeMap::new() }
175 impl<T> BTreeSet<T> {
176 /// Gets an iterator that visits the values in the `BTreeSet` in ascending order.
181 /// use std::collections::BTreeSet;
183 /// let set: BTreeSet<usize> = [1, 2, 3].iter().cloned().collect();
184 /// let mut set_iter = set.iter();
185 /// assert_eq!(set_iter.next(), Some(&1));
186 /// assert_eq!(set_iter.next(), Some(&2));
187 /// assert_eq!(set_iter.next(), Some(&3));
188 /// assert_eq!(set_iter.next(), None);
191 /// Values returned by the iterator are returned in ascending order:
194 /// use std::collections::BTreeSet;
196 /// let set: BTreeSet<usize> = [3, 1, 2].iter().cloned().collect();
197 /// let mut set_iter = set.iter();
198 /// assert_eq!(set_iter.next(), Some(&1));
199 /// assert_eq!(set_iter.next(), Some(&2));
200 /// assert_eq!(set_iter.next(), Some(&3));
201 /// assert_eq!(set_iter.next(), None);
203 #[stable(feature = "rust1", since = "1.0.0")]
204 pub fn iter(&self) -> Iter<T> {
205 Iter { iter: self.map.keys() }
209 impl<T: Ord> BTreeSet<T> {
210 /// Constructs a double-ended iterator over a sub-range of elements in the set.
211 /// The simplest way is to use the range syntax `min..max`, thus `range(min..max)` will
212 /// yield elements from min (inclusive) to max (exclusive).
213 /// The range may also be entered as `(Bound<T>, Bound<T>)`, so for example
214 /// `range((Excluded(4), Included(10)))` will yield a left-exclusive, right-inclusive
215 /// range from 4 to 10.
220 /// #![feature(btree_range, collections_bound)]
222 /// use std::collections::BTreeSet;
223 /// use std::collections::Bound::Included;
225 /// let mut set = BTreeSet::new();
229 /// for &elem in set.range((Included(&4), Included(&8))) {
230 /// println!("{}", elem);
232 /// assert_eq!(Some(&5), set.range(4..).next());
234 #[unstable(feature = "btree_range",
235 reason = "matches collection reform specification, waiting for dust to settle",
237 pub fn range<K: ?Sized, R>(&self, range: R) -> Range<T>
238 where K: Ord, T: Borrow<K>, R: RangeArgument<K>
240 Range { iter: self.map.range(range) }
244 impl<T: Ord> BTreeSet<T> {
245 /// Visits the values representing the difference, in ascending order.
250 /// use std::collections::BTreeSet;
252 /// let mut a = BTreeSet::new();
256 /// let mut b = BTreeSet::new();
260 /// let diff: Vec<_> = a.difference(&b).cloned().collect();
261 /// assert_eq!(diff, [1]);
263 #[stable(feature = "rust1", since = "1.0.0")]
264 pub fn difference<'a>(&'a self, other: &'a BTreeSet<T>) -> Difference<'a, T> {
266 a: self.iter().peekable(),
267 b: other.iter().peekable(),
271 /// Visits the values representing the symmetric difference, in ascending order.
276 /// use std::collections::BTreeSet;
278 /// let mut a = BTreeSet::new();
282 /// let mut b = BTreeSet::new();
286 /// let sym_diff: Vec<_> = a.symmetric_difference(&b).cloned().collect();
287 /// assert_eq!(sym_diff, [1, 3]);
289 #[stable(feature = "rust1", since = "1.0.0")]
290 pub fn symmetric_difference<'a>(&'a self,
291 other: &'a BTreeSet<T>)
292 -> SymmetricDifference<'a, T> {
293 SymmetricDifference {
294 a: self.iter().peekable(),
295 b: other.iter().peekable(),
299 /// Visits the values representing the intersection, in ascending order.
304 /// use std::collections::BTreeSet;
306 /// let mut a = BTreeSet::new();
310 /// let mut b = BTreeSet::new();
314 /// let intersection: Vec<_> = a.intersection(&b).cloned().collect();
315 /// assert_eq!(intersection, [2]);
317 #[stable(feature = "rust1", since = "1.0.0")]
318 pub fn intersection<'a>(&'a self, other: &'a BTreeSet<T>) -> Intersection<'a, T> {
320 a: self.iter().peekable(),
321 b: other.iter().peekable(),
325 /// Visits the values representing the union, in ascending order.
330 /// use std::collections::BTreeSet;
332 /// let mut a = BTreeSet::new();
335 /// let mut b = BTreeSet::new();
338 /// let union: Vec<_> = a.union(&b).cloned().collect();
339 /// assert_eq!(union, [1, 2]);
341 #[stable(feature = "rust1", since = "1.0.0")]
342 pub fn union<'a>(&'a self, other: &'a BTreeSet<T>) -> Union<'a, T> {
344 a: self.iter().peekable(),
345 b: other.iter().peekable(),
349 /// Returns the number of elements in the set.
354 /// use std::collections::BTreeSet;
356 /// let mut v = BTreeSet::new();
357 /// assert_eq!(v.len(), 0);
359 /// assert_eq!(v.len(), 1);
361 #[stable(feature = "rust1", since = "1.0.0")]
362 pub fn len(&self) -> usize {
366 /// Returns true if the set contains no elements.
371 /// use std::collections::BTreeSet;
373 /// let mut v = BTreeSet::new();
374 /// assert!(v.is_empty());
376 /// assert!(!v.is_empty());
378 #[stable(feature = "rust1", since = "1.0.0")]
379 pub fn is_empty(&self) -> bool {
383 /// Clears the set, removing all values.
388 /// use std::collections::BTreeSet;
390 /// let mut v = BTreeSet::new();
393 /// assert!(v.is_empty());
395 #[stable(feature = "rust1", since = "1.0.0")]
396 pub fn clear(&mut self) {
400 /// Returns `true` if the set contains a value.
402 /// The value may be any borrowed form of the set's value type,
403 /// but the ordering on the borrowed form *must* match the
404 /// ordering on the value type.
409 /// use std::collections::BTreeSet;
411 /// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
412 /// assert_eq!(set.contains(&1), true);
413 /// assert_eq!(set.contains(&4), false);
415 #[stable(feature = "rust1", since = "1.0.0")]
416 pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
420 self.map.contains_key(value)
423 /// Returns a reference to the value in the set, if any, that is equal to the given value.
425 /// The value may be any borrowed form of the set's value type,
426 /// but the ordering on the borrowed form *must* match the
427 /// ordering on the value type.
428 #[stable(feature = "set_recovery", since = "1.9.0")]
429 pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
433 Recover::get(&self.map, value)
436 /// Returns `true` if the set has no elements in common with `other`.
437 /// This is equivalent to checking for an empty intersection.
442 /// use std::collections::BTreeSet;
444 /// let a: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
445 /// let mut b = BTreeSet::new();
447 /// assert_eq!(a.is_disjoint(&b), true);
449 /// assert_eq!(a.is_disjoint(&b), true);
451 /// assert_eq!(a.is_disjoint(&b), false);
453 #[stable(feature = "rust1", since = "1.0.0")]
454 pub fn is_disjoint(&self, other: &BTreeSet<T>) -> bool {
455 self.intersection(other).next().is_none()
458 /// Returns `true` if the set is a subset of another.
463 /// use std::collections::BTreeSet;
465 /// let sup: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
466 /// let mut set = BTreeSet::new();
468 /// assert_eq!(set.is_subset(&sup), true);
470 /// assert_eq!(set.is_subset(&sup), true);
472 /// assert_eq!(set.is_subset(&sup), false);
474 #[stable(feature = "rust1", since = "1.0.0")]
475 pub fn is_subset(&self, other: &BTreeSet<T>) -> bool {
476 // Stolen from TreeMap
477 let mut x = self.iter();
478 let mut y = other.iter();
479 let mut a = x.next();
480 let mut b = y.next();
491 Greater => return false,
492 Equal => a = x.next(),
500 /// Returns `true` if the set is a superset of another.
505 /// use std::collections::BTreeSet;
507 /// let sub: BTreeSet<_> = [1, 2].iter().cloned().collect();
508 /// let mut set = BTreeSet::new();
510 /// assert_eq!(set.is_superset(&sub), false);
514 /// assert_eq!(set.is_superset(&sub), false);
517 /// assert_eq!(set.is_superset(&sub), true);
519 #[stable(feature = "rust1", since = "1.0.0")]
520 pub fn is_superset(&self, other: &BTreeSet<T>) -> bool {
521 other.is_subset(self)
524 /// Adds a value to the set.
526 /// If the set did not have this value present, `true` is returned.
528 /// If the set did have this value present, `false` is returned, and the
529 /// entry is not updated. See the [module-level documentation] for more.
531 /// [module-level documentation]: index.html#insert-and-complex-keys
536 /// use std::collections::BTreeSet;
538 /// let mut set = BTreeSet::new();
540 /// assert_eq!(set.insert(2), true);
541 /// assert_eq!(set.insert(2), false);
542 /// assert_eq!(set.len(), 1);
544 #[stable(feature = "rust1", since = "1.0.0")]
545 pub fn insert(&mut self, value: T) -> bool {
546 self.map.insert(value, ()).is_none()
549 /// Adds a value to the set, replacing the existing value, if any, that is equal to the given
550 /// one. Returns the replaced value.
551 #[stable(feature = "set_recovery", since = "1.9.0")]
552 pub fn replace(&mut self, value: T) -> Option<T> {
553 Recover::replace(&mut self.map, value)
556 /// Removes a value from the set. Returns `true` if the value was
557 /// present in the set.
559 /// The value may be any borrowed form of the set's value type,
560 /// but the ordering on the borrowed form *must* match the
561 /// ordering on the value type.
566 /// use std::collections::BTreeSet;
568 /// let mut set = BTreeSet::new();
571 /// assert_eq!(set.remove(&2), true);
572 /// assert_eq!(set.remove(&2), false);
574 #[stable(feature = "rust1", since = "1.0.0")]
575 pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
579 self.map.remove(value).is_some()
582 /// Removes and returns the value in the set, if any, that is equal to the given one.
584 /// The value may be any borrowed form of the set's value type,
585 /// but the ordering on the borrowed form *must* match the
586 /// ordering on the value type.
587 #[stable(feature = "set_recovery", since = "1.9.0")]
588 pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
592 Recover::take(&mut self.map, value)
595 /// Moves all elements from `other` into `Self`, leaving `other` empty.
600 /// use std::collections::BTreeSet;
602 /// let mut a = BTreeSet::new();
607 /// let mut b = BTreeSet::new();
612 /// a.append(&mut b);
614 /// assert_eq!(a.len(), 5);
615 /// assert_eq!(b.len(), 0);
617 /// assert!(a.contains(&1));
618 /// assert!(a.contains(&2));
619 /// assert!(a.contains(&3));
620 /// assert!(a.contains(&4));
621 /// assert!(a.contains(&5));
623 #[stable(feature = "btree_append", since = "1.11.0")]
624 pub fn append(&mut self, other: &mut Self) {
625 self.map.append(&mut other.map);
628 /// Splits the collection into two at the given key. Returns everything after the given key,
629 /// including the key.
636 /// use std::collections::BTreeMap;
638 /// let mut a = BTreeMap::new();
639 /// a.insert(1, "a");
640 /// a.insert(2, "b");
641 /// a.insert(3, "c");
642 /// a.insert(17, "d");
643 /// a.insert(41, "e");
645 /// let b = a.split_off(&3);
647 /// assert_eq!(a.len(), 2);
648 /// assert_eq!(b.len(), 3);
650 /// assert_eq!(a[&1], "a");
651 /// assert_eq!(a[&2], "b");
653 /// assert_eq!(b[&3], "c");
654 /// assert_eq!(b[&17], "d");
655 /// assert_eq!(b[&41], "e");
657 #[stable(feature = "btree_split_off", since = "1.11.0")]
658 pub fn split_off<Q: ?Sized + Ord>(&mut self, key: &Q) -> Self where T: Borrow<Q> {
659 BTreeSet { map: self.map.split_off(key) }
663 #[stable(feature = "rust1", since = "1.0.0")]
664 impl<T: Ord> FromIterator<T> for BTreeSet<T> {
665 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> BTreeSet<T> {
666 let mut set = BTreeSet::new();
672 #[stable(feature = "rust1", since = "1.0.0")]
673 impl<T> IntoIterator for BTreeSet<T> {
675 type IntoIter = IntoIter<T>;
677 /// Gets an iterator for moving out the BtreeSet's contents.
682 /// use std::collections::BTreeSet;
684 /// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
686 /// let v: Vec<_> = set.into_iter().collect();
687 /// assert_eq!(v, [1, 2, 3, 4]);
689 fn into_iter(self) -> IntoIter<T> {
690 IntoIter { iter: self.map.into_iter() }
694 #[stable(feature = "rust1", since = "1.0.0")]
695 impl<'a, T> IntoIterator for &'a BTreeSet<T> {
697 type IntoIter = Iter<'a, T>;
699 fn into_iter(self) -> Iter<'a, T> {
704 #[stable(feature = "rust1", since = "1.0.0")]
705 impl<T: Ord> Extend<T> for BTreeSet<T> {
707 fn extend<Iter: IntoIterator<Item = T>>(&mut self, iter: Iter) {
714 #[stable(feature = "extend_ref", since = "1.2.0")]
715 impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BTreeSet<T> {
716 fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
717 self.extend(iter.into_iter().cloned());
721 #[stable(feature = "rust1", since = "1.0.0")]
722 impl<T: Ord> Default for BTreeSet<T> {
723 /// Makes an empty `BTreeSet<T>` with a reasonable choice of B.
724 fn default() -> BTreeSet<T> {
729 #[stable(feature = "rust1", since = "1.0.0")]
730 impl<'a, 'b, T: Ord + Clone> Sub<&'b BTreeSet<T>> for &'a BTreeSet<T> {
731 type Output = BTreeSet<T>;
733 /// Returns the difference of `self` and `rhs` as a new `BTreeSet<T>`.
738 /// use std::collections::BTreeSet;
740 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
741 /// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect();
743 /// let result = &a - &b;
744 /// let result_vec: Vec<_> = result.into_iter().collect();
745 /// assert_eq!(result_vec, [1, 2]);
747 fn sub(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
748 self.difference(rhs).cloned().collect()
752 #[stable(feature = "rust1", since = "1.0.0")]
753 impl<'a, 'b, T: Ord + Clone> BitXor<&'b BTreeSet<T>> for &'a BTreeSet<T> {
754 type Output = BTreeSet<T>;
756 /// Returns the symmetric difference of `self` and `rhs` as a new `BTreeSet<T>`.
761 /// use std::collections::BTreeSet;
763 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
764 /// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();
766 /// let result = &a ^ &b;
767 /// let result_vec: Vec<_> = result.into_iter().collect();
768 /// assert_eq!(result_vec, [1, 4]);
770 fn bitxor(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
771 self.symmetric_difference(rhs).cloned().collect()
775 #[stable(feature = "rust1", since = "1.0.0")]
776 impl<'a, 'b, T: Ord + Clone> BitAnd<&'b BTreeSet<T>> for &'a BTreeSet<T> {
777 type Output = BTreeSet<T>;
779 /// Returns the intersection of `self` and `rhs` as a new `BTreeSet<T>`.
784 /// use std::collections::BTreeSet;
786 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
787 /// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();
789 /// let result = &a & &b;
790 /// let result_vec: Vec<_> = result.into_iter().collect();
791 /// assert_eq!(result_vec, [2, 3]);
793 fn bitand(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
794 self.intersection(rhs).cloned().collect()
798 #[stable(feature = "rust1", since = "1.0.0")]
799 impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>> for &'a BTreeSet<T> {
800 type Output = BTreeSet<T>;
802 /// Returns the union of `self` and `rhs` as a new `BTreeSet<T>`.
807 /// use std::collections::BTreeSet;
809 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
810 /// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect();
812 /// let result = &a | &b;
813 /// let result_vec: Vec<_> = result.into_iter().collect();
814 /// assert_eq!(result_vec, [1, 2, 3, 4, 5]);
816 fn bitor(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
817 self.union(rhs).cloned().collect()
821 #[stable(feature = "rust1", since = "1.0.0")]
822 impl<T: Debug> Debug for BTreeSet<T> {
823 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
824 f.debug_set().entries(self.iter()).finish()
828 #[stable(feature = "rust1", since = "1.0.0")]
829 impl<'a, T> Clone for Iter<'a, T> {
830 fn clone(&self) -> Iter<'a, T> {
831 Iter { iter: self.iter.clone() }
834 #[stable(feature = "rust1", since = "1.0.0")]
835 impl<'a, T> Iterator for Iter<'a, T> {
838 fn next(&mut self) -> Option<&'a T> {
841 fn size_hint(&self) -> (usize, Option<usize>) {
842 self.iter.size_hint()
845 #[stable(feature = "rust1", since = "1.0.0")]
846 impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
847 fn next_back(&mut self) -> Option<&'a T> {
848 self.iter.next_back()
851 #[stable(feature = "rust1", since = "1.0.0")]
852 impl<'a, T> ExactSizeIterator for Iter<'a, T> {
853 fn len(&self) -> usize { self.iter.len() }
856 #[unstable(feature = "fused", issue = "35602")]
857 impl<'a, T> FusedIterator for Iter<'a, T> {}
859 #[stable(feature = "rust1", since = "1.0.0")]
860 impl<T> Iterator for IntoIter<T> {
863 fn next(&mut self) -> Option<T> {
864 self.iter.next().map(|(k, _)| k)
866 fn size_hint(&self) -> (usize, Option<usize>) {
867 self.iter.size_hint()
870 #[stable(feature = "rust1", since = "1.0.0")]
871 impl<T> DoubleEndedIterator for IntoIter<T> {
872 fn next_back(&mut self) -> Option<T> {
873 self.iter.next_back().map(|(k, _)| k)
876 #[stable(feature = "rust1", since = "1.0.0")]
877 impl<T> ExactSizeIterator for IntoIter<T> {
878 fn len(&self) -> usize { self.iter.len() }
881 #[unstable(feature = "fused", issue = "35602")]
882 impl<T> FusedIterator for IntoIter<T> {}
884 impl<'a, T> Clone for Range<'a, T> {
885 fn clone(&self) -> Range<'a, T> {
886 Range { iter: self.iter.clone() }
889 impl<'a, T> Iterator for Range<'a, T> {
892 fn next(&mut self) -> Option<&'a T> {
893 self.iter.next().map(|(k, _)| k)
896 impl<'a, T> DoubleEndedIterator for Range<'a, T> {
897 fn next_back(&mut self) -> Option<&'a T> {
898 self.iter.next_back().map(|(k, _)| k)
902 #[unstable(feature = "fused", issue = "35602")]
903 impl<'a, T> FusedIterator for Range<'a, T> {}
905 /// Compare `x` and `y`, but return `short` if x is None and `long` if y is None
906 fn cmp_opt<T: Ord>(x: Option<&T>, y: Option<&T>, short: Ordering, long: Ordering) -> Ordering {
910 (Some(x1), Some(y1)) => x1.cmp(y1),
914 #[stable(feature = "rust1", since = "1.0.0")]
915 impl<'a, T> Clone for Difference<'a, T> {
916 fn clone(&self) -> Difference<'a, T> {
923 #[stable(feature = "rust1", since = "1.0.0")]
924 impl<'a, T: Ord> Iterator for Difference<'a, T> {
927 fn next(&mut self) -> Option<&'a T> {
929 match cmp_opt(self.a.peek(), self.b.peek(), Less, Less) {
930 Less => return self.a.next(),
942 fn size_hint(&self) -> (usize, Option<usize>) {
943 let a_len = self.a.len();
944 let b_len = self.b.len();
945 (a_len.saturating_sub(b_len), Some(a_len))
949 #[unstable(feature = "fused", issue = "35602")]
950 impl<'a, T: Ord> FusedIterator for Difference<'a, T> {}
952 #[stable(feature = "rust1", since = "1.0.0")]
953 impl<'a, T> Clone for SymmetricDifference<'a, T> {
954 fn clone(&self) -> SymmetricDifference<'a, T> {
955 SymmetricDifference {
961 #[stable(feature = "rust1", since = "1.0.0")]
962 impl<'a, T: Ord> Iterator for SymmetricDifference<'a, T> {
965 fn next(&mut self) -> Option<&'a T> {
967 match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) {
968 Less => return self.a.next(),
973 Greater => return self.b.next(),
978 fn size_hint(&self) -> (usize, Option<usize>) {
979 (0, Some(self.a.len() + self.b.len()))
983 #[unstable(feature = "fused", issue = "35602")]
984 impl<'a, T: Ord> FusedIterator for SymmetricDifference<'a, T> {}
986 #[stable(feature = "rust1", since = "1.0.0")]
987 impl<'a, T> Clone for Intersection<'a, T> {
988 fn clone(&self) -> Intersection<'a, T> {
995 #[stable(feature = "rust1", since = "1.0.0")]
996 impl<'a, T: Ord> Iterator for Intersection<'a, T> {
999 fn next(&mut self) -> Option<&'a T> {
1001 let o_cmp = match (self.a.peek(), self.b.peek()) {
1004 (Some(a1), Some(b1)) => Some(a1.cmp(b1)),
1007 None => return None,
1013 return self.a.next();
1022 fn size_hint(&self) -> (usize, Option<usize>) {
1023 (0, Some(min(self.a.len(), self.b.len())))
1027 #[unstable(feature = "fused", issue = "35602")]
1028 impl<'a, T: Ord> FusedIterator for Intersection<'a, T> {}
1030 #[stable(feature = "rust1", since = "1.0.0")]
1031 impl<'a, T> Clone for Union<'a, T> {
1032 fn clone(&self) -> Union<'a, T> {
1039 #[stable(feature = "rust1", since = "1.0.0")]
1040 impl<'a, T: Ord> Iterator for Union<'a, T> {
1043 fn next(&mut self) -> Option<&'a T> {
1045 match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) {
1046 Less => return self.a.next(),
1049 return self.a.next();
1051 Greater => return self.b.next(),
1056 fn size_hint(&self) -> (usize, Option<usize>) {
1057 let a_len = self.a.len();
1058 let b_len = self.b.len();
1059 (max(a_len, b_len), Some(a_len + b_len))
1063 #[unstable(feature = "fused", issue = "35602")]
1064 impl<'a, T: Ord> FusedIterator for Union<'a, T> {}