]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // This is pretty much entirely stolen from TreeSet, since BTreeMap has an identical interface |
2 | // to TreeMap | |
3 | ||
9fa01778 | 4 | use core::borrow::Borrow; |
e74abb32 | 5 | use core::cmp::Ordering::{Less, Greater, Equal}; |
e1599b0c | 6 | use core::cmp::{max, min}; |
9fa01778 | 7 | use core::fmt::{self, Debug}; |
9e0c209e | 8 | use core::iter::{Peekable, FromIterator, FusedIterator}; |
0531ce1d | 9 | use core::ops::{BitOr, BitAnd, BitXor, Sub, RangeBounds}; |
1a4d82fc | 10 | |
9fa01778 | 11 | use crate::collections::btree_map::{self, BTreeMap, Keys}; |
e9174d1e | 12 | use super::Recover; |
1a4d82fc JJ |
13 | |
14 | // FIXME(conventions): implement bounded iterators | |
15 | ||
16 | /// A set based on a B-Tree. | |
17 | /// | |
9cc50fc6 | 18 | /// See [`BTreeMap`]'s documentation for a detailed discussion of this collection's performance |
1a4d82fc | 19 | /// benefits and drawbacks. |
c34b1796 AL |
20 | /// |
21 | /// It is a logic error for an item to be modified in such a way that the item's ordering relative | |
9cc50fc6 SL |
22 | /// to any other item, as determined by the [`Ord`] trait, changes while it is in the set. This is |
23 | /// normally only possible through [`Cell`], [`RefCell`], global state, I/O, or unsafe code. | |
24 | /// | |
54a0048b SL |
25 | /// [`BTreeMap`]: struct.BTreeMap.html |
26 | /// [`Ord`]: ../../std/cmp/trait.Ord.html | |
9cc50fc6 SL |
27 | /// [`Cell`]: ../../std/cell/struct.Cell.html |
28 | /// [`RefCell`]: ../../std/cell/struct.RefCell.html | |
54a0048b SL |
29 | /// |
30 | /// # Examples | |
31 | /// | |
32 | /// ``` | |
33 | /// use std::collections::BTreeSet; | |
34 | /// | |
35 | /// // Type inference lets us omit an explicit type signature (which | |
36 | /// // would be `BTreeSet<&str>` in this example). | |
37 | /// let mut books = BTreeSet::new(); | |
38 | /// | |
39 | /// // Add some books. | |
40 | /// books.insert("A Dance With Dragons"); | |
41 | /// books.insert("To Kill a Mockingbird"); | |
42 | /// books.insert("The Odyssey"); | |
43 | /// books.insert("The Great Gatsby"); | |
44 | /// | |
45 | /// // Check for a specific one. | |
46 | /// if !books.contains("The Winds of Winter") { | |
47 | /// println!("We have {} books, but The Winds of Winter ain't one.", | |
48 | /// books.len()); | |
49 | /// } | |
50 | /// | |
51 | /// // Remove a book. | |
52 | /// books.remove("The Odyssey"); | |
53 | /// | |
54 | /// // Iterate over everything. | |
55 | /// for book in &books { | |
56 | /// println!("{}", book); | |
57 | /// } | |
58 | /// ``` | |
1a4d82fc | 59 | #[derive(Clone, Hash, PartialEq, Eq, Ord, PartialOrd)] |
85aaf69f | 60 | #[stable(feature = "rust1", since = "1.0.0")] |
92a42be0 | 61 | pub struct BTreeSet<T> { |
1a4d82fc JJ |
62 | map: BTreeMap<T, ()>, |
63 | } | |
64 | ||
cc61c64b | 65 | /// An iterator over the items of a `BTreeSet`. |
32a655c1 | 66 | /// |
cc61c64b XL |
67 | /// This `struct` is created by the [`iter`] method on [`BTreeSet`]. |
68 | /// See its documentation for more. | |
32a655c1 SL |
69 | /// |
70 | /// [`BTreeSet`]: struct.BTreeSet.html | |
71 | /// [`iter`]: struct.BTreeSet.html#method.iter | |
85aaf69f | 72 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 73 | pub struct Iter<'a, T: 'a> { |
92a42be0 | 74 | iter: Keys<'a, T, ()>, |
1a4d82fc JJ |
75 | } |
76 | ||
8bb4bdeb | 77 | #[stable(feature = "collection_debug", since = "1.17.0")] |
9fa01778 XL |
78 | impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> { |
79 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
8bb4bdeb XL |
80 | f.debug_tuple("Iter") |
81 | .field(&self.iter.clone()) | |
82 | .finish() | |
83 | } | |
84 | } | |
85 | ||
cc61c64b | 86 | /// An owning iterator over the items of a `BTreeSet`. |
32a655c1 | 87 | /// |
cc61c64b XL |
88 | /// This `struct` is created by the [`into_iter`] method on [`BTreeSet`][`BTreeSet`] |
89 | /// (provided by the `IntoIterator` trait). See its documentation for more. | |
32a655c1 SL |
90 | /// |
91 | /// [`BTreeSet`]: struct.BTreeSet.html | |
cc61c64b | 92 | /// [`into_iter`]: struct.BTreeSet.html#method.into_iter |
85aaf69f | 93 | #[stable(feature = "rust1", since = "1.0.0")] |
8bb4bdeb | 94 | #[derive(Debug)] |
1a4d82fc | 95 | pub struct IntoIter<T> { |
8faf50e0 | 96 | iter: btree_map::IntoIter<T, ()>, |
85aaf69f SL |
97 | } |
98 | ||
cc61c64b | 99 | /// An iterator over a sub-range of items in a `BTreeSet`. |
32a655c1 | 100 | /// |
cc61c64b XL |
101 | /// This `struct` is created by the [`range`] method on [`BTreeSet`]. |
102 | /// See its documentation for more. | |
32a655c1 SL |
103 | /// |
104 | /// [`BTreeSet`]: struct.BTreeSet.html | |
105 | /// [`range`]: struct.BTreeSet.html#method.range | |
8bb4bdeb XL |
106 | #[derive(Debug)] |
107 | #[stable(feature = "btree_range", since = "1.17.0")] | |
85aaf69f | 108 | pub struct Range<'a, T: 'a> { |
8faf50e0 | 109 | iter: btree_map::Range<'a, T, ()>, |
1a4d82fc JJ |
110 | } |
111 | ||
e74abb32 XL |
112 | /// Core of SymmetricDifference and Union. |
113 | /// More efficient than btree.map.MergeIter, | |
114 | /// and crucially for SymmetricDifference, nexts() reports on both sides. | |
115 | #[derive(Clone)] | |
116 | struct MergeIterInner<I> | |
117 | where I: Iterator, | |
118 | I::Item: Copy, | |
119 | { | |
120 | a: I, | |
121 | b: I, | |
122 | peeked: Option<MergeIterPeeked<I>>, | |
123 | } | |
124 | ||
125 | #[derive(Copy, Clone, Debug)] | |
126 | enum MergeIterPeeked<I: Iterator> { | |
127 | A(I::Item), | |
128 | B(I::Item), | |
129 | } | |
130 | ||
131 | impl<I> MergeIterInner<I> | |
132 | where I: ExactSizeIterator + FusedIterator, | |
133 | I::Item: Copy + Ord, | |
134 | { | |
135 | fn new(a: I, b: I) -> Self { | |
136 | MergeIterInner { a, b, peeked: None } | |
137 | } | |
138 | ||
139 | fn nexts(&mut self) -> (Option<I::Item>, Option<I::Item>) { | |
140 | let mut a_next = match self.peeked { | |
141 | Some(MergeIterPeeked::A(next)) => Some(next), | |
142 | _ => self.a.next(), | |
143 | }; | |
144 | let mut b_next = match self.peeked { | |
145 | Some(MergeIterPeeked::B(next)) => Some(next), | |
146 | _ => self.b.next(), | |
147 | }; | |
148 | let ord = match (a_next, b_next) { | |
149 | (None, None) => Equal, | |
150 | (_, None) => Less, | |
151 | (None, _) => Greater, | |
152 | (Some(a1), Some(b1)) => a1.cmp(&b1), | |
153 | }; | |
154 | self.peeked = match ord { | |
155 | Less => b_next.take().map(MergeIterPeeked::B), | |
156 | Equal => None, | |
157 | Greater => a_next.take().map(MergeIterPeeked::A), | |
158 | }; | |
159 | (a_next, b_next) | |
160 | } | |
161 | ||
162 | fn lens(&self) -> (usize, usize) { | |
163 | match self.peeked { | |
164 | Some(MergeIterPeeked::A(_)) => (1 + self.a.len(), self.b.len()), | |
165 | Some(MergeIterPeeked::B(_)) => (self.a.len(), 1 + self.b.len()), | |
166 | _ => (self.a.len(), self.b.len()), | |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | impl<I> Debug for MergeIterInner<I> | |
172 | where I: Iterator + Debug, | |
173 | I::Item: Copy + Debug, | |
174 | { | |
175 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
176 | f.debug_tuple("MergeIterInner") | |
177 | .field(&self.a) | |
178 | .field(&self.b) | |
179 | .finish() | |
180 | } | |
181 | } | |
182 | ||
cc61c64b | 183 | /// A lazy iterator producing elements in the difference of `BTreeSet`s. |
32a655c1 | 184 | /// |
cc61c64b XL |
185 | /// This `struct` is created by the [`difference`] method on [`BTreeSet`]. |
186 | /// See its documentation for more. | |
32a655c1 SL |
187 | /// |
188 | /// [`BTreeSet`]: struct.BTreeSet.html | |
189 | /// [`difference`]: struct.BTreeSet.html#method.difference | |
85aaf69f | 190 | #[stable(feature = "rust1", since = "1.0.0")] |
92a42be0 | 191 | pub struct Difference<'a, T: 'a> { |
532ac7d7 XL |
192 | inner: DifferenceInner<'a, T>, |
193 | } | |
e74abb32 | 194 | #[derive(Debug)] |
532ac7d7 XL |
195 | enum DifferenceInner<'a, T: 'a> { |
196 | Stitch { | |
60c5eb7d | 197 | // iterate all of `self` and some of `other`, spotting matches along the way |
532ac7d7 XL |
198 | self_iter: Iter<'a, T>, |
199 | other_iter: Peekable<Iter<'a, T>>, | |
200 | }, | |
201 | Search { | |
60c5eb7d | 202 | // iterate `self`, look up in `other` |
532ac7d7 XL |
203 | self_iter: Iter<'a, T>, |
204 | other_set: &'a BTreeSet<T>, | |
205 | }, | |
60c5eb7d | 206 | Iterate(Iter<'a, T>), // simply produce all values in `self` |
1a4d82fc JJ |
207 | } |
208 | ||
8bb4bdeb | 209 | #[stable(feature = "collection_debug", since = "1.17.0")] |
9fa01778 XL |
210 | impl<T: fmt::Debug> fmt::Debug for Difference<'_, T> { |
211 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
e74abb32 | 212 | f.debug_tuple("Difference").field(&self.inner).finish() |
8bb4bdeb XL |
213 | } |
214 | } | |
215 | ||
cc61c64b | 216 | /// A lazy iterator producing elements in the symmetric difference of `BTreeSet`s. |
32a655c1 | 217 | /// |
cc61c64b XL |
218 | /// This `struct` is created by the [`symmetric_difference`] method on |
219 | /// [`BTreeSet`]. See its documentation for more. | |
32a655c1 SL |
220 | /// |
221 | /// [`BTreeSet`]: struct.BTreeSet.html | |
222 | /// [`symmetric_difference`]: struct.BTreeSet.html#method.symmetric_difference | |
85aaf69f | 223 | #[stable(feature = "rust1", since = "1.0.0")] |
e74abb32 | 224 | pub struct SymmetricDifference<'a, T: 'a>(MergeIterInner<Iter<'a, T>>); |
1a4d82fc | 225 | |
8bb4bdeb | 226 | #[stable(feature = "collection_debug", since = "1.17.0")] |
9fa01778 XL |
227 | impl<T: fmt::Debug> fmt::Debug for SymmetricDifference<'_, T> { |
228 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
e74abb32 | 229 | f.debug_tuple("SymmetricDifference").field(&self.0).finish() |
8bb4bdeb XL |
230 | } |
231 | } | |
232 | ||
cc61c64b | 233 | /// A lazy iterator producing elements in the intersection of `BTreeSet`s. |
32a655c1 | 234 | /// |
cc61c64b XL |
235 | /// This `struct` is created by the [`intersection`] method on [`BTreeSet`]. |
236 | /// See its documentation for more. | |
32a655c1 SL |
237 | /// |
238 | /// [`BTreeSet`]: struct.BTreeSet.html | |
239 | /// [`intersection`]: struct.BTreeSet.html#method.intersection | |
85aaf69f | 240 | #[stable(feature = "rust1", since = "1.0.0")] |
92a42be0 | 241 | pub struct Intersection<'a, T: 'a> { |
532ac7d7 XL |
242 | inner: IntersectionInner<'a, T>, |
243 | } | |
e74abb32 | 244 | #[derive(Debug)] |
532ac7d7 XL |
245 | enum IntersectionInner<'a, T: 'a> { |
246 | Stitch { | |
e74abb32 | 247 | // iterate similarly sized sets jointly, spotting matches along the way |
e1599b0c XL |
248 | a: Iter<'a, T>, |
249 | b: Iter<'a, T>, | |
532ac7d7 XL |
250 | }, |
251 | Search { | |
e74abb32 | 252 | // iterate a small set, look up in the large set |
532ac7d7 XL |
253 | small_iter: Iter<'a, T>, |
254 | large_set: &'a BTreeSet<T>, | |
255 | }, | |
e74abb32 | 256 | Answer(Option<&'a T>), // return a specific value or emptiness |
1a4d82fc JJ |
257 | } |
258 | ||
8bb4bdeb | 259 | #[stable(feature = "collection_debug", since = "1.17.0")] |
9fa01778 XL |
260 | impl<T: fmt::Debug> fmt::Debug for Intersection<'_, T> { |
261 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
e74abb32 | 262 | f.debug_tuple("Intersection").field(&self.inner).finish() |
8bb4bdeb XL |
263 | } |
264 | } | |
265 | ||
cc61c64b | 266 | /// A lazy iterator producing elements in the union of `BTreeSet`s. |
32a655c1 | 267 | /// |
cc61c64b XL |
268 | /// This `struct` is created by the [`union`] method on [`BTreeSet`]. |
269 | /// See its documentation for more. | |
32a655c1 SL |
270 | /// |
271 | /// [`BTreeSet`]: struct.BTreeSet.html | |
272 | /// [`union`]: struct.BTreeSet.html#method.union | |
85aaf69f | 273 | #[stable(feature = "rust1", since = "1.0.0")] |
e74abb32 | 274 | pub struct Union<'a, T: 'a>(MergeIterInner<Iter<'a, T>>); |
1a4d82fc | 275 | |
8bb4bdeb | 276 | #[stable(feature = "collection_debug", since = "1.17.0")] |
9fa01778 XL |
277 | impl<T: fmt::Debug> fmt::Debug for Union<'_, T> { |
278 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
e74abb32 | 279 | f.debug_tuple("Union").field(&self.0).finish() |
8bb4bdeb XL |
280 | } |
281 | } | |
282 | ||
532ac7d7 XL |
283 | // This constant is used by functions that compare two sets. |
284 | // It estimates the relative size at which searching performs better | |
285 | // than iterating, based on the benchmarks in | |
286 | // https://github.com/ssomers/rust_bench_btreeset_intersection; | |
287 | // It's used to divide rather than multiply sizes, to rule out overflow, | |
288 | // and it's a power of two to make that division cheap. | |
289 | const ITER_PERFORMANCE_TIPPING_SIZE_DIFF: usize = 16; | |
290 | ||
1a4d82fc | 291 | impl<T: Ord> BTreeSet<T> { |
32a655c1 | 292 | /// Makes a new `BTreeSet` with a reasonable choice of B. |
1a4d82fc JJ |
293 | /// |
294 | /// # Examples | |
295 | /// | |
296 | /// ``` | |
92a42be0 | 297 | /// # #![allow(unused_mut)] |
1a4d82fc JJ |
298 | /// use std::collections::BTreeSet; |
299 | /// | |
85aaf69f | 300 | /// let mut set: BTreeSet<i32> = BTreeSet::new(); |
1a4d82fc | 301 | /// ``` |
85aaf69f | 302 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
303 | pub fn new() -> BTreeSet<T> { |
304 | BTreeSet { map: BTreeMap::new() } | |
305 | } | |
1a4d82fc | 306 | |
32a655c1 SL |
307 | /// Constructs a double-ended iterator over a sub-range of elements in the set. |
308 | /// The simplest way is to use the range syntax `min..max`, thus `range(min..max)` will | |
309 | /// yield elements from min (inclusive) to max (exclusive). | |
310 | /// The range may also be entered as `(Bound<T>, Bound<T>)`, so for example | |
311 | /// `range((Excluded(4), Included(10)))` will yield a left-exclusive, right-inclusive | |
312 | /// range from 4 to 10. | |
85aaf69f SL |
313 | /// |
314 | /// # Examples | |
315 | /// | |
316 | /// ``` | |
317 | /// use std::collections::BTreeSet; | |
0531ce1d | 318 | /// use std::ops::Bound::Included; |
85aaf69f SL |
319 | /// |
320 | /// let mut set = BTreeSet::new(); | |
321 | /// set.insert(3); | |
322 | /// set.insert(5); | |
323 | /// set.insert(8); | |
32a655c1 | 324 | /// for &elem in set.range((Included(&4), Included(&8))) { |
85aaf69f SL |
325 | /// println!("{}", elem); |
326 | /// } | |
32a655c1 | 327 | /// assert_eq!(Some(&5), set.range(4..).next()); |
85aaf69f | 328 | /// ``` |
8bb4bdeb | 329 | #[stable(feature = "btree_range", since = "1.17.0")] |
9fa01778 | 330 | pub fn range<K: ?Sized, R>(&self, range: R) -> Range<'_, T> |
0531ce1d | 331 | where K: Ord, T: Borrow<K>, R: RangeBounds<K> |
e9174d1e | 332 | { |
32a655c1 | 333 | Range { iter: self.map.range(range) } |
85aaf69f | 334 | } |
85aaf69f | 335 | |
8bb4bdeb | 336 | /// Visits the values representing the difference, |
0731742a | 337 | /// i.e., the values that are in `self` but not in `other`, |
8bb4bdeb | 338 | /// in ascending order. |
1a4d82fc JJ |
339 | /// |
340 | /// # Examples | |
341 | /// | |
342 | /// ``` | |
343 | /// use std::collections::BTreeSet; | |
344 | /// | |
345 | /// let mut a = BTreeSet::new(); | |
85aaf69f SL |
346 | /// a.insert(1); |
347 | /// a.insert(2); | |
1a4d82fc JJ |
348 | /// |
349 | /// let mut b = BTreeSet::new(); | |
85aaf69f SL |
350 | /// b.insert(2); |
351 | /// b.insert(3); | |
1a4d82fc | 352 | /// |
62682a34 | 353 | /// let diff: Vec<_> = a.difference(&b).cloned().collect(); |
c34b1796 | 354 | /// assert_eq!(diff, [1]); |
1a4d82fc | 355 | /// ``` |
85aaf69f | 356 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 357 | pub fn difference<'a>(&'a self, other: &'a BTreeSet<T>) -> Difference<'a, T> { |
e74abb32 | 358 | let (self_min, self_max) = if let (Some(self_min), Some(self_max)) = |
60c5eb7d | 359 | (self.first(), self.last()) |
e74abb32 XL |
360 | { |
361 | (self_min, self_max) | |
362 | } else { | |
363 | return Difference { | |
364 | inner: DifferenceInner::Iterate(self.iter()), | |
365 | }; | |
366 | }; | |
367 | let (other_min, other_max) = if let (Some(other_min), Some(other_max)) = | |
60c5eb7d | 368 | (other.first(), other.last()) |
e74abb32 XL |
369 | { |
370 | (other_min, other_max) | |
532ac7d7 | 371 | } else { |
e74abb32 XL |
372 | return Difference { |
373 | inner: DifferenceInner::Iterate(self.iter()), | |
374 | }; | |
375 | }; | |
376 | Difference { | |
377 | inner: match (self_min.cmp(other_max), self_max.cmp(other_min)) { | |
378 | (Greater, _) | (_, Less) => DifferenceInner::Iterate(self.iter()), | |
379 | (Equal, _) => { | |
380 | let mut self_iter = self.iter(); | |
381 | self_iter.next(); | |
382 | DifferenceInner::Iterate(self_iter) | |
383 | } | |
384 | (_, Equal) => { | |
385 | let mut self_iter = self.iter(); | |
386 | self_iter.next_back(); | |
387 | DifferenceInner::Iterate(self_iter) | |
388 | } | |
389 | _ if self.len() <= other.len() / ITER_PERFORMANCE_TIPPING_SIZE_DIFF => { | |
390 | DifferenceInner::Search { | |
391 | self_iter: self.iter(), | |
392 | other_set: other, | |
393 | } | |
394 | } | |
395 | _ => DifferenceInner::Stitch { | |
532ac7d7 | 396 | self_iter: self.iter(), |
e74abb32 | 397 | other_iter: other.iter().peekable(), |
532ac7d7 | 398 | }, |
e74abb32 | 399 | }, |
92a42be0 | 400 | } |
1a4d82fc JJ |
401 | } |
402 | ||
8bb4bdeb | 403 | /// Visits the values representing the symmetric difference, |
0731742a | 404 | /// i.e., the values that are in `self` or in `other` but not in both, |
8bb4bdeb | 405 | /// in ascending order. |
1a4d82fc JJ |
406 | /// |
407 | /// # Examples | |
408 | /// | |
409 | /// ``` | |
410 | /// use std::collections::BTreeSet; | |
411 | /// | |
412 | /// let mut a = BTreeSet::new(); | |
85aaf69f SL |
413 | /// a.insert(1); |
414 | /// a.insert(2); | |
1a4d82fc JJ |
415 | /// |
416 | /// let mut b = BTreeSet::new(); | |
85aaf69f SL |
417 | /// b.insert(2); |
418 | /// b.insert(3); | |
1a4d82fc | 419 | /// |
62682a34 | 420 | /// let sym_diff: Vec<_> = a.symmetric_difference(&b).cloned().collect(); |
c34b1796 | 421 | /// assert_eq!(sym_diff, [1, 3]); |
1a4d82fc | 422 | /// ``` |
85aaf69f | 423 | #[stable(feature = "rust1", since = "1.0.0")] |
92a42be0 SL |
424 | pub fn symmetric_difference<'a>(&'a self, |
425 | other: &'a BTreeSet<T>) | |
426 | -> SymmetricDifference<'a, T> { | |
e74abb32 | 427 | SymmetricDifference(MergeIterInner::new(self.iter(), other.iter())) |
1a4d82fc JJ |
428 | } |
429 | ||
8bb4bdeb | 430 | /// Visits the values representing the intersection, |
0731742a | 431 | /// i.e., the values that are both in `self` and `other`, |
8bb4bdeb | 432 | /// in ascending order. |
1a4d82fc JJ |
433 | /// |
434 | /// # Examples | |
435 | /// | |
436 | /// ``` | |
437 | /// use std::collections::BTreeSet; | |
438 | /// | |
439 | /// let mut a = BTreeSet::new(); | |
85aaf69f SL |
440 | /// a.insert(1); |
441 | /// a.insert(2); | |
1a4d82fc JJ |
442 | /// |
443 | /// let mut b = BTreeSet::new(); | |
85aaf69f SL |
444 | /// b.insert(2); |
445 | /// b.insert(3); | |
1a4d82fc | 446 | /// |
62682a34 | 447 | /// let intersection: Vec<_> = a.intersection(&b).cloned().collect(); |
c34b1796 | 448 | /// assert_eq!(intersection, [2]); |
1a4d82fc | 449 | /// ``` |
85aaf69f | 450 | #[stable(feature = "rust1", since = "1.0.0")] |
92a42be0 | 451 | pub fn intersection<'a>(&'a self, other: &'a BTreeSet<T>) -> Intersection<'a, T> { |
e74abb32 | 452 | let (self_min, self_max) = if let (Some(self_min), Some(self_max)) = |
60c5eb7d | 453 | (self.first(), self.last()) |
e74abb32 XL |
454 | { |
455 | (self_min, self_max) | |
532ac7d7 | 456 | } else { |
e74abb32 XL |
457 | return Intersection { |
458 | inner: IntersectionInner::Answer(None), | |
459 | }; | |
532ac7d7 | 460 | }; |
e74abb32 | 461 | let (other_min, other_max) = if let (Some(other_min), Some(other_max)) = |
60c5eb7d | 462 | (other.first(), other.last()) |
e74abb32 XL |
463 | { |
464 | (other_min, other_max) | |
532ac7d7 | 465 | } else { |
e74abb32 XL |
466 | return Intersection { |
467 | inner: IntersectionInner::Answer(None), | |
468 | }; | |
469 | }; | |
470 | Intersection { | |
471 | inner: match (self_min.cmp(other_max), self_max.cmp(other_min)) { | |
472 | (Greater, _) | (_, Less) => IntersectionInner::Answer(None), | |
473 | (Equal, _) => IntersectionInner::Answer(Some(self_min)), | |
474 | (_, Equal) => IntersectionInner::Answer(Some(self_max)), | |
475 | _ if self.len() <= other.len() / ITER_PERFORMANCE_TIPPING_SIZE_DIFF => { | |
476 | IntersectionInner::Search { | |
477 | small_iter: self.iter(), | |
478 | large_set: other, | |
479 | } | |
480 | } | |
481 | _ if other.len() <= self.len() / ITER_PERFORMANCE_TIPPING_SIZE_DIFF => { | |
482 | IntersectionInner::Search { | |
483 | small_iter: other.iter(), | |
484 | large_set: self, | |
485 | } | |
486 | } | |
487 | _ => IntersectionInner::Stitch { | |
488 | a: self.iter(), | |
489 | b: other.iter(), | |
532ac7d7 | 490 | }, |
e74abb32 | 491 | }, |
92a42be0 | 492 | } |
1a4d82fc JJ |
493 | } |
494 | ||
8bb4bdeb | 495 | /// Visits the values representing the union, |
0731742a | 496 | /// i.e., all the values in `self` or `other`, without duplicates, |
8bb4bdeb | 497 | /// in ascending order. |
1a4d82fc JJ |
498 | /// |
499 | /// # Examples | |
500 | /// | |
501 | /// ``` | |
502 | /// use std::collections::BTreeSet; | |
503 | /// | |
504 | /// let mut a = BTreeSet::new(); | |
85aaf69f | 505 | /// a.insert(1); |
1a4d82fc JJ |
506 | /// |
507 | /// let mut b = BTreeSet::new(); | |
85aaf69f | 508 | /// b.insert(2); |
1a4d82fc | 509 | /// |
62682a34 | 510 | /// let union: Vec<_> = a.union(&b).cloned().collect(); |
c34b1796 | 511 | /// assert_eq!(union, [1, 2]); |
1a4d82fc | 512 | /// ``` |
85aaf69f | 513 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 514 | pub fn union<'a>(&'a self, other: &'a BTreeSet<T>) -> Union<'a, T> { |
e74abb32 | 515 | Union(MergeIterInner::new(self.iter(), other.iter())) |
1a4d82fc JJ |
516 | } |
517 | ||
1a4d82fc JJ |
518 | /// Clears the set, removing all values. |
519 | /// | |
520 | /// # Examples | |
521 | /// | |
522 | /// ``` | |
523 | /// use std::collections::BTreeSet; | |
524 | /// | |
525 | /// let mut v = BTreeSet::new(); | |
85aaf69f | 526 | /// v.insert(1); |
1a4d82fc JJ |
527 | /// v.clear(); |
528 | /// assert!(v.is_empty()); | |
529 | /// ``` | |
85aaf69f | 530 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
531 | pub fn clear(&mut self) { |
532 | self.map.clear() | |
533 | } | |
534 | ||
535 | /// Returns `true` if the set contains a value. | |
536 | /// | |
537 | /// The value may be any borrowed form of the set's value type, | |
538 | /// but the ordering on the borrowed form *must* match the | |
539 | /// ordering on the value type. | |
540 | /// | |
541 | /// # Examples | |
542 | /// | |
543 | /// ``` | |
544 | /// use std::collections::BTreeSet; | |
545 | /// | |
85aaf69f | 546 | /// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect(); |
1a4d82fc JJ |
547 | /// assert_eq!(set.contains(&1), true); |
548 | /// assert_eq!(set.contains(&4), false); | |
549 | /// ``` | |
85aaf69f | 550 | #[stable(feature = "rust1", since = "1.0.0")] |
92a42be0 SL |
551 | pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool |
552 | where T: Borrow<Q>, | |
553 | Q: Ord | |
554 | { | |
1a4d82fc JJ |
555 | self.map.contains_key(value) |
556 | } | |
557 | ||
e9174d1e SL |
558 | /// Returns a reference to the value in the set, if any, that is equal to the given value. |
559 | /// | |
560 | /// The value may be any borrowed form of the set's value type, | |
561 | /// but the ordering on the borrowed form *must* match the | |
562 | /// ordering on the value type. | |
2c00a5a8 XL |
563 | /// |
564 | /// # Examples | |
565 | /// | |
566 | /// ``` | |
567 | /// use std::collections::BTreeSet; | |
568 | /// | |
569 | /// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect(); | |
570 | /// assert_eq!(set.get(&2), Some(&2)); | |
571 | /// assert_eq!(set.get(&4), None); | |
572 | /// ``` | |
54a0048b | 573 | #[stable(feature = "set_recovery", since = "1.9.0")] |
92a42be0 SL |
574 | pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T> |
575 | where T: Borrow<Q>, | |
576 | Q: Ord | |
577 | { | |
e9174d1e SL |
578 | Recover::get(&self.map, value) |
579 | } | |
580 | ||
8bb4bdeb | 581 | /// Returns `true` if `self` has no elements in common with `other`. |
1a4d82fc JJ |
582 | /// This is equivalent to checking for an empty intersection. |
583 | /// | |
584 | /// # Examples | |
585 | /// | |
586 | /// ``` | |
587 | /// use std::collections::BTreeSet; | |
588 | /// | |
85aaf69f SL |
589 | /// let a: BTreeSet<_> = [1, 2, 3].iter().cloned().collect(); |
590 | /// let mut b = BTreeSet::new(); | |
1a4d82fc JJ |
591 | /// |
592 | /// assert_eq!(a.is_disjoint(&b), true); | |
593 | /// b.insert(4); | |
594 | /// assert_eq!(a.is_disjoint(&b), true); | |
595 | /// b.insert(1); | |
596 | /// assert_eq!(a.is_disjoint(&b), false); | |
597 | /// ``` | |
85aaf69f | 598 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
599 | pub fn is_disjoint(&self, other: &BTreeSet<T>) -> bool { |
600 | self.intersection(other).next().is_none() | |
601 | } | |
602 | ||
8bb4bdeb | 603 | /// Returns `true` if the set is a subset of another, |
0731742a | 604 | /// i.e., `other` contains at least all the values in `self`. |
1a4d82fc JJ |
605 | /// |
606 | /// # Examples | |
607 | /// | |
608 | /// ``` | |
609 | /// use std::collections::BTreeSet; | |
610 | /// | |
85aaf69f SL |
611 | /// let sup: BTreeSet<_> = [1, 2, 3].iter().cloned().collect(); |
612 | /// let mut set = BTreeSet::new(); | |
1a4d82fc JJ |
613 | /// |
614 | /// assert_eq!(set.is_subset(&sup), true); | |
615 | /// set.insert(2); | |
616 | /// assert_eq!(set.is_subset(&sup), true); | |
617 | /// set.insert(4); | |
618 | /// assert_eq!(set.is_subset(&sup), false); | |
619 | /// ``` | |
85aaf69f | 620 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 621 | pub fn is_subset(&self, other: &BTreeSet<T>) -> bool { |
532ac7d7 | 622 | // Same result as self.difference(other).next().is_none() |
e74abb32 | 623 | // but the code below is faster (hugely in some cases). |
532ac7d7 | 624 | if self.len() > other.len() { |
e74abb32 XL |
625 | return false; |
626 | } | |
627 | let (self_min, self_max) = if let (Some(self_min), Some(self_max)) = | |
60c5eb7d | 628 | (self.first(), self.last()) |
e74abb32 XL |
629 | { |
630 | (self_min, self_max) | |
631 | } else { | |
632 | return true; // self is empty | |
633 | }; | |
634 | let (other_min, other_max) = if let (Some(other_min), Some(other_max)) = | |
60c5eb7d | 635 | (other.first(), other.last()) |
e74abb32 XL |
636 | { |
637 | (other_min, other_max) | |
638 | } else { | |
639 | return false; // other is empty | |
640 | }; | |
641 | let mut self_iter = self.iter(); | |
642 | match self_min.cmp(other_min) { | |
643 | Less => return false, | |
644 | Equal => { | |
645 | self_iter.next(); | |
646 | } | |
647 | Greater => (), | |
648 | } | |
649 | match self_max.cmp(other_max) { | |
650 | Greater => return false, | |
651 | Equal => { | |
652 | self_iter.next_back(); | |
653 | } | |
654 | Less => (), | |
655 | } | |
656 | if self_iter.len() <= other.len() / ITER_PERFORMANCE_TIPPING_SIZE_DIFF { | |
e74abb32 XL |
657 | for next in self_iter { |
658 | if !other.contains(next) { | |
532ac7d7 XL |
659 | return false; |
660 | } | |
532ac7d7 | 661 | } |
532ac7d7 | 662 | } else { |
e74abb32 XL |
663 | let mut other_iter = other.iter(); |
664 | other_iter.next(); | |
665 | other_iter.next_back(); | |
666 | let mut self_next = self_iter.next(); | |
667 | while let Some(self1) = self_next { | |
668 | match other_iter.next().map_or(Less, |other1| self1.cmp(other1)) { | |
669 | Less => return false, | |
670 | Equal => self_next = self_iter.next(), | |
671 | Greater => (), | |
532ac7d7 XL |
672 | } |
673 | } | |
1a4d82fc | 674 | } |
e74abb32 | 675 | true |
1a4d82fc JJ |
676 | } |
677 | ||
8bb4bdeb | 678 | /// Returns `true` if the set is a superset of another, |
0731742a | 679 | /// i.e., `self` contains at least all the values in `other`. |
1a4d82fc JJ |
680 | /// |
681 | /// # Examples | |
682 | /// | |
683 | /// ``` | |
684 | /// use std::collections::BTreeSet; | |
685 | /// | |
85aaf69f SL |
686 | /// let sub: BTreeSet<_> = [1, 2].iter().cloned().collect(); |
687 | /// let mut set = BTreeSet::new(); | |
1a4d82fc JJ |
688 | /// |
689 | /// assert_eq!(set.is_superset(&sub), false); | |
690 | /// | |
691 | /// set.insert(0); | |
692 | /// set.insert(1); | |
693 | /// assert_eq!(set.is_superset(&sub), false); | |
694 | /// | |
695 | /// set.insert(2); | |
696 | /// assert_eq!(set.is_superset(&sub), true); | |
697 | /// ``` | |
85aaf69f | 698 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
699 | pub fn is_superset(&self, other: &BTreeSet<T>) -> bool { |
700 | other.is_subset(self) | |
701 | } | |
702 | ||
60c5eb7d XL |
703 | /// Returns a reference to the first value in the set, if any. |
704 | /// This value is always the minimum of all values in the set. | |
705 | /// | |
706 | /// # Examples | |
707 | /// | |
708 | /// Basic usage: | |
709 | /// | |
710 | /// ``` | |
711 | /// #![feature(map_first_last)] | |
712 | /// use std::collections::BTreeSet; | |
713 | /// | |
714 | /// let mut map = BTreeSet::new(); | |
715 | /// assert_eq!(map.first(), None); | |
716 | /// map.insert(1); | |
717 | /// assert_eq!(map.first(), Some(&1)); | |
718 | /// map.insert(2); | |
719 | /// assert_eq!(map.first(), Some(&1)); | |
720 | /// ``` | |
721 | #[unstable(feature = "map_first_last", issue = "62924")] | |
722 | pub fn first(&self) -> Option<&T> { | |
723 | self.map.first_key_value().map(|(k, _)| k) | |
724 | } | |
725 | ||
726 | /// Returns a reference to the last value in the set, if any. | |
727 | /// This value is always the maximum of all values in the set. | |
728 | /// | |
729 | /// # Examples | |
730 | /// | |
731 | /// Basic usage: | |
732 | /// | |
733 | /// ``` | |
734 | /// #![feature(map_first_last)] | |
735 | /// use std::collections::BTreeSet; | |
736 | /// | |
737 | /// let mut map = BTreeSet::new(); | |
738 | /// assert_eq!(map.first(), None); | |
739 | /// map.insert(1); | |
740 | /// assert_eq!(map.last(), Some(&1)); | |
741 | /// map.insert(2); | |
742 | /// assert_eq!(map.last(), Some(&2)); | |
743 | /// ``` | |
744 | #[unstable(feature = "map_first_last", issue = "62924")] | |
745 | pub fn last(&self) -> Option<&T> { | |
746 | self.map.last_key_value().map(|(k, _)| k) | |
747 | } | |
748 | ||
749 | /// Removes the first value from the set and returns it, if any. | |
750 | /// The first value is always the minimum value in the set. | |
751 | /// | |
752 | /// # Examples | |
753 | /// | |
754 | /// ``` | |
755 | /// #![feature(map_first_last)] | |
756 | /// use std::collections::BTreeSet; | |
757 | /// | |
758 | /// let mut set = BTreeSet::new(); | |
759 | /// | |
760 | /// set.insert(1); | |
761 | /// while let Some(n) = set.pop_first() { | |
762 | /// assert_eq!(n, 1); | |
763 | /// } | |
764 | /// assert!(set.is_empty()); | |
765 | /// ``` | |
766 | #[unstable(feature = "map_first_last", issue = "62924")] | |
767 | pub fn pop_first(&mut self) -> Option<T> { | |
768 | self.map.first_entry().map(|entry| entry.remove_entry().0) | |
769 | } | |
770 | ||
771 | /// Removes the last value from the set and returns it, if any. | |
772 | /// The last value is always the maximum value in the set. | |
773 | /// | |
774 | /// # Examples | |
775 | /// | |
776 | /// ``` | |
777 | /// #![feature(map_first_last)] | |
778 | /// use std::collections::BTreeSet; | |
779 | /// | |
780 | /// let mut set = BTreeSet::new(); | |
781 | /// | |
782 | /// set.insert(1); | |
783 | /// while let Some(n) = set.pop_last() { | |
784 | /// assert_eq!(n, 1); | |
785 | /// } | |
786 | /// assert!(set.is_empty()); | |
787 | /// ``` | |
788 | #[unstable(feature = "map_first_last", issue = "62924")] | |
789 | pub fn pop_last(&mut self) -> Option<T> { | |
790 | self.map.last_entry().map(|entry| entry.remove_entry().0) | |
791 | } | |
792 | ||
b039eaaf SL |
793 | /// Adds a value to the set. |
794 | /// | |
a7813a04 | 795 | /// If the set did not have this value present, `true` is returned. |
b039eaaf | 796 | /// |
a7813a04 | 797 | /// If the set did have this value present, `false` is returned, and the |
b039eaaf SL |
798 | /// entry is not updated. See the [module-level documentation] for more. |
799 | /// | |
800 | /// [module-level documentation]: index.html#insert-and-complex-keys | |
1a4d82fc JJ |
801 | /// |
802 | /// # Examples | |
803 | /// | |
804 | /// ``` | |
805 | /// use std::collections::BTreeSet; | |
806 | /// | |
807 | /// let mut set = BTreeSet::new(); | |
808 | /// | |
85aaf69f SL |
809 | /// assert_eq!(set.insert(2), true); |
810 | /// assert_eq!(set.insert(2), false); | |
1a4d82fc JJ |
811 | /// assert_eq!(set.len(), 1); |
812 | /// ``` | |
85aaf69f | 813 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
814 | pub fn insert(&mut self, value: T) -> bool { |
815 | self.map.insert(value, ()).is_none() | |
816 | } | |
817 | ||
e9174d1e SL |
818 | /// Adds a value to the set, replacing the existing value, if any, that is equal to the given |
819 | /// one. Returns the replaced value. | |
2c00a5a8 XL |
820 | /// |
821 | /// # Examples | |
822 | /// | |
823 | /// ``` | |
824 | /// use std::collections::BTreeSet; | |
825 | /// | |
826 | /// let mut set = BTreeSet::new(); | |
827 | /// set.insert(Vec::<i32>::new()); | |
828 | /// | |
829 | /// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0); | |
830 | /// set.replace(Vec::with_capacity(10)); | |
831 | /// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10); | |
832 | /// ``` | |
54a0048b | 833 | #[stable(feature = "set_recovery", since = "1.9.0")] |
e9174d1e SL |
834 | pub fn replace(&mut self, value: T) -> Option<T> { |
835 | Recover::replace(&mut self.map, value) | |
836 | } | |
837 | ||
9fa01778 | 838 | /// Removes a value from the set. Returns whether the value was |
1a4d82fc JJ |
839 | /// present in the set. |
840 | /// | |
841 | /// The value may be any borrowed form of the set's value type, | |
842 | /// but the ordering on the borrowed form *must* match the | |
843 | /// ordering on the value type. | |
844 | /// | |
845 | /// # Examples | |
846 | /// | |
847 | /// ``` | |
848 | /// use std::collections::BTreeSet; | |
849 | /// | |
850 | /// let mut set = BTreeSet::new(); | |
851 | /// | |
85aaf69f | 852 | /// set.insert(2); |
1a4d82fc JJ |
853 | /// assert_eq!(set.remove(&2), true); |
854 | /// assert_eq!(set.remove(&2), false); | |
855 | /// ``` | |
85aaf69f | 856 | #[stable(feature = "rust1", since = "1.0.0")] |
92a42be0 SL |
857 | pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool |
858 | where T: Borrow<Q>, | |
859 | Q: Ord | |
860 | { | |
1a4d82fc JJ |
861 | self.map.remove(value).is_some() |
862 | } | |
e9174d1e SL |
863 | |
864 | /// Removes and returns the value in the set, if any, that is equal to the given one. | |
865 | /// | |
866 | /// The value may be any borrowed form of the set's value type, | |
867 | /// but the ordering on the borrowed form *must* match the | |
868 | /// ordering on the value type. | |
2c00a5a8 XL |
869 | /// |
870 | /// # Examples | |
871 | /// | |
872 | /// ``` | |
873 | /// use std::collections::BTreeSet; | |
874 | /// | |
875 | /// let mut set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect(); | |
876 | /// assert_eq!(set.take(&2), Some(2)); | |
877 | /// assert_eq!(set.take(&2), None); | |
878 | /// ``` | |
54a0048b | 879 | #[stable(feature = "set_recovery", since = "1.9.0")] |
92a42be0 SL |
880 | pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T> |
881 | where T: Borrow<Q>, | |
882 | Q: Ord | |
883 | { | |
e9174d1e SL |
884 | Recover::take(&mut self.map, value) |
885 | } | |
a7813a04 XL |
886 | |
887 | /// Moves all elements from `other` into `Self`, leaving `other` empty. | |
888 | /// | |
889 | /// # Examples | |
890 | /// | |
891 | /// ``` | |
a7813a04 XL |
892 | /// use std::collections::BTreeSet; |
893 | /// | |
894 | /// let mut a = BTreeSet::new(); | |
895 | /// a.insert(1); | |
896 | /// a.insert(2); | |
897 | /// a.insert(3); | |
898 | /// | |
899 | /// let mut b = BTreeSet::new(); | |
900 | /// b.insert(3); | |
901 | /// b.insert(4); | |
902 | /// b.insert(5); | |
903 | /// | |
904 | /// a.append(&mut b); | |
905 | /// | |
906 | /// assert_eq!(a.len(), 5); | |
907 | /// assert_eq!(b.len(), 0); | |
908 | /// | |
909 | /// assert!(a.contains(&1)); | |
910 | /// assert!(a.contains(&2)); | |
911 | /// assert!(a.contains(&3)); | |
912 | /// assert!(a.contains(&4)); | |
913 | /// assert!(a.contains(&5)); | |
914 | /// ``` | |
3157f602 | 915 | #[stable(feature = "btree_append", since = "1.11.0")] |
a7813a04 XL |
916 | pub fn append(&mut self, other: &mut Self) { |
917 | self.map.append(&mut other.map); | |
918 | } | |
3157f602 XL |
919 | |
920 | /// Splits the collection into two at the given key. Returns everything after the given key, | |
921 | /// including the key. | |
922 | /// | |
923 | /// # Examples | |
924 | /// | |
925 | /// Basic usage: | |
926 | /// | |
927 | /// ``` | |
2c00a5a8 | 928 | /// use std::collections::BTreeSet; |
3157f602 | 929 | /// |
2c00a5a8 XL |
930 | /// let mut a = BTreeSet::new(); |
931 | /// a.insert(1); | |
932 | /// a.insert(2); | |
933 | /// a.insert(3); | |
934 | /// a.insert(17); | |
935 | /// a.insert(41); | |
3157f602 XL |
936 | /// |
937 | /// let b = a.split_off(&3); | |
938 | /// | |
939 | /// assert_eq!(a.len(), 2); | |
940 | /// assert_eq!(b.len(), 3); | |
941 | /// | |
2c00a5a8 XL |
942 | /// assert!(a.contains(&1)); |
943 | /// assert!(a.contains(&2)); | |
3157f602 | 944 | /// |
2c00a5a8 XL |
945 | /// assert!(b.contains(&3)); |
946 | /// assert!(b.contains(&17)); | |
947 | /// assert!(b.contains(&41)); | |
3157f602 XL |
948 | /// ``` |
949 | #[stable(feature = "btree_split_off", since = "1.11.0")] | |
950 | pub fn split_off<Q: ?Sized + Ord>(&mut self, key: &Q) -> Self where T: Borrow<Q> { | |
951 | BTreeSet { map: self.map.split_off(key) } | |
952 | } | |
1a4d82fc JJ |
953 | } |
954 | ||
2c00a5a8 XL |
955 | impl<T> BTreeSet<T> { |
956 | /// Gets an iterator that visits the values in the `BTreeSet` in ascending order. | |
957 | /// | |
958 | /// # Examples | |
959 | /// | |
960 | /// ``` | |
961 | /// use std::collections::BTreeSet; | |
962 | /// | |
963 | /// let set: BTreeSet<usize> = [1, 2, 3].iter().cloned().collect(); | |
964 | /// let mut set_iter = set.iter(); | |
965 | /// assert_eq!(set_iter.next(), Some(&1)); | |
966 | /// assert_eq!(set_iter.next(), Some(&2)); | |
967 | /// assert_eq!(set_iter.next(), Some(&3)); | |
968 | /// assert_eq!(set_iter.next(), None); | |
969 | /// ``` | |
970 | /// | |
971 | /// Values returned by the iterator are returned in ascending order: | |
972 | /// | |
973 | /// ``` | |
974 | /// use std::collections::BTreeSet; | |
975 | /// | |
976 | /// let set: BTreeSet<usize> = [3, 1, 2].iter().cloned().collect(); | |
977 | /// let mut set_iter = set.iter(); | |
978 | /// assert_eq!(set_iter.next(), Some(&1)); | |
979 | /// assert_eq!(set_iter.next(), Some(&2)); | |
980 | /// assert_eq!(set_iter.next(), Some(&3)); | |
981 | /// assert_eq!(set_iter.next(), None); | |
982 | /// ``` | |
983 | #[stable(feature = "rust1", since = "1.0.0")] | |
9fa01778 | 984 | pub fn iter(&self) -> Iter<'_, T> { |
2c00a5a8 XL |
985 | Iter { iter: self.map.keys() } |
986 | } | |
987 | ||
988 | /// Returns the number of elements in the set. | |
989 | /// | |
990 | /// # Examples | |
991 | /// | |
992 | /// ``` | |
993 | /// use std::collections::BTreeSet; | |
994 | /// | |
995 | /// let mut v = BTreeSet::new(); | |
996 | /// assert_eq!(v.len(), 0); | |
997 | /// v.insert(1); | |
998 | /// assert_eq!(v.len(), 1); | |
999 | /// ``` | |
1000 | #[stable(feature = "rust1", since = "1.0.0")] | |
1001 | pub fn len(&self) -> usize { | |
1002 | self.map.len() | |
1003 | } | |
1004 | ||
1005 | /// Returns `true` if the set contains no elements. | |
1006 | /// | |
1007 | /// # Examples | |
1008 | /// | |
1009 | /// ``` | |
1010 | /// use std::collections::BTreeSet; | |
1011 | /// | |
1012 | /// let mut v = BTreeSet::new(); | |
1013 | /// assert!(v.is_empty()); | |
1014 | /// v.insert(1); | |
1015 | /// assert!(!v.is_empty()); | |
1016 | /// ``` | |
1017 | #[stable(feature = "rust1", since = "1.0.0")] | |
1018 | pub fn is_empty(&self) -> bool { | |
1019 | self.len() == 0 | |
1020 | } | |
1021 | } | |
1022 | ||
85aaf69f | 1023 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 1024 | impl<T: Ord> FromIterator<T> for BTreeSet<T> { |
92a42be0 | 1025 | fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> BTreeSet<T> { |
1a4d82fc JJ |
1026 | let mut set = BTreeSet::new(); |
1027 | set.extend(iter); | |
1028 | set | |
1029 | } | |
1030 | } | |
1031 | ||
85aaf69f SL |
1032 | #[stable(feature = "rust1", since = "1.0.0")] |
1033 | impl<T> IntoIterator for BTreeSet<T> { | |
1034 | type Item = T; | |
1035 | type IntoIter = IntoIter<T>; | |
1036 | ||
cc61c64b | 1037 | /// Gets an iterator for moving out the `BTreeSet`'s contents. |
9346a6ac AL |
1038 | /// |
1039 | /// # Examples | |
1040 | /// | |
1041 | /// ``` | |
9346a6ac AL |
1042 | /// use std::collections::BTreeSet; |
1043 | /// | |
1044 | /// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect(); | |
1045 | /// | |
62682a34 | 1046 | /// let v: Vec<_> = set.into_iter().collect(); |
9346a6ac AL |
1047 | /// assert_eq!(v, [1, 2, 3, 4]); |
1048 | /// ``` | |
85aaf69f | 1049 | fn into_iter(self) -> IntoIter<T> { |
9cc50fc6 | 1050 | IntoIter { iter: self.map.into_iter() } |
85aaf69f SL |
1051 | } |
1052 | } | |
1053 | ||
1054 | #[stable(feature = "rust1", since = "1.0.0")] | |
1055 | impl<'a, T> IntoIterator for &'a BTreeSet<T> { | |
1056 | type Item = &'a T; | |
1057 | type IntoIter = Iter<'a, T>; | |
1058 | ||
1059 | fn into_iter(self) -> Iter<'a, T> { | |
1060 | self.iter() | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | #[stable(feature = "rust1", since = "1.0.0")] | |
1a4d82fc JJ |
1065 | impl<T: Ord> Extend<T> for BTreeSet<T> { |
1066 | #[inline] | |
92a42be0 | 1067 | fn extend<Iter: IntoIterator<Item = T>>(&mut self, iter: Iter) { |
532ac7d7 | 1068 | iter.into_iter().for_each(move |elem| { |
1a4d82fc | 1069 | self.insert(elem); |
532ac7d7 | 1070 | }); |
1a4d82fc JJ |
1071 | } |
1072 | } | |
1073 | ||
62682a34 SL |
1074 | #[stable(feature = "extend_ref", since = "1.2.0")] |
1075 | impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BTreeSet<T> { | |
92a42be0 | 1076 | fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { |
62682a34 SL |
1077 | self.extend(iter.into_iter().cloned()); |
1078 | } | |
1079 | } | |
1080 | ||
85aaf69f | 1081 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 1082 | impl<T: Ord> Default for BTreeSet<T> { |
9e0c209e | 1083 | /// Makes an empty `BTreeSet<T>` with a reasonable choice of B. |
1a4d82fc JJ |
1084 | fn default() -> BTreeSet<T> { |
1085 | BTreeSet::new() | |
1086 | } | |
1087 | } | |
1088 | ||
85aaf69f | 1089 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 | 1090 | impl<T: Ord + Clone> Sub<&BTreeSet<T>> for &BTreeSet<T> { |
1a4d82fc JJ |
1091 | type Output = BTreeSet<T>; |
1092 | ||
1093 | /// Returns the difference of `self` and `rhs` as a new `BTreeSet<T>`. | |
1094 | /// | |
1095 | /// # Examples | |
1096 | /// | |
1097 | /// ``` | |
1098 | /// use std::collections::BTreeSet; | |
1099 | /// | |
85aaf69f SL |
1100 | /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect(); |
1101 | /// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect(); | |
1a4d82fc | 1102 | /// |
85aaf69f SL |
1103 | /// let result = &a - &b; |
1104 | /// let result_vec: Vec<_> = result.into_iter().collect(); | |
c34b1796 | 1105 | /// assert_eq!(result_vec, [1, 2]); |
1a4d82fc JJ |
1106 | /// ``` |
1107 | fn sub(self, rhs: &BTreeSet<T>) -> BTreeSet<T> { | |
1108 | self.difference(rhs).cloned().collect() | |
1109 | } | |
1110 | } | |
1111 | ||
85aaf69f | 1112 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 | 1113 | impl<T: Ord + Clone> BitXor<&BTreeSet<T>> for &BTreeSet<T> { |
1a4d82fc JJ |
1114 | type Output = BTreeSet<T>; |
1115 | ||
1116 | /// Returns the symmetric difference of `self` and `rhs` as a new `BTreeSet<T>`. | |
1117 | /// | |
1118 | /// # Examples | |
1119 | /// | |
1120 | /// ``` | |
1121 | /// use std::collections::BTreeSet; | |
1122 | /// | |
85aaf69f SL |
1123 | /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect(); |
1124 | /// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect(); | |
1a4d82fc | 1125 | /// |
85aaf69f SL |
1126 | /// let result = &a ^ &b; |
1127 | /// let result_vec: Vec<_> = result.into_iter().collect(); | |
c34b1796 | 1128 | /// assert_eq!(result_vec, [1, 4]); |
1a4d82fc JJ |
1129 | /// ``` |
1130 | fn bitxor(self, rhs: &BTreeSet<T>) -> BTreeSet<T> { | |
1131 | self.symmetric_difference(rhs).cloned().collect() | |
1132 | } | |
1133 | } | |
1134 | ||
85aaf69f | 1135 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 | 1136 | impl<T: Ord + Clone> BitAnd<&BTreeSet<T>> for &BTreeSet<T> { |
1a4d82fc JJ |
1137 | type Output = BTreeSet<T>; |
1138 | ||
1139 | /// Returns the intersection of `self` and `rhs` as a new `BTreeSet<T>`. | |
1140 | /// | |
1141 | /// # Examples | |
1142 | /// | |
1143 | /// ``` | |
1144 | /// use std::collections::BTreeSet; | |
1145 | /// | |
85aaf69f SL |
1146 | /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect(); |
1147 | /// let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect(); | |
1a4d82fc | 1148 | /// |
85aaf69f SL |
1149 | /// let result = &a & &b; |
1150 | /// let result_vec: Vec<_> = result.into_iter().collect(); | |
c34b1796 | 1151 | /// assert_eq!(result_vec, [2, 3]); |
1a4d82fc JJ |
1152 | /// ``` |
1153 | fn bitand(self, rhs: &BTreeSet<T>) -> BTreeSet<T> { | |
1154 | self.intersection(rhs).cloned().collect() | |
1155 | } | |
1156 | } | |
1157 | ||
85aaf69f | 1158 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 | 1159 | impl<T: Ord + Clone> BitOr<&BTreeSet<T>> for &BTreeSet<T> { |
1a4d82fc JJ |
1160 | type Output = BTreeSet<T>; |
1161 | ||
1162 | /// Returns the union of `self` and `rhs` as a new `BTreeSet<T>`. | |
1163 | /// | |
1164 | /// # Examples | |
1165 | /// | |
1166 | /// ``` | |
1167 | /// use std::collections::BTreeSet; | |
1168 | /// | |
85aaf69f SL |
1169 | /// let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect(); |
1170 | /// let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect(); | |
1a4d82fc | 1171 | /// |
85aaf69f SL |
1172 | /// let result = &a | &b; |
1173 | /// let result_vec: Vec<_> = result.into_iter().collect(); | |
c34b1796 | 1174 | /// assert_eq!(result_vec, [1, 2, 3, 4, 5]); |
1a4d82fc JJ |
1175 | /// ``` |
1176 | fn bitor(self, rhs: &BTreeSet<T>) -> BTreeSet<T> { | |
1177 | self.union(rhs).cloned().collect() | |
1178 | } | |
1179 | } | |
1180 | ||
85aaf69f SL |
1181 | #[stable(feature = "rust1", since = "1.0.0")] |
1182 | impl<T: Debug> Debug for BTreeSet<T> { | |
9fa01778 | 1183 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
62682a34 | 1184 | f.debug_set().entries(self.iter()).finish() |
1a4d82fc JJ |
1185 | } |
1186 | } | |
1187 | ||
c30ab7b3 | 1188 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 XL |
1189 | impl<T> Clone for Iter<'_, T> { |
1190 | fn clone(&self) -> Self { | |
92a42be0 SL |
1191 | Iter { iter: self.iter.clone() } |
1192 | } | |
c34b1796 | 1193 | } |
85aaf69f | 1194 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1195 | impl<'a, T> Iterator for Iter<'a, T> { |
1196 | type Item = &'a T; | |
1197 | ||
92a42be0 SL |
1198 | fn next(&mut self) -> Option<&'a T> { |
1199 | self.iter.next() | |
1200 | } | |
1201 | fn size_hint(&self) -> (usize, Option<usize>) { | |
1202 | self.iter.size_hint() | |
1203 | } | |
416331ca XL |
1204 | fn last(mut self) -> Option<&'a T> { |
1205 | self.next_back() | |
1206 | } | |
1a4d82fc | 1207 | } |
85aaf69f | 1208 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 1209 | impl<'a, T> DoubleEndedIterator for Iter<'a, T> { |
92a42be0 SL |
1210 | fn next_back(&mut self) -> Option<&'a T> { |
1211 | self.iter.next_back() | |
1212 | } | |
1a4d82fc | 1213 | } |
85aaf69f | 1214 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 | 1215 | impl<T> ExactSizeIterator for Iter<'_, T> { |
7453a54e SL |
1216 | fn len(&self) -> usize { self.iter.len() } |
1217 | } | |
1a4d82fc | 1218 | |
0531ce1d | 1219 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1220 | impl<T> FusedIterator for Iter<'_, T> {} |
1a4d82fc | 1221 | |
85aaf69f | 1222 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1223 | impl<T> Iterator for IntoIter<T> { |
1224 | type Item = T; | |
1225 | ||
92a42be0 | 1226 | fn next(&mut self) -> Option<T> { |
9cc50fc6 | 1227 | self.iter.next().map(|(k, _)| k) |
92a42be0 SL |
1228 | } |
1229 | fn size_hint(&self) -> (usize, Option<usize>) { | |
1230 | self.iter.size_hint() | |
1231 | } | |
1a4d82fc | 1232 | } |
85aaf69f | 1233 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 1234 | impl<T> DoubleEndedIterator for IntoIter<T> { |
92a42be0 | 1235 | fn next_back(&mut self) -> Option<T> { |
9cc50fc6 | 1236 | self.iter.next_back().map(|(k, _)| k) |
92a42be0 | 1237 | } |
1a4d82fc | 1238 | } |
85aaf69f | 1239 | #[stable(feature = "rust1", since = "1.0.0")] |
7453a54e SL |
1240 | impl<T> ExactSizeIterator for IntoIter<T> { |
1241 | fn len(&self) -> usize { self.iter.len() } | |
1242 | } | |
1a4d82fc | 1243 | |
0531ce1d | 1244 | #[stable(feature = "fused", since = "1.26.0")] |
9e0c209e | 1245 | impl<T> FusedIterator for IntoIter<T> {} |
85aaf69f | 1246 | |
cc61c64b | 1247 | #[stable(feature = "btree_range", since = "1.17.0")] |
9fa01778 XL |
1248 | impl<T> Clone for Range<'_, T> { |
1249 | fn clone(&self) -> Self { | |
92a42be0 SL |
1250 | Range { iter: self.iter.clone() } |
1251 | } | |
c34b1796 | 1252 | } |
cc61c64b XL |
1253 | |
1254 | #[stable(feature = "btree_range", since = "1.17.0")] | |
85aaf69f SL |
1255 | impl<'a, T> Iterator for Range<'a, T> { |
1256 | type Item = &'a T; | |
1257 | ||
92a42be0 | 1258 | fn next(&mut self) -> Option<&'a T> { |
9cc50fc6 | 1259 | self.iter.next().map(|(k, _)| k) |
92a42be0 | 1260 | } |
416331ca XL |
1261 | |
1262 | fn last(mut self) -> Option<&'a T> { | |
1263 | self.next_back() | |
1264 | } | |
85aaf69f | 1265 | } |
cc61c64b XL |
1266 | |
1267 | #[stable(feature = "btree_range", since = "1.17.0")] | |
85aaf69f | 1268 | impl<'a, T> DoubleEndedIterator for Range<'a, T> { |
92a42be0 | 1269 | fn next_back(&mut self) -> Option<&'a T> { |
9cc50fc6 | 1270 | self.iter.next_back().map(|(k, _)| k) |
92a42be0 | 1271 | } |
85aaf69f SL |
1272 | } |
1273 | ||
0531ce1d | 1274 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1275 | impl<T> FusedIterator for Range<'_, T> {} |
9e0c209e | 1276 | |
c30ab7b3 | 1277 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 XL |
1278 | impl<T> Clone for Difference<'_, T> { |
1279 | fn clone(&self) -> Self { | |
92a42be0 | 1280 | Difference { |
532ac7d7 XL |
1281 | inner: match &self.inner { |
1282 | DifferenceInner::Stitch { | |
1283 | self_iter, | |
1284 | other_iter, | |
1285 | } => DifferenceInner::Stitch { | |
1286 | self_iter: self_iter.clone(), | |
1287 | other_iter: other_iter.clone(), | |
1288 | }, | |
1289 | DifferenceInner::Search { | |
1290 | self_iter, | |
1291 | other_set, | |
1292 | } => DifferenceInner::Search { | |
1293 | self_iter: self_iter.clone(), | |
1294 | other_set, | |
1295 | }, | |
e74abb32 | 1296 | DifferenceInner::Iterate(iter) => DifferenceInner::Iterate(iter.clone()), |
532ac7d7 | 1297 | }, |
92a42be0 | 1298 | } |
c34b1796 AL |
1299 | } |
1300 | } | |
85aaf69f | 1301 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1302 | impl<'a, T: Ord> Iterator for Difference<'a, T> { |
1303 | type Item = &'a T; | |
1304 | ||
1305 | fn next(&mut self) -> Option<&'a T> { | |
532ac7d7 XL |
1306 | match &mut self.inner { |
1307 | DifferenceInner::Stitch { | |
1308 | self_iter, | |
1309 | other_iter, | |
1310 | } => { | |
1311 | let mut self_next = self_iter.next()?; | |
1312 | loop { | |
1313 | match other_iter | |
1314 | .peek() | |
e74abb32 | 1315 | .map_or(Less, |other_next| self_next.cmp(other_next)) |
532ac7d7 XL |
1316 | { |
1317 | Less => return Some(self_next), | |
1318 | Equal => { | |
1319 | self_next = self_iter.next()?; | |
1320 | other_iter.next(); | |
1321 | } | |
1322 | Greater => { | |
1323 | other_iter.next(); | |
1324 | } | |
1325 | } | |
92a42be0 | 1326 | } |
1a4d82fc | 1327 | } |
532ac7d7 XL |
1328 | DifferenceInner::Search { |
1329 | self_iter, | |
1330 | other_set, | |
1331 | } => loop { | |
1332 | let self_next = self_iter.next()?; | |
1333 | if !other_set.contains(&self_next) { | |
1334 | return Some(self_next); | |
1335 | } | |
1336 | }, | |
e74abb32 | 1337 | DifferenceInner::Iterate(iter) => iter.next(), |
1a4d82fc JJ |
1338 | } |
1339 | } | |
7453a54e SL |
1340 | |
1341 | fn size_hint(&self) -> (usize, Option<usize>) { | |
532ac7d7 XL |
1342 | let (self_len, other_len) = match &self.inner { |
1343 | DifferenceInner::Stitch { | |
1344 | self_iter, | |
e74abb32 | 1345 | other_iter, |
532ac7d7 XL |
1346 | } => (self_iter.len(), other_iter.len()), |
1347 | DifferenceInner::Search { | |
1348 | self_iter, | |
e74abb32 | 1349 | other_set, |
532ac7d7 | 1350 | } => (self_iter.len(), other_set.len()), |
e74abb32 | 1351 | DifferenceInner::Iterate(iter) => (iter.len(), 0), |
532ac7d7 XL |
1352 | }; |
1353 | (self_len.saturating_sub(other_len), Some(self_len)) | |
7453a54e | 1354 | } |
1a4d82fc JJ |
1355 | } |
1356 | ||
0531ce1d | 1357 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1358 | impl<T: Ord> FusedIterator for Difference<'_, T> {} |
9e0c209e | 1359 | |
c30ab7b3 | 1360 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 XL |
1361 | impl<T> Clone for SymmetricDifference<'_, T> { |
1362 | fn clone(&self) -> Self { | |
e74abb32 | 1363 | SymmetricDifference(self.0.clone()) |
c34b1796 AL |
1364 | } |
1365 | } | |
85aaf69f | 1366 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1367 | impl<'a, T: Ord> Iterator for SymmetricDifference<'a, T> { |
1368 | type Item = &'a T; | |
1369 | ||
1370 | fn next(&mut self) -> Option<&'a T> { | |
1371 | loop { | |
e74abb32 XL |
1372 | let (a_next, b_next) = self.0.nexts(); |
1373 | if a_next.and(b_next).is_none() { | |
1374 | return a_next.or(b_next); | |
1a4d82fc JJ |
1375 | } |
1376 | } | |
1377 | } | |
7453a54e SL |
1378 | |
1379 | fn size_hint(&self) -> (usize, Option<usize>) { | |
e74abb32 XL |
1380 | let (a_len, b_len) = self.0.lens(); |
1381 | // No checked_add, because even if a and b refer to the same set, | |
1382 | // and T is an empty type, the storage overhead of sets limits | |
1383 | // the number of elements to less than half the range of usize. | |
1384 | (0, Some(a_len + b_len)) | |
7453a54e | 1385 | } |
1a4d82fc JJ |
1386 | } |
1387 | ||
0531ce1d | 1388 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1389 | impl<T: Ord> FusedIterator for SymmetricDifference<'_, T> {} |
9e0c209e | 1390 | |
c30ab7b3 | 1391 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 XL |
1392 | impl<T> Clone for Intersection<'_, T> { |
1393 | fn clone(&self) -> Self { | |
92a42be0 | 1394 | Intersection { |
532ac7d7 XL |
1395 | inner: match &self.inner { |
1396 | IntersectionInner::Stitch { | |
e1599b0c XL |
1397 | a, |
1398 | b, | |
532ac7d7 | 1399 | } => IntersectionInner::Stitch { |
e1599b0c XL |
1400 | a: a.clone(), |
1401 | b: b.clone(), | |
532ac7d7 XL |
1402 | }, |
1403 | IntersectionInner::Search { | |
1404 | small_iter, | |
1405 | large_set, | |
1406 | } => IntersectionInner::Search { | |
1407 | small_iter: small_iter.clone(), | |
1408 | large_set, | |
1409 | }, | |
e74abb32 | 1410 | IntersectionInner::Answer(answer) => IntersectionInner::Answer(*answer), |
532ac7d7 | 1411 | }, |
92a42be0 | 1412 | } |
c34b1796 AL |
1413 | } |
1414 | } | |
85aaf69f | 1415 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1416 | impl<'a, T: Ord> Iterator for Intersection<'a, T> { |
1417 | type Item = &'a T; | |
1418 | ||
1419 | fn next(&mut self) -> Option<&'a T> { | |
532ac7d7 XL |
1420 | match &mut self.inner { |
1421 | IntersectionInner::Stitch { | |
e1599b0c XL |
1422 | a, |
1423 | b, | |
532ac7d7 | 1424 | } => { |
e1599b0c XL |
1425 | let mut a_next = a.next()?; |
1426 | let mut b_next = b.next()?; | |
532ac7d7 | 1427 | loop { |
e74abb32 | 1428 | match a_next.cmp(b_next) { |
e1599b0c XL |
1429 | Less => a_next = a.next()?, |
1430 | Greater => b_next = b.next()?, | |
1431 | Equal => return Some(a_next), | |
532ac7d7 | 1432 | } |
92a42be0 | 1433 | } |
1a4d82fc | 1434 | } |
532ac7d7 XL |
1435 | IntersectionInner::Search { |
1436 | small_iter, | |
1437 | large_set, | |
1438 | } => loop { | |
1439 | let small_next = small_iter.next()?; | |
1440 | if large_set.contains(&small_next) { | |
1441 | return Some(small_next); | |
1442 | } | |
1443 | }, | |
e74abb32 | 1444 | IntersectionInner::Answer(answer) => answer.take(), |
1a4d82fc JJ |
1445 | } |
1446 | } | |
7453a54e SL |
1447 | |
1448 | fn size_hint(&self) -> (usize, Option<usize>) { | |
e74abb32 XL |
1449 | match &self.inner { |
1450 | IntersectionInner::Stitch { a, b } => (0, Some(min(a.len(), b.len()))), | |
1451 | IntersectionInner::Search { small_iter, .. } => (0, Some(small_iter.len())), | |
1452 | IntersectionInner::Answer(None) => (0, Some(0)), | |
1453 | IntersectionInner::Answer(Some(_)) => (1, Some(1)), | |
1454 | } | |
7453a54e | 1455 | } |
1a4d82fc JJ |
1456 | } |
1457 | ||
0531ce1d | 1458 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1459 | impl<T: Ord> FusedIterator for Intersection<'_, T> {} |
9e0c209e | 1460 | |
c30ab7b3 | 1461 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 XL |
1462 | impl<T> Clone for Union<'_, T> { |
1463 | fn clone(&self) -> Self { | |
e74abb32 | 1464 | Union(self.0.clone()) |
c34b1796 AL |
1465 | } |
1466 | } | |
85aaf69f | 1467 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1468 | impl<'a, T: Ord> Iterator for Union<'a, T> { |
1469 | type Item = &'a T; | |
1470 | ||
1471 | fn next(&mut self) -> Option<&'a T> { | |
e74abb32 XL |
1472 | let (a_next, b_next) = self.0.nexts(); |
1473 | a_next.or(b_next) | |
1a4d82fc | 1474 | } |
7453a54e SL |
1475 | |
1476 | fn size_hint(&self) -> (usize, Option<usize>) { | |
e74abb32 XL |
1477 | let (a_len, b_len) = self.0.lens(); |
1478 | // No checked_add - see SymmetricDifference::size_hint. | |
7453a54e SL |
1479 | (max(a_len, b_len), Some(a_len + b_len)) |
1480 | } | |
1a4d82fc | 1481 | } |
9e0c209e | 1482 | |
0531ce1d | 1483 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1484 | impl<T: Ord> FusedIterator for Union<'_, T> {} |