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
16 use core::borrow::BorrowFrom;
17 use core::cmp::Ordering::{self, Less, Greater, Equal};
18 use core::default::Default;
21 use core::iter::{Peekable, Map, FromIterator, IntoIterator};
22 use core::ops::{BitOr, BitAnd, BitXor, Sub};
24 use btree_map::{BTreeMap, Keys};
27 // FIXME(conventions): implement bounded iterators
29 /// A set based on a B-Tree.
31 /// See BTreeMap's documentation for a detailed discussion of this collection's performance
32 /// benefits and drawbacks.
33 #[derive(Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub struct BTreeSet<T>{
39 /// An iterator over a BTreeSet's items.
40 #[stable(feature = "rust1", since = "1.0.0")]
41 pub struct Iter<'a, T: 'a> {
45 /// An owning iterator over a BTreeSet's items.
46 #[stable(feature = "rust1", since = "1.0.0")]
47 pub struct IntoIter<T> {
48 iter: Map<::btree_map::IntoIter<T, ()>, fn((T, ())) -> T>
51 /// An iterator over a sub-range of BTreeSet's items.
52 pub struct Range<'a, T: 'a> {
53 iter: Map<::btree_map::Range<'a, T, ()>, fn((&'a T, &'a ())) -> &'a T>
56 /// A lazy iterator producing elements in the set difference (in-order).
57 #[stable(feature = "rust1", since = "1.0.0")]
58 pub struct Difference<'a, T:'a> {
59 a: Peekable<Iter<'a, T>>,
60 b: Peekable<Iter<'a, T>>,
63 /// A lazy iterator producing elements in the set symmetric difference (in-order).
64 #[stable(feature = "rust1", since = "1.0.0")]
65 pub struct SymmetricDifference<'a, T:'a> {
66 a: Peekable<Iter<'a, T>>,
67 b: Peekable<Iter<'a, T>>,
70 /// A lazy iterator producing elements in the set intersection (in-order).
71 #[stable(feature = "rust1", since = "1.0.0")]
72 pub struct Intersection<'a, T:'a> {
73 a: Peekable<Iter<'a, T>>,
74 b: Peekable<Iter<'a, T>>,
77 /// A lazy iterator producing elements in the set union (in-order).
78 #[stable(feature = "rust1", since = "1.0.0")]
79 pub struct Union<'a, T:'a> {
80 a: Peekable<Iter<'a, T>>,
81 b: Peekable<Iter<'a, T>>,
84 impl<T: Ord> BTreeSet<T> {
85 /// Makes a new BTreeSet with a reasonable choice of B.
90 /// use std::collections::BTreeSet;
92 /// let mut set: BTreeSet<i32> = BTreeSet::new();
94 #[stable(feature = "rust1", since = "1.0.0")]
95 pub fn new() -> BTreeSet<T> {
96 BTreeSet { map: BTreeMap::new() }
99 /// Makes a new BTreeSet with the given B.
101 /// B cannot be less than 2.
102 #[unstable(feature = "collections",
103 reason = "probably want this to be on the type, eventually")]
104 pub fn with_b(b: usize) -> BTreeSet<T> {
105 BTreeSet { map: BTreeMap::with_b(b) }
109 impl<T> BTreeSet<T> {
110 /// Gets an iterator over the BTreeSet's contents.
115 /// use std::collections::BTreeSet;
117 /// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
119 /// for x in set.iter() {
120 /// println!("{}", x);
123 /// let v: Vec<usize> = set.iter().cloned().collect();
124 /// assert_eq!(v, vec![1,2,3,4]);
126 #[stable(feature = "rust1", since = "1.0.0")]
127 pub fn iter(&self) -> Iter<T> {
128 Iter { iter: self.map.keys() }
131 /// Gets an iterator for moving out the BtreeSet's contents.
136 /// use std::collections::BTreeSet;
138 /// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
140 /// let v: Vec<usize> = set.into_iter().collect();
141 /// assert_eq!(v, vec![1,2,3,4]);
143 #[stable(feature = "rust1", since = "1.0.0")]
144 pub fn into_iter(self) -> IntoIter<T> {
145 fn first<A, B>((a, _): (A, B)) -> A { a }
146 let first: fn((T, ())) -> T = first; // coerce to fn pointer
148 IntoIter { iter: self.map.into_iter().map(first) }
152 impl<T: Ord> BTreeSet<T> {
153 /// Constructs a double-ended iterator over a sub-range of elements in the set, starting
154 /// at min, and ending at max. If min is `Unbounded`, then it will be treated as "negative
155 /// infinity", and if max is `Unbounded`, then it will be treated as "positive infinity".
156 /// Thus range(Unbounded, Unbounded) will yield the whole collection.
161 /// use std::collections::BTreeSet;
162 /// use std::collections::Bound::{Included, Unbounded};
164 /// let mut set = BTreeSet::new();
168 /// for &elem in set.range(Included(&4), Included(&8)) {
169 /// println!("{}", elem);
171 /// assert_eq!(Some(&5), set.range(Included(&4), Unbounded).next());
173 #[unstable(feature = "collections",
174 reason = "matches collection reform specification, waiting for dust to settle")]
175 pub fn range<'a>(&'a self, min: Bound<&T>, max: Bound<&T>) -> Range<'a, T> {
176 fn first<A, B>((a, _): (A, B)) -> A { a }
177 let first: fn((&'a T, &'a ())) -> &'a T = first; // coerce to fn pointer
179 Range { iter: self.map.range(min, max).map(first) }
183 impl<T: Ord> BTreeSet<T> {
184 /// Visits the values representing the difference, in ascending order.
189 /// use std::collections::BTreeSet;
191 /// let mut a = BTreeSet::new();
195 /// let mut b = BTreeSet::new();
199 /// let diff: Vec<usize> = a.difference(&b).cloned().collect();
200 /// assert_eq!(diff, vec![1]);
202 #[stable(feature = "rust1", since = "1.0.0")]
203 pub fn difference<'a>(&'a self, other: &'a BTreeSet<T>) -> Difference<'a, T> {
204 Difference{a: self.iter().peekable(), b: other.iter().peekable()}
207 /// Visits the values representing the symmetric difference, in ascending order.
212 /// use std::collections::BTreeSet;
214 /// let mut a = BTreeSet::new();
218 /// let mut b = BTreeSet::new();
222 /// let sym_diff: Vec<usize> = a.symmetric_difference(&b).cloned().collect();
223 /// assert_eq!(sym_diff, vec![1,3]);
225 #[stable(feature = "rust1", since = "1.0.0")]
226 pub fn symmetric_difference<'a>(&'a self, other: &'a BTreeSet<T>)
227 -> SymmetricDifference<'a, T> {
228 SymmetricDifference{a: self.iter().peekable(), b: other.iter().peekable()}
231 /// Visits the values representing the intersection, in ascending order.
236 /// use std::collections::BTreeSet;
238 /// let mut a = BTreeSet::new();
242 /// let mut b = BTreeSet::new();
246 /// let intersection: Vec<usize> = a.intersection(&b).cloned().collect();
247 /// assert_eq!(intersection, vec![2]);
249 #[stable(feature = "rust1", since = "1.0.0")]
250 pub fn intersection<'a>(&'a self, other: &'a BTreeSet<T>)
251 -> Intersection<'a, T> {
252 Intersection{a: self.iter().peekable(), b: other.iter().peekable()}
255 /// Visits the values representing the union, in ascending order.
260 /// use std::collections::BTreeSet;
262 /// let mut a = BTreeSet::new();
265 /// let mut b = BTreeSet::new();
268 /// let union: Vec<usize> = a.union(&b).cloned().collect();
269 /// assert_eq!(union, vec![1,2]);
271 #[stable(feature = "rust1", since = "1.0.0")]
272 pub fn union<'a>(&'a self, other: &'a BTreeSet<T>) -> Union<'a, T> {
273 Union{a: self.iter().peekable(), b: other.iter().peekable()}
276 /// Return the number of elements in the set
281 /// use std::collections::BTreeSet;
283 /// let mut v = BTreeSet::new();
284 /// assert_eq!(v.len(), 0);
286 /// assert_eq!(v.len(), 1);
288 #[stable(feature = "rust1", since = "1.0.0")]
289 pub fn len(&self) -> usize { self.map.len() }
291 /// Returns true if the set contains no elements
296 /// use std::collections::BTreeSet;
298 /// let mut v = BTreeSet::new();
299 /// assert!(v.is_empty());
301 /// assert!(!v.is_empty());
303 #[stable(feature = "rust1", since = "1.0.0")]
304 pub fn is_empty(&self) -> bool { self.len() == 0 }
306 /// Clears the set, removing all values.
311 /// use std::collections::BTreeSet;
313 /// let mut v = BTreeSet::new();
316 /// assert!(v.is_empty());
318 #[stable(feature = "rust1", since = "1.0.0")]
319 pub fn clear(&mut self) {
323 /// Returns `true` if the set contains a value.
325 /// The value may be any borrowed form of the set's value type,
326 /// but the ordering on the borrowed form *must* match the
327 /// ordering on the value type.
332 /// use std::collections::BTreeSet;
334 /// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
335 /// assert_eq!(set.contains(&1), true);
336 /// assert_eq!(set.contains(&4), false);
338 #[stable(feature = "rust1", since = "1.0.0")]
339 pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool where Q: BorrowFrom<T> + Ord {
340 self.map.contains_key(value)
343 /// Returns `true` if the set has no elements in common with `other`.
344 /// This is equivalent to checking for an empty intersection.
349 /// use std::collections::BTreeSet;
351 /// let a: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
352 /// let mut b = BTreeSet::new();
354 /// assert_eq!(a.is_disjoint(&b), true);
356 /// assert_eq!(a.is_disjoint(&b), true);
358 /// assert_eq!(a.is_disjoint(&b), false);
360 #[stable(feature = "rust1", since = "1.0.0")]
361 pub fn is_disjoint(&self, other: &BTreeSet<T>) -> bool {
362 self.intersection(other).next().is_none()
365 /// Returns `true` if the set is a subset of another.
370 /// use std::collections::BTreeSet;
372 /// let sup: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
373 /// let mut set = BTreeSet::new();
375 /// assert_eq!(set.is_subset(&sup), true);
377 /// assert_eq!(set.is_subset(&sup), true);
379 /// assert_eq!(set.is_subset(&sup), false);
381 #[stable(feature = "rust1", since = "1.0.0")]
382 pub fn is_subset(&self, other: &BTreeSet<T>) -> bool {
383 // Stolen from TreeMap
384 let mut x = self.iter();
385 let mut y = other.iter();
386 let mut a = x.next();
387 let mut b = y.next();
398 Greater => return false,
399 Equal => a = x.next(),
407 /// Returns `true` if the set is a superset of another.
412 /// use std::collections::BTreeSet;
414 /// let sub: BTreeSet<_> = [1, 2].iter().cloned().collect();
415 /// let mut set = BTreeSet::new();
417 /// assert_eq!(set.is_superset(&sub), false);
421 /// assert_eq!(set.is_superset(&sub), false);
424 /// assert_eq!(set.is_superset(&sub), true);
426 #[stable(feature = "rust1", since = "1.0.0")]
427 pub fn is_superset(&self, other: &BTreeSet<T>) -> bool {
428 other.is_subset(self)
431 /// Adds a value to the set. Returns `true` if the value was not already
432 /// present in the set.
437 /// use std::collections::BTreeSet;
439 /// let mut set = BTreeSet::new();
441 /// assert_eq!(set.insert(2), true);
442 /// assert_eq!(set.insert(2), false);
443 /// assert_eq!(set.len(), 1);
445 #[stable(feature = "rust1", since = "1.0.0")]
446 pub fn insert(&mut self, value: T) -> bool {
447 self.map.insert(value, ()).is_none()
450 /// Removes a value from the set. Returns `true` if the value was
451 /// present in the set.
453 /// The value may be any borrowed form of the set's value type,
454 /// but the ordering on the borrowed form *must* match the
455 /// ordering on the value type.
460 /// use std::collections::BTreeSet;
462 /// let mut set = BTreeSet::new();
465 /// assert_eq!(set.remove(&2), true);
466 /// assert_eq!(set.remove(&2), false);
468 #[stable(feature = "rust1", since = "1.0.0")]
469 pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool where Q: BorrowFrom<T> + Ord {
470 self.map.remove(value).is_some()
474 #[stable(feature = "rust1", since = "1.0.0")]
475 impl<T: Ord> FromIterator<T> for BTreeSet<T> {
476 fn from_iter<Iter: Iterator<Item=T>>(iter: Iter) -> BTreeSet<T> {
477 let mut set = BTreeSet::new();
483 #[stable(feature = "rust1", since = "1.0.0")]
484 impl<T> IntoIterator for BTreeSet<T> {
486 type IntoIter = IntoIter<T>;
488 fn into_iter(self) -> IntoIter<T> {
493 #[stable(feature = "rust1", since = "1.0.0")]
494 impl<'a, T> IntoIterator for &'a BTreeSet<T> {
496 type IntoIter = Iter<'a, T>;
498 fn into_iter(self) -> Iter<'a, T> {
503 #[stable(feature = "rust1", since = "1.0.0")]
504 impl<T: Ord> Extend<T> for BTreeSet<T> {
506 fn extend<Iter: Iterator<Item=T>>(&mut self, iter: Iter) {
513 #[stable(feature = "rust1", since = "1.0.0")]
514 impl<T: Ord> Default for BTreeSet<T> {
515 #[stable(feature = "rust1", since = "1.0.0")]
516 fn default() -> BTreeSet<T> {
521 #[stable(feature = "rust1", since = "1.0.0")]
522 impl<'a, 'b, T: Ord + Clone> Sub<&'b BTreeSet<T>> for &'a BTreeSet<T> {
523 type Output = BTreeSet<T>;
525 /// Returns the difference of `self` and `rhs` as a new `BTreeSet<T>`.
530 /// use std::collections::BTreeSet;
532 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
533 /// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect();
535 /// let result = &a - &b;
536 /// let result_vec: Vec<_> = result.into_iter().collect();
537 /// assert_eq!(result_vec, vec![1, 2]);
539 fn sub(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
540 self.difference(rhs).cloned().collect()
544 #[stable(feature = "rust1", since = "1.0.0")]
545 impl<'a, 'b, T: Ord + Clone> BitXor<&'b BTreeSet<T>> for &'a BTreeSet<T> {
546 type Output = BTreeSet<T>;
548 /// Returns the symmetric difference of `self` and `rhs` as a new `BTreeSet<T>`.
553 /// use std::collections::BTreeSet;
555 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
556 /// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();
558 /// let result = &a ^ &b;
559 /// let result_vec: Vec<_> = result.into_iter().collect();
560 /// assert_eq!(result_vec, vec![1, 4]);
562 fn bitxor(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
563 self.symmetric_difference(rhs).cloned().collect()
567 #[stable(feature = "rust1", since = "1.0.0")]
568 impl<'a, 'b, T: Ord + Clone> BitAnd<&'b BTreeSet<T>> for &'a BTreeSet<T> {
569 type Output = BTreeSet<T>;
571 /// Returns the intersection of `self` and `rhs` as a new `BTreeSet<T>`.
576 /// use std::collections::BTreeSet;
578 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
579 /// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();
581 /// let result = &a & &b;
582 /// let result_vec: Vec<_> = result.into_iter().collect();
583 /// assert_eq!(result_vec, vec![2, 3]);
585 fn bitand(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
586 self.intersection(rhs).cloned().collect()
590 #[stable(feature = "rust1", since = "1.0.0")]
591 impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>> for &'a BTreeSet<T> {
592 type Output = BTreeSet<T>;
594 /// Returns the union of `self` and `rhs` as a new `BTreeSet<T>`.
599 /// use std::collections::BTreeSet;
601 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
602 /// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect();
604 /// let result = &a | &b;
605 /// let result_vec: Vec<_> = result.into_iter().collect();
606 /// assert_eq!(result_vec, vec![1, 2, 3, 4, 5]);
608 fn bitor(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
609 self.union(rhs).cloned().collect()
613 #[stable(feature = "rust1", since = "1.0.0")]
614 impl<T: Debug> Debug for BTreeSet<T> {
615 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
616 try!(write!(f, "BTreeSet {{"));
618 for (i, x) in self.iter().enumerate() {
619 if i != 0 { try!(write!(f, ", ")); }
620 try!(write!(f, "{:?}", *x));
627 #[stable(feature = "rust1", since = "1.0.0")]
628 impl<'a, T> Iterator for Iter<'a, T> {
631 fn next(&mut self) -> Option<&'a T> { self.iter.next() }
632 fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
634 #[stable(feature = "rust1", since = "1.0.0")]
635 impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
636 fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() }
638 #[stable(feature = "rust1", since = "1.0.0")]
639 impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
642 #[stable(feature = "rust1", since = "1.0.0")]
643 impl<T> Iterator for IntoIter<T> {
646 fn next(&mut self) -> Option<T> { self.iter.next() }
647 fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
649 #[stable(feature = "rust1", since = "1.0.0")]
650 impl<T> DoubleEndedIterator for IntoIter<T> {
651 fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
653 #[stable(feature = "rust1", since = "1.0.0")]
654 impl<T> ExactSizeIterator for IntoIter<T> {}
657 impl<'a, T> Iterator for Range<'a, T> {
660 fn next(&mut self) -> Option<&'a T> { self.iter.next() }
662 impl<'a, T> DoubleEndedIterator for Range<'a, T> {
663 fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() }
666 /// Compare `x` and `y`, but return `short` if x is None and `long` if y is None
667 fn cmp_opt<T: Ord>(x: Option<&T>, y: Option<&T>,
668 short: Ordering, long: Ordering) -> Ordering {
670 (None , _ ) => short,
672 (Some(x1), Some(y1)) => x1.cmp(y1),
676 #[stable(feature = "rust1", since = "1.0.0")]
677 impl<'a, T: Ord> Iterator for Difference<'a, T> {
680 fn next(&mut self) -> Option<&'a T> {
682 match cmp_opt(self.a.peek(), self.b.peek(), Less, Less) {
683 Less => return self.a.next(),
684 Equal => { self.a.next(); self.b.next(); }
685 Greater => { self.b.next(); }
691 #[stable(feature = "rust1", since = "1.0.0")]
692 impl<'a, T: Ord> Iterator for SymmetricDifference<'a, T> {
695 fn next(&mut self) -> Option<&'a T> {
697 match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) {
698 Less => return self.a.next(),
699 Equal => { self.a.next(); self.b.next(); }
700 Greater => return self.b.next(),
706 #[stable(feature = "rust1", since = "1.0.0")]
707 impl<'a, T: Ord> Iterator for Intersection<'a, T> {
710 fn next(&mut self) -> Option<&'a T> {
712 let o_cmp = match (self.a.peek(), self.b.peek()) {
715 (Some(a1), Some(b1)) => Some(a1.cmp(b1)),
719 Some(Less) => { self.a.next(); }
720 Some(Equal) => { self.b.next(); return self.a.next() }
721 Some(Greater) => { self.b.next(); }
727 #[stable(feature = "rust1", since = "1.0.0")]
728 impl<'a, T: Ord> Iterator for Union<'a, T> {
731 fn next(&mut self) -> Option<&'a T> {
733 match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) {
734 Less => return self.a.next(),
735 Equal => { self.b.next(); return self.a.next() }
736 Greater => return self.b.next(),
748 use std::hash::{self, SipHasher};
752 let mut m = BTreeSet::new();
757 assert!(m.clone() == m);
762 let mut x = BTreeSet::new();
763 let mut y = BTreeSet::new();
773 assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y));
776 struct Counter<'a, 'b> {
781 impl<'a, 'b, 'c> FnMut<(&'c i32,)> for Counter<'a, 'b> {
784 extern "rust-call" fn call_mut(&mut self, (&x,): (&'c i32,)) -> bool {
785 assert_eq!(x, self.expected[*self.i]);
791 fn check<F>(a: &[i32], b: &[i32], expected: &[i32], f: F) where
792 // FIXME Replace Counter with `Box<FnMut(_) -> _>`
793 F: FnOnce(&BTreeSet<i32>, &BTreeSet<i32>, Counter) -> bool,
795 let mut set_a = BTreeSet::new();
796 let mut set_b = BTreeSet::new();
798 for x in a { assert!(set_a.insert(*x)) }
799 for y in b { assert!(set_b.insert(*y)) }
802 f(&set_a, &set_b, Counter { i: &mut i, expected: expected });
803 assert_eq!(i, expected.len());
807 fn test_intersection() {
808 fn check_intersection(a: &[i32], b: &[i32], expected: &[i32]) {
809 check(a, b, expected, |x, y, f| x.intersection(y).all(f))
812 check_intersection(&[], &[], &[]);
813 check_intersection(&[1, 2, 3], &[], &[]);
814 check_intersection(&[], &[1, 2, 3], &[]);
815 check_intersection(&[2], &[1, 2, 3], &[2]);
816 check_intersection(&[1, 2, 3], &[2], &[2]);
817 check_intersection(&[11, 1, 3, 77, 103, 5, -5],
818 &[2, 11, 77, -9, -42, 5, 3],
823 fn test_difference() {
824 fn check_difference(a: &[i32], b: &[i32], expected: &[i32]) {
825 check(a, b, expected, |x, y, f| x.difference(y).all(f))
828 check_difference(&[], &[], &[]);
829 check_difference(&[1, 12], &[], &[1, 12]);
830 check_difference(&[], &[1, 2, 3, 9], &[]);
831 check_difference(&[1, 3, 5, 9, 11],
834 check_difference(&[-5, 11, 22, 33, 40, 42],
835 &[-12, -5, 14, 23, 34, 38, 39, 50],
836 &[11, 22, 33, 40, 42]);
840 fn test_symmetric_difference() {
841 fn check_symmetric_difference(a: &[i32], b: &[i32], expected: &[i32]) {
842 check(a, b, expected, |x, y, f| x.symmetric_difference(y).all(f))
845 check_symmetric_difference(&[], &[], &[]);
846 check_symmetric_difference(&[1, 2, 3], &[2], &[1, 3]);
847 check_symmetric_difference(&[2], &[1, 2, 3], &[1, 3]);
848 check_symmetric_difference(&[1, 3, 5, 9, 11],
850 &[-2, 1, 5, 11, 14, 22]);
855 fn check_union(a: &[i32], b: &[i32], expected: &[i32]) {
856 check(a, b, expected, |x, y, f| x.union(y).all(f))
859 check_union(&[], &[], &[]);
860 check_union(&[1, 2, 3], &[2], &[1, 2, 3]);
861 check_union(&[2], &[1, 2, 3], &[1, 2, 3]);
862 check_union(&[1, 3, 5, 9, 11, 16, 19, 24],
863 &[-2, 1, 5, 9, 13, 19],
864 &[-2, 1, 3, 5, 9, 11, 13, 16, 19, 24]);
869 let mut x = BTreeSet::new();
874 let mut y = BTreeSet::new();
880 let mut z = x.iter().zip(y.iter());
882 // FIXME: #5801: this needs a type hint to compile...
883 let result: Option<(&usize, & &'static str)> = z.next();
884 assert_eq!(result.unwrap(), (&5, &("bar")));
886 let result: Option<(&usize, & &'static str)> = z.next();
887 assert_eq!(result.unwrap(), (&11, &("foo")));
889 let result: Option<(&usize, & &'static str)> = z.next();
890 assert!(result.is_none());
894 fn test_from_iter() {
895 let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9];
897 let set: BTreeSet<_> = xs.iter().cloned().collect();
900 assert!(set.contains(x));
906 let mut set = BTreeSet::new();
907 let empty = BTreeSet::<i32>::new();
912 let set_str = format!("{:?}", set);
914 assert_eq!(set_str, "BTreeSet {1, 2}");
915 assert_eq!(format!("{:?}", empty), "BTreeSet {}");