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