refactored flip_bound

This commit is contained in:
ripytide 2022-12-04 01:09:37 +00:00
parent d161626acd
commit 2a92be6515
4 changed files with 21 additions and 51 deletions

View File

@ -112,13 +112,12 @@ Issue (or even open a new one) and I'd be happy to implement it.
To summarise:
- No coalescing/merge insert functions, yet
- No `gaps()` iterator function, yet
- Missing some functions common to BTreeMap and BTreeSet like:
- `clear()`
- `is_subset()`
- etc... a bunch more
- Sub-optimal use of unnecessary `cloned()` just to placate the borrow checker
- Use TryFrom<(Bound, Bound)> instead of [`TryFromBounds`] (relys on
- Can't use TryFrom<(Bound, Bound)> instead of [`TryFromBounds`] (relys on
upstream to impl)
- The data structures are lacking a lot of useful traits, such as:
- FromIterator

View File

@ -70,15 +70,6 @@ impl<T> StartBound<T> {
_ => panic!("unsuitable operation"),
}
}
pub(crate) fn into_opposite(self) -> StartBound<T> {
match self {
StartBound::Included(point) => StartBound::Excluded(point),
StartBound::Excluded(point) => StartBound::Included(point),
StartBound::Unbounded => StartBound::Unbounded,
_ => panic!("unsuitable operation"),
}
}
}
impl<T> Eq for StartBound<T> where T: PartialEq {}

View File

@ -686,24 +686,12 @@ where
// the bounds included not excluded like with other bounds in
// artificials
let artificial_start = (
Bound::from(
StartBound::from(outer_range_bounds.start_bound())
.into_opposite(),
),
Bound::from(
StartBound::from(outer_range_bounds.start_bound())
.into_opposite(),
),
flip_bound(outer_range_bounds.start_bound()),
flip_bound(outer_range_bounds.start_bound()),
);
let artificial_end = (
Bound::from(
StartBound::from(outer_range_bounds.end_bound())
.into_opposite(),
),
Bound::from(
StartBound::from(outer_range_bounds.end_bound())
.into_opposite(),
),
flip_bound(outer_range_bounds.end_bound()),
flip_bound(outer_range_bounds.end_bound()),
);
let artificials = once(artificial_start)
.chain(inners)
@ -714,12 +702,7 @@ where
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()),
)
(flip_bound(first_end), flip_bound(second_start))
})
.filter(is_valid_range_bounds::<(Bound<&I>, Bound<&I>), I>);
}
@ -822,19 +805,16 @@ where
false => None,
true => Some((
base_start_bound.cloned(),
Bound::from(StartBound::from(cut_start_bound).into_opposite())
.cloned(),
flip_bound(cut_start_bound).cloned(),
)),
};
let right_section = match StartBound::from(cut_end_bound).into_end_bound()
< StartBound::from(base_end_bound).into_end_bound()
{
false => None,
true => Some((
Bound::from(StartBound::from(cut_end_bound).into_opposite())
.cloned(),
base_end_bound.cloned(),
)),
true => {
Some((flip_bound(cut_end_bound).cloned(), base_end_bound.cloned()))
}
};
match (left_section, right_section) {
@ -891,6 +871,14 @@ where
}
}
fn flip_bound<I>(bound: Bound<&I>) -> Bound<&I> {
match bound {
Bound::Included(point) => Bound::Excluded(point),
Bound::Excluded(point) => Bound::Included(point),
Bound::Unbounded => Bound::Unbounded,
}
}
#[cfg(test)]
mod tests {
use std::ops::{Bound, Range, RangeBounds};

View File

@ -1,23 +1,15 @@
- make sure all returned iterators are DoubleEndedIterators and the
documentation mentions it
- add caveat for TryFrom on ranges
- write more tests for more complicated functions
- write docs for everything again
- check every function that takes range_Bounds to make sure they panic
on invalid range_bounds
- use it in robot_Sweet_graph for a bit before publishing
- add issues to github for all the caveats and cloned() optimisations
- add issues to github for all the caveats
- review caveats again
- copy readme to lib.rs docs again
- run cargo fmt
- run and fix cargo clippy
- take a look around idiomatic rust for a bit first
@ -26,10 +18,10 @@
- refactor to undo into_StartBound -> StartBound method ->
from_StartBound and replace with a similar TraitExt for Bound
- refactor opposuite for bound use too
- update lines of code figures on docs
- use it in robot_Sweet_graph for a bit before publishing
- PUBLISH
- add links to [`RangeBoundsSet`] and map after docs.rs is live with