This crate provides [RangeBoundsMap
] and [RangeBoundsSet
.]
[RangeBoundsMap
] is an ordered map of non-overlapping [RangeBounds
]
based on [BTreeMap
].
[RangeBoundsSet
] is an ordered set of non-overlapping [RangeBounds
]
based on [RangeBoundsMap
].
Range
]s```rust use rangeboundsmap::RangeBoundsMap;
let mut rangeboundsmap = RangeBoundsMap::new();
rangeboundsmap.insertplatonic(0..5, true); rangeboundsmap.insertplatonic(5..10, false);
asserteq!(rangeboundsmap.overlaps(&(-2..12)), true); asserteq!(rangeboundsmap.containspoint(&20), false); asserteq!(rangeboundsmap.contains_point(&5), true); ```
RangeBounds
] type```rust use std::ops::{Bound, RangeBounds};
use rangeboundsmap::RangeBoundsMap;
enum Reservation { // Start, End (Inclusive-Inclusive) Finite(u8, u8), // Start (Exclusive) Infinite(u8), }
// First, we need to implement RangeBounds
impl RangeBounds
// Next we can create a custom typed RangeBoundsMap let reservationmap = RangeBoundsMap::tryfrom([ (Reservation::Finite(10, 20), "Ferris".tostring()), (Reservation::Infinite(20), "Corro".tostring()), ]) .unwrap();
for (reservation, name) in reservation_map.overlapping(&(16..17)) { println!( "{name} has reserved {reservation:?} inside the range 16..17" ); }
for (reservation, name) in reservation_map.iter() { println!("{name} has reserved {reservation:?}"); }
asserteq!( reservationmap.overlaps(&Reservation::Infinite(0)), true ); ```
Two RangeBounds
are "overlapping" if there exists a point that is
contained within both RangeBounds
.
Two RangeBounds
are "touching" if they do not overlap and
there exists no value between them. For example, 2..4
and
4..6
are touching but 2..4
and 6..8
are not, neither are
2..6
and 4..8
.
When a RangeBounds
"coalesces" other RangeBounds
it absorbs them
to become larger.
clear()
is_subset()
TryFromBounds
] (relys on
upstream to impl, see this thread)I originally came up with the StartBound
: [Ord
] bodge on my own,
however, I later stumbled across [rangemap
] which also used a
StartBound
: [Ord
] bodge. [rangemap
] then became my main source
of inspiration.
The aim for my library was to become a more generic
superset of [rangemap
], following from this
issue and this
pull request in
which I changed [rangemap
]'s [RangeMap
] to use [RangeBounds
]s as
keys before I realized it might be easier and simpler to just write it
all from scratch. Which ended up working really well with some
simplifications (BoundOrd) I made which made some of the code much
easier to work with.
Here are some relevant crates I found whilst searching around the topic area:
Range
]s and
[RangeInclusive
]s as keys in it's map
and set
structs (separately).Ranges
datastructure for storing them (Vec-based
unfortunately)gaps()
function and only
for [Range
]s and not [RangeInclusive
]s. And also no fancy coalescing
functions.Box<Node>
based tree, however it also supports overlapping
RangeBounds which my library does not.