make everything pub(crate) except the map, set, and traitExt

This commit is contained in:
ripytide 2022-11-27 00:49:02 +00:00
parent 2f359534c3
commit a5ce518a53
5 changed files with 54 additions and 34 deletions

View File

@ -1,8 +1,8 @@
use std::cmp::Ordering;
use std::ops::Bound;
#[derive(PartialEq)]
pub enum StartBound<T> {
#[derive(PartialEq, Debug)]
pub(crate) enum StartBound<T> {
Included(T),
Excluded(T),
Unbounded,
@ -15,7 +15,7 @@ pub enum StartBound<T> {
impl<T> StartBound<T> {
//when using this as an end value in a range search
pub fn as_end_value(self) -> StartBound<T> {
pub(crate) fn as_end_bound(self) -> StartBound<T> {
match self {
//flipping is unnecessary
StartBound::Included(point) => StartBound::Included(point),

View File

@ -1,12 +1,12 @@
#![feature(is_some_and)]
#![feature(let_chains)]
pub mod bounds;
pub(crate) mod bounds;
pub mod range_bounds_ext;
pub mod range_bounds_map;
pub mod range_bounds_set;
#[cfg(test)]
pub mod test_helpers;
pub(crate) mod test_helpers;
pub use crate::range_bounds_map::RangeBoundsMap;
pub use crate::range_bounds_set::RangeBoundsSet;

View File

@ -1,4 +1,5 @@
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::iter::once;
use std::ops::Bound;
@ -13,8 +14,8 @@ pub struct RangeBoundsMap<I, K, V> {
impl<I, K, V> RangeBoundsMap<I, K, V>
where
K: RangeBoundsExt<I>,
I: Ord + Clone,
K: RangeBoundsExt<I> + Debug,
I: Ord + Clone + Debug,
{
pub fn new() -> Self {
RangeBoundsMap {
@ -51,33 +52,38 @@ where
&self,
search_range_bounds: &K,
) -> impl Iterator<Item = (&K, &V)> {
let start =
StartBound::from(search_range_bounds.start_bound().cloned());
let end = StartBound::from(search_range_bounds.end_bound().cloned())
.as_end_bound();
dbg!(&start);
dbg!(&end);
let start_range_bounds = (
//Included is lossless regarding meta-bounds searches
Bound::Included(StartBound::from(
search_range_bounds.start_bound().cloned(),
)),
Bound::Included(
StartBound::from(search_range_bounds.end_bound().cloned())
.as_end_value(),
),
//which is what we want
Bound::Included(start),
Bound::Included(end),
);
//this range will hold all the ranges we want except possibly
//the first RangeBound in the range
let most_range_bounds = self.starts.range(start_range_bounds);
//then we check for this possibly missing range_bounds
if let Some(missing_entry @ (_, (possible_missing_range_bounds, _))) =
//Excluded is lossless regarding meta-bounds searches
//because we don't want equal bound as they would have be
//coverded in the previous step
//because we don't want equal bounds as they would have be
//covered in the previous step and we don't want duplicates
self.starts
.range((
Bound::Unbounded,
Bound::Excluded(
//optimisation fix this without cloning
//todo should probably use .as_end_value()
StartBound::from(
search_range_bounds.start_bound().cloned(),
),
)
.as_end_bound(),
),
))
.next()
@ -134,11 +140,14 @@ mod tests {
//if that works everything is likely to work so there are only
//tests for overlapping()
use std::ops::RangeBounds;
use crate::bounds::StartBound;
use crate::range_bounds_ext::RangeBoundsExt;
use crate::test_helpers::{all_valid_test_bounds, TestBounds};
use crate::RangeBoundsSet;
#[test]
#[test]
fn mass_overlapping_test() {
//case zero
for overlap_range in all_valid_test_bounds() {
@ -157,17 +166,19 @@ mod tests {
let mut range_bounds_set = RangeBoundsSet::new();
range_bounds_set.insert(inside_range).unwrap();
let expected_answer = overlap_range.overlaps(&inside_range);
let answer = range_bounds_set
.overlapping(&overlap_range)
.next()
.is_some_and(|overlapped_range| {
*overlapped_range == inside_range
});
let mut expected_overlapping = Vec::new();
if overlap_range.overlaps(&inside_range) {
expected_overlapping.push(inside_range);
}
if answer != expected_answer {
let overlapping = range_bounds_set
.overlapping(&overlap_range)
.copied()
.collect::<Vec<_>>();
if overlapping != expected_overlapping {
dbg!(overlap_range, inside_range);
dbg!(answer, expected_answer);
dbg!(overlapping, expected_overlapping);
panic!(
"Discrepency in .overlapping() with single inside range detected!"
);
@ -191,6 +202,15 @@ mod tests {
if overlap_range.overlaps(&inside_range2) {
expected_overlapping.push(inside_range2);
}
//make our expected_overlapping the correct order
if expected_overlapping.len() > 1 {
if StartBound::from(expected_overlapping[0].start_bound())
> StartBound::from(
expected_overlapping[1].start_bound(),
) {
expected_overlapping.swap(0, 1);
}
}
let overlapping = range_bounds_set
.overlapping(&overlap_range)

View File

@ -7,8 +7,8 @@ pub struct RangeBoundsSet<I, K> {
impl<I, K> RangeBoundsSet<I, K>
where
K: RangeBoundsExt<I>,
I: Ord + Clone,
K: RangeBoundsExt<I> + std::fmt::Debug,
I: Ord + Clone + std::fmt::Debug,
{
pub fn new() -> Self {
RangeBoundsSet {

View File

@ -1,8 +1,8 @@
use std::ops::Bound;
pub type TestBounds = (Bound<u8>, Bound<u8>);
pub(crate) type TestBounds = (Bound<u8>, Bound<u8>);
pub fn all_valid_test_bounds() -> Vec<TestBounds> {
pub(crate) fn all_valid_test_bounds() -> Vec<TestBounds> {
let mut output = Vec::new();
//bounded-bounded
@ -23,9 +23,9 @@ pub fn all_valid_test_bounds() -> Vec<TestBounds> {
//only every other number to allow mathematical_overlapping_definition
//to test between bounds in finite using smaller intervalled finite
pub const NUMBERS: &'static [u8] = &[2, 4, 6, 8, 10];
pub(crate) const NUMBERS: &'static [u8] = &[2, 4, 6, 8, 10];
//go a bit around on either side to compensate for Unbounded
pub const NUMBERS_DOMAIN: &'static [u8] =
pub(crate) const NUMBERS_DOMAIN: &'static [u8] =
&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
fn all_finite_bounded_pairs() -> Vec<(Bound<u8>, Bound<u8>)> {