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