From 26aa4dd4ea2f413c1998ef1051cdea8b1144f796 Mon Sep 17 00:00:00 2001 From: Kinesin Data Technologies Incorporated <93931750+kinesintech@users.noreply.github.com> Date: Tue, 4 Oct 2022 09:06:20 -0300 Subject: [PATCH] Giving up on implementing Add for complex types --- src/resource_interval.rs | 45 +++++++++++++++++++++++++--------------- src/runner.rs | 2 +- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/resource_interval.rs b/src/resource_interval.rs index 7f8fb9d..2138a72 100644 --- a/src/resource_interval.rs +++ b/src/resource_interval.rs @@ -20,20 +20,29 @@ impl ResourceInterval { .merge(intervals); } - pub fn union(&mut self, other: ResourceInterval) { - for (res, is) in other.iter() { - self.0 - .entry(res.clone()) - .or_insert(IntervalSet::new()) - .merge(is); - } + pub fn union(&self, other: &ResourceInterval) -> Self { + let res: HashMap = + other.0.iter().fold(self.0.clone(), |mut acc, (res, is)| { + acc.entry(res.clone()) + .or_insert(IntervalSet::new()) + .merge(is); + acc + }); + ResourceInterval(res) } - pub fn subtract(&mut self, other: ResourceInterval) { - for (res, is) in other.iter() { - let avail = self.0.entry(res.clone()).or_insert(IntervalSet::new()); - *avail = avail.difference(is); - } + pub fn difference(&mut self, other: &ResourceInterval) -> Self { + let res: HashMap = self + .0 + .iter() + .map(|(res, is)| { + ( + res.clone(), + is.difference(other.get(res).unwrap_or(&IntervalSet::new())), + ) + }) + .collect(); + ResourceInterval(res) } } @@ -62,6 +71,7 @@ impl From<&HashMap> for ResourceInterval { } } +/* impl Add for &ResourceInterval { type Output = ResourceInterval; fn add(self, other: &ResourceInterval) -> Self::Output { @@ -93,11 +103,12 @@ impl Sub for &ResourceInterval { } } -impl AsRef for ResourceInterval { - fn as_ref(&self) -> &ResourceInterval { +impl AsRef for ResourceInterval { + fn as_ref(&self) -> &Self { self } } +*/ #[cfg(test)] mod tests { @@ -131,17 +142,17 @@ mod tests { fn test_addition() { let a = ri!("alpha", (13, 15)); - assert_eq!(&a + &ri!("alpha", (15, 18)), ri!("alpha", (13, 18))); + assert_eq!(a.union(&ri!("alpha", (15, 18))), ri!("alpha", (13, 18))); } #[test] fn test_subtraction() { assert_eq!( - &ri!("alpha", (13, 18)) - &ri!("alpha", (15, 16)), + ri!("alpha", (13, 18)).difference(&ri!("alpha", (15, 16))), ri!("alpha", (13, 15), (16, 18)) ); assert_eq!( - &ri!("alpha", (13, 18)) - &ResourceInterval::new(), + ri!("alpha", (13, 18)).difference(&ResourceInterval::new()), ri!("alpha", (13, 18)) ); } diff --git a/src/runner.rs b/src/runner.rs index 216840e..b133370 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -207,7 +207,7 @@ impl Runner { }; // Create queue - let required = &runner.target - &runner.current; + let required = runner.target.difference(&runner.current); runner.queue = runner .tasks .iter()