continued to refactor to use the super traits and start converting to no_std

This commit is contained in:
ripytide 2023-12-03 20:13:08 +00:00
parent 6eb28f6137
commit dc02011b6c
No known key found for this signature in database
GPG Key ID: B2629F9EC7C2FE8C
5 changed files with 38 additions and 36 deletions

View File

@ -1,4 +1,4 @@
edition="2024" edition="2021"
version = "Two" version = "Two"
hard_tabs=true hard_tabs=true
imports_granularity="Module" imports_granularity="Module"

View File

@ -17,10 +17,14 @@ You should have received a copy of the GNU Affero General Public License
along with discrete_range_map. If not, see <https://www.gnu.org/licenses/>. along with discrete_range_map. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::cmp::Ordering; #![cfg_attr(not(any(feature = "std", test)), no_std)]
use std::fmt::{self, Debug};
use std::iter::once; extern crate alloc;
use std::marker::PhantomData;
use core::cmp::Ordering;
use core::fmt::{self, Debug};
use core::iter::once;
use core::marker::PhantomData;
use btree_monstrousity::btree_map::{ use btree_monstrousity::btree_map::{
IntoIter as BTreeMapIntoIter, SearchBoundCustom, IntoIter as BTreeMapIntoIter, SearchBoundCustom,

View File

@ -1,5 +1,5 @@
use std::fmt; use core::fmt;
use std::marker::PhantomData; use core::marker::PhantomData;
use serde::de::{SeqAccess, Visitor}; use serde::de::{SeqAccess, Visitor};
use serde::ser::SerializeSeq; use serde::ser::SerializeSeq;

View File

@ -17,12 +17,11 @@ You should have received a copy of the GNU Affero General Public License
along with discrete_range_map. If not, see <https://www.gnu.org/licenses/>. along with discrete_range_map. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::ops::{Bound, RangeBounds}; use core::ops::{Bound, RangeBounds};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::discrete_range_map::InclusiveRange; use crate::discrete_range_map::{InclusiveRange, PointType};
use crate::DiscreteFinite;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct InclusiveInterval<I> { pub struct InclusiveInterval<I> {
@ -30,11 +29,11 @@ pub struct InclusiveInterval<I> {
pub end: I, pub end: I,
} }
impl<I> InclusiveInterval<I> where I: Ord + DiscreteFinite + Copy {} impl<I> InclusiveInterval<I> where I: PointType {}
impl<I> RangeBounds<I> for InclusiveInterval<I> impl<I> RangeBounds<I> for InclusiveInterval<I>
where where
I: Copy, I: PointType,
{ {
fn start_bound(&self) -> Bound<&I> { fn start_bound(&self) -> Bound<&I> {
Bound::Included(&self.start) Bound::Included(&self.start)
@ -47,7 +46,7 @@ where
impl<I> InclusiveRange<I> for InclusiveInterval<I> impl<I> InclusiveRange<I> for InclusiveInterval<I>
where where
I: Copy, I: PointType,
{ {
fn start(&self) -> I { fn start(&self) -> I {
self.start self.start

View File

@ -17,16 +17,15 @@ You should have received a copy of the GNU Affero General Public License
along with discrete_range_map. If not, see <https://www.gnu.org/licenses/>. along with discrete_range_map. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::cmp::Ordering; use core::cmp::Ordering;
use crate::discrete_finite::DiscreteFinite; use crate::discrete_range_map::{PointType, RangeType};
use crate::discrete_range_map::InclusiveRange;
use crate::interval::InclusiveInterval; use crate::interval::InclusiveInterval;
pub(crate) fn cmp_point_with_range<I, K>(point: I, range: K) -> Ordering pub(crate) fn cmp_point_with_range<I, K>(point: I, range: K) -> Ordering
where where
I: Ord, I: PointType,
K: InclusiveRange<I>, K: RangeType<I>,
{ {
if point < range.start() { if point < range.start() {
Ordering::Less Ordering::Less
@ -49,9 +48,9 @@ pub(crate) enum Config {
} }
pub(crate) fn config<I, A, B>(a: A, b: B) -> Config pub(crate) fn config<I, A, B>(a: A, b: B) -> Config
where where
A: InclusiveRange<I> + Copy, I: PointType,
B: InclusiveRange<I> + Copy, A: RangeType<I>,
I: Ord, B: RangeType<I>,
{ {
if a.start() < b.start() { if a.start() < b.start() {
match (contains_point(a, b.start()), contains_point(a, b.end())) { match (contains_point(a, b.start()), contains_point(a, b.end())) {
@ -77,9 +76,9 @@ enum SortedConfig<I> {
} }
fn sorted_config<I, A, B>(a: A, b: B) -> SortedConfig<I> fn sorted_config<I, A, B>(a: A, b: B) -> SortedConfig<I>
where where
A: InclusiveRange<I> + Copy, I: PointType,
B: InclusiveRange<I> + Copy, A: RangeType<I>,
I: Ord, B: RangeType<I>,
{ {
let ae = InclusiveInterval { let ae = InclusiveInterval {
start: a.start(), start: a.start(),
@ -104,10 +103,10 @@ where
} }
} }
pub(crate) fn contains_point<I, A>(range: A, point: I) -> bool pub(crate) fn contains_point<I, K>(range: K, point: I) -> bool
where where
A: InclusiveRange<I>, I: PointType,
I: Ord, K: RangeType<I>,
{ {
cmp_point_with_range(point, range).is_eq() cmp_point_with_range(point, range).is_eq()
} }
@ -118,11 +117,11 @@ pub(crate) struct CutResult<I> {
pub(crate) inside_cut: Option<InclusiveInterval<I>>, pub(crate) inside_cut: Option<InclusiveInterval<I>>,
pub(crate) after_cut: Option<InclusiveInterval<I>>, pub(crate) after_cut: Option<InclusiveInterval<I>>,
} }
pub(crate) fn cut_range<I, B, C>(base: B, cut: C) -> CutResult<I> pub(crate) fn cut_range<I, A, B>(base: A, cut: B) -> CutResult<I>
where where
B: InclusiveRange<I> + Copy, I: PointType,
C: InclusiveRange<I> + Copy, A: RangeType<I>,
I: Ord + Copy + DiscreteFinite, B: RangeType<I>,
{ {
let mut result = CutResult { let mut result = CutResult {
before_cut: None, before_cut: None,
@ -200,17 +199,17 @@ where
pub(crate) fn is_valid_range<I, K>(range: K) -> bool pub(crate) fn is_valid_range<I, K>(range: K) -> bool
where where
I: Ord, I: PointType,
K: InclusiveRange<I>, K: RangeType<I>,
{ {
range.start() <= range.end() range.start() <= range.end()
} }
pub(crate) fn overlaps<I, A, B>(a: A, b: B) -> bool pub(crate) fn overlaps<I, A, B>(a: A, b: B) -> bool
where where
A: InclusiveRange<I> + Copy, I: PointType,
B: InclusiveRange<I> + Copy, A: RangeType<I>,
I: Ord, B: RangeType<I>,
{ {
!matches!(sorted_config(a, b), SortedConfig::NonOverlapping(_, _)) !matches!(sorted_config(a, b), SortedConfig::NonOverlapping(_, _))
} }