Skip to content

Commit

Permalink
extracted the InclusiveInterval into a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
ripytide committed Dec 27, 2023
1 parent 05e2d14 commit 6de9923
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 73 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand Down
91 changes: 28 additions & 63 deletions src/discrete_range_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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`].
///
Expand All @@ -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
Expand Down Expand Up @@ -93,40 +92,6 @@ pub struct OverlapError<V> {
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<I> {
/// The start of the interval, inclusive.
pub start: I,
/// The end of the interval, inclusive.
pub end: I,
}
impl<I> RangeBounds<I> for InclusiveInterval<I>
where
I: PointType,
{
fn start_bound(&self) -> Bound<&I> {
Bound::Included(&self.start)
}

fn end_bound(&self) -> Bound<&I> {
Bound::Included(&self.end)
}
}
impl<I> InclusiveRange<I> for InclusiveInterval<I>
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 {}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand All @@ -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)])
Expand All @@ -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([
Expand All @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -1332,7 +1297,7 @@ impl<I, K, V> DiscreteRangeMap<I, K, V> {
///
/// # 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();
Expand All @@ -1350,7 +1315,7 @@ impl<I, K, V> DiscreteRangeMap<I, K, V> {
///
/// # 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();
Expand All @@ -1368,7 +1333,7 @@ impl<I, K, V> DiscreteRangeMap<I, K, V> {
///
/// # 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([
Expand All @@ -1394,7 +1359,7 @@ impl<I, K, V> DiscreteRangeMap<I, K, V> {
///
/// # 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([
Expand Down Expand Up @@ -1422,7 +1387,7 @@ impl<I, K, V> DiscreteRangeMap<I, K, V> {
///
/// # 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([
Expand All @@ -1443,7 +1408,7 @@ impl<I, K, V> DiscreteRangeMap<I, K, V> {
/// # 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),
Expand Down Expand Up @@ -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
Expand Down
44 changes: 41 additions & 3 deletions src/test_ranges.rs → src/inclusive_interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://www.gnu.org/licenses/>.
*/

//! 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<I> {
/// The start of the interval, inclusive.
pub start: I,
/// The end of the interval, inclusive.
pub end: I,
}
impl<I> RangeBounds<I> for InclusiveInterval<I>
where
I: PointType,
{
fn start_bound(&self) -> Bound<&I> {
Bound::Included(&self.start)
}

fn end_bound(&self) -> Bound<&I> {
Bound::Included(&self.end)
}
}
impl<I> InclusiveRange<I> for InclusiveInterval<I>
where
I: PointType,
{
fn start(&self) -> I {
self.start
}

fn end(&self) -> I {
self.end
}
}

/// An unbounded-unbounded interval
pub fn uu() -> InclusiveInterval<i8> {
Expand Down
Loading

0 comments on commit 6de9923

Please sign in to comment.