finished implementing gaps() and contains_range_bounds() on map

This commit is contained in:
ripytide 2022-12-03 20:24:29 +00:00
parent ac054199f1
commit ad56e8df00
5 changed files with 68 additions and 2 deletions

10
Cargo.lock generated
View File

@ -14,6 +14,15 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "num-traits"
version = "0.2.15"
@ -55,6 +64,7 @@ name = "range_bounds_map"
version = "0.0.1"
dependencies = [
"either",
"itertools",
"ordered-float",
"serde",
]

View File

@ -26,6 +26,7 @@ categories = ["data-structures"]
[dependencies]
either = "1.8.0"
serde = {version = "1.0.148", features = ["derive"]}
itertools = "0.10.5"
[dev-dependencies]
ordered-float = "3.4.0"

View File

@ -75,6 +75,7 @@ impl<T> StartBound<T> {
match self {
StartBound::Included(point) => StartBound::Excluded(point),
StartBound::Excluded(point) => StartBound::Included(point),
StartBound::Unbounded => StartBound::Unbounded,
_ => panic!("unsuitable operation"),
}
}

View File

@ -23,6 +23,7 @@ use std::iter::once;
use std::ops::{Bound, RangeBounds};
use either::Either;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use crate::bounds::StartBound;
@ -640,6 +641,46 @@ where
return Ok(());
}
pub fn gaps<'a, Q>(
&'a mut self,
outer_range_bounds: &'a Q,
) -> impl Iterator<Item = impl RangeBounds<I> + 'a>
where
Q: RangeBounds<I>,
{
// I'm in love with how clean/mindblowing this entire function is
let inners = self
.overlapping(outer_range_bounds)
.map(|(key, _)| (key.start_bound(), key.end_bound()));
let tuple_outer = (
outer_range_bounds.start_bound(),
outer_range_bounds.end_bound(),
);
let artificials =
once(tuple_outer).chain(inners).chain(once(tuple_outer));
return artificials
.tuple_windows()
.map(|((_, first_end), (second_start, _))| {
(
// Flip the ends of the inside RangeBounds between
// adjacent RangeBounds in the map
Bound::from(StartBound::from(first_end).into_opposite()),
Bound::from(StartBound::from(second_start).into_opposite()),
)
})
.filter(is_valid_range_bounds::<(Bound<&I>, Bound<&I>), I>);
}
pub fn contains_range_bounds<Q>(&mut self, range_bounds: &Q) -> bool
where
Q: RangeBounds<I>,
{
// Soooo clean and mathematical 🥰!
self.gaps(range_bounds).next().is_some()
}
}
impl<const N: usize, I, K, V> TryFrom<[(K, V); N]> for RangeBoundsMap<I, K, V>
@ -865,9 +906,14 @@ mod tests {
}
});
if our_answer != mathematical_definition_of_contains_range_bounds {
if our_answer
!= mathematical_definition_of_contains_range_bounds
{
dbg!(base_range_bounds, query_range_bounds);
dbg!(mathematical_definition_of_contains_range_bounds, our_answer);
dbg!(
mathematical_definition_of_contains_range_bounds,
our_answer
);
panic!("Discrepency in .contains_range_bounds() detected!");
}
}

View File

@ -21,9 +21,17 @@
- run and fix cargo clippy
- take a look around idiomatic rust for a bit first
- review method parameter names for all public functions
- refactor to undo into_StartBound -> StartBound method ->
from_StartBound and replace with a similar TraitExt for Bound
- update lines of code figures on docs
- PUBLISH
- add links to [`RangeBoundsSet`] and map after docs.rs is live with
the docs, and generally check for dead links on docs and readme
- tell people in issues of other rangemap libraries about my library
stonks advertising