| 1 |
use super::range_wrapper::RangeInclusiveStartWrapper; |
| 2 |
use crate::range_wrapper::RangeInclusiveEndWrapper; |
| 3 |
use crate::std_ext::*; |
| 4 |
use alloc::collections::BTreeMap; |
| 5 |
use core::borrow::Borrow; |
| 6 |
use core::cmp::Ordering; |
| 7 |
use core::fmt::{self, Debug}; |
| 8 |
use core::hash::Hash; |
| 9 |
use core::iter::{DoubleEndedIterator, FromIterator}; |
| 10 |
use core::marker::PhantomData; |
| 11 |
use core::ops::{RangeFrom, RangeInclusive}; |
| 12 |
use core::prelude::v1::*; |
| 13 |
|
| 14 |
#[cfg(feature = "serde1")] |
| 15 |
use serde::{ |
| 16 |
de::{Deserialize, Deserializer, SeqAccess, Visitor}, |
| 17 |
ser::{Serialize, Serializer}, |
| 18 |
}; |
| 19 |
|
| 20 |
/// A map whose keys are stored as ranges bounded |
| 21 |
/// inclusively below and above `(start..=end)`. |
| 22 |
/// |
| 23 |
/// Contiguous and overlapping ranges that map to the same value |
| 24 |
/// are coalesced into a single range. |
| 25 |
/// |
| 26 |
/// Successor and predecessor functions must be provided for |
| 27 |
/// the key type `K`, so that we can detect adjacent but non-overlapping |
| 28 |
/// (closed) ranges. (This is not a problem for half-open ranges, |
| 29 |
/// because adjacent ranges can be detected using equality of range ends alone.) |
| 30 |
/// |
| 31 |
/// You can provide these functions either by implementing the |
| 32 |
/// [`StepLite`] trait for your key type `K`, or, |
| 33 |
/// if this is impossible because of Rust's "orphan rules", |
| 34 |
/// you can provide equivalent free functions using the `StepFnsT` type parameter. |
| 35 |
/// [`StepLite`] is implemented for all standard integer types, |
| 36 |
/// but not for any third party crate types. |
| 37 |
#[derive(Clone)] |
| 38 |
pub struct RangeInclusiveMap<K, V, StepFnsT = K> { |
| 39 |
// Wrap ranges so that they are `Ord`. |
| 40 |
// See `range_wrapper.rs` for explanation. |
| 41 |
pub(crate) btm: BTreeMap<RangeInclusiveStartWrapper<K>, V>, |
| 42 |
_phantom: PhantomData<StepFnsT>, |
| 43 |
} |
| 44 |
|
| 45 |
impl<K, V, StepFnsT> Default for RangeInclusiveMap<K, V, StepFnsT> { |
| 46 |
fn default() -> Self { |
| 47 |
Self { |
| 48 |
btm: BTreeMap::default(), |
| 49 |
_phantom: PhantomData, |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
impl<K, V, StepFnsT> Hash for RangeInclusiveMap<K, V, StepFnsT> |
| 55 |
where |
| 56 |
K: Hash, |
| 57 |
V: Hash, |
| 58 |
{ |
| 59 |
fn hash<H: core::hash::Hasher>(&self, state: &mut H) { |
| 60 |
state.write_usize(self.btm.len()); |
| 61 |
for elt in self.iter() { |
| 62 |
elt.hash(state); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl<K, V, StepFnsT> PartialEq for RangeInclusiveMap<K, V, StepFnsT> |
| 68 |
where |
| 69 |
K: PartialEq, |
| 70 |
V: PartialEq, |
| 71 |
{ |
| 72 |
fn eq(&self, other: &RangeInclusiveMap<K, V, StepFnsT>) -> bool { |
| 73 |
self.iter().eq(other.iter()) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl<K, V, StepFnsT> Eq for RangeInclusiveMap<K, V, StepFnsT> |
| 78 |
where |
| 79 |
K: Eq, |
| 80 |
V: Eq, |
| 81 |
{ |
| 82 |
} |
| 83 |
|
| 84 |
impl<K, V, StepFnsT> PartialOrd for RangeInclusiveMap<K, V, StepFnsT> |
| 85 |
where |
| 86 |
K: PartialOrd, |
| 87 |
V: PartialOrd, |
| 88 |
{ |
| 89 |
#[inline] |
| 90 |
fn partial_cmp(&self, other: &RangeInclusiveMap<K, V, StepFnsT>) -> Option<Ordering> { |
| 91 |
self.expanded_iter().partial_cmp(other.expanded_iter()) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl<K, V, StepFnsT> Ord for RangeInclusiveMap<K, V, StepFnsT> |
| 96 |
where |
| 97 |
K: Ord, |
| 98 |
V: Ord, |
| 99 |
{ |
| 100 |
#[inline] |
| 101 |
fn cmp(&self, other: &RangeInclusiveMap<K, V, StepFnsT>) -> Ordering { |
| 102 |
self.expanded_iter().cmp(other.expanded_iter()) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
#[cfg(feature = "quickcheck")] |
| 107 |
impl<K, V> quickcheck::Arbitrary for RangeInclusiveMap<K, V> |
| 108 |
where |
| 109 |
K: quickcheck::Arbitrary + Ord + StepLite, |
| 110 |
V: quickcheck::Arbitrary + PartialEq, |
| 111 |
{ |
| 112 |
fn arbitrary(g: &mut quickcheck::Gen) -> Self { |
| 113 |
// REVISIT: allocation could be avoided if Gen::gen_size were public (https://github.com/BurntSushi/quickcheck/issues/326#issue-2653601170) |
| 114 |
<alloc::vec::Vec<(RangeInclusive<_>, _)>>::arbitrary(g) |
| 115 |
.into_iter() |
| 116 |
.filter(|(range, _)| !range.is_empty()) |
| 117 |
.collect() |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
impl<K, V, StepFnsT> RangeInclusiveMap<K, V, StepFnsT> { |
| 122 |
/// Gets an iterator over all pairs of key range and value, |
| 123 |
/// ordered by key range. |
| 124 |
/// |
| 125 |
/// The iterator element type is `(&'a RangeInclusive<K>, &'a V)`. |
| 126 |
pub fn iter(&self) -> Iter<'_, K, V> { |
| 127 |
Iter { |
| 128 |
inner: self.btm.iter(), |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// Iterate pairs of (range start, range end, value) for comparisons. |
| 133 |
fn expanded_iter(&self) -> impl Iterator<Item = (&K, &K, &V)> { |
| 134 |
self.btm.iter().map(|(k, v)| (k.start(), k.end(), v)) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
impl<K, V> RangeInclusiveMap<K, V, K> |
| 139 |
where |
| 140 |
K: Ord + Clone + StepLite, |
| 141 |
V: PartialEq + Clone, |
| 142 |
{ |
| 143 |
/// Makes a new empty `RangeInclusiveMap`. |
| 144 |
#[cfg(feature = "const_fn")] |
| 145 |
pub const fn new() -> Self { |
| 146 |
Self::new_with_step_fns() |
| 147 |
} |
| 148 |
|
| 149 |
/// Makes a new empty `RangeInclusiveMap`. |
| 150 |
#[cfg(not(feature = "const_fn"))] |
| 151 |
pub fn new() -> Self { |
| 152 |
Self::new_with_step_fns() |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl<K, V, StepFnsT> RangeInclusiveMap<K, V, StepFnsT> |
| 157 |
where |
| 158 |
K: Ord + Clone, |
| 159 |
V: PartialEq + Clone, |
| 160 |
StepFnsT: StepFns<K>, |
| 161 |
{ |
| 162 |
/// Makes a new empty `RangeInclusiveMap`, specifying successor and |
| 163 |
/// predecessor functions defined separately from `K` itself. |
| 164 |
/// |
| 165 |
/// This is useful as a workaround for Rust's "orphan rules", |
| 166 |
/// which prevent you from implementing `StepLite` for `K` if `K` |
| 167 |
/// is a foreign type. |
| 168 |
/// |
| 169 |
/// **NOTE:** This will likely be deprecated and then eventually |
| 170 |
/// removed once the standard library's [Step](core::iter::Step) |
| 171 |
/// trait is stabilised, as most crates will then likely implement [Step](core::iter::Step) |
| 172 |
/// for their types where appropriate. |
| 173 |
/// |
| 174 |
/// See [this issue](https://github.com/rust-lang/rust/issues/42168) |
| 175 |
/// for details about that stabilization process. |
| 176 |
#[cfg(not(feature = "const_fn"))] |
| 177 |
pub fn new_with_step_fns() -> Self { |
| 178 |
Self { |
| 179 |
btm: BTreeMap::new(), |
| 180 |
_phantom: PhantomData, |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
#[cfg(feature = "const_fn")] |
| 185 |
pub const fn new_with_step_fns() -> Self { |
| 186 |
Self { |
| 187 |
btm: BTreeMap::new(), |
| 188 |
_phantom: PhantomData, |
| 189 |
} |
| 190 |
} |
| 191 |
/// Returns a reference to the value corresponding to the given key, |
| 192 |
/// if the key is covered by any range in the map. |
| 193 |
pub fn get(&self, key: &K) -> Option<&V> { |
| 194 |
self.get_key_value(key).map(|(_range, value)| value) |
| 195 |
} |
| 196 |
|
| 197 |
/// Returns the range-value pair (as a pair of references) corresponding |
| 198 |
/// to the given key, if the key is covered by any range in the map. |
| 199 |
pub fn get_key_value(&self, key: &K) -> Option<(&RangeInclusive<K>, &V)> { |
| 200 |
use core::ops::Bound; |
| 201 |
|
| 202 |
// The only stored range that could contain the given key is the |
| 203 |
// last stored range whose start is less than or equal to this key. |
| 204 |
let key_as_start = RangeInclusiveStartWrapper::new(key.clone()..=key.clone()); |
| 205 |
self.btm |
| 206 |
.range((Bound::Unbounded, Bound::Included(key_as_start))) |
| 207 |
.next_back() |
| 208 |
.filter(|(range_start_wrapper, _value)| { |
| 209 |
// Does the only candidate range contain |
| 210 |
// the requested key? |
| 211 |
range_start_wrapper.contains(key) |
| 212 |
}) |
| 213 |
.map(|(range_start_wrapper, value)| (&range_start_wrapper.range, value)) |
| 214 |
} |
| 215 |
|
| 216 |
/// Returns `true` if any range in the map covers the specified key. |
| 217 |
pub fn contains_key(&self, key: &K) -> bool { |
| 218 |
self.get(key).is_some() |
| 219 |
} |
| 220 |
|
| 221 |
/// Clears the map, removing all elements. |
| 222 |
pub fn clear(&mut self) { |
| 223 |
self.btm.clear(); |
| 224 |
} |
| 225 |
|
| 226 |
/// Returns the number of elements in the map. |
| 227 |
pub fn len(&self) -> usize { |
| 228 |
self.btm.len() |
| 229 |
} |
| 230 |
|
| 231 |
/// Returns true if the map contains no elements. |
| 232 |
pub fn is_empty(&self) -> bool { |
| 233 |
self.btm.is_empty() |
| 234 |
} |
| 235 |
|
| 236 |
/// Insert a pair of key range and value into the map. |
| 237 |
/// |
| 238 |
/// If the inserted range partially or completely overlaps any |
| 239 |
/// existing range in the map, then the existing range (or ranges) will be |
| 240 |
/// partially or completely replaced by the inserted range. |
| 241 |
/// |
| 242 |
/// If the inserted range either overlaps or is immediately adjacent |
| 243 |
/// any existing range _mapping to the same value_, then the ranges |
| 244 |
/// will be coalesced into a single contiguous range. |
| 245 |
/// |
| 246 |
/// # Panics |
| 247 |
/// |
| 248 |
/// Panics if range `start > end`. |
| 249 |
pub fn insert(&mut self, range: RangeInclusive<K>, value: V) { |
| 250 |
use core::ops::Bound; |
| 251 |
|
| 252 |
// Backwards ranges don't make sense. |
| 253 |
// `RangeInclusive` doesn't enforce this, |
| 254 |
// and we don't want weird explosions further down |
| 255 |
// if someone gives us such a range. |
| 256 |
assert!( |
| 257 |
range.start() <= range.end(), |
| 258 |
"Range start can not be after range end" |
| 259 |
); |
| 260 |
|
| 261 |
// Wrap up the given range so that we can "borrow" |
| 262 |
// it as a wrapper reference to either its start or end. |
| 263 |
// See `range_wrapper.rs` for explanation of these hacks. |
| 264 |
let mut new_range_start_wrapper: RangeInclusiveStartWrapper<K> = |
| 265 |
RangeInclusiveStartWrapper::new(range); |
| 266 |
let new_value = value; |
| 267 |
|
| 268 |
// Is there a stored range either overlapping the start of |
| 269 |
// the range to insert or immediately preceding it? |
| 270 |
// |
| 271 |
// If there is any such stored range, it will be the last |
| 272 |
// whose start is less than or equal to _one less than_ |
| 273 |
// the start of the range to insert, or the one before that |
| 274 |
// if both of the above cases exist. |
| 275 |
let mut candidates = self |
| 276 |
.btm |
| 277 |
.range::<RangeInclusiveStartWrapper<K>, ( |
| 278 |
Bound<&RangeInclusiveStartWrapper<K>>, |
| 279 |
Bound<&RangeInclusiveStartWrapper<K>>, |
| 280 |
)>((Bound::Unbounded, Bound::Included(&new_range_start_wrapper))) |
| 281 |
.rev() |
| 282 |
.take(2) |
| 283 |
.filter(|(stored_range_start_wrapper, _stored_value)| { |
| 284 |
// Does the candidate range either overlap |
| 285 |
// or immediately precede the range to insert? |
| 286 |
// (Remember that it might actually cover the _whole_ |
| 287 |
// range to insert and then some.) |
| 288 |
stored_range_start_wrapper |
| 289 |
.touches::<StepFnsT>(&new_range_start_wrapper.end_wrapper.range) |
| 290 |
}); |
| 291 |
if let Some(mut candidate) = candidates.next() { |
| 292 |
// Or the one before it if both cases described above exist. |
| 293 |
if let Some(another_candidate) = candidates.next() { |
| 294 |
candidate = another_candidate; |
| 295 |
} |
| 296 |
let (stored_range_start_wrapper, stored_value) = |
| 297 |
(candidate.0.clone(), candidate.1.clone()); |
| 298 |
self.adjust_touching_ranges_for_insert( |
| 299 |
stored_range_start_wrapper, |
| 300 |
stored_value, |
| 301 |
&mut new_range_start_wrapper.end_wrapper.range, |
| 302 |
&new_value, |
| 303 |
); |
| 304 |
} |
| 305 |
|
| 306 |
// Are there any stored ranges whose heads overlap or immediately |
| 307 |
// follow the range to insert? |
| 308 |
// |
| 309 |
// If there are any such stored ranges (that weren't already caught above), |
| 310 |
// their starts will fall somewhere after the start of the range to insert, |
| 311 |
// and on, before, or _immediately after_ its end. To handle that last case |
| 312 |
// without risking arithmetic overflow, we'll consider _one more_ stored item past |
| 313 |
// the end of the end of the range to insert. |
| 314 |
// |
| 315 |
// REVISIT: Possible micro-optimisation: `impl Borrow<T> for RangeInclusiveStartWrapper<T>` |
| 316 |
// and use that to search here, to avoid constructing another `RangeInclusiveStartWrapper`. |
| 317 |
let second_last_possible_start = new_range_start_wrapper.end().clone(); |
| 318 |
let second_last_possible_start = RangeInclusiveStartWrapper::new( |
| 319 |
second_last_possible_start.clone()..=second_last_possible_start, |
| 320 |
); |
| 321 |
while let Some((stored_range_start_wrapper, stored_value)) = self |
| 322 |
.btm |
| 323 |
.range::<RangeInclusiveStartWrapper<K>, ( |
| 324 |
Bound<&RangeInclusiveStartWrapper<K>>, |
| 325 |
Bound<&RangeInclusiveStartWrapper<K>>, |
| 326 |
)>(( |
| 327 |
Bound::Included(&new_range_start_wrapper), |
| 328 |
// We would use something like `Bound::Included(&last_possible_start)`, |
| 329 |
// but making `last_possible_start` might cause arithmetic overflow; |
| 330 |
// instead decide inside the loop whether we've gone too far and break. |
| 331 |
Bound::Unbounded, |
| 332 |
)) |
| 333 |
.next() |
| 334 |
{ |
| 335 |
// A couple of extra exceptions are needed at the |
| 336 |
// end of the subset of stored ranges we want to consider, |
| 337 |
// in part because we use `Bound::Unbounded` above. |
| 338 |
// (See comments up there, and in the individual cases below.) |
| 339 |
let stored_start = stored_range_start_wrapper.start(); |
| 340 |
if *stored_start > *second_last_possible_start.start() { |
| 341 |
let latest_possible_start = StepFnsT::add_one(second_last_possible_start.start()); |
| 342 |
if *stored_start > latest_possible_start { |
| 343 |
// We're beyond the last stored range that could be relevant. |
| 344 |
// Avoid wasting time on irrelevant ranges, or even worse, looping forever. |
| 345 |
// (`adjust_touching_ranges_for_insert` below assumes that the given range |
| 346 |
// is relevant, and behaves very poorly if it is handed a range that it |
| 347 |
// shouldn't be touching.) |
| 348 |
break; |
| 349 |
} |
| 350 |
|
| 351 |
if *stored_start == latest_possible_start && *stored_value != new_value { |
| 352 |
// We are looking at the last stored range that could be relevant, |
| 353 |
// but it has a different value, so we don't want to merge with it. |
| 354 |
// We must explicitly break here as well, because `adjust_touching_ranges_for_insert` |
| 355 |
// below assumes that the given range is relevant, and behaves very poorly if it |
| 356 |
// is handed a range that it shouldn't be touching. |
| 357 |
break; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
let stored_range_start_wrapper = stored_range_start_wrapper.clone(); |
| 362 |
let stored_value = stored_value.clone(); |
| 363 |
|
| 364 |
self.adjust_touching_ranges_for_insert( |
| 365 |
stored_range_start_wrapper, |
| 366 |
stored_value, |
| 367 |
&mut new_range_start_wrapper.end_wrapper.range, |
| 368 |
&new_value, |
| 369 |
); |
| 370 |
} |
| 371 |
|
| 372 |
// Insert the (possibly expanded) new range, and we're done! |
| 373 |
self.btm.insert(new_range_start_wrapper, new_value); |
| 374 |
} |
| 375 |
|
| 376 |
/// Removes a range from the map, if all or any of it was present. |
| 377 |
/// |
| 378 |
/// If the range to be removed _partially_ overlaps any ranges |
| 379 |
/// in the map, then those ranges will be contracted to no |
| 380 |
/// longer cover the removed range. |
| 381 |
/// |
| 382 |
/// |
| 383 |
/// # Panics |
| 384 |
/// |
| 385 |
/// Panics if range `start > end`. |
| 386 |
pub fn remove(&mut self, range: RangeInclusive<K>) { |
| 387 |
use core::ops::Bound; |
| 388 |
|
| 389 |
// Backwards ranges don't make sense. |
| 390 |
// `RangeInclusive` doesn't enforce this, |
| 391 |
// and we don't want weird explosions further down |
| 392 |
// if someone gives us such a range. |
| 393 |
assert!( |
| 394 |
range.start() <= range.end(), |
| 395 |
"Range start can not be after range end" |
| 396 |
); |
| 397 |
|
| 398 |
let range_start_wrapper: RangeInclusiveStartWrapper<K> = |
| 399 |
RangeInclusiveStartWrapper::new(range); |
| 400 |
let range = &range_start_wrapper.range; |
| 401 |
|
| 402 |
// Is there a stored range overlapping the start of |
| 403 |
// the range to insert? |
| 404 |
// |
| 405 |
// If there is any such stored range, it will be the last |
| 406 |
// whose start is less than or equal to the start of the range to insert. |
| 407 |
if let Some((stored_range_start_wrapper, stored_value)) = self |
| 408 |
.btm |
| 409 |
.range::<RangeInclusiveStartWrapper<K>, ( |
| 410 |
Bound<&RangeInclusiveStartWrapper<K>>, |
| 411 |
Bound<&RangeInclusiveStartWrapper<K>>, |
| 412 |
)>((Bound::Unbounded, Bound::Included(&range_start_wrapper))) |
| 413 |
.next_back() |
| 414 |
.filter(|(stored_range_start_wrapper, _stored_value)| { |
| 415 |
// Does the only candidate range overlap |
| 416 |
// the range to insert? |
| 417 |
stored_range_start_wrapper.overlaps(range) |
| 418 |
}) |
| 419 |
.map(|(stored_range_start_wrapper, stored_value)| { |
| 420 |
(stored_range_start_wrapper.clone(), stored_value.clone()) |
| 421 |
}) |
| 422 |
{ |
| 423 |
self.adjust_overlapping_ranges_for_remove( |
| 424 |
stored_range_start_wrapper, |
| 425 |
stored_value, |
| 426 |
range, |
| 427 |
); |
| 428 |
} |
| 429 |
|
| 430 |
// Are there any stored ranges whose heads overlap the range to insert? |
| 431 |
// |
| 432 |
// If there are any such stored ranges (that weren't already caught above), |
| 433 |
// their starts will fall somewhere after the start of the range to insert, |
| 434 |
// and on or before its end. |
| 435 |
// |
| 436 |
// REVISIT: Possible micro-optimisation: `impl Borrow<T> for RangeInclusiveStartWrapper<T>` |
| 437 |
// and use that to search here, to avoid constructing another `RangeInclusiveStartWrapper`. |
| 438 |
let new_range_end_as_start = |
| 439 |
RangeInclusiveStartWrapper::new(range.end().clone()..=range.end().clone()); |
| 440 |
while let Some((stored_range_start_wrapper, stored_value)) = self |
| 441 |
.btm |
| 442 |
.range::<RangeInclusiveStartWrapper<K>, ( |
| 443 |
Bound<&RangeInclusiveStartWrapper<K>>, |
| 444 |
Bound<&RangeInclusiveStartWrapper<K>>, |
| 445 |
)>(( |
| 446 |
Bound::Excluded(&range_start_wrapper), |
| 447 |
Bound::Included(&new_range_end_as_start), |
| 448 |
)) |
| 449 |
.next() |
| 450 |
.map(|(stored_range_start_wrapper, stored_value)| { |
| 451 |
(stored_range_start_wrapper.clone(), stored_value.clone()) |
| 452 |
}) |
| 453 |
{ |
| 454 |
self.adjust_overlapping_ranges_for_remove( |
| 455 |
stored_range_start_wrapper, |
| 456 |
stored_value, |
| 457 |
range, |
| 458 |
); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
fn adjust_touching_ranges_for_insert( |
| 463 |
&mut self, |
| 464 |
stored_range_start_wrapper: RangeInclusiveStartWrapper<K>, |
| 465 |
stored_value: V, |
| 466 |
new_range: &mut RangeInclusive<K>, |
| 467 |
new_value: &V, |
| 468 |
) { |
| 469 |
use core::cmp::{max, min}; |
| 470 |
|
| 471 |
if stored_value == *new_value { |
| 472 |
// The ranges have the same value, so we can "adopt" |
| 473 |
// the stored range. |
| 474 |
// |
| 475 |
// This means that no matter how big or where the stored range is, |
| 476 |
// we will expand the new range's bounds to subsume it, |
| 477 |
// and then delete the stored range. |
| 478 |
let new_start = min(new_range.start(), stored_range_start_wrapper.start()).clone(); |
| 479 |
let new_end = max(new_range.end(), stored_range_start_wrapper.end()).clone(); |
| 480 |
*new_range = new_start..=new_end; |
| 481 |
self.btm.remove(&stored_range_start_wrapper); |
| 482 |
} else { |
| 483 |
// The ranges have different values. |
| 484 |
if new_range.overlaps(&stored_range_start_wrapper.range) { |
| 485 |
// The ranges overlap. This is a little bit more complicated. |
| 486 |
// Delete the stored range, and then add back between |
| 487 |
// 0 and 2 subranges at the ends of the range to insert. |
| 488 |
self.btm.remove(&stored_range_start_wrapper); |
| 489 |
if stored_range_start_wrapper.start() < new_range.start() { |
| 490 |
// Insert the piece left of the range to insert. |
| 491 |
self.btm.insert( |
| 492 |
RangeInclusiveStartWrapper::new( |
| 493 |
stored_range_start_wrapper.start().clone() |
| 494 |
..=StepFnsT::sub_one(new_range.start()), |
| 495 |
), |
| 496 |
stored_value.clone(), |
| 497 |
); |
| 498 |
} |
| 499 |
if stored_range_start_wrapper.end() > new_range.end() { |
| 500 |
// Insert the piece right of the range to insert. |
| 501 |
self.btm.insert( |
| 502 |
RangeInclusiveStartWrapper::new( |
| 503 |
StepFnsT::add_one(new_range.end()) |
| 504 |
..=stored_range_start_wrapper.end().clone(), |
| 505 |
), |
| 506 |
stored_value, |
| 507 |
); |
| 508 |
} |
| 509 |
} else { |
| 510 |
// No-op; they're not overlapping, |
| 511 |
// so we can just keep both ranges as they are. |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
fn adjust_overlapping_ranges_for_remove( |
| 517 |
&mut self, |
| 518 |
stored_range_start_wrapper: RangeInclusiveStartWrapper<K>, |
| 519 |
stored_value: V, |
| 520 |
range_to_remove: &RangeInclusive<K>, |
| 521 |
) { |
| 522 |
// Delete the stored range, and then add back between |
| 523 |
// 0 and 2 subranges at the ends of the range to insert. |
| 524 |
self.btm.remove(&stored_range_start_wrapper); |
| 525 |
let stored_range = stored_range_start_wrapper.end_wrapper.range; |
| 526 |
if stored_range.start() < range_to_remove.start() { |
| 527 |
// Insert the piece left of the range to insert. |
| 528 |
self.btm.insert( |
| 529 |
RangeInclusiveStartWrapper::new( |
| 530 |
stored_range.start().clone()..=StepFnsT::sub_one(range_to_remove.start()), |
| 531 |
), |
| 532 |
stored_value.clone(), |
| 533 |
); |
| 534 |
} |
| 535 |
if stored_range.end() > range_to_remove.end() { |
| 536 |
// Insert the piece right of the range to insert. |
| 537 |
self.btm.insert( |
| 538 |
RangeInclusiveStartWrapper::new( |
| 539 |
StepFnsT::add_one(range_to_remove.end())..=stored_range.end().clone(), |
| 540 |
), |
| 541 |
stored_value, |
| 542 |
); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
/// Gets an iterator over all the maximally-sized ranges |
| 547 |
/// contained in `outer_range` that are not covered by |
| 548 |
/// any range stored in the map. |
| 549 |
/// |
| 550 |
/// The iterator element type is `RangeInclusive<K>`. |
| 551 |
pub fn gaps<'a>(&'a self, outer_range: &'a RangeInclusive<K>) -> Gaps<'a, K, V, StepFnsT> { |
| 552 |
let overlap_iter = self.overlapping(outer_range); |
| 553 |
Gaps { |
| 554 |
candidate_needs_plus_one: false, |
| 555 |
candidate_start: outer_range.start(), |
| 556 |
query_end: outer_range.end(), |
| 557 |
btm_range_iter: overlap_iter.btm_range_iter, |
| 558 |
// We'll start the candidate range at the start of the outer range |
| 559 |
// without checking what's there. Each time we yield an item, |
| 560 |
// we'll skip any ranges we find before the next gap. |
| 561 |
_phantom: PhantomData, |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/// Gets an iterator over all the stored ranges that are |
| 566 |
/// either partially or completely overlapped by the given range. |
| 567 |
pub fn overlapping<R: Borrow<RangeInclusive<K>>>( |
| 568 |
&'_ self, |
| 569 |
range: R, |
| 570 |
) -> Overlapping<'_, K, V, R> { |
| 571 |
// Find the first matching stored range by its _end_, |
| 572 |
// using sneaky layering and `Borrow` implementation. (See `range_wrappers` module.) |
| 573 |
let start_sliver = RangeInclusiveEndWrapper::new( |
| 574 |
range.borrow().start().clone()..=range.borrow().start().clone(), |
| 575 |
); |
| 576 |
let btm_range_iter = self |
| 577 |
.btm |
| 578 |
.range::<RangeInclusiveEndWrapper<K>, RangeFrom<&RangeInclusiveEndWrapper<K>>>( |
| 579 |
&start_sliver.., |
| 580 |
); |
| 581 |
Overlapping { |
| 582 |
query_range: range, |
| 583 |
btm_range_iter, |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
/// Returns `true` if any range in the map completely or partially |
| 588 |
/// overlaps the given range. |
| 589 |
pub fn overlaps(&self, range: &RangeInclusive<K>) -> bool { |
| 590 |
self.overlapping(range).next().is_some() |
| 591 |
} |
| 592 |
|
| 593 |
/// Returns the first range-value pair in this map, if one exists. The range in this pair is the |
| 594 |
/// minimum range in the map. |
| 595 |
pub fn first_range_value(&self) -> Option<(&RangeInclusive<K>, &V)> { |
| 596 |
self.btm |
| 597 |
.first_key_value() |
| 598 |
.map(|(range, value)| (&range.end_wrapper.range, value)) |
| 599 |
} |
| 600 |
|
| 601 |
/// Returns the last range-value pair in this map, if one exists. The range in this pair is the |
| 602 |
/// maximum range in the map. |
| 603 |
pub fn last_range_value(&self) -> Option<(&RangeInclusive<K>, &V)> { |
| 604 |
self.btm |
| 605 |
.last_key_value() |
| 606 |
.map(|(range, value)| (&range.end_wrapper.range, value)) |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
/// An iterator over the entries of a `RangeInclusiveMap`, ordered by key range. |
| 611 |
/// |
| 612 |
/// The iterator element type is `(&'a RangeInclusive<K>, &'a V)`. |
| 613 |
/// |
| 614 |
/// This `struct` is created by the [`iter`] method on [`RangeInclusiveMap`]. See its |
| 615 |
/// documentation for more. |
| 616 |
/// |
| 617 |
/// [`iter`]: RangeInclusiveMap::iter |
| 618 |
pub struct Iter<'a, K, V> { |
| 619 |
inner: alloc::collections::btree_map::Iter<'a, RangeInclusiveStartWrapper<K>, V>, |
| 620 |
} |
| 621 |
|
| 622 |
impl<'a, K, V> Iterator for Iter<'a, K, V> |
| 623 |
where |
| 624 |
K: 'a, |
| 625 |
V: 'a, |
| 626 |
{ |
| 627 |
type Item = (&'a RangeInclusive<K>, &'a V); |
| 628 |
|
| 629 |
fn next(&mut self) -> Option<Self::Item> { |
| 630 |
self.inner.next().map(|(by_start, v)| (&by_start.range, v)) |
| 631 |
} |
| 632 |
|
| 633 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 634 |
self.inner.size_hint() |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> |
| 639 |
where |
| 640 |
K: 'a, |
| 641 |
V: 'a, |
| 642 |
{ |
| 643 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 644 |
self.inner |
| 645 |
.next_back() |
| 646 |
.map(|(range, value)| (&range.end_wrapper.range, value)) |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
/// An owning iterator over the entries of a `RangeInclusiveMap`, ordered by key range. |
| 651 |
/// |
| 652 |
/// The iterator element type is `(RangeInclusive<K>, V)`. |
| 653 |
/// |
| 654 |
/// This `struct` is created by the [`into_iter`] method on [`RangeInclusiveMap`] |
| 655 |
/// (provided by the `IntoIterator` trait). See its documentation for more. |
| 656 |
/// |
| 657 |
/// [`into_iter`]: IntoIterator::into_iter |
| 658 |
pub struct IntoIter<K, V> { |
| 659 |
inner: alloc::collections::btree_map::IntoIter<RangeInclusiveStartWrapper<K>, V>, |
| 660 |
} |
| 661 |
|
| 662 |
impl<K, V> IntoIterator for RangeInclusiveMap<K, V> { |
| 663 |
type Item = (RangeInclusive<K>, V); |
| 664 |
type IntoIter = IntoIter<K, V>; |
| 665 |
fn into_iter(self) -> Self::IntoIter { |
| 666 |
IntoIter { |
| 667 |
inner: self.btm.into_iter(), |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
impl<K, V> Iterator for IntoIter<K, V> { |
| 673 |
type Item = (RangeInclusive<K>, V); |
| 674 |
fn next(&mut self) -> Option<(RangeInclusive<K>, V)> { |
| 675 |
self.inner |
| 676 |
.next() |
| 677 |
.map(|(by_start, v)| (by_start.end_wrapper.range, v)) |
| 678 |
} |
| 679 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 680 |
self.inner.size_hint() |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
impl<K, V> DoubleEndedIterator for IntoIter<K, V> { |
| 685 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 686 |
self.inner |
| 687 |
.next_back() |
| 688 |
.map(|(range, value)| (range.end_wrapper.range, value)) |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
// We can't just derive this automatically, because that would |
| 693 |
// expose irrelevant (and private) implementation details. |
| 694 |
// Instead implement it in the same way that the underlying BTreeMap does. |
| 695 |
impl<K: Debug, V: Debug> Debug for RangeInclusiveMap<K, V> |
| 696 |
where |
| 697 |
K: Ord + Clone + StepLite, |
| 698 |
V: PartialEq + Clone, |
| 699 |
{ |
| 700 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 701 |
f.debug_map().entries(self.iter()).finish() |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
impl<K, V> FromIterator<(RangeInclusive<K>, V)> for RangeInclusiveMap<K, V> |
| 706 |
where |
| 707 |
K: Ord + Clone + StepLite, |
| 708 |
V: PartialEq + Clone, |
| 709 |
{ |
| 710 |
fn from_iter<T: IntoIterator<Item = (RangeInclusive<K>, V)>>(iter: T) -> Self { |
| 711 |
let mut range_map = RangeInclusiveMap::new(); |
| 712 |
range_map.extend(iter); |
| 713 |
range_map |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
impl<K, V> Extend<(RangeInclusive<K>, V)> for RangeInclusiveMap<K, V> |
| 718 |
where |
| 719 |
K: Ord + Clone + StepLite, |
| 720 |
V: PartialEq + Clone, |
| 721 |
{ |
| 722 |
fn extend<T: IntoIterator<Item = (RangeInclusive<K>, V)>>(&mut self, iter: T) { |
| 723 |
iter.into_iter().for_each(move |(k, v)| { |
| 724 |
self.insert(k, v); |
| 725 |
}) |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
#[cfg(feature = "serde1")] |
| 730 |
impl<K, V> Serialize for RangeInclusiveMap<K, V> |
| 731 |
where |
| 732 |
K: Ord + Clone + StepLite + Serialize, |
| 733 |
V: PartialEq + Clone + Serialize, |
| 734 |
{ |
| 735 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 736 |
where |
| 737 |
S: Serializer, |
| 738 |
{ |
| 739 |
use serde::ser::SerializeSeq; |
| 740 |
let mut seq = serializer.serialize_seq(Some(self.btm.len()))?; |
| 741 |
for (k, v) in self.iter() { |
| 742 |
seq.serialize_element(&((k.start(), k.end()), &v))?; |
| 743 |
} |
| 744 |
seq.end() |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
#[cfg(feature = "serde1")] |
| 749 |
impl<'de, K, V> Deserialize<'de> for RangeInclusiveMap<K, V> |
| 750 |
where |
| 751 |
K: Ord + Clone + StepLite + Deserialize<'de>, |
| 752 |
V: PartialEq + Clone + Deserialize<'de>, |
| 753 |
{ |
| 754 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 755 |
where |
| 756 |
D: Deserializer<'de>, |
| 757 |
{ |
| 758 |
deserializer.deserialize_seq(RangeInclusiveMapVisitor::new()) |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
#[cfg(feature = "serde1")] |
| 763 |
struct RangeInclusiveMapVisitor<K, V> { |
| 764 |
marker: PhantomData<fn() -> RangeInclusiveMap<K, V>>, |
| 765 |
} |
| 766 |
|
| 767 |
#[cfg(feature = "serde1")] |
| 768 |
impl<K, V> RangeInclusiveMapVisitor<K, V> { |
| 769 |
fn new() -> Self { |
| 770 |
RangeInclusiveMapVisitor { |
| 771 |
marker: PhantomData, |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
#[cfg(feature = "serde1")] |
| 777 |
impl<'de, K, V> Visitor<'de> for RangeInclusiveMapVisitor<K, V> |
| 778 |
where |
| 779 |
K: Ord + Clone + StepLite + Deserialize<'de>, |
| 780 |
V: PartialEq + Clone + Deserialize<'de>, |
| 781 |
{ |
| 782 |
type Value = RangeInclusiveMap<K, V>; |
| 783 |
|
| 784 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 785 |
formatter.write_str("RangeInclusiveMap") |
| 786 |
} |
| 787 |
|
| 788 |
fn visit_seq<A>(self, mut access: A) -> Result<Self::Value, A::Error> |
| 789 |
where |
| 790 |
A: SeqAccess<'de>, |
| 791 |
{ |
| 792 |
let mut range_inclusive_map = RangeInclusiveMap::new(); |
| 793 |
while let Some(((start, end), value)) = access.next_element()? { |
| 794 |
range_inclusive_map.insert(start..=end, value); |
| 795 |
} |
| 796 |
Ok(range_inclusive_map) |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
/// An iterator over all ranges not covered by a `RangeInclusiveMap`. |
| 801 |
/// |
| 802 |
/// The iterator element type is `RangeInclusive<K>`. |
| 803 |
/// |
| 804 |
/// This `struct` is created by the [`gaps`] method on [`RangeInclusiveMap`]. See its |
| 805 |
/// documentation for more. |
| 806 |
/// |
| 807 |
/// [`gaps`]: RangeInclusiveMap::gaps |
| 808 |
pub struct Gaps<'a, K, V, StepFnsT> { |
| 809 |
/// Would be redundant, but we need an extra flag to |
| 810 |
/// avoid overflowing when dealing with inclusive ranges. |
| 811 |
/// |
| 812 |
/// All other things here are ignored if `done` is `true`. |
| 813 |
candidate_needs_plus_one: bool, |
| 814 |
candidate_start: &'a K, |
| 815 |
query_end: &'a K, |
| 816 |
btm_range_iter: alloc::collections::btree_map::Range<'a, RangeInclusiveStartWrapper<K>, V>, |
| 817 |
_phantom: PhantomData<StepFnsT>, |
| 818 |
} |
| 819 |
|
| 820 |
// `Gaps` is always fused. (See definition of `next` below.) |
| 821 |
impl<'a, K, V, StepFnsT> core::iter::FusedIterator for Gaps<'a, K, V, StepFnsT> |
| 822 |
where |
| 823 |
K: Ord + Clone, |
| 824 |
StepFnsT: StepFns<K>, |
| 825 |
{ |
| 826 |
} |
| 827 |
|
| 828 |
impl<'a, K, V, StepFnsT> Iterator for Gaps<'a, K, V, StepFnsT> |
| 829 |
where |
| 830 |
K: Ord + Clone, |
| 831 |
StepFnsT: StepFns<K>, |
| 832 |
{ |
| 833 |
type Item = RangeInclusive<K>; |
| 834 |
|
| 835 |
fn next(&mut self) -> Option<Self::Item> { |
| 836 |
for overlap in self.btm_range_iter.by_ref() { |
| 837 |
let overlap = overlap.0; |
| 838 |
|
| 839 |
// If the range in the map has advanced beyond the query range, return |
| 840 |
// any tail gap. |
| 841 |
if *self.query_end < *overlap.start() { |
| 842 |
break; |
| 843 |
} |
| 844 |
|
| 845 |
let candidate_needs_plus_one = |
| 846 |
core::mem::replace(&mut self.candidate_needs_plus_one, true); |
| 847 |
|
| 848 |
let cur_candidate_start = core::mem::replace(&mut self.candidate_start, overlap.end()); |
| 849 |
|
| 850 |
let cur_candidate_start = if candidate_needs_plus_one { |
| 851 |
StepFnsT::add_one(cur_candidate_start) |
| 852 |
} else { |
| 853 |
cur_candidate_start.clone() |
| 854 |
}; |
| 855 |
|
| 856 |
if cur_candidate_start < *overlap.start() { |
| 857 |
let gap = cur_candidate_start..=StepFnsT::sub_one(overlap.start()); |
| 858 |
return Some(gap); |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
// Now that we've run out of items, the only other possible |
| 863 |
// gap is one at the end of the outer range. |
| 864 |
let candidate_needs_plus_one = core::mem::replace(&mut self.candidate_needs_plus_one, true); |
| 865 |
|
| 866 |
let cur_candidate_start = core::mem::replace(&mut self.candidate_start, self.query_end); |
| 867 |
if candidate_needs_plus_one { |
| 868 |
if *cur_candidate_start < *self.query_end { |
| 869 |
return Some(StepFnsT::add_one(cur_candidate_start)..=self.query_end.clone()); |
| 870 |
} |
| 871 |
} else if *cur_candidate_start <= *self.query_end { |
| 872 |
// There's a gap at the end! |
| 873 |
return Some(cur_candidate_start.clone()..=self.query_end.clone()); |
| 874 |
} |
| 875 |
|
| 876 |
None |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
/// An iterator over all stored ranges partially or completely |
| 881 |
/// overlapped by a given range. |
| 882 |
/// |
| 883 |
/// The iterator element type is `(&'a RangeInclusive<K>, &'a V)`. |
| 884 |
/// |
| 885 |
/// This `struct` is created by the [`overlapping`] method on [`RangeInclusiveMap`]. See its |
| 886 |
/// documentation for more. |
| 887 |
/// |
| 888 |
/// [`overlapping`]: RangeInclusiveMap::overlapping |
| 889 |
pub struct Overlapping<'a, K, V, R: Borrow<RangeInclusive<K>> = &'a RangeInclusive<K>> { |
| 890 |
query_range: R, |
| 891 |
btm_range_iter: alloc::collections::btree_map::Range<'a, RangeInclusiveStartWrapper<K>, V>, |
| 892 |
} |
| 893 |
|
| 894 |
// `Overlapping` is always fused. (See definition of `next` below.) |
| 895 |
impl<'a, K, V, R: Borrow<RangeInclusive<K>>> core::iter::FusedIterator for Overlapping<'a, K, V, R> where |
| 896 |
K: Ord + Clone |
| 897 |
{ |
| 898 |
} |
| 899 |
|
| 900 |
impl<'a, K, V, R: Borrow<RangeInclusive<K>>> Iterator for Overlapping<'a, K, V, R> |
| 901 |
where |
| 902 |
K: Ord + Clone, |
| 903 |
{ |
| 904 |
type Item = (&'a RangeInclusive<K>, &'a V); |
| 905 |
|
| 906 |
fn next(&mut self) -> Option<Self::Item> { |
| 907 |
if let Some((k, v)) = self.btm_range_iter.next() { |
| 908 |
if k.start() <= self.query_range.borrow().end() { |
| 909 |
Some((&k.range, v)) |
| 910 |
} else { |
| 911 |
// The rest of the items in the underlying iterator |
| 912 |
// are past the query range. We can keep taking items |
| 913 |
// from that iterator and this will remain true, |
| 914 |
// so this is enough to make the iterator fused. |
| 915 |
None |
| 916 |
} |
| 917 |
} else { |
| 918 |
None |
| 919 |
} |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
impl<'a, K, V, R: Borrow<RangeInclusive<K>>> DoubleEndedIterator for Overlapping<'a, K, V, R> |
| 924 |
where |
| 925 |
K: Ord + Clone, |
| 926 |
{ |
| 927 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 928 |
while let Some((k, v)) = self.btm_range_iter.next_back() { |
| 929 |
if k.start() <= self.query_range.borrow().end() { |
| 930 |
return Some((&k.range, v)); |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
None |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
impl<K: Ord + Clone + StepLite, V: Eq + Clone, const N: usize> From<[(RangeInclusive<K>, V); N]> |
| 939 |
for RangeInclusiveMap<K, V> |
| 940 |
{ |
| 941 |
fn from(value: [(RangeInclusive<K>, V); N]) -> Self { |
| 942 |
let mut map = Self::new(); |
| 943 |
for (range, value) in IntoIterator::into_iter(value) { |
| 944 |
map.insert(range, value); |
| 945 |
} |
| 946 |
map |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
/// Create a [`RangeInclusiveMap`] from key-value pairs. |
| 951 |
/// |
| 952 |
/// # Example |
| 953 |
/// |
| 954 |
/// ```rust |
| 955 |
/// # use rangemap::range_inclusive_map; |
| 956 |
/// let map = range_inclusive_map!{ |
| 957 |
/// 0..=100 => "abc", |
| 958 |
/// 100..=200 => "def", |
| 959 |
/// 200..=300 => "ghi" |
| 960 |
/// }; |
| 961 |
/// ``` |
| 962 |
#[macro_export] |
| 963 |
macro_rules! range_inclusive_map { |
| 964 |
($($k:expr => $v:expr),* $(,)?) => {{ |
| 965 |
$crate::RangeInclusiveMap::from([$(($k, $v)),*]) |
| 966 |
}}; |
| 967 |
} |
| 968 |
|
| 969 |
#[cfg(test)] |
| 970 |
mod tests { |
| 971 |
use super::*; |
| 972 |
use alloc as std; |
| 973 |
use alloc::{format, string::String, vec, vec::Vec}; |
| 974 |
use proptest::prelude::*; |
| 975 |
use test_strategy::proptest; |
| 976 |
|
| 977 |
impl<K, V> Arbitrary for RangeInclusiveMap<K, V> |
| 978 |
where |
| 979 |
K: Ord + Clone + Debug + StepLite + Arbitrary + 'static, |
| 980 |
V: Clone + PartialEq + Arbitrary + 'static, |
| 981 |
{ |
| 982 |
type Parameters = (); |
| 983 |
type Strategy = BoxedStrategy<Self>; |
| 984 |
|
| 985 |
fn arbitrary_with(_parameters: Self::Parameters) -> Self::Strategy { |
| 986 |
any::<Vec<(RangeInclusive<K>, V)>>() |
| 987 |
.prop_map(|ranges| ranges.into_iter().collect::<RangeInclusiveMap<K, V>>()) |
| 988 |
.boxed() |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
#[proptest] |
| 993 |
#[allow(clippy::len_zero)] |
| 994 |
fn test_len(mut map: RangeInclusiveMap<u64, String>) { |
| 995 |
assert_eq!(map.len(), map.iter().count()); |
| 996 |
assert_eq!(map.is_empty(), map.len() == 0); |
| 997 |
map.clear(); |
| 998 |
assert_eq!(map.len(), 0); |
| 999 |
assert!(map.is_empty()); |
| 1000 |
assert_eq!(map.iter().count(), 0); |
| 1001 |
} |
| 1002 |
|
| 1003 |
#[proptest] |
| 1004 |
fn test_first(set: RangeInclusiveMap<u64, String>) { |
| 1005 |
assert_eq!( |
| 1006 |
set.first_range_value(), |
| 1007 |
set.iter().min_by_key(|(range, _)| range.start()) |
| 1008 |
); |
| 1009 |
} |
| 1010 |
|
| 1011 |
#[proptest] |
| 1012 |
fn test_last(set: RangeInclusiveMap<u64, String>) { |
| 1013 |
assert_eq!( |
| 1014 |
set.last_range_value(), |
| 1015 |
set.iter().max_by_key(|(range, _)| range.end()) |
| 1016 |
); |
| 1017 |
} |
| 1018 |
|
| 1019 |
#[proptest] |
| 1020 |
fn test_iter_reversible(set: RangeInclusiveMap<u64, String>) { |
| 1021 |
let forward: Vec<_> = set.iter().collect(); |
| 1022 |
let mut backward: Vec<_> = set.iter().rev().collect(); |
| 1023 |
backward.reverse(); |
| 1024 |
assert_eq!(forward, backward); |
| 1025 |
} |
| 1026 |
|
| 1027 |
#[proptest] |
| 1028 |
fn test_into_iter_reversible(set: RangeInclusiveMap<u64, String>) { |
| 1029 |
let forward: Vec<_> = set.clone().into_iter().collect(); |
| 1030 |
let mut backward: Vec<_> = set.into_iter().rev().collect(); |
| 1031 |
backward.reverse(); |
| 1032 |
assert_eq!(forward, backward); |
| 1033 |
} |
| 1034 |
|
| 1035 |
#[proptest] |
| 1036 |
fn test_overlapping_reversible( |
| 1037 |
set: RangeInclusiveMap<u64, String>, |
| 1038 |
range: RangeInclusive<u64>, |
| 1039 |
) { |
| 1040 |
let forward: Vec<_> = set.overlapping(&range).collect(); |
| 1041 |
let mut backward: Vec<_> = set.overlapping(&range).rev().collect(); |
| 1042 |
backward.reverse(); |
| 1043 |
assert_eq!(forward, backward); |
| 1044 |
} |
| 1045 |
|
| 1046 |
#[proptest] |
| 1047 |
fn test_arbitrary_map_u8(ranges: Vec<(RangeInclusive<u8>, String)>) { |
| 1048 |
let ranges: Vec<_> = ranges |
| 1049 |
.into_iter() |
| 1050 |
.filter(|(range, _value)| range.start() != range.end()) |
| 1051 |
.collect(); |
| 1052 |
let set = ranges |
| 1053 |
.iter() |
| 1054 |
.fold(RangeInclusiveMap::new(), |mut set, (range, value)| { |
| 1055 |
set.insert(range.clone(), value.clone()); |
| 1056 |
set |
| 1057 |
}); |
| 1058 |
|
| 1059 |
for value in 0..u8::MAX { |
| 1060 |
assert_eq!( |
| 1061 |
set.get(&value), |
| 1062 |
ranges |
| 1063 |
.iter() |
| 1064 |
.rev() |
| 1065 |
.find(|(range, _value)| range.contains(&value)) |
| 1066 |
.map(|(_range, value)| value) |
| 1067 |
); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
#[proptest] |
| 1072 |
#[allow(deprecated)] |
| 1073 |
fn test_hash(left: RangeInclusiveMap<u64, u64>, right: RangeInclusiveMap<u64, u64>) { |
| 1074 |
use core::hash::{Hash, Hasher, SipHasher}; |
| 1075 |
|
| 1076 |
let hash = |set: &RangeInclusiveMap<_, _>| { |
| 1077 |
let mut hasher = SipHasher::new(); |
| 1078 |
set.hash(&mut hasher); |
| 1079 |
hasher.finish() |
| 1080 |
}; |
| 1081 |
|
| 1082 |
if left == right { |
| 1083 |
assert!( |
| 1084 |
hash(&left) == hash(&right), |
| 1085 |
"if two values are equal, their hash must be equal" |
| 1086 |
); |
| 1087 |
} |
| 1088 |
|
| 1089 |
// if the hashes are equal the values might not be the same (collision) |
| 1090 |
if hash(&left) != hash(&right) { |
| 1091 |
assert!( |
| 1092 |
left != right, |
| 1093 |
"if two value's hashes are not equal, they must not be equal" |
| 1094 |
); |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
#[proptest] |
| 1099 |
fn test_ord(left: RangeInclusiveMap<u64, u64>, right: RangeInclusiveMap<u64, u64>) { |
| 1100 |
assert_eq!( |
| 1101 |
left == right, |
| 1102 |
left.cmp(&right).is_eq(), |
| 1103 |
"ordering and equality must match" |
| 1104 |
); |
| 1105 |
assert_eq!( |
| 1106 |
left.cmp(&right), |
| 1107 |
left.partial_cmp(&right).unwrap(), |
| 1108 |
"ordering is total for ordered parameters" |
| 1109 |
); |
| 1110 |
} |
| 1111 |
|
| 1112 |
#[test] |
| 1113 |
fn test_from_array() { |
| 1114 |
let mut map = RangeInclusiveMap::new(); |
| 1115 |
map.insert(0..=100, "hello"); |
| 1116 |
map.insert(200..=300, "world"); |
| 1117 |
assert_eq!( |
| 1118 |
map, |
| 1119 |
RangeInclusiveMap::from([(0..=100, "hello"), (200..=300, "world")]) |
| 1120 |
); |
| 1121 |
} |
| 1122 |
|
| 1123 |
#[test] |
| 1124 |
fn test_macro() { |
| 1125 |
assert_eq!( |
| 1126 |
range_inclusive_map![], |
| 1127 |
RangeInclusiveMap::<i64, i64>::default() |
| 1128 |
); |
| 1129 |
assert_eq!( |
| 1130 |
range_inclusive_map!(0..=100 => "abc", 100..=200 => "def", 200..=300 => "ghi"), |
| 1131 |
[(0..=100, "abc"), (100..=200, "def"), (200..=300, "ghi")] |
| 1132 |
.iter() |
| 1133 |
.cloned() |
| 1134 |
.collect(), |
| 1135 |
); |
| 1136 |
} |
| 1137 |
|
| 1138 |
trait RangeInclusiveMapExt<K, V> { |
| 1139 |
fn to_vec(&self) -> Vec<(RangeInclusive<K>, V)>; |
| 1140 |
} |
| 1141 |
|
| 1142 |
impl<K, V> RangeInclusiveMapExt<K, V> for RangeInclusiveMap<K, V, K> |
| 1143 |
where |
| 1144 |
K: Ord + Clone + StepLite, |
| 1145 |
V: PartialEq + Clone, |
| 1146 |
{ |
| 1147 |
fn to_vec(&self) -> Vec<(RangeInclusive<K>, V)> { |
| 1148 |
self.iter().map(|(kr, v)| (kr.clone(), v.clone())).collect() |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
// |
| 1153 |
// Insertion tests |
| 1154 |
// |
| 1155 |
|
| 1156 |
#[test] |
| 1157 |
fn empty_map_is_empty() { |
| 1158 |
let range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1159 |
assert_eq!(range_map.to_vec(), vec![]); |
| 1160 |
} |
| 1161 |
|
| 1162 |
#[test] |
| 1163 |
fn insert_into_empty_map() { |
| 1164 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1165 |
range_map.insert(0..=50, false); |
| 1166 |
assert_eq!(range_map.to_vec(), vec![(0..=50, false)]); |
| 1167 |
} |
| 1168 |
|
| 1169 |
#[test] |
| 1170 |
fn new_same_value_immediately_following_stored() { |
| 1171 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1172 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1173 |
// ◌ ●---● ◌ ◌ ◌ ◌ ◌ ◌ |
| 1174 |
range_map.insert(1..=3, false); |
| 1175 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1176 |
// ◌ ◌ ◌ ◌ ●---◌ ◌ ◌ ◌ |
| 1177 |
range_map.insert(4..=6, false); |
| 1178 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1179 |
// ◌ ●---------◌ ◌ ◌ ◌ |
| 1180 |
assert_eq!(range_map.to_vec(), vec![(1..=6, false)]); |
| 1181 |
} |
| 1182 |
|
| 1183 |
#[test] |
| 1184 |
fn new_different_value_immediately_following_stored() { |
| 1185 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1186 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1187 |
// ◌ ●---● ◌ ◌ ◌ ◌ ◌ ◌ |
| 1188 |
range_map.insert(1..=3, false); |
| 1189 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1190 |
// ◌ ◌ ◌ ◌ ◆---◇ ◌ ◌ ◌ |
| 1191 |
range_map.insert(4..=6, true); |
| 1192 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1193 |
// ◌ ●---● ◌ ◌ ◌ ◌ ◌ ◌ |
| 1194 |
// ◌ ◌ ◌ ◌ ◆---◇ ◌ ◌ ◌ |
| 1195 |
assert_eq!(range_map.to_vec(), vec![(1..=3, false), (4..=6, true)]); |
| 1196 |
} |
| 1197 |
|
| 1198 |
#[test] |
| 1199 |
fn new_same_value_overlapping_end_of_stored() { |
| 1200 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1201 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1202 |
// ◌ ●-----● ◌ ◌ ◌ ◌ ◌ |
| 1203 |
range_map.insert(1..=4, false); |
| 1204 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1205 |
// ◌ ◌ ◌ ◌ ●---● ◌ ◌ ◌ |
| 1206 |
range_map.insert(4..=6, false); |
| 1207 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1208 |
// ◌ ●---------● ◌ ◌ ◌ |
| 1209 |
assert_eq!(range_map.to_vec(), vec![(1..=6, false)]); |
| 1210 |
} |
| 1211 |
|
| 1212 |
#[test] |
| 1213 |
fn new_different_value_overlapping_end_of_stored() { |
| 1214 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1215 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1216 |
// ◌ ●---● ◌ ◌ ◌ ◌ ◌ ◌ |
| 1217 |
range_map.insert(1..=3, false); |
| 1218 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1219 |
// ◌ ◌ ◌ ◆---◆ ◌ ◌ ◌ ◌ |
| 1220 |
range_map.insert(3..=5, true); |
| 1221 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1222 |
// ◌ ●-● ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1223 |
// ◌ ◌ ◌ ◆---◇ ◌ ◌ ◌ ◌ |
| 1224 |
assert_eq!(range_map.to_vec(), vec![(1..=2, false), (3..=5, true)]); |
| 1225 |
} |
| 1226 |
|
| 1227 |
#[test] |
| 1228 |
fn new_same_value_immediately_preceding_stored() { |
| 1229 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1230 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1231 |
// ◌ ◌ ◌ ●---● ◌ ◌ ◌ ◌ |
| 1232 |
range_map.insert(3..=5, false); |
| 1233 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1234 |
// ◌ ●-● ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1235 |
range_map.insert(1..=2, false); |
| 1236 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1237 |
// ◌ ●-------● ◌ ◌ ◌ ◌ |
| 1238 |
assert_eq!(range_map.to_vec(), vec![(1..=5, false)]); |
| 1239 |
} |
| 1240 |
|
| 1241 |
#[test] |
| 1242 |
fn new_different_value_immediately_preceding_stored() { |
| 1243 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1244 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1245 |
// ◌ ◌ ◌ ◆---◆ ◌ ◌ ◌ ◌ |
| 1246 |
range_map.insert(3..=5, true); |
| 1247 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1248 |
// ◌ ●-● ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1249 |
range_map.insert(1..=2, false); |
| 1250 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1251 |
// ◌ ●-● ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1252 |
// ◌ ◌ ◌ ◆---◇ ◌ ◌ ◌ ◌ |
| 1253 |
assert_eq!(range_map.to_vec(), vec![(1..=2, false), (3..=5, true)]); |
| 1254 |
} |
| 1255 |
|
| 1256 |
#[test] |
| 1257 |
fn new_same_value_wholly_inside_stored() { |
| 1258 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1259 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1260 |
// ◌ ●-------● ◌ ◌ ◌ ◌ |
| 1261 |
range_map.insert(1..=5, false); |
| 1262 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1263 |
// ◌ ◌ ●---● ◌ ◌ ◌ ◌ ◌ ◌ |
| 1264 |
range_map.insert(2..=4, false); |
| 1265 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1266 |
// ◌ ●-------● ◌ ◌ ◌ ◌ |
| 1267 |
assert_eq!(range_map.to_vec(), vec![(1..=5, false)]); |
| 1268 |
} |
| 1269 |
|
| 1270 |
#[test] |
| 1271 |
fn new_different_value_wholly_inside_stored() { |
| 1272 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1273 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1274 |
// ◌ ◆-------◆ ◌ ◌ ◌ ◌ |
| 1275 |
range_map.insert(1..=5, true); |
| 1276 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1277 |
// ◌ ◌ ●---● ◌ ◌ ◌ ◌ ◌ ◌ |
| 1278 |
range_map.insert(2..=4, false); |
| 1279 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1280 |
// ◌ ◆ ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1281 |
// ◌ ◌ ●---● ◌ ◌ ◌ ◌ ◌ |
| 1282 |
// ◌ ◌ ◌ ◌ ◌ ◆ ◌ ◌ ◌ ◌ |
| 1283 |
assert_eq!( |
| 1284 |
range_map.to_vec(), |
| 1285 |
vec![(1..=1, true), (2..=4, false), (5..=5, true)] |
| 1286 |
); |
| 1287 |
} |
| 1288 |
|
| 1289 |
#[test] |
| 1290 |
fn replace_at_end_of_existing_range_should_coalesce() { |
| 1291 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1292 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1293 |
// ◌ ●---● ◌ ◌ ◌ ◌ ◌ ◌ |
| 1294 |
range_map.insert(1..=3, false); |
| 1295 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1296 |
// ◌ ◌ ◌ ◌ ●---● ◌ ◌ ◌ |
| 1297 |
range_map.insert(4..=6, true); |
| 1298 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1299 |
// ◌ ◌ ◌ ◌ ●---● ◌ ◌ ◌ |
| 1300 |
range_map.insert(4..=6, false); |
| 1301 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1302 |
// ◌ ●---------● ◌ ◌ ◌ |
| 1303 |
assert_eq!(range_map.to_vec(), vec![(1..=6, false)]); |
| 1304 |
} |
| 1305 |
|
| 1306 |
#[test] |
| 1307 |
// Test every permutation of a bunch of touching and overlapping ranges. |
| 1308 |
fn lots_of_interesting_ranges() { |
| 1309 |
use crate::dense::DenseU32RangeMap; |
| 1310 |
use permutator::Permutation; |
| 1311 |
|
| 1312 |
let mut ranges_with_values = [ |
| 1313 |
(2..=3, false), |
| 1314 |
// A duplicate range |
| 1315 |
(2..=3, false), |
| 1316 |
// Almost a duplicate, but with a different value |
| 1317 |
(2..=3, true), |
| 1318 |
// A few small ranges, some of them overlapping others, |
| 1319 |
// some of them touching others |
| 1320 |
(3..=5, true), |
| 1321 |
(4..=6, true), |
| 1322 |
(6..=7, true), |
| 1323 |
// A really big range |
| 1324 |
(2..=6, true), |
| 1325 |
]; |
| 1326 |
|
| 1327 |
ranges_with_values.permutation().for_each(|permutation| { |
| 1328 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1329 |
let mut dense: DenseU32RangeMap<bool> = DenseU32RangeMap::new(); |
| 1330 |
|
| 1331 |
for (k, v) in permutation { |
| 1332 |
// Insert it into both maps. |
| 1333 |
range_map.insert(k.clone(), v); |
| 1334 |
dense.insert(k, v); |
| 1335 |
|
| 1336 |
// At every step, both maps should contain the same stuff. |
| 1337 |
let sparse = range_map.to_vec(); |
| 1338 |
let dense = dense.to_vec(); |
| 1339 |
assert_eq!(sparse, dense); |
| 1340 |
} |
| 1341 |
}); |
| 1342 |
} |
| 1343 |
|
| 1344 |
// |
| 1345 |
// Get* tests |
| 1346 |
// |
| 1347 |
|
| 1348 |
#[test] |
| 1349 |
fn get() { |
| 1350 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1351 |
range_map.insert(0..=50, false); |
| 1352 |
assert_eq!(range_map.get(&50), Some(&false)); |
| 1353 |
assert_eq!(range_map.get(&51), None); |
| 1354 |
} |
| 1355 |
|
| 1356 |
#[test] |
| 1357 |
fn get_key_value() { |
| 1358 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1359 |
range_map.insert(0..=50, false); |
| 1360 |
assert_eq!(range_map.get_key_value(&50), Some((&(0..=50), &false))); |
| 1361 |
assert_eq!(range_map.get_key_value(&51), None); |
| 1362 |
} |
| 1363 |
|
| 1364 |
// |
| 1365 |
// Removal tests |
| 1366 |
// |
| 1367 |
|
| 1368 |
#[test] |
| 1369 |
fn remove_from_empty_map() { |
| 1370 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1371 |
range_map.remove(0..=50); |
| 1372 |
assert_eq!(range_map.to_vec(), vec![]); |
| 1373 |
} |
| 1374 |
|
| 1375 |
#[test] |
| 1376 |
fn remove_non_covered_range_before_stored() { |
| 1377 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1378 |
range_map.insert(25..=75, false); |
| 1379 |
range_map.remove(0..=24); |
| 1380 |
assert_eq!(range_map.to_vec(), vec![(25..=75, false)]); |
| 1381 |
} |
| 1382 |
|
| 1383 |
#[test] |
| 1384 |
fn remove_non_covered_range_after_stored() { |
| 1385 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1386 |
range_map.insert(25..=75, false); |
| 1387 |
range_map.remove(76..=100); |
| 1388 |
assert_eq!(range_map.to_vec(), vec![(25..=75, false)]); |
| 1389 |
} |
| 1390 |
|
| 1391 |
#[test] |
| 1392 |
fn remove_overlapping_start_of_stored() { |
| 1393 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1394 |
range_map.insert(25..=75, false); |
| 1395 |
range_map.remove(0..=25); |
| 1396 |
assert_eq!(range_map.to_vec(), vec![(26..=75, false)]); |
| 1397 |
} |
| 1398 |
|
| 1399 |
#[test] |
| 1400 |
fn remove_middle_of_stored() { |
| 1401 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1402 |
range_map.insert(25..=75, false); |
| 1403 |
range_map.remove(30..=70); |
| 1404 |
assert_eq!(range_map.to_vec(), vec![(25..=29, false), (71..=75, false)]); |
| 1405 |
} |
| 1406 |
|
| 1407 |
#[test] |
| 1408 |
fn remove_overlapping_end_of_stored() { |
| 1409 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1410 |
range_map.insert(25..=75, false); |
| 1411 |
range_map.remove(75..=100); |
| 1412 |
assert_eq!(range_map.to_vec(), vec![(25..=74, false)]); |
| 1413 |
} |
| 1414 |
|
| 1415 |
#[test] |
| 1416 |
fn remove_exactly_stored() { |
| 1417 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1418 |
range_map.insert(25..=75, false); |
| 1419 |
range_map.remove(25..=75); |
| 1420 |
assert_eq!(range_map.to_vec(), vec![]); |
| 1421 |
} |
| 1422 |
|
| 1423 |
#[test] |
| 1424 |
fn remove_superset_of_stored() { |
| 1425 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1426 |
range_map.insert(25..=75, false); |
| 1427 |
range_map.remove(0..=100); |
| 1428 |
assert_eq!(range_map.to_vec(), vec![]); |
| 1429 |
} |
| 1430 |
|
| 1431 |
// |
| 1432 |
// Test extremes of key ranges; we do addition/subtraction in |
| 1433 |
// the range domain so I want to make sure I haven't accidentally |
| 1434 |
// introduced some arithmetic overflow there. |
| 1435 |
// |
| 1436 |
|
| 1437 |
#[test] |
| 1438 |
fn no_overflow_at_key_domain_extremes() { |
| 1439 |
let mut range_map: RangeInclusiveMap<u8, bool> = RangeInclusiveMap::new(); |
| 1440 |
range_map.insert(0..=255, false); |
| 1441 |
range_map.insert(0..=10, true); |
| 1442 |
range_map.insert(245..=255, true); |
| 1443 |
range_map.remove(0..=5); |
| 1444 |
range_map.remove(0..=5); |
| 1445 |
range_map.remove(250..=255); |
| 1446 |
range_map.remove(250..=255); |
| 1447 |
range_map.insert(0..=255, true); |
| 1448 |
range_map.remove(1..=254); |
| 1449 |
range_map.insert(254..=254, true); |
| 1450 |
range_map.insert(255..=255, true); |
| 1451 |
range_map.insert(255..=255, false); |
| 1452 |
range_map.insert(0..=0, false); |
| 1453 |
range_map.insert(1..=1, true); |
| 1454 |
range_map.insert(0..=0, true); |
| 1455 |
} |
| 1456 |
|
| 1457 |
// Gaps tests |
| 1458 |
|
| 1459 |
#[test] |
| 1460 |
fn whole_range_is_a_gap() { |
| 1461 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1462 |
// ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1463 |
let range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1464 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1465 |
// ◌ ◆-------------◆ ◌ |
| 1466 |
let outer_range = 1..=8; |
| 1467 |
let mut gaps = range_map.gaps(&outer_range); |
| 1468 |
// Should yield the entire outer range. |
| 1469 |
assert_eq!(gaps.next(), Some(1..=8)); |
| 1470 |
assert_eq!(gaps.next(), None); |
| 1471 |
// Gaps iterator should be fused. |
| 1472 |
assert_eq!(gaps.next(), None); |
| 1473 |
assert_eq!(gaps.next(), None); |
| 1474 |
} |
| 1475 |
|
| 1476 |
#[test] |
| 1477 |
fn whole_range_is_covered_exactly() { |
| 1478 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1479 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1480 |
// ◌ ●---------● ◌ ◌ ◌ |
| 1481 |
range_map.insert(1..=6, ()); |
| 1482 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1483 |
// ◌ ◆---------◆ ◌ ◌ ◌ |
| 1484 |
let outer_range = 1..=6; |
| 1485 |
let mut gaps = range_map.gaps(&outer_range); |
| 1486 |
// Should yield no gaps. |
| 1487 |
assert_eq!(gaps.next(), None); |
| 1488 |
// Gaps iterator should be fused. |
| 1489 |
assert_eq!(gaps.next(), None); |
| 1490 |
assert_eq!(gaps.next(), None); |
| 1491 |
} |
| 1492 |
|
| 1493 |
#[test] |
| 1494 |
fn item_before_outer_range() { |
| 1495 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1496 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1497 |
// ◌ ●---● ◌ ◌ ◌ ◌ ◌ ◌ |
| 1498 |
range_map.insert(1..=3, ()); |
| 1499 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1500 |
// ◌ ◌ ◌ ◌ ◌ ◆-----◆ ◌ |
| 1501 |
let outer_range = 5..=8; |
| 1502 |
let mut gaps = range_map.gaps(&outer_range); |
| 1503 |
// Should yield the entire outer range. |
| 1504 |
assert_eq!(gaps.next(), Some(5..=8)); |
| 1505 |
assert_eq!(gaps.next(), None); |
| 1506 |
// Gaps iterator should be fused. |
| 1507 |
assert_eq!(gaps.next(), None); |
| 1508 |
assert_eq!(gaps.next(), None); |
| 1509 |
} |
| 1510 |
|
| 1511 |
#[test] |
| 1512 |
fn item_touching_start_of_outer_range() { |
| 1513 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1514 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1515 |
// ◌ ●-----● ◌ ◌ ◌ ◌ ◌ |
| 1516 |
range_map.insert(1..=4, ()); |
| 1517 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1518 |
// ◌ ◌ ◌ ◌ ◌ ◆-----◆ ◌ |
| 1519 |
let outer_range = 5..=8; |
| 1520 |
let mut gaps = range_map.gaps(&outer_range); |
| 1521 |
// Should yield the entire outer range. |
| 1522 |
assert_eq!(gaps.next(), Some(5..=8)); |
| 1523 |
assert_eq!(gaps.next(), None); |
| 1524 |
// Gaps iterator should be fused. |
| 1525 |
assert_eq!(gaps.next(), None); |
| 1526 |
assert_eq!(gaps.next(), None); |
| 1527 |
} |
| 1528 |
|
| 1529 |
#[test] |
| 1530 |
fn item_overlapping_start_of_outer_range() { |
| 1531 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1532 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1533 |
// ◌ ●-------● ◌ ◌ ◌ ◌ |
| 1534 |
range_map.insert(1..=5, ()); |
| 1535 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1536 |
// ◌ ◌ ◌ ◌ ◌ ◆-----◆ ◌ |
| 1537 |
let outer_range = 5..=8; |
| 1538 |
let mut gaps = range_map.gaps(&outer_range); |
| 1539 |
// Should yield from just past the end of the stored item |
| 1540 |
// to the end of the outer range. |
| 1541 |
assert_eq!(gaps.next(), Some(6..=8)); |
| 1542 |
assert_eq!(gaps.next(), None); |
| 1543 |
// Gaps iterator should be fused. |
| 1544 |
assert_eq!(gaps.next(), None); |
| 1545 |
assert_eq!(gaps.next(), None); |
| 1546 |
} |
| 1547 |
|
| 1548 |
#[test] |
| 1549 |
fn item_starting_at_start_of_outer_range() { |
| 1550 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1551 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1552 |
// ◌ ◌ ◌ ◌ ◌ ●-● ◌ ◌ ◌ |
| 1553 |
range_map.insert(5..=6, ()); |
| 1554 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1555 |
// ◌ ◌ ◌ ◌ ◌ ◆-----◆ ◌ |
| 1556 |
let outer_range = 5..=8; |
| 1557 |
let mut gaps = range_map.gaps(&outer_range); |
| 1558 |
// Should yield from just past the item onwards. |
| 1559 |
assert_eq!(gaps.next(), Some(7..=8)); |
| 1560 |
assert_eq!(gaps.next(), None); |
| 1561 |
// Gaps iterator should be fused. |
| 1562 |
assert_eq!(gaps.next(), None); |
| 1563 |
assert_eq!(gaps.next(), None); |
| 1564 |
} |
| 1565 |
|
| 1566 |
#[test] |
| 1567 |
fn items_floating_inside_outer_range() { |
| 1568 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1569 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1570 |
// ◌ ◌ ◌ ◌ ◌ ◌ ●-● ◌ ◌ |
| 1571 |
range_map.insert(6..=7, ()); |
| 1572 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1573 |
// ◌ ◌ ◌ ●-● ◌ ◌ ◌ ◌ ◌ |
| 1574 |
range_map.insert(3..=4, ()); |
| 1575 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1576 |
// ◌ ◆-------------◆ ◌ |
| 1577 |
let outer_range = 1..=8; |
| 1578 |
let mut gaps = range_map.gaps(&outer_range); |
| 1579 |
// Should yield gaps at start, between items, |
| 1580 |
// and at end. |
| 1581 |
assert_eq!(gaps.next(), Some(1..=2)); |
| 1582 |
assert_eq!(gaps.next(), Some(5..=5)); |
| 1583 |
assert_eq!(gaps.next(), Some(8..=8)); |
| 1584 |
assert_eq!(gaps.next(), None); |
| 1585 |
// Gaps iterator should be fused. |
| 1586 |
assert_eq!(gaps.next(), None); |
| 1587 |
assert_eq!(gaps.next(), None); |
| 1588 |
} |
| 1589 |
|
| 1590 |
#[test] |
| 1591 |
fn item_ending_at_end_of_outer_range() { |
| 1592 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1593 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1594 |
// ◌ ◌ ◌ ◌ ◌ ◌ ◌ ●-● ◌ |
| 1595 |
range_map.insert(7..=8, ()); |
| 1596 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1597 |
// ◌ ◌ ◌ ◌ ◌ ◆-----◆ ◌ |
| 1598 |
let outer_range = 5..=8; |
| 1599 |
let mut gaps = range_map.gaps(&outer_range); |
| 1600 |
// Should yield from the start of the outer range |
| 1601 |
// up to just before the start of the stored item. |
| 1602 |
assert_eq!(gaps.next(), Some(5..=6)); |
| 1603 |
assert_eq!(gaps.next(), None); |
| 1604 |
// Gaps iterator should be fused. |
| 1605 |
assert_eq!(gaps.next(), None); |
| 1606 |
assert_eq!(gaps.next(), None); |
| 1607 |
} |
| 1608 |
|
| 1609 |
#[test] |
| 1610 |
fn item_overlapping_end_of_outer_range() { |
| 1611 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1612 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1613 |
// ◌ ◌ ◌ ◌ ◌ ●---● ◌ ◌ |
| 1614 |
range_map.insert(5..=6, ()); |
| 1615 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1616 |
// ◌ ◌ ◆-----◆ ◌ ◌ ◌ ◌ |
| 1617 |
let outer_range = 2..=5; |
| 1618 |
let mut gaps = range_map.gaps(&outer_range); |
| 1619 |
// Should yield from the start of the outer range |
| 1620 |
// up to the start of the stored item. |
| 1621 |
assert_eq!(gaps.next(), Some(2..=4)); |
| 1622 |
assert_eq!(gaps.next(), None); |
| 1623 |
// Gaps iterator should be fused. |
| 1624 |
assert_eq!(gaps.next(), None); |
| 1625 |
assert_eq!(gaps.next(), None); |
| 1626 |
} |
| 1627 |
|
| 1628 |
#[test] |
| 1629 |
fn item_touching_end_of_outer_range() { |
| 1630 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1631 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1632 |
// ◌ ◌ ◌ ◌ ◌ ●-----● ◌ |
| 1633 |
range_map.insert(5..=9, ()); |
| 1634 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1635 |
// ◌ ◆-----◆ ◌ ◌ ◌ ◌ ◌ |
| 1636 |
let outer_range = 1..=4; |
| 1637 |
let mut gaps = range_map.gaps(&outer_range); |
| 1638 |
// Should yield the entire outer range. |
| 1639 |
assert_eq!(gaps.next(), Some(1..=4)); |
| 1640 |
assert_eq!(gaps.next(), None); |
| 1641 |
// Gaps iterator should be fused. |
| 1642 |
assert_eq!(gaps.next(), None); |
| 1643 |
assert_eq!(gaps.next(), None); |
| 1644 |
} |
| 1645 |
|
| 1646 |
#[test] |
| 1647 |
fn item_after_outer_range() { |
| 1648 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1649 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1650 |
// ◌ ◌ ◌ ◌ ◌ ◌ ●---● ◌ |
| 1651 |
range_map.insert(6..=7, ()); |
| 1652 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1653 |
// ◌ ◆-----◆ ◌ ◌ ◌ ◌ ◌ |
| 1654 |
let outer_range = 1..=4; |
| 1655 |
let mut gaps = range_map.gaps(&outer_range); |
| 1656 |
// Should yield the entire outer range. |
| 1657 |
assert_eq!(gaps.next(), Some(1..=4)); |
| 1658 |
assert_eq!(gaps.next(), None); |
| 1659 |
// Gaps iterator should be fused. |
| 1660 |
assert_eq!(gaps.next(), None); |
| 1661 |
assert_eq!(gaps.next(), None); |
| 1662 |
} |
| 1663 |
|
| 1664 |
#[test] |
| 1665 |
fn zero_width_outer_range_with_items_away_from_both_sides() { |
| 1666 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1667 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1668 |
// ◌ ◆---◆ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1669 |
range_map.insert(1..=3, ()); |
| 1670 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1671 |
// ◌ ◌ ◌ ◌ ◌ ◆---◆ ◌ ◌ |
| 1672 |
range_map.insert(5..=7, ()); |
| 1673 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1674 |
// ◌ ◌ ◌ ◌ ◆ ◌ ◌ ◌ ◌ ◌ |
| 1675 |
let outer_range = 4..=4; |
| 1676 |
let mut gaps = range_map.gaps(&outer_range); |
| 1677 |
// Should yield a zero-width gap. |
| 1678 |
assert_eq!(gaps.next(), Some(4..=4)); |
| 1679 |
// Gaps iterator should be fused. |
| 1680 |
assert_eq!(gaps.next(), None); |
| 1681 |
assert_eq!(gaps.next(), None); |
| 1682 |
} |
| 1683 |
|
| 1684 |
#[test] |
| 1685 |
fn zero_width_outer_range_with_items_touching_both_sides() { |
| 1686 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1687 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1688 |
// ◌ ◌ ◆-◆ ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1689 |
range_map.insert(2..=3, ()); |
| 1690 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1691 |
// ◌ ◌ ◌ ◌ ◌ ◆---◆ ◌ ◌ ◌ |
| 1692 |
range_map.insert(5..=6, ()); |
| 1693 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1694 |
// ◌ ◌ ◌ ◌ ◆ ◌ ◌ ◌ ◌ ◌ |
| 1695 |
let outer_range = 4..=4; |
| 1696 |
let mut gaps = range_map.gaps(&outer_range); |
| 1697 |
// Should yield no gaps. |
| 1698 |
assert_eq!(gaps.next(), Some(4..=4)); |
| 1699 |
// Gaps iterator should be fused. |
| 1700 |
assert_eq!(gaps.next(), None); |
| 1701 |
assert_eq!(gaps.next(), None); |
| 1702 |
} |
| 1703 |
|
| 1704 |
#[test] |
| 1705 |
fn empty_outer_range_with_item_straddling() { |
| 1706 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1707 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1708 |
// ◌ ◌ ◆-----◆ ◌ ◌ ◌ ◌ ◌ |
| 1709 |
range_map.insert(2..=5, ()); |
| 1710 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1711 |
// ◌ ◌ ◌ ◌ ◆ ◌ ◌ ◌ ◌ ◌ |
| 1712 |
let outer_range = 4..=4; |
| 1713 |
let mut gaps = range_map.gaps(&outer_range); |
| 1714 |
// Should yield no gaps. |
| 1715 |
assert_eq!(gaps.next(), None); |
| 1716 |
// Gaps iterator should be fused. |
| 1717 |
assert_eq!(gaps.next(), None); |
| 1718 |
assert_eq!(gaps.next(), None); |
| 1719 |
} |
| 1720 |
|
| 1721 |
#[test] |
| 1722 |
fn no_empty_gaps() { |
| 1723 |
// Make two ranges different values so they don't |
| 1724 |
// get coalesced. |
| 1725 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1726 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1727 |
// ◌ ◌ ◌ ◌ ◆-◆ ◌ ◌ ◌ ◌ |
| 1728 |
range_map.insert(4..=5, true); |
| 1729 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1730 |
// ◌ ◌ ◆-◆ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1731 |
range_map.insert(2..=3, false); |
| 1732 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1733 |
// ◌ ●-------------● ◌ |
| 1734 |
let outer_range = 1..=8; |
| 1735 |
let mut gaps = range_map.gaps(&outer_range); |
| 1736 |
// Should yield gaps at start and end, but not between the |
| 1737 |
// two touching items. |
| 1738 |
assert_eq!(gaps.next(), Some(1..=1)); |
| 1739 |
assert_eq!(gaps.next(), Some(6..=8)); |
| 1740 |
assert_eq!(gaps.next(), None); |
| 1741 |
// Gaps iterator should be fused. |
| 1742 |
assert_eq!(gaps.next(), None); |
| 1743 |
assert_eq!(gaps.next(), None); |
| 1744 |
} |
| 1745 |
|
| 1746 |
#[test] |
| 1747 |
fn no_overflow_finding_gaps_at_key_domain_extremes() { |
| 1748 |
// Items and outer range both at extremes. |
| 1749 |
let mut range_map: RangeInclusiveMap<u8, bool> = RangeInclusiveMap::new(); |
| 1750 |
range_map.insert(0..=255, false); |
| 1751 |
range_map.gaps(&(0..=255)); |
| 1752 |
|
| 1753 |
// Items at extremes with gaps in middle. |
| 1754 |
let mut range_map: RangeInclusiveMap<u8, bool> = RangeInclusiveMap::new(); |
| 1755 |
range_map.insert(0..=255, false); |
| 1756 |
range_map.gaps(&(0..=5)); |
| 1757 |
range_map.gaps(&(250..=255)); |
| 1758 |
|
| 1759 |
// Items just in from extremes. |
| 1760 |
let mut range_map: RangeInclusiveMap<u8, bool> = RangeInclusiveMap::new(); |
| 1761 |
range_map.insert(0..=255, false); |
| 1762 |
range_map.gaps(&(1..=5)); |
| 1763 |
range_map.gaps(&(250..=254)); |
| 1764 |
|
| 1765 |
// Outer range just in from extremes, |
| 1766 |
// items at extremes. |
| 1767 |
let mut range_map: RangeInclusiveMap<u8, bool> = RangeInclusiveMap::new(); |
| 1768 |
range_map.insert(1..=254, false); |
| 1769 |
range_map.gaps(&(0..=5)); |
| 1770 |
range_map.gaps(&(250..=255)); |
| 1771 |
} |
| 1772 |
|
| 1773 |
#[test] |
| 1774 |
fn adjacent_unit_width_items() { |
| 1775 |
// Items two items next to each other at the start, and at the end. |
| 1776 |
let mut range_map: RangeInclusiveMap<u8, bool> = RangeInclusiveMap::new(); |
| 1777 |
range_map.insert(0..=0, false); |
| 1778 |
range_map.insert(1..=1, true); |
| 1779 |
range_map.insert(254..=254, false); |
| 1780 |
range_map.insert(255..=255, true); |
| 1781 |
|
| 1782 |
let outer_range = 0..=255; |
| 1783 |
let mut gaps = range_map.gaps(&outer_range); |
| 1784 |
// Should yield one big gap in the middle. |
| 1785 |
assert_eq!(gaps.next(), Some(2..=253)); |
| 1786 |
// Gaps iterator should be fused. |
| 1787 |
assert_eq!(gaps.next(), None); |
| 1788 |
assert_eq!(gaps.next(), None); |
| 1789 |
} |
| 1790 |
|
| 1791 |
// Overlapping tests |
| 1792 |
|
| 1793 |
#[test] |
| 1794 |
fn overlapping_ref_with_empty_map() { |
| 1795 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1796 |
// ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1797 |
let range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1798 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1799 |
// ◌ ◆-------------◆ ◌ |
| 1800 |
let query_range = 1..=8; |
| 1801 |
let mut overlapping = range_map.overlapping(&query_range); |
| 1802 |
// Should not yield any items. |
| 1803 |
assert_eq!(overlapping.next(), None); |
| 1804 |
// Gaps iterator should be fused. |
| 1805 |
assert_eq!(overlapping.next(), None); |
| 1806 |
} |
| 1807 |
|
| 1808 |
#[test] |
| 1809 |
fn overlapping_owned_with_empty_map() { |
| 1810 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1811 |
// ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1812 |
let range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1813 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1814 |
// ◌ ◆-------------◆ ◌ |
| 1815 |
let query_range = 1..=8; |
| 1816 |
let mut overlapping = range_map.overlapping(query_range); |
| 1817 |
// Should not yield any items. |
| 1818 |
assert_eq!(overlapping.next(), None); |
| 1819 |
// Gaps iterator should be fused. |
| 1820 |
assert_eq!(overlapping.next(), None); |
| 1821 |
} |
| 1822 |
|
| 1823 |
#[test] |
| 1824 |
fn overlapping_partial_edges_complete_middle() { |
| 1825 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1826 |
|
| 1827 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1828 |
// ●-● ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1829 |
range_map.insert(0..=1, ()); |
| 1830 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1831 |
// ◌ ◌ ◌ ●-● ◌ ◌ ◌ ◌ ◌ |
| 1832 |
range_map.insert(3..=4, ()); |
| 1833 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1834 |
// ◌ ◌ ◌ ◌ ◌ ◌ ●-● ◌ ◌ |
| 1835 |
range_map.insert(6..=7, ()); |
| 1836 |
|
| 1837 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1838 |
// ◌ ◆---------◆ ◌ ◌ ◌ |
| 1839 |
let query_range = 1..=6; |
| 1840 |
|
| 1841 |
let mut overlapping = range_map.overlapping(&query_range); |
| 1842 |
|
| 1843 |
// Should yield partially overlapped range at start. |
| 1844 |
assert_eq!(overlapping.next(), Some((&(0..=1), &()))); |
| 1845 |
// Should yield completely overlapped range in middle. |
| 1846 |
assert_eq!(overlapping.next(), Some((&(3..=4), &()))); |
| 1847 |
// Should yield partially overlapped range at end. |
| 1848 |
assert_eq!(overlapping.next(), Some((&(6..=7), &()))); |
| 1849 |
// Gaps iterator should be fused. |
| 1850 |
assert_eq!(overlapping.next(), None); |
| 1851 |
assert_eq!(overlapping.next(), None); |
| 1852 |
} |
| 1853 |
|
| 1854 |
#[test] |
| 1855 |
fn overlapping_non_overlapping_edges_complete_middle() { |
| 1856 |
let mut range_map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1857 |
|
| 1858 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1859 |
// ●-● ◌ ◌ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1860 |
range_map.insert(0..=1, ()); |
| 1861 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1862 |
// ◌ ◌ ◌ ●-● ◌ ◌ ◌ ◌ ◌ |
| 1863 |
range_map.insert(3..=4, ()); |
| 1864 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1865 |
// ◌ ◌ ◌ ◌ ◌ ◌ ●-● ◌ ◌ |
| 1866 |
range_map.insert(6..=7, ()); |
| 1867 |
|
| 1868 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1869 |
// ◌ ◌ ◆-----◆ ◌ ◌ ◌ ◌ |
| 1870 |
let query_range = 2..=5; |
| 1871 |
|
| 1872 |
let mut overlapping = range_map.overlapping(&query_range); |
| 1873 |
|
| 1874 |
// Should only yield the completely overlapped range in middle. |
| 1875 |
// (Not the ranges that are touched by not covered to either side.) |
| 1876 |
assert_eq!(overlapping.next(), Some((&(3..=4), &()))); |
| 1877 |
// Gaps iterator should be fused. |
| 1878 |
assert_eq!(overlapping.next(), None); |
| 1879 |
assert_eq!(overlapping.next(), None); |
| 1880 |
} |
| 1881 |
|
| 1882 |
/// |
| 1883 |
/// impl Debug |
| 1884 |
/// |
| 1885 |
|
| 1886 |
#[test] |
| 1887 |
fn map_debug_repr_looks_right() { |
| 1888 |
let mut map: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1889 |
|
| 1890 |
// Empty |
| 1891 |
assert_eq!(format!("{:?}", map), "{}"); |
| 1892 |
|
| 1893 |
// One entry |
| 1894 |
map.insert(2..=5, ()); |
| 1895 |
assert_eq!(format!("{:?}", map), "{2..=5: ()}"); |
| 1896 |
|
| 1897 |
// Many entries |
| 1898 |
map.insert(7..=8, ()); |
| 1899 |
map.insert(10..=11, ()); |
| 1900 |
assert_eq!(format!("{:?}", map), "{2..=5: (), 7..=8: (), 10..=11: ()}"); |
| 1901 |
} |
| 1902 |
|
| 1903 |
// impl Default where T: ?Default |
| 1904 |
|
| 1905 |
#[test] |
| 1906 |
fn always_default() { |
| 1907 |
struct NoDefault; |
| 1908 |
RangeInclusiveMap::<NoDefault, NoDefault>::default(); |
| 1909 |
} |
| 1910 |
|
| 1911 |
// impl Serialize |
| 1912 |
|
| 1913 |
#[cfg(feature = "serde1")] |
| 1914 |
#[test] |
| 1915 |
fn serialization() { |
| 1916 |
let mut range_map: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1917 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1918 |
// ◌ ◆---◆ ◌ ◌ ◌ ◌ ◌ ◌ |
| 1919 |
range_map.insert(1..=3, false); |
| 1920 |
// 0 1 2 3 4 5 6 7 8 9 |
| 1921 |
// ◌ ◌ ◌ ◌ ◌ ◆---◆ ◌ ◌ |
| 1922 |
range_map.insert(5..=7, true); |
| 1923 |
let output = serde_json::to_string(&range_map).expect("Failed to serialize"); |
| 1924 |
assert_eq!(output, "[[[1,3],false],[[5,7],true]]"); |
| 1925 |
} |
| 1926 |
|
| 1927 |
// impl Deserialize |
| 1928 |
|
| 1929 |
#[cfg(feature = "serde1")] |
| 1930 |
#[test] |
| 1931 |
fn deserialization() { |
| 1932 |
let input = "[[[1,3],false],[[5,7],true]]"; |
| 1933 |
let range_map: RangeInclusiveMap<u32, bool> = |
| 1934 |
serde_json::from_str(input).expect("Failed to deserialize"); |
| 1935 |
let reserialized = serde_json::to_string(&range_map).expect("Failed to re-serialize"); |
| 1936 |
assert_eq!(reserialized, input); |
| 1937 |
} |
| 1938 |
|
| 1939 |
// const fn |
| 1940 |
|
| 1941 |
#[cfg(feature = "const_fn")] |
| 1942 |
const _MAP: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new(); |
| 1943 |
#[cfg(feature = "const_fn")] |
| 1944 |
const _MAP2: RangeInclusiveMap<u32, bool> = RangeInclusiveMap::new_with_step_fns(); |
| 1945 |
|
| 1946 |
// Equality |
| 1947 |
|
| 1948 |
#[test] |
| 1949 |
fn test_equality_same_start_different_end() { |
| 1950 |
let mut a: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1951 |
a.insert(0..=5, ()); |
| 1952 |
|
| 1953 |
let mut b: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1954 |
b.insert(0..=2, ()); |
| 1955 |
|
| 1956 |
assert_ne!(a, b); |
| 1957 |
} |
| 1958 |
|
| 1959 |
#[test] |
| 1960 |
fn test_equality_identical_ranges() { |
| 1961 |
let mut a: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1962 |
a.insert(0..=5, ()); |
| 1963 |
|
| 1964 |
let mut b: RangeInclusiveMap<u32, ()> = RangeInclusiveMap::new(); |
| 1965 |
b.insert(0..=5, ()); |
| 1966 |
|
| 1967 |
assert_eq!(a, b); |
| 1968 |
} |
| 1969 |
|
| 1970 |
#[cfg(feature = "quickcheck")] |
| 1971 |
quickcheck::quickcheck! { |
| 1972 |
fn prop(xs: RangeInclusiveMap<usize, usize>) -> bool { |
| 1973 |
xs == xs |
| 1974 |
} |
| 1975 |
} |
| 1976 |
} |