From 6de99232df21adaf9df99dfa6afe48955ba49fe9 Mon Sep 17 00:00:00 2001 From: ripytide Date: Wed, 27 Dec 2023 13:26:50 +0000 Subject: [PATCH] extracted the InclusiveInterval into a separate module --- README.md | 4 +- src/discrete_range_map.rs | 91 ++++++------------- src/{test_ranges.rs => inclusive_interval.rs} | 44 ++++++++- src/lib.rs | 11 ++- 4 files changed, 77 insertions(+), 73 deletions(-) rename src/{test_ranges.rs => inclusive_interval.rs} (67%) diff --git a/README.md b/README.md index f7cf4c4..67b79f0 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ for some methods so if in doubt check a methods trait bounds. ## Example using an Inclusive-Exclusive range ```rust -use discrete_range_map::test_ranges::ie; +use discrete_range_map::inclusive_interval::ie; use discrete_range_map::DiscreteRangeMap; let mut map = DiscreteRangeMap::new(); @@ -45,7 +45,7 @@ assert_eq!(map.contains_point(5), true); ```rust use std::ops::{Bound, RangeBounds}; -use discrete_range_map::test_ranges::ie; +use discrete_range_map::inclusive_interval::ie; use discrete_range_map::{ DiscreteFinite, DiscreteRangeMap, InclusiveInterval, InclusiveRange, diff --git a/src/discrete_range_map.rs b/src/discrete_range_map.rs index ed55985..a2d1c53 100644 --- a/src/discrete_range_map.rs +++ b/src/discrete_range_map.rs @@ -24,7 +24,6 @@ use core::cmp::Ordering; use core::fmt::{self, Debug}; use core::iter::once; use core::marker::PhantomData; -use core::ops::{Bound, RangeBounds}; use btree_monstrousity::btree_map::{ IntoIter as BTreeMapIntoIter, SearchBoundCustom, @@ -37,7 +36,7 @@ use serde::ser::SerializeSeq; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::utils::{cmp_point_with_range, cut_range, is_valid_range, overlaps}; -use crate::DiscreteFinite; +use crate::{DiscreteFinite, InclusiveInterval}; /// An ordered map of non-overlapping ranges based on [`BTreeMap`]. /// @@ -54,7 +53,7 @@ use crate::DiscreteFinite; /// /// # Examples /// ``` -/// use discrete_range_map::test_ranges::ie; +/// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// // Make a map of ranges to booleans @@ -93,40 +92,6 @@ pub struct OverlapError { pub value: V, } -/// A compatibility type used in [`RangeType`] for allowing the library to -/// create the custom K type used in the map when necessary. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub struct InclusiveInterval { - /// The start of the interval, inclusive. - pub start: I, - /// The end of the interval, inclusive. - pub end: I, -} -impl RangeBounds for InclusiveInterval -where - I: PointType, -{ - fn start_bound(&self) -> Bound<&I> { - Bound::Included(&self.start) - } - - fn end_bound(&self) -> Bound<&I> { - Bound::Included(&self.end) - } -} -impl InclusiveRange for InclusiveInterval -where - I: PointType, -{ - fn start(&self) -> I { - self.start - } - - fn end(&self) -> I { - self.end - } -} - /// The marker trait for valid point types, a blanket implementation is provided for all types /// which implement this traits' super-traits so you shouln't need to implement this yourself. pub trait PointType: Ord + Copy + DiscreteFinite {} @@ -161,7 +126,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::{ie, ii}; + /// use discrete_range_map::inclusive_interval::{ie, ii}; /// use discrete_range_map::DiscreteRangeMap; /// /// let mut map = DiscreteRangeMap::new(); @@ -194,7 +159,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let map = DiscreteRangeMap::from_slice_strict([ @@ -241,7 +206,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let mut map = DiscreteRangeMap::from_slice_strict([ @@ -283,7 +248,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let map = DiscreteRangeMap::from_slice_strict([ @@ -306,7 +271,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// let mut map = /// DiscreteRangeMap::from_slice_strict([(ie(1, 4), false)]) @@ -327,7 +292,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let map = DiscreteRangeMap::from_slice_strict([ @@ -353,7 +318,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::{ie, iu}; + /// use discrete_range_map::inclusive_interval::{ie, iu}; /// use discrete_range_map::DiscreteRangeMap; /// /// let map = DiscreteRangeMap::from_slice_strict([ @@ -402,7 +367,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let mut map = DiscreteRangeMap::from_slice_strict([ @@ -466,7 +431,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::{ie, ii}; + /// use discrete_range_map::inclusive_interval::{ie, ii}; /// use discrete_range_map::DiscreteRangeMap; /// /// let mut base = DiscreteRangeMap::from_slice_strict([ @@ -633,7 +598,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::{ie, iu}; + /// use discrete_range_map::inclusive_interval::{ie, iu}; /// use discrete_range_map::DiscreteRangeMap; /// /// let map = DiscreteRangeMap::from_slice_strict([ @@ -727,7 +692,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let map = DiscreteRangeMap::from_slice_strict([ @@ -765,7 +730,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::{DiscreteRangeMap, OverlapError}; /// /// let mut map = DiscreteRangeMap::new(); @@ -865,7 +830,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::{DiscreteRangeMap, OverlapError}; /// /// let mut map = DiscreteRangeMap::from_slice_strict([ @@ -953,7 +918,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::{DiscreteRangeMap, OverlapError}; /// /// let mut map = DiscreteRangeMap::from_slice_strict([ @@ -1054,7 +1019,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::{DiscreteRangeMap, OverlapError}; /// /// let mut map = DiscreteRangeMap::from_slice_strict([ @@ -1127,7 +1092,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::{DiscreteRangeMap, OverlapError}; /// /// let mut map = DiscreteRangeMap::from_slice_strict([ @@ -1215,7 +1180,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let mut map = @@ -1254,7 +1219,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let map = DiscreteRangeMap::from_slice_strict([ @@ -1288,7 +1253,7 @@ where /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let slice = @@ -1332,7 +1297,7 @@ impl DiscreteRangeMap { /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let mut map = DiscreteRangeMap::new(); @@ -1350,7 +1315,7 @@ impl DiscreteRangeMap { /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let mut map = DiscreteRangeMap::new(); @@ -1368,7 +1333,7 @@ impl DiscreteRangeMap { /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let map = DiscreteRangeMap::from_slice_strict([ @@ -1394,7 +1359,7 @@ impl DiscreteRangeMap { /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let mut map = DiscreteRangeMap::from_slice_strict([ @@ -1422,7 +1387,7 @@ impl DiscreteRangeMap { /// /// # Examples /// ``` - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// use discrete_range_map::DiscreteRangeMap; /// /// let map = DiscreteRangeMap::from_slice_strict([ @@ -1443,7 +1408,7 @@ impl DiscreteRangeMap { /// # Examples /// ``` /// use discrete_range_map::DiscreteRangeMap; - /// use discrete_range_map::test_ranges::ie; + /// use discrete_range_map::inclusive_interval::ie; /// /// let map = DiscreteRangeMap::from_slice_strict([ /// (ie(1, 4), false), @@ -1713,7 +1678,7 @@ mod tests { use pretty_assertions::assert_eq; use super::*; - use crate::test_ranges::{ee, ei, ie, ii, iu, ue, ui, uu}; + use crate::inclusive_interval::{ee, ei, ie, ii, iu, ue, ui, uu}; use crate::utils::{config, contains_point, Config, CutResult}; //only every other number to allow mathematical_overlapping_definition diff --git a/src/test_ranges.rs b/src/inclusive_interval.rs similarity index 67% rename from src/test_ranges.rs rename to src/inclusive_interval.rs index fa85bbc..83ce0e8 100644 --- a/src/test_ranges.rs +++ b/src/inclusive_interval.rs @@ -17,10 +17,48 @@ You should have received a copy of the GNU Affero General Public License along with discrete_range_map. If not, see . */ -//! A collection of helper functions for making [`InclusiveInterval`]s from `i8`'s used for testing -//! and example purposes. +//! A module containing [`InclusiveInterval`] and it's various constructor functions. -use crate::{DiscreteFinite, InclusiveInterval}; +use core::ops::{RangeBounds, Bound}; + +use serde::{Serialize, Deserialize}; + +use crate::{DiscreteFinite, PointType, InclusiveRange}; + +/// The interval type used throughout this crate both for the examples and +/// for use by library users if they don't wish to create their own +/// interval types. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct InclusiveInterval { + /// The start of the interval, inclusive. + pub start: I, + /// The end of the interval, inclusive. + pub end: I, +} +impl RangeBounds for InclusiveInterval +where + I: PointType, +{ + fn start_bound(&self) -> Bound<&I> { + Bound::Included(&self.start) + } + + fn end_bound(&self) -> Bound<&I> { + Bound::Included(&self.end) + } +} +impl InclusiveRange for InclusiveInterval +where + I: PointType, +{ + fn start(&self) -> I { + self.start + } + + fn end(&self) -> I { + self.end + } +} /// An unbounded-unbounded interval pub fn uu() -> InclusiveInterval { diff --git a/src/lib.rs b/src/lib.rs index 1a54f39..79290a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,7 @@ along with discrete_range_map. If not, see . //! ## Example using an Inclusive-Exclusive range //! //! ```rust -//! use discrete_range_map::test_ranges::ie; +//! use discrete_range_map::inclusive_interval::ie; //! use discrete_range_map::DiscreteRangeMap; //! //! let mut map = DiscreteRangeMap::new(); @@ -53,7 +53,7 @@ along with discrete_range_map. If not, see . //! ```rust //! use std::ops::{Bound, RangeBounds}; //! -//! use discrete_range_map::test_ranges::ie; +//! use discrete_range_map::inclusive_interval::ie; //! use discrete_range_map::{ //! DiscreteFinite, DiscreteRangeMap, InclusiveInterval, //! InclusiveRange, @@ -384,11 +384,12 @@ pub(crate) mod utils; pub mod discrete_finite; pub mod discrete_range_map; pub mod discrete_range_set; -pub mod test_ranges; +pub mod inclusive_interval; pub use crate::discrete_finite::DiscreteFinite; pub use crate::discrete_range_map::{ - DiscreteRangeMap, InclusiveInterval, InclusiveRange, OverlapError, - PointType, RangeType, + DiscreteRangeMap, InclusiveRange, OverlapError, PointType, RangeType, }; pub use crate::discrete_range_set::DiscreteRangeSet; +pub use crate::inclusive_interval::InclusiveInterval; +