added range_bound_map.rs main examples and documentation
This commit is contained in:
parent
53b13cbd4f
commit
78dff0d99e
10
README.md
10
README.md
@ -6,10 +6,6 @@
|
|||||||
|
|
||||||
- add examples for all functions in rust doc comments and test them with cargo doc-test
|
- add examples for all functions in rust doc comments and test them with cargo doc-test
|
||||||
|
|
||||||
- add some actual rust example files in an example folder
|
|
||||||
|
|
||||||
- convert README to cool format using links to docs
|
|
||||||
|
|
||||||
- add github badges including lib.rs
|
- add github badges including lib.rs
|
||||||
- make logo
|
- make logo
|
||||||
|
|
||||||
@ -19,14 +15,12 @@
|
|||||||
|
|
||||||
- use it in robot_Sweet_graph for a bit before publishing
|
- use it in robot_Sweet_graph for a bit before publishing
|
||||||
|
|
||||||
|
- copy docs from set to map and update appropriately
|
||||||
|
|
||||||
- update lines of code figures on docs
|
- update lines of code figures on docs
|
||||||
|
|
||||||
- make all functions' trait bounds specific instead of at impl level
|
- make all functions' trait bounds specific instead of at impl level
|
||||||
|
|
||||||
- convert all examples not using `from` to use `from`, and tests
|
|
||||||
|
|
||||||
- turn overlaps back into a trait to make it public, mabye
|
|
||||||
|
|
||||||
- review caveats again
|
- review caveats again
|
||||||
|
|
||||||
- run cargo fmt
|
- run cargo fmt
|
||||||
|
@ -4,3 +4,6 @@ hard_tabs=true
|
|||||||
imports_granularity="Module"
|
imports_granularity="Module"
|
||||||
group_imports="StdExternalCrate"
|
group_imports="StdExternalCrate"
|
||||||
max_width=80
|
max_width=80
|
||||||
|
format_code_in_doc_comments=true
|
||||||
|
doc_comment_code_block_width=70
|
||||||
|
unstable_features = true
|
||||||
|
11
src/lib.rs
11
src/lib.rs
@ -45,8 +45,7 @@ along with range_bounds_map. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
//!
|
//!
|
||||||
//! # Example using a custom [`RangeBounds`] type
|
//! # Example using a custom [`RangeBounds`] type
|
||||||
//! ```
|
//! ```
|
||||||
//! use std::ops::RangeBounds;
|
//! use std::ops::{Bound, RangeBounds};
|
||||||
//! use std::ops::Bound;
|
|
||||||
//!
|
//!
|
||||||
//! use range_bounds_map::RangeBoundsMap;
|
//! use range_bounds_map::RangeBoundsMap;
|
||||||
//!
|
//!
|
||||||
@ -55,7 +54,7 @@ along with range_bounds_map. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
//! // Start, End (Inclusive-Inclusive)
|
//! // Start, End (Inclusive-Inclusive)
|
||||||
//! Finite(u8, u8),
|
//! Finite(u8, u8),
|
||||||
//! // Start (Exclusive)
|
//! // Start (Exclusive)
|
||||||
//! Infinite(u8)
|
//! Infinite(u8),
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! // First, we need to implement RangeBounds
|
//! // First, we need to implement RangeBounds
|
||||||
@ -78,7 +77,8 @@ along with range_bounds_map. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
//! let reservation_map = RangeBoundsMap::try_from([
|
//! let reservation_map = RangeBoundsMap::try_from([
|
||||||
//! (Reservation::Finite(10, 20), "Ferris".to_string()),
|
//! (Reservation::Finite(10, 20), "Ferris".to_string()),
|
||||||
//! (Reservation::Infinite(20), "Corro".to_string()),
|
//! (Reservation::Infinite(20), "Corro".to_string()),
|
||||||
//! ]).unwrap();
|
//! ])
|
||||||
|
//! .unwrap();
|
||||||
//!
|
//!
|
||||||
//! for (reservation, name) in reservation_map.overlapping(&(16..17)) {
|
//! for (reservation, name) in reservation_map.overlapping(&(16..17)) {
|
||||||
//! println!("{name} has reserved {reservation:?} inside the range 16..17");
|
//! println!("{name} has reserved {reservation:?} inside the range 16..17");
|
||||||
@ -91,7 +91,6 @@ along with range_bounds_map. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
//! assert_eq!(reservation_map.overlaps(&Reservation::Infinite(0)), true);
|
//! assert_eq!(reservation_map.overlaps(&Reservation::Infinite(0)), true);
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//!
|
|
||||||
//! # How
|
//! # How
|
||||||
//!
|
//!
|
||||||
//! Most of the [`RangeBounds`]-specific methods on [`RangeBoundsMap`]
|
//! Most of the [`RangeBounds`]-specific methods on [`RangeBoundsMap`]
|
||||||
@ -173,7 +172,7 @@ along with range_bounds_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
|
||||||
//! [`BTreeSet`]: https://doc.rust-lang.org/std/collections/struct.BTreeSet.html
|
//! [`BTreeSet`]: https://doc.rust-lang.org/std/collections/struct.BTreeSet.html
|
||||||
//! [`RangeBounds`]: https://doc.rust-lang.org/std/collections/struct.BTreeSet.html
|
//! [`RangeBounds`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html
|
||||||
//! [`start_bound()`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html#tymethod.start_bound
|
//! [`start_bound()`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html#tymethod.start_bound
|
||||||
//! [`end_bound()`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html#tymethod.end_bound
|
//! [`end_bound()`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html#tymethod.end_bound
|
||||||
//! [`Range`]: https://doc.rust-lang.org/std/ops/struct.Range.html
|
//! [`Range`]: https://doc.rust-lang.org/std/ops/struct.Range.html
|
||||||
|
@ -26,6 +26,91 @@ use either::Either;
|
|||||||
|
|
||||||
use crate::bounds::StartBound;
|
use crate::bounds::StartBound;
|
||||||
|
|
||||||
|
/// An ordered map of [`RangeBounds`] based on [`BTreeMap`]
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// use range_bounds_map::RangeBoundsMap;
|
||||||
|
///
|
||||||
|
/// // Make a map of ranges to booleans
|
||||||
|
/// let mut map = RangeBoundsMap::try_from([
|
||||||
|
/// (4..8, false),
|
||||||
|
/// (8..18, true),
|
||||||
|
/// (20..100, false),
|
||||||
|
/// ])
|
||||||
|
/// .unwrap();
|
||||||
|
///
|
||||||
|
/// // Change a value in the map
|
||||||
|
/// *map.get_at_point_mut(&(7)).unwrap() = true;
|
||||||
|
///
|
||||||
|
/// // Get a value in the map
|
||||||
|
/// if map.contains_point(&99) {
|
||||||
|
/// println!("Map contains value at 99 :)");
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// // Iterate over the entries in the map
|
||||||
|
/// for (range, value) in map.iter() {
|
||||||
|
/// println!("{range:?}, {value:?}");
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
/// Example using a custom [`RangeBounds`] type:
|
||||||
|
/// ```
|
||||||
|
/// use std::ops::{Bound, RangeBounds};
|
||||||
|
///
|
||||||
|
/// use ordered_float::NotNan;
|
||||||
|
/// use range_bounds_map::RangeBoundsMap;
|
||||||
|
///
|
||||||
|
/// // An Exlusive-Exlusive range of [`f32`]s not provided by any
|
||||||
|
/// // std::ops ranges
|
||||||
|
/// // We use [`ordered_float::NotNan`]s as the inner type must be Ord
|
||||||
|
/// // similar to a normal [`BTreeSet`]
|
||||||
|
/// #[derive(Debug, PartialEq)]
|
||||||
|
/// struct ExEx {
|
||||||
|
/// start: NotNan<f32>,
|
||||||
|
/// end: NotNan<f32>,
|
||||||
|
/// }
|
||||||
|
/// # impl ExEx {
|
||||||
|
/// # fn new(start: f32, end: f32) -> ExEx {
|
||||||
|
/// # ExEx {
|
||||||
|
/// # start: NotNan::new(start).unwrap(),
|
||||||
|
/// # end: NotNan::new(end).unwrap(),
|
||||||
|
/// # }
|
||||||
|
/// # }
|
||||||
|
/// # }
|
||||||
|
///
|
||||||
|
/// // Implement RangeBounds<f32> on our new type
|
||||||
|
/// impl RangeBounds<NotNan<f32>> for ExEx {
|
||||||
|
/// fn start_bound(&self) -> Bound<&NotNan<f32>> {
|
||||||
|
/// Bound::Excluded(&self.start)
|
||||||
|
/// }
|
||||||
|
/// fn end_bound(&self) -> Bound<&NotNan<f32>> {
|
||||||
|
/// Bound::Excluded(&self.end)
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// // Now we can make a [`RangeBoundsMap`] of [`ExEx`]s to `u8`
|
||||||
|
/// let mut map = RangeBoundsMap::new();
|
||||||
|
///
|
||||||
|
/// map.insert(ExEx::new(0.0, 5.0), 8).unwrap();
|
||||||
|
/// map.insert(ExEx::new(5.0, 7.5), 32).unwrap();
|
||||||
|
///
|
||||||
|
/// assert_eq!(map.contains_point(&NotNan::new(5.0).unwrap()), false);
|
||||||
|
///
|
||||||
|
/// assert_eq!(map.get_at_point(&NotNan::new(9.0).unwrap()), None);
|
||||||
|
/// assert_eq!(
|
||||||
|
/// map.get_at_point(&NotNan::new(7.0).unwrap()),
|
||||||
|
/// Some(&32)
|
||||||
|
/// );
|
||||||
|
///
|
||||||
|
/// assert_eq!(
|
||||||
|
/// map.get_key_value_at_point(&NotNan::new(2.0).unwrap()),
|
||||||
|
/// Some((&ExEx::new(0.0, 5.0), &8))
|
||||||
|
/// );
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// [`RangeBounds`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html
|
||||||
|
/// [`BTreeMap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct RangeBoundsMap<I, K, V> {
|
pub struct RangeBoundsMap<I, K, V> {
|
||||||
starts: BTreeMap<StartBound<I>, (K, V)>,
|
starts: BTreeMap<StartBound<I>, (K, V)>,
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ use crate::range_bounds_map::RangeBoundsMap;
|
|||||||
/// let visits = RangeBoundsSet::try_from([4..8, 8..18, 20..100]).unwrap();
|
/// let visits = RangeBoundsSet::try_from([4..8, 8..18, 20..100]).unwrap();
|
||||||
///
|
///
|
||||||
/// // Check if a point is contained in the set
|
/// // Check if a point is contained in the set
|
||||||
/// if visits.contains_point(&0) {
|
/// if !visits.contains_point(&0) {
|
||||||
/// println!("No visit at the beginning ;(");
|
/// println!("No visit at the beginning ;(");
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
@ -42,10 +42,9 @@ use crate::range_bounds_map::RangeBoundsMap;
|
|||||||
/// ```
|
/// ```
|
||||||
/// Example using a custom [`RangeBounds`] type:
|
/// Example using a custom [`RangeBounds`] type:
|
||||||
/// ```
|
/// ```
|
||||||
/// use std::ops::Bound;
|
/// use std::ops::{Bound, RangeBounds};
|
||||||
/// use std::ops::RangeBounds;
|
|
||||||
/// use ordered_float::NotNan;
|
|
||||||
///
|
///
|
||||||
|
/// use ordered_float::NotNan;
|
||||||
/// use range_bounds_map::RangeBoundsSet;
|
/// use range_bounds_map::RangeBoundsSet;
|
||||||
///
|
///
|
||||||
/// // An Exlusive-Exlusive range of [`f32`]s not provided by any
|
/// // An Exlusive-Exlusive range of [`f32`]s not provided by any
|
||||||
@ -81,12 +80,12 @@ use crate::range_bounds_map::RangeBoundsMap;
|
|||||||
/// set.insert(ExEx::new(0.0, 5.0)).unwrap();
|
/// set.insert(ExEx::new(0.0, 5.0)).unwrap();
|
||||||
/// set.insert(ExEx::new(5.0, 7.5)).unwrap();
|
/// set.insert(ExEx::new(5.0, 7.5)).unwrap();
|
||||||
///
|
///
|
||||||
/// assert_eq!(set.contains_point(&(NotNan::new(5.0).unwrap())), false);
|
/// assert_eq!(set.contains_point(&NotNan::new(5.0).unwrap()), false);
|
||||||
/// assert_eq!(set.contains_point(&(NotNan::new(7.0).unwrap())), true);
|
/// assert_eq!(set.contains_point(&NotNan::new(7.0).unwrap()), true);
|
||||||
/// assert_eq!(set.contains_point(&(NotNan::new(7.5).unwrap())), false);
|
/// assert_eq!(set.contains_point(&NotNan::new(7.5).unwrap()), false);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`RangeBounds`]: https://doc.rust-lang.org/std/collections/struct.BTreeSet.html
|
/// [`RangeBounds`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html
|
||||||
/// [`BTreeSet`]: https://doc.rust-lang.org/std/collections/struct.BTreeSet.html
|
/// [`BTreeSet`]: https://doc.rust-lang.org/std/collections/struct.BTreeSet.html
|
||||||
pub struct RangeBoundsSet<I, K> {
|
pub struct RangeBoundsSet<I, K> {
|
||||||
map: RangeBoundsMap<I, K, ()>,
|
map: RangeBoundsMap<I, K, ()>,
|
||||||
@ -102,6 +101,7 @@ where
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// use std::ops::Range;
|
/// use std::ops::Range;
|
||||||
|
///
|
||||||
/// use range_bounds_map::RangeBoundsSet;
|
/// use range_bounds_map::RangeBoundsSet;
|
||||||
///
|
///
|
||||||
/// let range_bounds_set: RangeBoundsSet<u8, Range<u8>> = RangeBoundsSet::new();
|
/// let range_bounds_set: RangeBoundsSet<u8, Range<u8>> = RangeBoundsSet::new();
|
||||||
@ -179,7 +179,8 @@ where
|
|||||||
/// ```
|
/// ```
|
||||||
/// use range_bounds_map::RangeBoundsSet;
|
/// use range_bounds_map::RangeBoundsSet;
|
||||||
///
|
///
|
||||||
/// let range_bounds_set = RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap();
|
/// let range_bounds_set =
|
||||||
|
/// RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap();
|
||||||
///
|
///
|
||||||
/// let mut overlapping = range_bounds_set.overlapping(&(2..8));
|
/// let mut overlapping = range_bounds_set.overlapping(&(2..8));
|
||||||
///
|
///
|
||||||
@ -206,7 +207,8 @@ where
|
|||||||
/// ```
|
/// ```
|
||||||
/// use range_bounds_map::RangeBoundsSet;
|
/// use range_bounds_map::RangeBoundsSet;
|
||||||
///
|
///
|
||||||
/// let range_bounds_set = RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap();
|
/// let range_bounds_set =
|
||||||
|
/// RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap();
|
||||||
///
|
///
|
||||||
/// assert_eq!(range_bounds_set.get_at_point(&3), Some(&(1..4)));
|
/// assert_eq!(range_bounds_set.get_at_point(&3), Some(&(1..4)));
|
||||||
/// assert_eq!(range_bounds_set.get_at_point(&4), Some(&(4..8)));
|
/// assert_eq!(range_bounds_set.get_at_point(&4), Some(&(4..8)));
|
||||||
@ -223,7 +225,8 @@ where
|
|||||||
/// ```
|
/// ```
|
||||||
/// use range_bounds_map::RangeBoundsSet;
|
/// use range_bounds_map::RangeBoundsSet;
|
||||||
///
|
///
|
||||||
/// let range_bounds_set = RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap();
|
/// let range_bounds_set =
|
||||||
|
/// RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap();
|
||||||
///
|
///
|
||||||
/// assert_eq!(range_bounds_set.contains_point(&3), true);
|
/// assert_eq!(range_bounds_set.contains_point(&3), true);
|
||||||
/// assert_eq!(range_bounds_set.contains_point(&4), true);
|
/// assert_eq!(range_bounds_set.contains_point(&4), true);
|
||||||
@ -240,7 +243,8 @@ where
|
|||||||
/// ```
|
/// ```
|
||||||
/// use range_bounds_map::RangeBoundsSet;
|
/// use range_bounds_map::RangeBoundsSet;
|
||||||
///
|
///
|
||||||
/// let range_bounds_set = RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap();
|
/// let range_bounds_set =
|
||||||
|
/// RangeBoundsSet::try_from([1..4, 4..8, 8..100]).unwrap();
|
||||||
///
|
///
|
||||||
/// let mut iter = range_bounds_set.overlapping(&(2..8));
|
/// let mut iter = range_bounds_set.overlapping(&(2..8));
|
||||||
///
|
///
|
||||||
|
Loading…
x
Reference in New Issue
Block a user