From c2cb70aee7a148da14d5a6928e7bc21fc580e819 Mon Sep 17 00:00:00 2001 From: ripytide Date: Wed, 27 Dec 2023 12:53:16 +0000 Subject: [PATCH] replace readme tabs with spaces to make it look nicer on github --- README.md | 206 ++++++++++++++++++++++++++--------------------------- src/lib.rs | 25 ++----- 2 files changed, 110 insertions(+), 121 deletions(-) diff --git a/README.md b/README.md index db230a3..54d3eb8 100644 --- a/README.md +++ b/README.md @@ -44,73 +44,73 @@ use std::ops::{Bound, RangeBounds}; use discrete_range_map::test_ranges::ie; use discrete_range_map::{ - DiscreteFinite, DiscreteRangeMap, InclusiveInterval, - InclusiveRange, + DiscreteFinite, DiscreteRangeMap, InclusiveInterval, + InclusiveRange, }; #[derive(Debug, Copy, Clone)] enum Reservation { - // Start, End (Inclusive-Inclusive) - Finite(i8, i8), - // Start (Inclusive-Infinity) - Infinite(i8), + // Start, End (Inclusive-Inclusive) + Finite(i8, i8), + // Start (Inclusive-Infinity) + Infinite(i8), } // First, we need to implement InclusiveRange impl InclusiveRange for Reservation { - fn start(&self) -> i8 { - match self { - Reservation::Finite(start, _) => *start, - Reservation::Infinite(start) => *start, - } - } - fn end(&self) -> i8 { - match self { - Reservation::Finite(_, end) => *end, - Reservation::Infinite(_) => i8::MAX, - } - } + fn start(&self) -> i8 { + match self { + Reservation::Finite(start, _) => *start, + Reservation::Infinite(start) => *start, + } + } + fn end(&self) -> i8 { + match self { + Reservation::Finite(_, end) => *end, + Reservation::Infinite(_) => i8::MAX, + } + } } // Second, we need to implement From> impl From> for Reservation { - fn from(value: InclusiveInterval) -> Self { - if value.end == i8::MAX { - Reservation::Infinite(value.start) - } else { - Reservation::Finite( - value.start, - value.end.up().unwrap(), - ) - } - } + fn from(value: InclusiveInterval) -> Self { + if value.end == i8::MAX { + Reservation::Infinite(value.start) + } else { + Reservation::Finite( + value.start, + value.end.up().unwrap(), + ) + } + } } // Next we can create a custom typed DiscreteRangeMap let reservation_map = DiscreteRangeMap::from_slice_strict([ - (Reservation::Finite(10, 20), "Ferris".to_string()), - (Reservation::Infinite(21), "Corro".to_string()), + (Reservation::Finite(10, 20), "Ferris".to_string()), + (Reservation::Infinite(21), "Corro".to_string()), ]) .unwrap(); for (reservation, name) in reservation_map.overlapping(ie(16, 17)) { - println!( - "{name} has reserved {reservation:?} inside the range 16..17" - ); + println!( + "{name} has reserved {reservation:?} inside the range 16..17" + ); } for (reservation, name) in reservation_map.iter() { - println!("{name} has reserved {reservation:?}"); + println!("{name} has reserved {reservation:?}"); } assert_eq!( - reservation_map.overlaps(Reservation::Infinite(0)), - true + reservation_map.overlaps(Reservation::Infinite(0)), + true ); ``` -## Key Understandings and Philosophies: +## Key Understandings and Philosophies ### Discrete-ness @@ -146,74 +146,74 @@ use discrete_range_map::DiscreteFinite; #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum WithInfinity { - Finite(T), - Infinity, + Finite(T), + Infinity, } impl Ord for WithInfinity where - T: Ord, + T: Ord, { - fn cmp(&self, other: &Self) -> Ordering { - match (self, other) { - ( - WithInfinity::Finite(x), - WithInfinity::Finite(y), - ) => x.cmp(y), - (WithInfinity::Finite(_), WithInfinity::Infinity) => { - Ordering::Less - } - (WithInfinity::Infinity, WithInfinity::Finite(_)) => { - Ordering::Greater - } - (WithInfinity::Infinity, WithInfinity::Infinity) => { - Ordering::Equal - } - } - } + fn cmp(&self, other: &Self) -> Ordering { + match (self, other) { + ( + WithInfinity::Finite(x), + WithInfinity::Finite(y), + ) => x.cmp(y), + (WithInfinity::Finite(_), WithInfinity::Infinity) => { + Ordering::Less + } + (WithInfinity::Infinity, WithInfinity::Finite(_)) => { + Ordering::Greater + } + (WithInfinity::Infinity, WithInfinity::Infinity) => { + Ordering::Equal + } + } + } } impl PartialOrd for WithInfinity where - T: Ord, + T: Ord, { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } } impl DiscreteFinite for WithInfinity where - T: DiscreteFinite, + T: DiscreteFinite, { - const MIN: Self = WithInfinity::Finite(T::MIN); - const MAX: Self = WithInfinity::Infinity; + const MIN: Self = WithInfinity::Finite(T::MIN); + const MAX: Self = WithInfinity::Infinity; - fn up(self) -> Option - where - Self: Sized, - { - match self { - WithInfinity::Finite(x) => match x.up() { - Some(y) => Some(WithInfinity::Finite(y)), - None => Some(WithInfinity::Infinity), - }, - WithInfinity::Infinity => None, - } - } - fn down(self) -> Option - where - Self: Sized, - { - match self { - WithInfinity::Finite(x) => { - Some(WithInfinity::Finite(x.down()?)) - } - WithInfinity::Infinity => { - Some(WithInfinity::Finite(T::MAX)) - } - } - } + fn up(self) -> Option + where + Self: Sized, + { + match self { + WithInfinity::Finite(x) => match x.up() { + Some(y) => Some(WithInfinity::Finite(y)), + None => Some(WithInfinity::Infinity), + }, + WithInfinity::Infinity => None, + } + } + fn down(self) -> Option + where + Self: Sized, + { + match self { + WithInfinity::Finite(x) => { + Some(WithInfinity::Finite(x.down()?)) + } + WithInfinity::Infinity => { + Some(WithInfinity::Finite(T::MAX)) + } + } + } } // And then you this means you can be explicit with when @@ -223,19 +223,19 @@ where use discrete_range_map::{DiscreteRangeMap, InclusiveInterval}; let map: DiscreteRangeMap< - WithInfinity, - InclusiveInterval>, - bool, + WithInfinity, + InclusiveInterval>, + bool, > = DiscreteRangeMap::new(); let mut gap = map.get_entry_at_point(WithInfinity::Finite(4)); assert_eq!( - gap, - Err(InclusiveInterval { - start: WithInfinity::Finite(0), - end: WithInfinity::Infinity, - }) + gap, + Err(InclusiveInterval { + start: WithInfinity::Finite(0), + end: WithInfinity::Infinity, + }) ); ``` @@ -278,21 +278,23 @@ value between them. For example, `2..4` and `4..6` are touching but See Wikipedia's article on mathematical Intervals: -# Features This crate currently has no features. +## Features -# Credit +This crate currently has no features + +## Credit Lots of my inspiration came from the [`rangemap`] crate. The BTreeMap implementation ([`btree_monstrousity`]) used under the hood was inspired and forked from the [`copse`] crate. -# Name Change +## Name Change This crate was previously named [`range_bounds_map`] it was renamed around about 2023-04-24 due to it no longer being an accurate name. -# Similar Crates +## Similar Crates Here are some relevant crates I found whilst searching around the topic area, beware my biases when reading: @@ -341,11 +343,9 @@ topic area, beware my biases when reading: [`btreemap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html [`btree_monstrousity`]: https://github.com/ripytide/btree_monstrousity -[`rangebounds`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html [`range`]: https://doc.rust-lang.org/std/ops/struct.Range.html [`rangemap`]: https://docs.rs/rangemap/latest/rangemap/ [`rangeinclusive`]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html -[`ord`]: https://doc.rust-lang.org/std/cmp/trait.Ord.html [`copse`]: https://github.com/eggyal/copse [`discrete`]: https://en.wikipedia.org/wiki/Discrete_mathematics [`continuous`]: https://en.wikipedia.org/wiki/List_of_continuity-related_mathematical_topics diff --git a/src/lib.rs b/src/lib.rs index 8d6d996..82e5758 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,17 +17,6 @@ You should have received a copy of the GNU Affero General Public License along with discrete_range_map. If not, see . */ -//! # discrete_range_map -//! -//! [![License](https://img.shields.io/github/license/ripytide/discrete_range_map)](https://www.gnu.org/licenses/agpl-3.0.en.html) -//! [![Docs](https://docs.rs/discrete_range_map/badge.svg)](https://docs.rs/discrete_range_map) -//! [![Maintained](https://img.shields.io/maintenance/yes/2023)](https://github.com/ripytide) -//! [![Crates.io](https://img.shields.io/crates/v/discrete_range_map)](https://crates.io/crates/discrete_range_map) -//! -//!

