fully implemented the OverlapError returning value

This commit is contained in:
ripytide
2023-12-26 12:26:16 +00:00
parent 4bc4458625
commit 36997d947a
2 changed files with 32 additions and 26 deletions
+31 -25
View File
@@ -85,10 +85,13 @@ pub struct DiscreteRangeMap<I, K, V> {
phantom: PhantomData<I>, phantom: PhantomData<I>,
} }
/// An error type to represent a range overlapping another range when /// The error returned when inserting a range that overlaps another range when
/// it should not have. /// it should not have. Contains the value that was not inserted.
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub struct OverlapError; pub struct OverlapError<V> {
/// The value which was not inserted, because of the overlap error.
pub value: V,
}
/// A compatibility type used in [`RangeType`] for allowing the library to /// A compatibility type used in [`RangeType`] for allowing the library to
/// create the custom K type used in the map when necessary. /// create the custom K type used in the map when necessary.
@@ -768,18 +771,21 @@ where
/// let mut map = DiscreteRangeMap::new(); /// let mut map = DiscreteRangeMap::new();
/// ///
/// assert_eq!(map.insert_strict(ie(5, 10), 9), Ok(())); /// assert_eq!(map.insert_strict(ie(5, 10), 9), Ok(()));
/// assert_eq!(map.insert_strict(ie(5, 10), 2), Err(OverlapError)); /// assert_eq!(
/// map.insert_strict(ie(5, 10), 2),
/// Err(OverlapError { value: 2 })
/// );
/// assert_eq!(map.len(), 1); /// assert_eq!(map.len(), 1);
/// ``` /// ```
pub fn insert_strict( pub fn insert_strict(
&mut self, &mut self,
range: K, range: K,
value: V, value: V,
) -> Result<(), OverlapError> { ) -> Result<(), OverlapError<V>> {
invalid_range_panic(range); invalid_range_panic(range);
if self.overlaps(range) { if self.overlaps(range) {
return Err(OverlapError); return Err(OverlapError { value });
} }
self.insert_unchecked(range, value); self.insert_unchecked(range, value);
@@ -877,7 +883,7 @@ where
/// // Overlapping /// // Overlapping
/// assert_eq!( /// assert_eq!(
/// map.insert_merge_touching(ie(4, 8), false), /// map.insert_merge_touching(ie(4, 8), false),
/// Err(OverlapError), /// Err(OverlapError { value: false }),
/// ); /// );
/// ///
/// // Neither Touching or Overlapping /// // Neither Touching or Overlapping
@@ -895,11 +901,11 @@ where
&mut self, &mut self,
range: K, range: K,
value: V, value: V,
) -> Result<K, OverlapError> { ) -> Result<K, OverlapError<V>> {
invalid_range_panic(range); invalid_range_panic(range);
if self.overlaps(range) { if self.overlaps(range) {
return Err(OverlapError); return Err(OverlapError { value });
} }
Ok(self.insert_merge_with_comps( Ok(self.insert_merge_with_comps(
@@ -965,7 +971,7 @@ where
/// // Overlapping /// // Overlapping
/// assert_eq!( /// assert_eq!(
/// map.insert_merge_touching_if_values_equal(ie(4, 8), false), /// map.insert_merge_touching_if_values_equal(ie(4, 8), false),
/// Err(OverlapError), /// Err(OverlapError { value: false }),
/// ); /// );
/// ///
/// // Neither Touching or Overlapping /// // Neither Touching or Overlapping
@@ -983,14 +989,14 @@ where
&mut self, &mut self,
range: K, range: K,
value: V, value: V,
) -> Result<K, OverlapError> ) -> Result<K, OverlapError<V>>
where where
V: Eq, V: Eq,
{ {
invalid_range_panic(range); invalid_range_panic(range);
if self.overlaps(range) { if self.overlaps(range) {
return Err(OverlapError); return Err(OverlapError { value });
} }
let get_start = |selfy: &Self, value: &V| { let get_start = |selfy: &Self, value: &V| {
@@ -1260,7 +1266,7 @@ where
/// ``` /// ```
pub fn from_slice_strict<const N: usize>( pub fn from_slice_strict<const N: usize>(
slice: [(K, V); N], slice: [(K, V); N],
) -> Result<DiscreteRangeMap<I, K, V>, OverlapError> { ) -> Result<DiscreteRangeMap<I, K, V>, OverlapError<V>> {
let mut map = DiscreteRangeMap::new(); let mut map = DiscreteRangeMap::new();
for (range, value) in slice { for (range, value) in slice {
map.insert_strict(range, value)?; map.insert_strict(range, value)?;
@@ -1296,7 +1302,7 @@ where
/// ``` /// ```
pub fn from_iter_strict( pub fn from_iter_strict(
iter: impl Iterator<Item = (K, V)>, iter: impl Iterator<Item = (K, V)>,
) -> Result<DiscreteRangeMap<I, K, V>, OverlapError> { ) -> Result<DiscreteRangeMap<I, K, V>, OverlapError<V>> {
let mut map = DiscreteRangeMap::new(); let mut map = DiscreteRangeMap::new();
for (range, value) in iter { for (range, value) in iter {
map.insert_strict(range, value)?; map.insert_strict(range, value)?;
@@ -1511,7 +1517,7 @@ pub trait InclusiveRange<I> {
/// The end of the range, inclusive. /// The end of the range, inclusive.
fn end(&self) -> I; fn end(&self) -> I;
/// Does the range contain the given point? /// Does the range contain the given point?
fn contains(&self, point: I) -> bool fn contains(&self, point: I) -> bool
where where
I: PointType, I: PointType,
@@ -1519,8 +1525,8 @@ pub trait InclusiveRange<I> {
point >= self.start() && point <= self.end() point >= self.start() && point <= self.end()
} }
/// Is the range is valid, which according to this crate means `start()` /// Is the range is valid, which according to this crate means `start()`
/// <= `end()` /// <= `end()`
fn is_valid(&self) -> bool fn is_valid(&self) -> bool
where where
I: PointType, I: PointType,
@@ -1740,19 +1746,19 @@ mod tests {
assert_insert_strict( assert_insert_strict(
basic(), basic(),
(ii(0, 4), false), (ii(0, 4), false),
Err(OverlapError), Err(OverlapError { value: false }),
basic_slice(), basic_slice(),
); );
assert_insert_strict( assert_insert_strict(
basic(), basic(),
(ii(5, 6), false), (ii(5, 6), false),
Err(OverlapError), Err(OverlapError { value: false }),
basic_slice(), basic_slice(),
); );
assert_insert_strict( assert_insert_strict(
basic(), basic(),
(ii(4, 5), true), (ii(4, 5), true),
Err(OverlapError), Err(OverlapError { value: true }),
basic_slice(), basic_slice(),
); );
assert_insert_strict( assert_insert_strict(
@@ -1771,7 +1777,7 @@ mod tests {
fn assert_insert_strict<const N: usize>( fn assert_insert_strict<const N: usize>(
mut before: DiscreteRangeMap<i8, InclusiveInterval<i8>, bool>, mut before: DiscreteRangeMap<i8, InclusiveInterval<i8>, bool>,
to_insert: (InclusiveInterval<i8>, bool), to_insert: (InclusiveInterval<i8>, bool),
result: Result<(), OverlapError>, result: Result<(), OverlapError<bool>>,
after: [(InclusiveInterval<i8>, bool); N], after: [(InclusiveInterval<i8>, bool); N],
) { ) {
assert_eq!(before.insert_strict(to_insert.0, to_insert.1), result); assert_eq!(before.insert_strict(to_insert.0, to_insert.1), result);
@@ -1988,7 +1994,7 @@ mod tests {
assert_insert_merge_touching( assert_insert_merge_touching(
basic(), basic(),
(ii(0, 4), false), (ii(0, 4), false),
Err(OverlapError), Err(OverlapError { value: false }),
[ [
(ui(4), false), (ui(4), false),
(ee(5, 7), true), (ee(5, 7), true),
@@ -2028,7 +2034,7 @@ mod tests {
fn assert_insert_merge_touching<const N: usize>( fn assert_insert_merge_touching<const N: usize>(
mut before: DiscreteRangeMap<i8, InclusiveInterval<i8>, bool>, mut before: DiscreteRangeMap<i8, InclusiveInterval<i8>, bool>,
to_insert: (InclusiveInterval<i8>, bool), to_insert: (InclusiveInterval<i8>, bool),
result: Result<InclusiveInterval<i8>, OverlapError>, result: Result<InclusiveInterval<i8>, OverlapError<bool>>,
after: [(InclusiveInterval<i8>, bool); N], after: [(InclusiveInterval<i8>, bool); N],
) { ) {
assert_eq!( assert_eq!(
@@ -2042,7 +2048,7 @@ mod tests {
assert_insert_merge_touching_if_values_equal( assert_insert_merge_touching_if_values_equal(
basic(), basic(),
(ii(0, 4), false), (ii(0, 4), false),
Err(OverlapError), Err(OverlapError { value: false }),
basic_slice(), basic_slice(),
); );
dbg!("hererere"); dbg!("hererere");
@@ -2084,7 +2090,7 @@ mod tests {
fn assert_insert_merge_touching_if_values_equal<const N: usize>( fn assert_insert_merge_touching_if_values_equal<const N: usize>(
mut before: DiscreteRangeMap<i8, InclusiveInterval<i8>, bool>, mut before: DiscreteRangeMap<i8, InclusiveInterval<i8>, bool>,
to_insert: (InclusiveInterval<i8>, bool), to_insert: (InclusiveInterval<i8>, bool),
result: Result<InclusiveInterval<i8>, OverlapError>, result: Result<InclusiveInterval<i8>, OverlapError<bool>>,
after: [(InclusiveInterval<i8>, bool); N], after: [(InclusiveInterval<i8>, bool); N],
) { ) {
assert_eq!( assert_eq!(
+1 -1
View File
@@ -145,7 +145,7 @@ where
/// See [`DiscreteRangeMap::from_iter_strict()`] for more details. /// See [`DiscreteRangeMap::from_iter_strict()`] for more details.
pub fn from_iter_strict( pub fn from_iter_strict(
iter: impl Iterator<Item = K>, iter: impl Iterator<Item = K>,
) -> Result<DiscreteRangeSet<I, K>, OverlapError> { ) -> Result<DiscreteRangeSet<I, K>, OverlapError<()>> {
let mut set = DiscreteRangeSet::new(); let mut set = DiscreteRangeSet::new();
for range in iter { for range in iter {
set.insert_strict(range)?; set.insert_strict(range)?;