replace readme tabs with spaces to make it look nicer on github
This commit is contained in:
@@ -44,73 +44,73 @@ use std::ops::{Bound, RangeBounds};
|
|||||||
|
|
||||||
use discrete_range_map::test_ranges::ie;
|
use discrete_range_map::test_ranges::ie;
|
||||||
use discrete_range_map::{
|
use discrete_range_map::{
|
||||||
DiscreteFinite, DiscreteRangeMap, InclusiveInterval,
|
DiscreteFinite, DiscreteRangeMap, InclusiveInterval,
|
||||||
InclusiveRange,
|
InclusiveRange,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
enum Reservation {
|
enum Reservation {
|
||||||
// Start, End (Inclusive-Inclusive)
|
// Start, End (Inclusive-Inclusive)
|
||||||
Finite(i8, i8),
|
Finite(i8, i8),
|
||||||
// Start (Inclusive-Infinity)
|
// Start (Inclusive-Infinity)
|
||||||
Infinite(i8),
|
Infinite(i8),
|
||||||
}
|
}
|
||||||
|
|
||||||
// First, we need to implement InclusiveRange
|
// First, we need to implement InclusiveRange
|
||||||
impl InclusiveRange<i8> for Reservation {
|
impl InclusiveRange<i8> for Reservation {
|
||||||
fn start(&self) -> i8 {
|
fn start(&self) -> i8 {
|
||||||
match self {
|
match self {
|
||||||
Reservation::Finite(start, _) => *start,
|
Reservation::Finite(start, _) => *start,
|
||||||
Reservation::Infinite(start) => *start,
|
Reservation::Infinite(start) => *start,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn end(&self) -> i8 {
|
fn end(&self) -> i8 {
|
||||||
match self {
|
match self {
|
||||||
Reservation::Finite(_, end) => *end,
|
Reservation::Finite(_, end) => *end,
|
||||||
Reservation::Infinite(_) => i8::MAX,
|
Reservation::Infinite(_) => i8::MAX,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Second, we need to implement From<InclusiveInterval<i8>>
|
// Second, we need to implement From<InclusiveInterval<i8>>
|
||||||
impl From<InclusiveInterval<i8>> for Reservation {
|
impl From<InclusiveInterval<i8>> for Reservation {
|
||||||
fn from(value: InclusiveInterval<i8>) -> Self {
|
fn from(value: InclusiveInterval<i8>) -> Self {
|
||||||
if value.end == i8::MAX {
|
if value.end == i8::MAX {
|
||||||
Reservation::Infinite(value.start)
|
Reservation::Infinite(value.start)
|
||||||
} else {
|
} else {
|
||||||
Reservation::Finite(
|
Reservation::Finite(
|
||||||
value.start,
|
value.start,
|
||||||
value.end.up().unwrap(),
|
value.end.up().unwrap(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next we can create a custom typed DiscreteRangeMap
|
// Next we can create a custom typed DiscreteRangeMap
|
||||||
let reservation_map = DiscreteRangeMap::from_slice_strict([
|
let reservation_map = DiscreteRangeMap::from_slice_strict([
|
||||||
(Reservation::Finite(10, 20), "Ferris".to_string()),
|
(Reservation::Finite(10, 20), "Ferris".to_string()),
|
||||||
(Reservation::Infinite(21), "Corro".to_string()),
|
(Reservation::Infinite(21), "Corro".to_string()),
|
||||||
])
|
])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
for (reservation, name) in reservation_map.overlapping(ie(16, 17))
|
for (reservation, name) in reservation_map.overlapping(ie(16, 17))
|
||||||
{
|
{
|
||||||
println!(
|
println!(
|
||||||
"{name} has reserved {reservation:?} inside the range 16..17"
|
"{name} has reserved {reservation:?} inside the range 16..17"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (reservation, name) in reservation_map.iter() {
|
for (reservation, name) in reservation_map.iter() {
|
||||||
println!("{name} has reserved {reservation:?}");
|
println!("{name} has reserved {reservation:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
reservation_map.overlaps(Reservation::Infinite(0)),
|
reservation_map.overlaps(Reservation::Infinite(0)),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
## Key Understandings and Philosophies:
|
## Key Understandings and Philosophies
|
||||||
|
|
||||||
### Discrete-ness
|
### Discrete-ness
|
||||||
|
|
||||||
@@ -146,74 +146,74 @@ use discrete_range_map::DiscreteFinite;
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
enum WithInfinity<T> {
|
enum WithInfinity<T> {
|
||||||
Finite(T),
|
Finite(T),
|
||||||
Infinity,
|
Infinity,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Ord for WithInfinity<T>
|
impl<T> Ord for WithInfinity<T>
|
||||||
where
|
where
|
||||||
T: Ord,
|
T: Ord,
|
||||||
{
|
{
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(
|
(
|
||||||
WithInfinity::Finite(x),
|
WithInfinity::Finite(x),
|
||||||
WithInfinity::Finite(y),
|
WithInfinity::Finite(y),
|
||||||
) => x.cmp(y),
|
) => x.cmp(y),
|
||||||
(WithInfinity::Finite(_), WithInfinity::Infinity) => {
|
(WithInfinity::Finite(_), WithInfinity::Infinity) => {
|
||||||
Ordering::Less
|
Ordering::Less
|
||||||
}
|
}
|
||||||
(WithInfinity::Infinity, WithInfinity::Finite(_)) => {
|
(WithInfinity::Infinity, WithInfinity::Finite(_)) => {
|
||||||
Ordering::Greater
|
Ordering::Greater
|
||||||
}
|
}
|
||||||
(WithInfinity::Infinity, WithInfinity::Infinity) => {
|
(WithInfinity::Infinity, WithInfinity::Infinity) => {
|
||||||
Ordering::Equal
|
Ordering::Equal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> PartialOrd for WithInfinity<T>
|
impl<T> PartialOrd for WithInfinity<T>
|
||||||
where
|
where
|
||||||
T: Ord,
|
T: Ord,
|
||||||
{
|
{
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
Some(self.cmp(other))
|
Some(self.cmp(other))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> DiscreteFinite for WithInfinity<T>
|
impl<T> DiscreteFinite for WithInfinity<T>
|
||||||
where
|
where
|
||||||
T: DiscreteFinite,
|
T: DiscreteFinite,
|
||||||
{
|
{
|
||||||
const MIN: Self = WithInfinity::Finite(T::MIN);
|
const MIN: Self = WithInfinity::Finite(T::MIN);
|
||||||
const MAX: Self = WithInfinity::Infinity;
|
const MAX: Self = WithInfinity::Infinity;
|
||||||
|
|
||||||
fn up(self) -> Option<Self>
|
fn up(self) -> Option<Self>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
WithInfinity::Finite(x) => match x.up() {
|
WithInfinity::Finite(x) => match x.up() {
|
||||||
Some(y) => Some(WithInfinity::Finite(y)),
|
Some(y) => Some(WithInfinity::Finite(y)),
|
||||||
None => Some(WithInfinity::Infinity),
|
None => Some(WithInfinity::Infinity),
|
||||||
},
|
},
|
||||||
WithInfinity::Infinity => None,
|
WithInfinity::Infinity => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn down(self) -> Option<Self>
|
fn down(self) -> Option<Self>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
WithInfinity::Finite(x) => {
|
WithInfinity::Finite(x) => {
|
||||||
Some(WithInfinity::Finite(x.down()?))
|
Some(WithInfinity::Finite(x.down()?))
|
||||||
}
|
}
|
||||||
WithInfinity::Infinity => {
|
WithInfinity::Infinity => {
|
||||||
Some(WithInfinity::Finite(T::MAX))
|
Some(WithInfinity::Finite(T::MAX))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// And then you this means you can be explicit with when
|
// And then you this means you can be explicit with when
|
||||||
@@ -223,19 +223,19 @@ where
|
|||||||
use discrete_range_map::{DiscreteRangeMap, InclusiveInterval};
|
use discrete_range_map::{DiscreteRangeMap, InclusiveInterval};
|
||||||
|
|
||||||
let map: DiscreteRangeMap<
|
let map: DiscreteRangeMap<
|
||||||
WithInfinity<u8>,
|
WithInfinity<u8>,
|
||||||
InclusiveInterval<WithInfinity<u8>>,
|
InclusiveInterval<WithInfinity<u8>>,
|
||||||
bool,
|
bool,
|
||||||
> = DiscreteRangeMap::new();
|
> = DiscreteRangeMap::new();
|
||||||
|
|
||||||
let mut gap = map.get_entry_at_point(WithInfinity::Finite(4));
|
let mut gap = map.get_entry_at_point(WithInfinity::Finite(4));
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
gap,
|
gap,
|
||||||
Err(InclusiveInterval {
|
Err(InclusiveInterval {
|
||||||
start: WithInfinity::Finite(0),
|
start: WithInfinity::Finite(0),
|
||||||
end: WithInfinity::Infinity,
|
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:
|
See Wikipedia's article on mathematical Intervals:
|
||||||
<https://en.wikipedia.org/wiki/Interval_(mathematics)>
|
<https://en.wikipedia.org/wiki/Interval_(mathematics)>
|
||||||
|
|
||||||
# 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.
|
Lots of my inspiration came from the [`rangemap`] crate.
|
||||||
|
|
||||||
The BTreeMap implementation ([`btree_monstrousity`]) used under the
|
The BTreeMap implementation ([`btree_monstrousity`]) used under the
|
||||||
hood was inspired and forked from the [`copse`] crate.
|
hood was inspired and forked from the [`copse`] crate.
|
||||||
|
|
||||||
# Name Change
|
## Name Change
|
||||||
|
|
||||||
This crate was previously named [`range_bounds_map`] it was renamed
|
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.
|
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
|
Here are some relevant crates I found whilst searching around the
|
||||||
topic area, beware my biases when reading:
|
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
|
[`btreemap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
|
||||||
[`btree_monstrousity`]: https://github.com/ripytide/btree_monstrousity
|
[`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
|
[`range`]: https://doc.rust-lang.org/std/ops/struct.Range.html
|
||||||
[`rangemap`]: https://docs.rs/rangemap/latest/rangemap/
|
[`rangemap`]: https://docs.rs/rangemap/latest/rangemap/
|
||||||
[`rangeinclusive`]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html
|
[`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
|
[`copse`]: https://github.com/eggyal/copse
|
||||||
[`discrete`]: https://en.wikipedia.org/wiki/Discrete_mathematics
|
[`discrete`]: https://en.wikipedia.org/wiki/Discrete_mathematics
|
||||||
[`continuous`]: https://en.wikipedia.org/wiki/List_of_continuity-related_mathematical_topics
|
[`continuous`]: https://en.wikipedia.org/wiki/List_of_continuity-related_mathematical_topics
|
||||||
|
|||||||
+7
-18
@@ -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 <https://www.gnu.org/licenses/>.
|
along with discrete_range_map. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//! # discrete_range_map
|
|
||||||
//!
|
|
||||||
//! [](https://www.gnu.org/licenses/agpl-3.0.en.html)
|
|
||||||
//! [](https://docs.rs/discrete_range_map)
|
|
||||||
//! [](https://github.com/ripytide)
|
|
||||||
//! [](https://crates.io/crates/discrete_range_map)
|
|
||||||
//!
|
|
||||||
//! <p align="center">
|
|
||||||
//! <img src="logo.png" alt="discrete_range_map_logo" width="350">
|
|
||||||
//! </p>
|
|
||||||
//!
|
|
||||||
//! This crate provides [`DiscreteRangeMap`] and [`DiscreteRangeSet`],
|
//! This crate provides [`DiscreteRangeMap`] and [`DiscreteRangeSet`],
|
||||||
//! Data Structures for storing non-overlapping discrete intervals based
|
//! Data Structures for storing non-overlapping discrete intervals based
|
||||||
//! off [`BTreeMap`].
|
//! off [`BTreeMap`].
|
||||||
@@ -129,7 +118,7 @@ along with discrete_range_map. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
//! );
|
//! );
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Key Understandings and Philosophies:
|
//! ## Key Understandings and Philosophies
|
||||||
//!
|
//!
|
||||||
//! ### Discrete-ness
|
//! ### Discrete-ness
|
||||||
//!
|
//!
|
||||||
@@ -297,21 +286,23 @@ along with discrete_range_map. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
//! See Wikipedia's article on mathematical Intervals:
|
//! See Wikipedia's article on mathematical Intervals:
|
||||||
//! <https://en.wikipedia.org/wiki/Interval_(mathematics)>
|
//! <https://en.wikipedia.org/wiki/Interval_(mathematics)>
|
||||||
//!
|
//!
|
||||||
//! # 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.
|
//! Lots of my inspiration came from the [`rangemap`] crate.
|
||||||
//!
|
//!
|
||||||
//! The BTreeMap implementation ([`btree_monstrousity`]) used under the
|
//! The BTreeMap implementation ([`btree_monstrousity`]) used under the
|
||||||
//! hood was inspired and forked from the [`copse`] crate.
|
//! hood was inspired and forked from the [`copse`] crate.
|
||||||
//!
|
//!
|
||||||
//! # Name Change
|
//! ## Name Change
|
||||||
//!
|
//!
|
||||||
//! This crate was previously named [`range_bounds_map`] it was renamed
|
//! 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.
|
//! 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
|
//! Here are some relevant crates I found whilst searching around the
|
||||||
//! topic area, beware my biases when reading:
|
//! topic area, beware my biases when reading:
|
||||||
@@ -360,11 +351,9 @@ along with discrete_range_map. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
//!
|
//!
|
||||||
//! [`btreemap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
|
//! [`btreemap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
|
||||||
//! [`btree_monstrousity`]: https://github.com/ripytide/btree_monstrousity
|
//! [`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
|
//! [`range`]: https://doc.rust-lang.org/std/ops/struct.Range.html
|
||||||
//! [`rangemap`]: https://docs.rs/rangemap/latest/rangemap/
|
//! [`rangemap`]: https://docs.rs/rangemap/latest/rangemap/
|
||||||
//! [`rangeinclusive`]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html
|
//! [`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
|
//! [`copse`]: https://github.com/eggyal/copse
|
||||||
//! [`discrete`]: https://en.wikipedia.org/wiki/Discrete_mathematics
|
//! [`discrete`]: https://en.wikipedia.org/wiki/Discrete_mathematics
|
||||||
//! [`continuous`]: https://en.wikipedia.org/wiki/List_of_continuity-related_mathematical_topics
|
//! [`continuous`]: https://en.wikipedia.org/wiki/List_of_continuity-related_mathematical_topics
|
||||||
|
|||||||
Reference in New Issue
Block a user