-//! discrete_range_map_logo -//!

-//! //! This crate provides [`DiscreteRangeMap`] and [`DiscreteRangeSet`], //! Data Structures for storing non-overlapping discrete intervals based //! off [`BTreeMap`]. @@ -129,7 +118,7 @@ along with discrete_range_map. If not, see . //! ); //! ``` //! -//! ## Key Understandings and Philosophies: +//! ## Key Understandings and Philosophies //! //! ### Discrete-ness //! @@ -297,21 +286,23 @@ along with discrete_range_map. If not, see . //! See Wikipedia's article on mathematical Intervals: //! //! -//! # Features This crate currently has no features. +//! ## Features //! -//! # Credit +//! This crate currently has no features +//! +//! ## Credit //! //! Lots of my inspiration came from the [`rangemap`] crate. //! //! The BTreeMap implementation ([`btree_monstrousity`]) used under the //! hood was inspired and forked from the [`copse`] crate. //! -//! # Name Change +//! ## Name Change //! //! This crate was previously named [`range_bounds_map`] it was renamed //! around about 2023-04-24 due to it no longer being an accurate name. //! -//! # Similar Crates +//! ## Similar Crates //! //! Here are some relevant crates I found whilst searching around the //! topic area, beware my biases when reading: @@ -360,11 +351,9 @@ along with discrete_range_map. If not, see . //! //! [`btreemap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html //! [`btree_monstrousity`]: https://github.com/ripytide/btree_monstrousity -//! [`rangebounds`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html //! [`range`]: https://doc.rust-lang.org/std/ops/struct.Range.html //! [`rangemap`]: https://docs.rs/rangemap/latest/rangemap/ //! [`rangeinclusive`]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html -//! [`ord`]: https://doc.rust-lang.org/std/cmp/trait.Ord.html //! [`copse`]: https://github.com/eggyal/copse //! [`discrete`]: https://en.wikipedia.org/wiki/Discrete_mathematics //! [`continuous`]: https://en.wikipedia.org/wiki/List_of_continuity-related_mathematical_topics