]> git.proxmox.com Git - rustc.git/blame - src/libcollections/btree/set.rs
Imported Upstream version 1.3.0+dfsg1
[rustc.git] / src / libcollections / btree / set.rs
CommitLineData
1a4d82fc
JJ
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.
4//
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.
10
11// This is pretty much entirely stolen from TreeSet, since BTreeMap has an identical interface
12// to TreeMap
13
14use core::prelude::*;
15
1a4d82fc 16use core::cmp::Ordering::{self, Less, Greater, Equal};
85aaf69f 17use core::fmt::Debug;
1a4d82fc 18use core::fmt;
9346a6ac 19use core::iter::{Peekable, Map, FromIterator};
1a4d82fc
JJ
20use core::ops::{BitOr, BitAnd, BitXor, Sub};
21
85aaf69f 22use borrow::Borrow;
1a4d82fc 23use btree_map::{BTreeMap, Keys};
85aaf69f 24use Bound;
1a4d82fc
JJ
25
26// FIXME(conventions): implement bounded iterators
27
28/// A set based on a B-Tree.
29///
30/// See BTreeMap's documentation for a detailed discussion of this collection's performance
31/// benefits and drawbacks.
c34b1796
AL
32///
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.
1a4d82fc 36#[derive(Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
85aaf69f 37#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
38pub struct BTreeSet<T>{
39 map: BTreeMap<T, ()>,
40}
41
42/// An iterator over a BTreeSet's items.
85aaf69f 43#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
44pub struct Iter<'a, T: 'a> {
45 iter: Keys<'a, T, ()>
46}
47
48/// An owning iterator over a BTreeSet's items.
85aaf69f 49#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 50pub struct IntoIter<T> {
85aaf69f
SL
51 iter: Map<::btree_map::IntoIter<T, ()>, fn((T, ())) -> T>
52}
53
54/// An iterator over a sub-range of BTreeSet's items.
55pub struct Range<'a, T: 'a> {
56 iter: Map<::btree_map::Range<'a, T, ()>, fn((&'a T, &'a ())) -> &'a T>
1a4d82fc
JJ
57}
58
59/// A lazy iterator producing elements in the set difference (in-order).
85aaf69f 60#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 61pub struct Difference<'a, T:'a> {
85aaf69f
SL
62 a: Peekable<Iter<'a, T>>,
63 b: Peekable<Iter<'a, T>>,
1a4d82fc
JJ
64}
65
66/// A lazy iterator producing elements in the set symmetric difference (in-order).
85aaf69f 67#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 68pub struct SymmetricDifference<'a, T:'a> {
85aaf69f
SL
69 a: Peekable<Iter<'a, T>>,
70 b: Peekable<Iter<'a, T>>,
1a4d82fc
JJ
71}
72
73/// A lazy iterator producing elements in the set intersection (in-order).
85aaf69f 74#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 75pub struct Intersection<'a, T:'a> {
85aaf69f
SL
76 a: Peekable<Iter<'a, T>>,
77 b: Peekable<Iter<'a, T>>,
1a4d82fc
JJ
78}
79
80/// A lazy iterator producing elements in the set union (in-order).
85aaf69f 81#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 82pub struct Union<'a, T:'a> {
85aaf69f
SL
83 a: Peekable<Iter<'a, T>>,
84 b: Peekable<Iter<'a, T>>,
1a4d82fc
JJ
85}
86
87impl<T: Ord> BTreeSet<T> {
88 /// Makes a new BTreeSet with a reasonable choice of B.
89 ///
90 /// # Examples
91 ///
92 /// ```
93 /// use std::collections::BTreeSet;
94 ///
85aaf69f 95 /// let mut set: BTreeSet<i32> = BTreeSet::new();
1a4d82fc 96 /// ```
85aaf69f 97 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
98 pub fn new() -> BTreeSet<T> {
99 BTreeSet { map: BTreeMap::new() }
100 }
101
102 /// Makes a new BTreeSet with the given B.
103 ///
104 /// B cannot be less than 2.
62682a34 105 #[unstable(feature = "btree_b",
85aaf69f
SL
106 reason = "probably want this to be on the type, eventually")]
107 pub fn with_b(b: usize) -> BTreeSet<T> {
1a4d82fc
JJ
108 BTreeSet { map: BTreeMap::with_b(b) }
109 }
110}
111
112impl<T> BTreeSet<T> {
113 /// Gets an iterator over the BTreeSet's contents.
114 ///
115 /// # Examples
116 ///
117 /// ```
118 /// use std::collections::BTreeSet;
119 ///
85aaf69f 120 /// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
1a4d82fc
JJ
121 ///
122 /// for x in set.iter() {
123 /// println!("{}", x);
124 /// }
125 ///
62682a34 126 /// let v: Vec<_> = set.iter().cloned().collect();
c34b1796 127 /// assert_eq!(v, [1, 2, 3, 4]);
1a4d82fc 128 /// ```
85aaf69f 129 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
130 pub fn iter(&self) -> Iter<T> {
131 Iter { iter: self.map.keys() }
132 }
1a4d82fc
JJ
133}
134
85aaf69f
SL
135impl<T: Ord> BTreeSet<T> {
136 /// Constructs a double-ended iterator over a sub-range of elements in the set, starting
137 /// at min, and ending at max. If min is `Unbounded`, then it will be treated as "negative
138 /// infinity", and if max is `Unbounded`, then it will be treated as "positive infinity".
139 /// Thus range(Unbounded, Unbounded) will yield the whole collection.
140 ///
141 /// # Examples
142 ///
143 /// ```
c1a9b12d
SL
144 /// #![feature(btree_range, collections_bound)]
145 ///
85aaf69f
SL
146 /// use std::collections::BTreeSet;
147 /// use std::collections::Bound::{Included, Unbounded};
148 ///
149 /// let mut set = BTreeSet::new();
150 /// set.insert(3);
151 /// set.insert(5);
152 /// set.insert(8);
153 /// for &elem in set.range(Included(&4), Included(&8)) {
154 /// println!("{}", elem);
155 /// }
156 /// assert_eq!(Some(&5), set.range(Included(&4), Unbounded).next());
157 /// ```
62682a34 158 #[unstable(feature = "btree_range",
85aaf69f
SL
159 reason = "matches collection reform specification, waiting for dust to settle")]
160 pub fn range<'a>(&'a self, min: Bound<&T>, max: Bound<&T>) -> Range<'a, T> {
161 fn first<A, B>((a, _): (A, B)) -> A { a }
162 let first: fn((&'a T, &'a ())) -> &'a T = first; // coerce to fn pointer
163
164 Range { iter: self.map.range(min, max).map(first) }
165 }
166}
167
1a4d82fc
JJ
168impl<T: Ord> BTreeSet<T> {
169 /// Visits the values representing the difference, in ascending order.
170 ///
171 /// # Examples
172 ///
173 /// ```
174 /// use std::collections::BTreeSet;
175 ///
176 /// let mut a = BTreeSet::new();
85aaf69f
SL
177 /// a.insert(1);
178 /// a.insert(2);
1a4d82fc
JJ
179 ///
180 /// let mut b = BTreeSet::new();
85aaf69f
SL
181 /// b.insert(2);
182 /// b.insert(3);
1a4d82fc 183 ///
62682a34 184 /// let diff: Vec<_> = a.difference(&b).cloned().collect();
c34b1796 185 /// assert_eq!(diff, [1]);
1a4d82fc 186 /// ```
85aaf69f 187 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
188 pub fn difference<'a>(&'a self, other: &'a BTreeSet<T>) -> Difference<'a, T> {
189 Difference{a: self.iter().peekable(), b: other.iter().peekable()}
190 }
191
192 /// Visits the values representing the symmetric difference, in ascending order.
193 ///
194 /// # Examples
195 ///
196 /// ```
197 /// use std::collections::BTreeSet;
198 ///
199 /// let mut a = BTreeSet::new();
85aaf69f
SL
200 /// a.insert(1);
201 /// a.insert(2);
1a4d82fc
JJ
202 ///
203 /// let mut b = BTreeSet::new();
85aaf69f
SL
204 /// b.insert(2);
205 /// b.insert(3);
1a4d82fc 206 ///
62682a34 207 /// let sym_diff: Vec<_> = a.symmetric_difference(&b).cloned().collect();
c34b1796 208 /// assert_eq!(sym_diff, [1, 3]);
1a4d82fc 209 /// ```
85aaf69f 210 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
211 pub fn symmetric_difference<'a>(&'a self, other: &'a BTreeSet<T>)
212 -> SymmetricDifference<'a, T> {
213 SymmetricDifference{a: self.iter().peekable(), b: other.iter().peekable()}
214 }
215
216 /// Visits the values representing the intersection, in ascending order.
217 ///
218 /// # Examples
219 ///
220 /// ```
221 /// use std::collections::BTreeSet;
222 ///
223 /// let mut a = BTreeSet::new();
85aaf69f
SL
224 /// a.insert(1);
225 /// a.insert(2);
1a4d82fc
JJ
226 ///
227 /// let mut b = BTreeSet::new();
85aaf69f
SL
228 /// b.insert(2);
229 /// b.insert(3);
1a4d82fc 230 ///
62682a34 231 /// let intersection: Vec<_> = a.intersection(&b).cloned().collect();
c34b1796 232 /// assert_eq!(intersection, [2]);
1a4d82fc 233 /// ```
85aaf69f 234 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
235 pub fn intersection<'a>(&'a self, other: &'a BTreeSet<T>)
236 -> Intersection<'a, T> {
237 Intersection{a: self.iter().peekable(), b: other.iter().peekable()}
238 }
239
240 /// Visits the values representing the union, in ascending order.
241 ///
242 /// # Examples
243 ///
244 /// ```
245 /// use std::collections::BTreeSet;
246 ///
247 /// let mut a = BTreeSet::new();
85aaf69f 248 /// a.insert(1);
1a4d82fc
JJ
249 ///
250 /// let mut b = BTreeSet::new();
85aaf69f 251 /// b.insert(2);
1a4d82fc 252 ///
62682a34 253 /// let union: Vec<_> = a.union(&b).cloned().collect();
c34b1796 254 /// assert_eq!(union, [1, 2]);
1a4d82fc 255 /// ```
85aaf69f 256 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
257 pub fn union<'a>(&'a self, other: &'a BTreeSet<T>) -> Union<'a, T> {
258 Union{a: self.iter().peekable(), b: other.iter().peekable()}
259 }
260
9346a6ac 261 /// Returns the number of elements in the set.
1a4d82fc
JJ
262 ///
263 /// # Examples
264 ///
265 /// ```
266 /// use std::collections::BTreeSet;
267 ///
268 /// let mut v = BTreeSet::new();
269 /// assert_eq!(v.len(), 0);
85aaf69f 270 /// v.insert(1);
1a4d82fc
JJ
271 /// assert_eq!(v.len(), 1);
272 /// ```
85aaf69f
SL
273 #[stable(feature = "rust1", since = "1.0.0")]
274 pub fn len(&self) -> usize { self.map.len() }
1a4d82fc 275
9346a6ac 276 /// Returns true if the set contains no elements.
1a4d82fc
JJ
277 ///
278 /// # Examples
279 ///
280 /// ```
281 /// use std::collections::BTreeSet;
282 ///
283 /// let mut v = BTreeSet::new();
284 /// assert!(v.is_empty());
85aaf69f 285 /// v.insert(1);
1a4d82fc
JJ
286 /// assert!(!v.is_empty());
287 /// ```
85aaf69f 288 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
289 pub fn is_empty(&self) -> bool { self.len() == 0 }
290
291 /// Clears the set, removing all values.
292 ///
293 /// # Examples
294 ///
295 /// ```
296 /// use std::collections::BTreeSet;
297 ///
298 /// let mut v = BTreeSet::new();
85aaf69f 299 /// v.insert(1);
1a4d82fc
JJ
300 /// v.clear();
301 /// assert!(v.is_empty());
302 /// ```
85aaf69f 303 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
304 pub fn clear(&mut self) {
305 self.map.clear()
306 }
307
308 /// Returns `true` if the set contains a value.
309 ///
310 /// The value may be any borrowed form of the set's value type,
311 /// but the ordering on the borrowed form *must* match the
312 /// ordering on the value type.
313 ///
314 /// # Examples
315 ///
316 /// ```
317 /// use std::collections::BTreeSet;
318 ///
85aaf69f 319 /// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
1a4d82fc
JJ
320 /// assert_eq!(set.contains(&1), true);
321 /// assert_eq!(set.contains(&4), false);
322 /// ```
85aaf69f
SL
323 #[stable(feature = "rust1", since = "1.0.0")]
324 pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool where T: Borrow<Q>, Q: Ord {
1a4d82fc
JJ
325 self.map.contains_key(value)
326 }
327
328 /// Returns `true` if the set has no elements in common with `other`.
329 /// This is equivalent to checking for an empty intersection.
330 ///
331 /// # Examples
332 ///
333 /// ```
334 /// use std::collections::BTreeSet;
335 ///
85aaf69f
SL
336 /// let a: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
337 /// let mut b = BTreeSet::new();
1a4d82fc
JJ
338 ///
339 /// assert_eq!(a.is_disjoint(&b), true);
340 /// b.insert(4);
341 /// assert_eq!(a.is_disjoint(&b), true);
342 /// b.insert(1);
343 /// assert_eq!(a.is_disjoint(&b), false);
344 /// ```
85aaf69f 345 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
346 pub fn is_disjoint(&self, other: &BTreeSet<T>) -> bool {
347 self.intersection(other).next().is_none()
348 }
349
350 /// Returns `true` if the set is a subset of another.
351 ///
352 /// # Examples
353 ///
354 /// ```
355 /// use std::collections::BTreeSet;
356 ///
85aaf69f
SL
357 /// let sup: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
358 /// let mut set = BTreeSet::new();
1a4d82fc
JJ
359 ///
360 /// assert_eq!(set.is_subset(&sup), true);
361 /// set.insert(2);
362 /// assert_eq!(set.is_subset(&sup), true);
363 /// set.insert(4);
364 /// assert_eq!(set.is_subset(&sup), false);
365 /// ```
85aaf69f 366 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
367 pub fn is_subset(&self, other: &BTreeSet<T>) -> bool {
368 // Stolen from TreeMap
369 let mut x = self.iter();
370 let mut y = other.iter();
371 let mut a = x.next();
372 let mut b = y.next();
373 while a.is_some() {
374 if b.is_none() {
375 return false;
376 }
377
378 let a1 = a.unwrap();
379 let b1 = b.unwrap();
380
381 match b1.cmp(a1) {
382 Less => (),
383 Greater => return false,
384 Equal => a = x.next(),
385 }
386
387 b = y.next();
388 }
389 true
390 }
391
392 /// Returns `true` if the set is a superset of another.
393 ///
394 /// # Examples
395 ///
396 /// ```
397 /// use std::collections::BTreeSet;
398 ///
85aaf69f
SL
399 /// let sub: BTreeSet<_> = [1, 2].iter().cloned().collect();
400 /// let mut set = BTreeSet::new();
1a4d82fc
JJ
401 ///
402 /// assert_eq!(set.is_superset(&sub), false);
403 ///
404 /// set.insert(0);
405 /// set.insert(1);
406 /// assert_eq!(set.is_superset(&sub), false);
407 ///
408 /// set.insert(2);
409 /// assert_eq!(set.is_superset(&sub), true);
410 /// ```
85aaf69f 411 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
412 pub fn is_superset(&self, other: &BTreeSet<T>) -> bool {
413 other.is_subset(self)
414 }
415
416 /// Adds a value to the set. Returns `true` if the value was not already
417 /// present in the set.
418 ///
419 /// # Examples
420 ///
421 /// ```
422 /// use std::collections::BTreeSet;
423 ///
424 /// let mut set = BTreeSet::new();
425 ///
85aaf69f
SL
426 /// assert_eq!(set.insert(2), true);
427 /// assert_eq!(set.insert(2), false);
1a4d82fc
JJ
428 /// assert_eq!(set.len(), 1);
429 /// ```
85aaf69f 430 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
431 pub fn insert(&mut self, value: T) -> bool {
432 self.map.insert(value, ()).is_none()
433 }
434
435 /// Removes a value from the set. Returns `true` if the value was
436 /// present in the set.
437 ///
438 /// The value may be any borrowed form of the set's value type,
439 /// but the ordering on the borrowed form *must* match the
440 /// ordering on the value type.
441 ///
442 /// # Examples
443 ///
444 /// ```
445 /// use std::collections::BTreeSet;
446 ///
447 /// let mut set = BTreeSet::new();
448 ///
85aaf69f 449 /// set.insert(2);
1a4d82fc
JJ
450 /// assert_eq!(set.remove(&2), true);
451 /// assert_eq!(set.remove(&2), false);
452 /// ```
85aaf69f
SL
453 #[stable(feature = "rust1", since = "1.0.0")]
454 pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool where T: Borrow<Q>, Q: Ord {
1a4d82fc
JJ
455 self.map.remove(value).is_some()
456 }
457}
458
85aaf69f 459#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 460impl<T: Ord> FromIterator<T> for BTreeSet<T> {
85aaf69f 461 fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> BTreeSet<T> {
1a4d82fc
JJ
462 let mut set = BTreeSet::new();
463 set.extend(iter);
464 set
465 }
466}
467
85aaf69f
SL
468#[stable(feature = "rust1", since = "1.0.0")]
469impl<T> IntoIterator for BTreeSet<T> {
470 type Item = T;
471 type IntoIter = IntoIter<T>;
472
9346a6ac
AL
473 /// Gets an iterator for moving out the BtreeSet's contents.
474 ///
475 /// # Examples
476 ///
477 /// ```
9346a6ac
AL
478 /// use std::collections::BTreeSet;
479 ///
480 /// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
481 ///
62682a34 482 /// let v: Vec<_> = set.into_iter().collect();
9346a6ac
AL
483 /// assert_eq!(v, [1, 2, 3, 4]);
484 /// ```
85aaf69f 485 fn into_iter(self) -> IntoIter<T> {
9346a6ac
AL
486 fn first<A, B>((a, _): (A, B)) -> A { a }
487 let first: fn((T, ())) -> T = first; // coerce to fn pointer
488
489 IntoIter { iter: self.map.into_iter().map(first) }
85aaf69f
SL
490 }
491}
492
493#[stable(feature = "rust1", since = "1.0.0")]
494impl<'a, T> IntoIterator for &'a BTreeSet<T> {
495 type Item = &'a T;
496 type IntoIter = Iter<'a, T>;
497
498 fn into_iter(self) -> Iter<'a, T> {
499 self.iter()
500 }
501}
502
503#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
504impl<T: Ord> Extend<T> for BTreeSet<T> {
505 #[inline]
85aaf69f 506 fn extend<Iter: IntoIterator<Item=T>>(&mut self, iter: Iter) {
1a4d82fc
JJ
507 for elem in iter {
508 self.insert(elem);
509 }
510 }
511}
512
62682a34
SL
513#[stable(feature = "extend_ref", since = "1.2.0")]
514impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BTreeSet<T> {
515 fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iter: I) {
516 self.extend(iter.into_iter().cloned());
517 }
518}
519
85aaf69f 520#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 521impl<T: Ord> Default for BTreeSet<T> {
85aaf69f 522 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
523 fn default() -> BTreeSet<T> {
524 BTreeSet::new()
525 }
526}
527
85aaf69f 528#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
529impl<'a, 'b, T: Ord + Clone> Sub<&'b BTreeSet<T>> for &'a BTreeSet<T> {
530 type Output = BTreeSet<T>;
531
532 /// Returns the difference of `self` and `rhs` as a new `BTreeSet<T>`.
533 ///
534 /// # Examples
535 ///
536 /// ```
537 /// use std::collections::BTreeSet;
538 ///
85aaf69f
SL
539 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
540 /// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect();
1a4d82fc 541 ///
85aaf69f
SL
542 /// let result = &a - &b;
543 /// let result_vec: Vec<_> = result.into_iter().collect();
c34b1796 544 /// assert_eq!(result_vec, [1, 2]);
1a4d82fc
JJ
545 /// ```
546 fn sub(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
547 self.difference(rhs).cloned().collect()
548 }
549}
550
85aaf69f 551#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
552impl<'a, 'b, T: Ord + Clone> BitXor<&'b BTreeSet<T>> for &'a BTreeSet<T> {
553 type Output = BTreeSet<T>;
554
555 /// Returns the symmetric difference of `self` and `rhs` as a new `BTreeSet<T>`.
556 ///
557 /// # Examples
558 ///
559 /// ```
560 /// use std::collections::BTreeSet;
561 ///
85aaf69f
SL
562 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
563 /// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();
1a4d82fc 564 ///
85aaf69f
SL
565 /// let result = &a ^ &b;
566 /// let result_vec: Vec<_> = result.into_iter().collect();
c34b1796 567 /// assert_eq!(result_vec, [1, 4]);
1a4d82fc
JJ
568 /// ```
569 fn bitxor(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
570 self.symmetric_difference(rhs).cloned().collect()
571 }
572}
573
85aaf69f 574#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
575impl<'a, 'b, T: Ord + Clone> BitAnd<&'b BTreeSet<T>> for &'a BTreeSet<T> {
576 type Output = BTreeSet<T>;
577
578 /// Returns the intersection of `self` and `rhs` as a new `BTreeSet<T>`.
579 ///
580 /// # Examples
581 ///
582 /// ```
583 /// use std::collections::BTreeSet;
584 ///
85aaf69f
SL
585 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
586 /// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();
1a4d82fc 587 ///
85aaf69f
SL
588 /// let result = &a & &b;
589 /// let result_vec: Vec<_> = result.into_iter().collect();
c34b1796 590 /// assert_eq!(result_vec, [2, 3]);
1a4d82fc
JJ
591 /// ```
592 fn bitand(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
593 self.intersection(rhs).cloned().collect()
594 }
595}
596
85aaf69f 597#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
598impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>> for &'a BTreeSet<T> {
599 type Output = BTreeSet<T>;
600
601 /// Returns the union of `self` and `rhs` as a new `BTreeSet<T>`.
602 ///
603 /// # Examples
604 ///
605 /// ```
606 /// use std::collections::BTreeSet;
607 ///
85aaf69f
SL
608 /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
609 /// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect();
1a4d82fc 610 ///
85aaf69f
SL
611 /// let result = &a | &b;
612 /// let result_vec: Vec<_> = result.into_iter().collect();
c34b1796 613 /// assert_eq!(result_vec, [1, 2, 3, 4, 5]);
1a4d82fc
JJ
614 /// ```
615 fn bitor(self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
616 self.union(rhs).cloned().collect()
617 }
618}
619
85aaf69f
SL
620#[stable(feature = "rust1", since = "1.0.0")]
621impl<T: Debug> Debug for BTreeSet<T> {
1a4d82fc 622 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62682a34 623 f.debug_set().entries(self.iter()).finish()
1a4d82fc
JJ
624 }
625}
626
c34b1796
AL
627impl<'a, T> Clone for Iter<'a, T> {
628 fn clone(&self) -> Iter<'a, T> { Iter { iter: self.iter.clone() } }
629}
85aaf69f 630#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
631impl<'a, T> Iterator for Iter<'a, T> {
632 type Item = &'a T;
633
634 fn next(&mut self) -> Option<&'a T> { self.iter.next() }
85aaf69f 635 fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
1a4d82fc 636}
85aaf69f 637#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
638impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
639 fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() }
640}
85aaf69f 641#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
642impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
643
644
85aaf69f 645#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
646impl<T> Iterator for IntoIter<T> {
647 type Item = T;
648
649 fn next(&mut self) -> Option<T> { self.iter.next() }
85aaf69f 650 fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
1a4d82fc 651}
85aaf69f 652#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
653impl<T> DoubleEndedIterator for IntoIter<T> {
654 fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
655}
85aaf69f 656#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
657impl<T> ExactSizeIterator for IntoIter<T> {}
658
85aaf69f 659
c34b1796
AL
660impl<'a, T> Clone for Range<'a, T> {
661 fn clone(&self) -> Range<'a, T> { Range { iter: self.iter.clone() } }
662}
85aaf69f
SL
663impl<'a, T> Iterator for Range<'a, T> {
664 type Item = &'a T;
665
666 fn next(&mut self) -> Option<&'a T> { self.iter.next() }
667}
668impl<'a, T> DoubleEndedIterator for Range<'a, T> {
669 fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() }
670}
671
1a4d82fc
JJ
672/// Compare `x` and `y`, but return `short` if x is None and `long` if y is None
673fn cmp_opt<T: Ord>(x: Option<&T>, y: Option<&T>,
674 short: Ordering, long: Ordering) -> Ordering {
675 match (x, y) {
676 (None , _ ) => short,
677 (_ , None ) => long,
678 (Some(x1), Some(y1)) => x1.cmp(y1),
679 }
680}
681
c34b1796
AL
682impl<'a, T> Clone for Difference<'a, T> {
683 fn clone(&self) -> Difference<'a, T> {
684 Difference { a: self.a.clone(), b: self.b.clone() }
685 }
686}
85aaf69f 687#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
688impl<'a, T: Ord> Iterator for Difference<'a, T> {
689 type Item = &'a T;
690
691 fn next(&mut self) -> Option<&'a T> {
692 loop {
693 match cmp_opt(self.a.peek(), self.b.peek(), Less, Less) {
694 Less => return self.a.next(),
695 Equal => { self.a.next(); self.b.next(); }
696 Greater => { self.b.next(); }
697 }
698 }
699 }
700}
701
c34b1796
AL
702impl<'a, T> Clone for SymmetricDifference<'a, T> {
703 fn clone(&self) -> SymmetricDifference<'a, T> {
704 SymmetricDifference { a: self.a.clone(), b: self.b.clone() }
705 }
706}
85aaf69f 707#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
708impl<'a, T: Ord> Iterator for SymmetricDifference<'a, T> {
709 type Item = &'a T;
710
711 fn next(&mut self) -> Option<&'a T> {
712 loop {
713 match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) {
714 Less => return self.a.next(),
715 Equal => { self.a.next(); self.b.next(); }
716 Greater => return self.b.next(),
717 }
718 }
719 }
720}
721
c34b1796
AL
722impl<'a, T> Clone for Intersection<'a, T> {
723 fn clone(&self) -> Intersection<'a, T> {
724 Intersection { a: self.a.clone(), b: self.b.clone() }
725 }
726}
85aaf69f 727#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
728impl<'a, T: Ord> Iterator for Intersection<'a, T> {
729 type Item = &'a T;
730
731 fn next(&mut self) -> Option<&'a T> {
732 loop {
733 let o_cmp = match (self.a.peek(), self.b.peek()) {
734 (None , _ ) => None,
735 (_ , None ) => None,
736 (Some(a1), Some(b1)) => Some(a1.cmp(b1)),
737 };
738 match o_cmp {
739 None => return None,
740 Some(Less) => { self.a.next(); }
741 Some(Equal) => { self.b.next(); return self.a.next() }
742 Some(Greater) => { self.b.next(); }
743 }
744 }
745 }
746}
747
c34b1796
AL
748impl<'a, T> Clone for Union<'a, T> {
749 fn clone(&self) -> Union<'a, T> {
750 Union { a: self.a.clone(), b: self.b.clone() }
751 }
752}
85aaf69f 753#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
754impl<'a, T: Ord> Iterator for Union<'a, T> {
755 type Item = &'a T;
756
757 fn next(&mut self) -> Option<&'a T> {
758 loop {
759 match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) {
760 Less => return self.a.next(),
761 Equal => { self.b.next(); return self.a.next() }
762 Greater => return self.b.next(),
763 }
764 }
765 }
766}