| 1 |
use core::borrow::Borrow; |
| 2 |
use core::fmt::{self, Debug}; |
| 3 |
use core::iter::FromIterator; |
| 4 |
use core::ops::{BitAnd, BitOr, Range}; |
| 5 |
use core::prelude::v1::*; |
| 6 |
|
| 7 |
#[cfg(feature = "serde1")] |
| 8 |
use core::marker::PhantomData; |
| 9 |
#[cfg(feature = "serde1")] |
| 10 |
use serde::{ |
| 11 |
de::{Deserialize, Deserializer, SeqAccess, Visitor}, |
| 12 |
ser::{Serialize, Serializer}, |
| 13 |
}; |
| 14 |
|
| 15 |
use crate::RangeMap; |
| 16 |
|
| 17 |
/// Intersection iterator over two [`RangeSet`]. |
| 18 |
pub type Intersection<'a, T> = crate::operations::Intersection<'a, Range<T>, Iter<'a, T>>; |
| 19 |
|
| 20 |
/// Union iterator over two [`RangeSet`]. |
| 21 |
pub type Union<'a, T> = crate::operations::Union<'a, Range<T>, Iter<'a, T>>; |
| 22 |
|
| 23 |
#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Ord)] |
| 24 |
/// A set whose items are stored as (half-open) ranges bounded |
| 25 |
/// inclusively below and exclusively above `(start..end)`. |
| 26 |
/// |
| 27 |
/// See [`RangeMap`]'s documentation for more details. |
| 28 |
/// |
| 29 |
/// [`RangeMap`]: crate::RangeMap |
| 30 |
pub struct RangeSet<T> { |
| 31 |
rm: RangeMap<T, ()>, |
| 32 |
} |
| 33 |
|
| 34 |
impl<T> Default for RangeSet<T> { |
| 35 |
fn default() -> Self { |
| 36 |
Self { |
| 37 |
rm: RangeMap::default(), |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
#[cfg(feature = "quickcheck")] |
| 43 |
impl<K> quickcheck::Arbitrary for RangeSet<K> |
| 44 |
where |
| 45 |
K: quickcheck::Arbitrary + Ord, |
| 46 |
{ |
| 47 |
fn arbitrary(g: &mut quickcheck::Gen) -> Self { |
| 48 |
Self { |
| 49 |
rm: RangeMap::arbitrary(g), |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
impl<T> RangeSet<T> |
| 55 |
where |
| 56 |
T: Ord + Clone, |
| 57 |
{ |
| 58 |
/// Makes a new empty `RangeSet`. |
| 59 |
#[cfg(feature = "const_fn")] |
| 60 |
pub const fn new() -> Self { |
| 61 |
RangeSet { |
| 62 |
rm: RangeMap::new(), |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/// Makes a new empty `RangeSet`. |
| 67 |
#[cfg(not(feature = "const_fn"))] |
| 68 |
pub fn new() -> Self { |
| 69 |
RangeSet { |
| 70 |
rm: RangeMap::new(), |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/// Returns a reference to the range covering the given key, if any. |
| 75 |
pub fn get(&self, value: &T) -> Option<&Range<T>> { |
| 76 |
self.rm.get_key_value(value).map(|(range, _)| range) |
| 77 |
} |
| 78 |
|
| 79 |
/// Returns `true` if any range in the set covers the specified value. |
| 80 |
pub fn contains(&self, value: &T) -> bool { |
| 81 |
self.rm.contains_key(value) |
| 82 |
} |
| 83 |
|
| 84 |
/// Gets an ordered iterator over all ranges, |
| 85 |
/// ordered by range. |
| 86 |
pub fn iter(&self) -> Iter<'_, T> { |
| 87 |
Iter { |
| 88 |
inner: self.rm.iter(), |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/// Clears the set, removing all elements. |
| 93 |
pub fn clear(&mut self) { |
| 94 |
self.rm.clear(); |
| 95 |
} |
| 96 |
|
| 97 |
/// Returns the number of elements in the set. |
| 98 |
pub fn len(&self) -> usize { |
| 99 |
self.rm.len() |
| 100 |
} |
| 101 |
|
| 102 |
/// Returns true if the set contains no elements. |
| 103 |
pub fn is_empty(&self) -> bool { |
| 104 |
self.rm.is_empty() |
| 105 |
} |
| 106 |
|
| 107 |
/// Return an iterator over the intersection of two range sets. |
| 108 |
pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, T> { |
| 109 |
Intersection::new(self.iter(), other.iter()) |
| 110 |
} |
| 111 |
|
| 112 |
/// Return an iterator over the union of two range sets. |
| 113 |
pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, T> { |
| 114 |
Union::new(self.iter(), other.iter()) |
| 115 |
} |
| 116 |
|
| 117 |
/// Insert a range into the set. |
| 118 |
/// |
| 119 |
/// If the inserted range either overlaps or is immediately adjacent |
| 120 |
/// any existing range, then the ranges will be coalesced into |
| 121 |
/// a single contiguous range. |
| 122 |
/// |
| 123 |
/// # Panics |
| 124 |
/// |
| 125 |
/// Panics if range `start >= end`. |
| 126 |
pub fn insert(&mut self, range: Range<T>) { |
| 127 |
self.rm.insert(range, ()); |
| 128 |
} |
| 129 |
|
| 130 |
/// Removes a range from the set, if all or any of it was present. |
| 131 |
/// |
| 132 |
/// If the range to be removed _partially_ overlaps any ranges |
| 133 |
/// in the set, then those ranges will be contracted to no |
| 134 |
/// longer cover the removed range. |
| 135 |
/// |
| 136 |
/// # Panics |
| 137 |
/// |
| 138 |
/// Panics if range `start >= end`. |
| 139 |
pub fn remove(&mut self, range: Range<T>) { |
| 140 |
self.rm.remove(range); |
| 141 |
} |
| 142 |
|
| 143 |
/// Gets an iterator over all the maximally-sized ranges |
| 144 |
/// contained in `outer_range` that are not covered by |
| 145 |
/// any range stored in the set. |
| 146 |
/// |
| 147 |
/// If the start and end of the outer range are the same |
| 148 |
/// and it does not overlap any stored range, then a single |
| 149 |
/// empty gap will be returned. |
| 150 |
/// |
| 151 |
/// The iterator element type is `Range<T>`. |
| 152 |
pub fn gaps<'a>(&'a self, outer_range: &'a Range<T>) -> Gaps<'a, T> { |
| 153 |
Gaps { |
| 154 |
inner: self.rm.gaps(outer_range), |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/// Gets an iterator over all the stored ranges that are |
| 159 |
/// either partially or completely overlapped by the given range. |
| 160 |
/// |
| 161 |
/// The iterator element type is `&Range<T>`. |
| 162 |
pub fn overlapping<R: Borrow<Range<T>>>(&'_ self, range: R) -> Overlapping<'_, T, R> { |
| 163 |
Overlapping { |
| 164 |
inner: self.rm.overlapping(range), |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/// Returns `true` if any range in the set completely or partially |
| 169 |
/// overlaps the given range. |
| 170 |
pub fn overlaps(&self, range: &Range<T>) -> bool { |
| 171 |
self.overlapping(range).next().is_some() |
| 172 |
} |
| 173 |
|
| 174 |
/// Returns the first range in the set, if one exists. The range is the minimum range in this |
| 175 |
/// set. |
| 176 |
pub fn first(&self) -> Option<&Range<T>> { |
| 177 |
self.rm.first_range_value().map(|(range, _)| range) |
| 178 |
} |
| 179 |
|
| 180 |
/// Returns the last range in the set, if one exists. The range is the maximum range in this |
| 181 |
/// set. |
| 182 |
pub fn last(&self) -> Option<&Range<T>> { |
| 183 |
self.rm.last_range_value().map(|(range, _)| range) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/// An iterator over the ranges of a `RangeSet`. |
| 188 |
/// |
| 189 |
/// This `struct` is created by the [`iter`] method on [`RangeSet`]. See its |
| 190 |
/// documentation for more. |
| 191 |
/// |
| 192 |
/// [`iter`]: RangeSet::iter |
| 193 |
pub struct Iter<'a, T> { |
| 194 |
inner: super::map::Iter<'a, T, ()>, |
| 195 |
} |
| 196 |
|
| 197 |
impl<'a, T> Iterator for Iter<'a, T> { |
| 198 |
type Item = &'a Range<T>; |
| 199 |
|
| 200 |
fn next(&mut self) -> Option<Self::Item> { |
| 201 |
self.inner.next().map(|(range, _)| range) |
| 202 |
} |
| 203 |
|
| 204 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 205 |
self.inner.size_hint() |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
impl<'a, K> DoubleEndedIterator for Iter<'a, K> |
| 210 |
where |
| 211 |
K: 'a, |
| 212 |
{ |
| 213 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 214 |
self.inner.next_back().map(|(range, _)| range) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/// An owning iterator over the ranges of a `RangeSet`. |
| 219 |
/// |
| 220 |
/// This `struct` is created by the [`into_iter`] method on [`RangeSet`] |
| 221 |
/// (provided by the `IntoIterator` trait). See its documentation for more. |
| 222 |
/// |
| 223 |
/// [`into_iter`]: IntoIterator::into_iter |
| 224 |
pub struct IntoIter<T> { |
| 225 |
inner: super::map::IntoIter<T, ()>, |
| 226 |
} |
| 227 |
|
| 228 |
impl<T> IntoIterator for RangeSet<T> { |
| 229 |
type Item = Range<T>; |
| 230 |
type IntoIter = IntoIter<T>; |
| 231 |
fn into_iter(self) -> Self::IntoIter { |
| 232 |
IntoIter { |
| 233 |
inner: self.rm.into_iter(), |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
impl<T> Iterator for IntoIter<T> { |
| 239 |
type Item = Range<T>; |
| 240 |
fn next(&mut self) -> Option<Range<T>> { |
| 241 |
self.inner.next().map(|(range, _)| range) |
| 242 |
} |
| 243 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 244 |
self.inner.size_hint() |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
impl<K> DoubleEndedIterator for IntoIter<K> { |
| 249 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 250 |
self.inner.next_back().map(|(range, _)| range) |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
// We can't just derive this automatically, because that would |
| 255 |
// expose irrelevant (and private) implementation details. |
| 256 |
// Instead implement it in the same way that the underlying BTreeSet does. |
| 257 |
impl<T: Debug> Debug for RangeSet<T> |
| 258 |
where |
| 259 |
T: Ord + Clone, |
| 260 |
{ |
| 261 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 262 |
f.debug_set().entries(self.iter()).finish() |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
impl<T> FromIterator<Range<T>> for RangeSet<T> |
| 267 |
where |
| 268 |
T: Ord + Clone, |
| 269 |
{ |
| 270 |
fn from_iter<I: IntoIterator<Item = Range<T>>>(iter: I) -> Self { |
| 271 |
let mut range_set = RangeSet::new(); |
| 272 |
range_set.extend(iter); |
| 273 |
range_set |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
impl<T> Extend<Range<T>> for RangeSet<T> |
| 278 |
where |
| 279 |
T: Ord + Clone, |
| 280 |
{ |
| 281 |
fn extend<I: IntoIterator<Item = Range<T>>>(&mut self, iter: I) { |
| 282 |
iter.into_iter().for_each(move |range| { |
| 283 |
self.insert(range); |
| 284 |
}) |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
#[cfg(feature = "serde1")] |
| 289 |
impl<T> Serialize for RangeSet<T> |
| 290 |
where |
| 291 |
T: Ord + Clone + Serialize, |
| 292 |
{ |
| 293 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 294 |
where |
| 295 |
S: Serializer, |
| 296 |
{ |
| 297 |
use serde::ser::SerializeSeq; |
| 298 |
let mut seq = serializer.serialize_seq(Some(self.rm.btm.len()))?; |
| 299 |
for range in self.iter() { |
| 300 |
seq.serialize_element(&(&range.start, &range.end))?; |
| 301 |
} |
| 302 |
seq.end() |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
#[cfg(feature = "serde1")] |
| 307 |
impl<'de, T> Deserialize<'de> for RangeSet<T> |
| 308 |
where |
| 309 |
T: Ord + Clone + Deserialize<'de>, |
| 310 |
{ |
| 311 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 312 |
where |
| 313 |
D: Deserializer<'de>, |
| 314 |
{ |
| 315 |
deserializer.deserialize_seq(RangeSetVisitor::new()) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
#[cfg(feature = "serde1")] |
| 320 |
struct RangeSetVisitor<T> { |
| 321 |
marker: PhantomData<fn() -> RangeSet<T>>, |
| 322 |
} |
| 323 |
|
| 324 |
#[cfg(feature = "serde1")] |
| 325 |
impl<T> RangeSetVisitor<T> { |
| 326 |
fn new() -> Self { |
| 327 |
RangeSetVisitor { |
| 328 |
marker: PhantomData, |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
#[cfg(feature = "serde1")] |
| 334 |
impl<'de, T> Visitor<'de> for RangeSetVisitor<T> |
| 335 |
where |
| 336 |
T: Ord + Clone + Deserialize<'de>, |
| 337 |
{ |
| 338 |
type Value = RangeSet<T>; |
| 339 |
|
| 340 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 341 |
formatter.write_str("RangeSet") |
| 342 |
} |
| 343 |
|
| 344 |
fn visit_seq<A>(self, mut access: A) -> Result<Self::Value, A::Error> |
| 345 |
where |
| 346 |
A: SeqAccess<'de>, |
| 347 |
{ |
| 348 |
let mut range_set = RangeSet::new(); |
| 349 |
while let Some((start, end)) = access.next_element()? { |
| 350 |
range_set.insert(start..end); |
| 351 |
} |
| 352 |
Ok(range_set) |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
/// An iterator over all ranges not covered by a `RangeSet`. |
| 357 |
/// |
| 358 |
/// This `struct` is created by the [`gaps`] method on [`RangeSet`]. See its |
| 359 |
/// documentation for more. |
| 360 |
/// |
| 361 |
/// [`gaps`]: RangeSet::gaps |
| 362 |
pub struct Gaps<'a, T> { |
| 363 |
inner: crate::map::Gaps<'a, T, ()>, |
| 364 |
} |
| 365 |
|
| 366 |
// `Gaps` is always fused. (See definition of `next` below.) |
| 367 |
impl<'a, T> core::iter::FusedIterator for Gaps<'a, T> where T: Ord + Clone {} |
| 368 |
|
| 369 |
impl<'a, T> Iterator for Gaps<'a, T> |
| 370 |
where |
| 371 |
T: Ord + Clone, |
| 372 |
{ |
| 373 |
type Item = Range<T>; |
| 374 |
|
| 375 |
fn next(&mut self) -> Option<Self::Item> { |
| 376 |
self.inner.next() |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
/// An iterator over all stored ranges partially or completely |
| 381 |
/// overlapped by a given range. |
| 382 |
/// |
| 383 |
/// This `struct` is created by the [`overlapping`] method on [`RangeSet`]. See its |
| 384 |
/// documentation for more. |
| 385 |
/// |
| 386 |
/// [`overlapping`]: RangeSet::overlapping |
| 387 |
pub struct Overlapping<'a, T, R: Borrow<Range<T>> = &'a Range<T>> { |
| 388 |
inner: crate::map::Overlapping<'a, T, (), R>, |
| 389 |
} |
| 390 |
|
| 391 |
// `Overlapping` is always fused. (See definition of `next` below.) |
| 392 |
impl<'a, T, R: Borrow<Range<T>>> core::iter::FusedIterator for Overlapping<'a, T, R> where |
| 393 |
T: Ord + Clone |
| 394 |
{ |
| 395 |
} |
| 396 |
|
| 397 |
impl<'a, T, R: Borrow<Range<T>>> Iterator for Overlapping<'a, T, R> |
| 398 |
where |
| 399 |
T: Ord + Clone, |
| 400 |
{ |
| 401 |
type Item = &'a Range<T>; |
| 402 |
|
| 403 |
fn next(&mut self) -> Option<Self::Item> { |
| 404 |
self.inner.next().map(|(k, _v)| k) |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
impl<'a, T, R: Borrow<Range<T>>> DoubleEndedIterator for Overlapping<'a, T, R> |
| 409 |
where |
| 410 |
T: Ord + Clone, |
| 411 |
{ |
| 412 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 413 |
self.inner.next_back().map(|(k, _v)| k) |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
impl<T: Ord + Clone> BitAnd for &RangeSet<T> { |
| 418 |
type Output = RangeSet<T>; |
| 419 |
|
| 420 |
fn bitand(self, other: Self) -> Self::Output { |
| 421 |
self.intersection(other).collect() |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
impl<T: Ord + Clone> BitOr for &RangeSet<T> { |
| 426 |
type Output = RangeSet<T>; |
| 427 |
|
| 428 |
fn bitor(self, other: Self) -> Self::Output { |
| 429 |
self.union(other).collect() |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
impl<T: Ord + Clone, const N: usize> From<[Range<T>; N]> for RangeSet<T> { |
| 434 |
fn from(value: [Range<T>; N]) -> Self { |
| 435 |
let mut set = Self::new(); |
| 436 |
for value in IntoIterator::into_iter(value) { |
| 437 |
set.insert(value); |
| 438 |
} |
| 439 |
set |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
/// Create a [`RangeSet`] from a list of ranges. |
| 444 |
/// |
| 445 |
/// # Example |
| 446 |
/// |
| 447 |
/// ```rust |
| 448 |
/// # use rangemap::range_set; |
| 449 |
/// let set = range_set![0..100, 200..300, 400..500]; |
| 450 |
/// ``` |
| 451 |
#[macro_export] |
| 452 |
macro_rules! range_set { |
| 453 |
($($range:expr),* $(,)?) => {{ |
| 454 |
$crate::RangeSet::from([$($range),*]) |
| 455 |
}}; |
| 456 |
} |
| 457 |
|
| 458 |
#[cfg(test)] |
| 459 |
mod tests { |
| 460 |
use super::*; |
| 461 |
use alloc as std; |
| 462 |
use alloc::{format, vec, vec::Vec}; |
| 463 |
use proptest::prelude::*; |
| 464 |
use test_strategy::proptest; |
| 465 |
|
| 466 |
impl<T> Arbitrary for RangeSet<T> |
| 467 |
where |
| 468 |
T: Ord + Clone + Debug + Arbitrary + 'static, |
| 469 |
{ |
| 470 |
type Parameters = (); |
| 471 |
type Strategy = BoxedStrategy<Self>; |
| 472 |
|
| 473 |
fn arbitrary_with(_parameters: Self::Parameters) -> Self::Strategy { |
| 474 |
any::<Vec<Range<T>>>() |
| 475 |
.prop_map(|ranges| { |
| 476 |
ranges |
| 477 |
.into_iter() |
| 478 |
.filter(|range| range.start != range.end) |
| 479 |
.collect::<RangeSet<T>>() |
| 480 |
}) |
| 481 |
.boxed() |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
#[proptest] |
| 486 |
fn test_first(set: RangeSet<u64>) { |
| 487 |
assert_eq!(set.first(), set.iter().min_by_key(|range| range.start)); |
| 488 |
} |
| 489 |
|
| 490 |
#[proptest] |
| 491 |
#[allow(clippy::len_zero)] |
| 492 |
fn test_len(mut map: RangeSet<u64>) { |
| 493 |
assert_eq!(map.len(), map.iter().count()); |
| 494 |
assert_eq!(map.is_empty(), map.len() == 0); |
| 495 |
map.clear(); |
| 496 |
assert_eq!(map.len(), 0); |
| 497 |
assert!(map.is_empty()); |
| 498 |
assert_eq!(map.iter().count(), 0); |
| 499 |
} |
| 500 |
|
| 501 |
#[proptest] |
| 502 |
fn test_last(set: RangeSet<u64>) { |
| 503 |
assert_eq!(set.last(), set.iter().max_by_key(|range| range.end)); |
| 504 |
} |
| 505 |
|
| 506 |
#[proptest] |
| 507 |
fn test_iter_reversible(set: RangeSet<u64>) { |
| 508 |
let forward: Vec<_> = set.iter().collect(); |
| 509 |
let mut backward: Vec<_> = set.iter().rev().collect(); |
| 510 |
backward.reverse(); |
| 511 |
assert_eq!(forward, backward); |
| 512 |
} |
| 513 |
|
| 514 |
#[proptest] |
| 515 |
fn test_into_iter_reversible(set: RangeSet<u64>) { |
| 516 |
let forward: Vec<_> = set.clone().into_iter().collect(); |
| 517 |
let mut backward: Vec<_> = set.into_iter().rev().collect(); |
| 518 |
backward.reverse(); |
| 519 |
assert_eq!(forward, backward); |
| 520 |
} |
| 521 |
|
| 522 |
#[proptest] |
| 523 |
fn test_overlapping_reversible(set: RangeSet<u64>, range: Range<u64>) { |
| 524 |
let forward: Vec<_> = set.overlapping(&range).collect(); |
| 525 |
let mut backward: Vec<_> = set.overlapping(&range).rev().collect(); |
| 526 |
backward.reverse(); |
| 527 |
assert_eq!(forward, backward); |
| 528 |
} |
| 529 |
|
| 530 |
// neccessary due to assertion on empty ranges |
| 531 |
fn filter_ranges<T: Ord>(ranges: Vec<Range<T>>) -> Vec<Range<T>> { |
| 532 |
ranges |
| 533 |
.into_iter() |
| 534 |
.filter(|range| range.start != range.end) |
| 535 |
.collect() |
| 536 |
} |
| 537 |
|
| 538 |
#[proptest] |
| 539 |
fn test_arbitrary_set_u8(ranges: Vec<Range<u8>>) { |
| 540 |
let ranges = filter_ranges(ranges); |
| 541 |
let set = ranges.iter().fold(RangeSet::new(), |mut set, range| { |
| 542 |
set.insert(range.clone()); |
| 543 |
set |
| 544 |
}); |
| 545 |
|
| 546 |
for value in 0..u8::MAX { |
| 547 |
assert_eq!( |
| 548 |
set.contains(&value), |
| 549 |
ranges.iter().any(|range| range.contains(&value)) |
| 550 |
); |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
#[proptest] |
| 555 |
#[allow(deprecated)] |
| 556 |
fn test_hash(left: RangeSet<u64>, right: RangeSet<u64>) { |
| 557 |
use core::hash::{Hash, Hasher, SipHasher}; |
| 558 |
|
| 559 |
let hash = |set: &RangeSet<_>| { |
| 560 |
let mut hasher = SipHasher::new(); |
| 561 |
set.hash(&mut hasher); |
| 562 |
hasher.finish() |
| 563 |
}; |
| 564 |
|
| 565 |
if left == right { |
| 566 |
assert!( |
| 567 |
hash(&left) == hash(&right), |
| 568 |
"if two values are equal, their hash must be equal" |
| 569 |
); |
| 570 |
} |
| 571 |
|
| 572 |
// if the hashes are equal the values might not be the same (collision) |
| 573 |
if hash(&left) != hash(&right) { |
| 574 |
assert!( |
| 575 |
left != right, |
| 576 |
"if two value's hashes are not equal, they must not be equal" |
| 577 |
); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
#[proptest] |
| 582 |
fn test_ord(left: RangeSet<u64>, right: RangeSet<u64>) { |
| 583 |
assert_eq!( |
| 584 |
left == right, |
| 585 |
left.cmp(&right).is_eq(), |
| 586 |
"ordering and equality must match" |
| 587 |
); |
| 588 |
assert_eq!( |
| 589 |
left.cmp(&right), |
| 590 |
left.partial_cmp(&right).unwrap(), |
| 591 |
"ordering is total for ordered parameters" |
| 592 |
); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn test_from_array() { |
| 597 |
let mut set = RangeSet::new(); |
| 598 |
set.insert(0..100); |
| 599 |
set.insert(200..300); |
| 600 |
assert_eq!(set, RangeSet::from([0..100, 200..300])); |
| 601 |
} |
| 602 |
|
| 603 |
#[test] |
| 604 |
fn test_macro() { |
| 605 |
assert_eq!(range_set![], RangeSet::<i64>::new()); |
| 606 |
assert_eq!( |
| 607 |
range_set![0..100, 200..300, 400..500], |
| 608 |
[0..100, 200..300, 400..500].iter().cloned().collect(), |
| 609 |
); |
| 610 |
} |
| 611 |
|
| 612 |
#[proptest] |
| 613 |
fn test_union_overlaps_u8(left: RangeSet<u8>, right: RangeSet<u8>) { |
| 614 |
let mut union = RangeSet::new(); |
| 615 |
for range in left.union(&right) { |
| 616 |
// there should not be any overlaps in the ranges returned by the union |
| 617 |
assert!(union.overlapping(&range).next().is_none()); |
| 618 |
union.insert(range); |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
#[proptest] |
| 623 |
fn test_union_contains_u8(left: RangeSet<u8>, right: RangeSet<u8>) { |
| 624 |
let union = (&left) | (&right); |
| 625 |
|
| 626 |
// value should be in the union if and only if it is in either set |
| 627 |
for value in 0..u8::MAX { |
| 628 |
assert_eq!( |
| 629 |
union.contains(&value), |
| 630 |
left.contains(&value) || right.contains(&value) |
| 631 |
); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
#[proptest] |
| 636 |
fn test_intersection_contains_u8(left: RangeSet<u8>, right: RangeSet<u8>) { |
| 637 |
let intersection = (&left) & (&right); |
| 638 |
|
| 639 |
// value should be in the intersection if and only if it is in both sets |
| 640 |
for value in 0..u8::MAX { |
| 641 |
assert_eq!( |
| 642 |
intersection.contains(&value), |
| 643 |
left.contains(&value) && right.contains(&value) |
| 644 |
); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
#[proptest] |
| 649 |
fn test_intersection_overlaps_u8(left: RangeSet<u8>, right: RangeSet<u8>) { |
| 650 |
let mut union = RangeSet::new(); |
| 651 |
for range in left.intersection(&right) { |
| 652 |
// there should not be any overlaps in the ranges returned by the |
| 653 |
// intersection |
| 654 |
assert!(union.overlapping(&range).next().is_none()); |
| 655 |
union.insert(range); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
trait RangeSetExt<T> { |
| 660 |
fn to_vec(&self) -> Vec<Range<T>>; |
| 661 |
} |
| 662 |
|
| 663 |
impl<T> RangeSetExt<T> for RangeSet<T> |
| 664 |
where |
| 665 |
T: Ord + Clone, |
| 666 |
{ |
| 667 |
fn to_vec(&self) -> Vec<Range<T>> { |
| 668 |
self.iter().cloned().collect() |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
#[test] |
| 673 |
fn empty_set_is_empty() { |
| 674 |
let range_set: RangeSet<u32> = RangeSet::new(); |
| 675 |
assert_eq!(range_set.to_vec(), vec![]); |
| 676 |
} |
| 677 |
|
| 678 |
#[test] |
| 679 |
fn insert_into_empty_map() { |
| 680 |
let mut range_set: RangeSet<u32> = RangeSet::new(); |
| 681 |
range_set.insert(0..50); |
| 682 |
assert_eq!(range_set.to_vec(), vec![0..50]); |
| 683 |
} |
| 684 |
|
| 685 |
#[test] |
| 686 |
fn remove_partially_overlapping() { |
| 687 |
let mut range_set: RangeSet<u32> = RangeSet::new(); |
| 688 |
range_set.insert(0..50); |
| 689 |
range_set.remove(25..75); |
| 690 |
assert_eq!(range_set.to_vec(), vec![0..25]); |
| 691 |
} |
| 692 |
|
| 693 |
#[test] |
| 694 |
fn gaps_between_items_floating_inside_outer_range() { |
| 695 |
let mut range_set: RangeSet<u32> = RangeSet::new(); |
| 696 |
// 0 1 2 3 4 5 6 7 8 9 |
| 697 |
// ◌ ◌ ◌ ◌ ◌ ●-◌ ◌ ◌ ◌ |
| 698 |
range_set.insert(5..6); |
| 699 |
// 0 1 2 3 4 5 6 7 8 9 |
| 700 |
// ◌ ◌ ◌ ●-◌ ◌ ◌ ◌ ◌ ◌ |
| 701 |
range_set.insert(3..4); |
| 702 |
// 0 1 2 3 4 5 6 7 8 9 |
| 703 |
// ◌ ◆-------------◇ ◌ |
| 704 |
let outer_range = 1..8; |
| 705 |
let mut gaps = range_set.gaps(&outer_range); |
| 706 |
// Should yield gaps at start, between items, |
| 707 |
// and at end. |
| 708 |
assert_eq!(gaps.next(), Some(1..3)); |
| 709 |
assert_eq!(gaps.next(), Some(4..5)); |
| 710 |
assert_eq!(gaps.next(), Some(6..8)); |
| 711 |
assert_eq!(gaps.next(), None); |
| 712 |
// Gaps iterator should be fused. |
| 713 |
assert_eq!(gaps.next(), None); |
| 714 |
assert_eq!(gaps.next(), None); |
| 715 |
} |
| 716 |
|
| 717 |
#[test] |
| 718 |
fn overlapping_partial_edges_complete_middle() { |
| 719 |
let mut range_map: RangeSet<u32> = RangeSet::new(); |
| 720 |
|
| 721 |
// 0 1 2 3 4 5 6 7 8 9 |
| 722 |
// ●---◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 723 |
range_map.insert(0..2); |
| 724 |
// 0 1 2 3 4 5 6 7 8 9 |
| 725 |
// ◌ ◌ ◌ ●-◌ ◌ ◌ ◌ ◌ ◌ |
| 726 |
range_map.insert(3..4); |
| 727 |
// 0 1 2 3 4 5 6 7 8 9 |
| 728 |
// ◌ ◌ ◌ ◌ ◌ ●---◌ ◌ ◌ |
| 729 |
range_map.insert(5..7); |
| 730 |
|
| 731 |
// 0 1 2 3 4 5 6 7 8 9 |
| 732 |
// ◌ ◆---------◇ ◌ ◌ ◌ |
| 733 |
let query_range = 1..6; |
| 734 |
|
| 735 |
let mut overlapping = range_map.overlapping(&query_range); |
| 736 |
|
| 737 |
// Should yield partially overlapped range at start. |
| 738 |
assert_eq!(overlapping.next(), Some(&(0..2))); |
| 739 |
// Should yield completely overlapped range in middle. |
| 740 |
assert_eq!(overlapping.next(), Some(&(3..4))); |
| 741 |
// Should yield partially overlapped range at end. |
| 742 |
assert_eq!(overlapping.next(), Some(&(5..7))); |
| 743 |
// Gaps iterator should be fused. |
| 744 |
assert_eq!(overlapping.next(), None); |
| 745 |
assert_eq!(overlapping.next(), None); |
| 746 |
} |
| 747 |
|
| 748 |
/// |
| 749 |
/// impl Debug |
| 750 |
/// |
| 751 |
|
| 752 |
#[test] |
| 753 |
fn set_debug_repr_looks_right() { |
| 754 |
let mut set: RangeSet<u32> = RangeSet::new(); |
| 755 |
|
| 756 |
// Empty |
| 757 |
assert_eq!(format!("{:?}", set), "{}"); |
| 758 |
|
| 759 |
// One entry |
| 760 |
set.insert(2..5); |
| 761 |
assert_eq!(format!("{:?}", set), "{2..5}"); |
| 762 |
|
| 763 |
// Many entries |
| 764 |
set.insert(7..8); |
| 765 |
set.insert(10..11); |
| 766 |
assert_eq!(format!("{:?}", set), "{2..5, 7..8, 10..11}"); |
| 767 |
} |
| 768 |
|
| 769 |
// impl Default where T: ?Default |
| 770 |
|
| 771 |
#[test] |
| 772 |
fn always_default() { |
| 773 |
struct NoDefault; |
| 774 |
RangeSet::<NoDefault>::default(); |
| 775 |
} |
| 776 |
|
| 777 |
// impl Serialize |
| 778 |
|
| 779 |
#[cfg(feature = "serde1")] |
| 780 |
#[test] |
| 781 |
fn serialization() { |
| 782 |
let mut range_set: RangeSet<u32> = RangeSet::new(); |
| 783 |
// 0 1 2 3 4 5 6 7 8 9 |
| 784 |
// ◌ ◆---◇ ◌ ◌ ◌ ◌ ◌ ◌ |
| 785 |
range_set.insert(1..3); |
| 786 |
// 0 1 2 3 4 5 6 7 8 9 |
| 787 |
// ◌ ◌ ◌ ◌ ◌ ◆---◇ ◌ ◌ |
| 788 |
range_set.insert(5..7); |
| 789 |
let output = serde_json::to_string(&range_set).expect("Failed to serialize"); |
| 790 |
assert_eq!(output, "[[1,3],[5,7]]"); |
| 791 |
} |
| 792 |
|
| 793 |
// impl Deserialize |
| 794 |
|
| 795 |
#[cfg(feature = "serde1")] |
| 796 |
#[test] |
| 797 |
fn deserialization() { |
| 798 |
let input = "[[1,3],[5,7]]"; |
| 799 |
let range_set: RangeSet<u32> = serde_json::from_str(input).expect("Failed to deserialize"); |
| 800 |
let reserialized = serde_json::to_string(&range_set).expect("Failed to re-serialize"); |
| 801 |
assert_eq!(reserialized, input); |
| 802 |
} |
| 803 |
|
| 804 |
// const fn |
| 805 |
|
| 806 |
#[cfg(feature = "const_fn")] |
| 807 |
const _SET: RangeSet<u32> = RangeSet::new(); |
| 808 |
|
| 809 |
#[cfg(feature = "quickcheck")] |
| 810 |
quickcheck::quickcheck! { |
| 811 |
fn prop(xs: RangeSet<usize>) -> bool { |
| 812 |
xs == xs |
| 813 |
} |
| 814 |
} |
| 815 |
} |