add DiscreteBounds constructors

This commit is contained in:
ripytide
2023-04-20 15:30:28 +01:00
parent 0b0251ee9c
commit e91428619e
4 changed files with 39 additions and 20 deletions
-7
View File
@@ -1,10 +1,3 @@
edition="2024"
version = "Two"
hard_tabs=true
imports_granularity="Module"
group_imports="StdExternalCrate"
max_width=80
format_code_in_doc_comments=true
doc_comment_code_block_width=70
unstable_features = true
comment_width=70
+24
View File
@@ -17,6 +17,10 @@ You should have received a copy of the GNU Affero General Public License
along with range_bounds_map. If not, see <https://www.gnu.org/licenses/>.
*/
use std::ops::Bound;
use crate::stepable::Stepable;
pub struct DiscreteBounds<I> {
start: DiscreteBound<I>,
end: DiscreteBound<I>,
@@ -26,3 +30,23 @@ pub enum DiscreteBound<I> {
Included(I),
Unbounded,
}
impl<I> DiscreteBound<I>
where
I: Stepable,
{
pub fn start(bound: Bound<I>) -> Self {
match bound {
Bound::Included(x) => DiscreteBound::Included(x),
Bound::Excluded(x) => DiscreteBound::Included(x.up().unwrap()),
Bound::Unbounded => DiscreteBound::Unbounded,
}
}
pub fn end(bound: Bound<I>) -> Self {
match bound {
Bound::Included(x) => DiscreteBound::Included(x),
Bound::Excluded(x) => DiscreteBound::Included(x.down().unwrap()),
Bound::Unbounded => DiscreteBound::Unbounded,
}
}
}
+8 -6
View File
@@ -33,7 +33,7 @@ use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::bound_ord::DiscreteBoundOrd;
use crate::discrete_bounds::DiscreteBounds;
use crate::discrete_bounds::{DiscreteBound, DiscreteBounds};
use crate::stepable::Stepable;
use crate::utils::{
cmp_range_with_discrete_bound_ord, cut_range, flip_bound, is_valid_range,
@@ -1759,7 +1759,7 @@ where
/// A simple helper trait to make my implemtation nicer, if you
/// already implement RangeBounds and Copy on your type then this will
/// also be implemted.
pub trait DiscreteRange<I>: Copy {
pub trait DiscreteRange<I> {
fn start(&self) -> DiscreteBoundOrd<I>;
fn end(&self) -> DiscreteBoundOrd<I>;
}
@@ -1768,11 +1768,13 @@ where
I: Copy + Stepable,
K: RangeBounds<I> + Copy,
{
fn start(&self) -> Bound<I> {
self.start_bound().cloned()
fn start(&self) -> DiscreteBoundOrd<I> {
DiscreteBoundOrd::start(DiscreteBound::start(
self.start_bound().cloned(),
))
}
fn end(&self) -> Bound<I> {
self.end_bound().cloned()
fn end(&self) -> DiscreteBoundOrd<I> {
DiscreteBoundOrd::end(DiscreteBound::end(self.end_bound().cloned()))
}
}
+7 -7
View File
@@ -59,8 +59,8 @@ where
match a.start() < b.start() {
true => {
match (
contains_bound_ord(a, DiscreteBoundOrd::start(b.start())),
contains_bound_ord(a, DiscreteBoundOrd::end(b.end())),
contains_bound_ord(a, b.start()),
contains_bound_ord(a, b.end()),
) {
(false, false) => Config::LeftFirstNonOverlapping,
(true, false) => Config::LeftFirstPartialOverlap,
@@ -70,8 +70,8 @@ where
}
false => {
match (
contains_bound_ord(b, DiscreteBoundOrd::start(a.start())),
contains_bound_ord(b, DiscreteBoundOrd::end(a.end())),
contains_bound_ord(b, a.start()),
contains_bound_ord(b, a.end()),
) {
(false, false) => Config::RightFirstNonOverlapping,
(true, false) => Config::RightFirstPartialOverlap,
@@ -83,9 +83,9 @@ where
}
enum SortedConfig<I> {
NonOverlapping((Bound<I>, Bound<I>), (Bound<I>, Bound<I>)),
PartialOverlap((Bound<I>, Bound<I>), (Bound<I>, Bound<I>)),
Swallowed((Bound<I>, Bound<I>), (Bound<I>, Bound<I>)),
NonOverlapping((DiscreteBoundOrd<I>, DiscreteBoundOrd<I>), (DiscreteBoundOrd<I>, DiscreteBoundOrd<I>)),
PartialOverlap((DiscreteBoundOrd<I>, DiscreteBoundOrd<I>), (DiscreteBoundOrd<I>, DiscreteBoundOrd<I>)),
Swallowed((DiscreteBoundOrd<I>, DiscreteBoundOrd<I>), (DiscreteBoundOrd<I>, DiscreteBoundOrd<I>)),
}
fn sorted_config<I, A, B>(a: A, b: B) -> SortedConfig<I>
where