Giving up on implementing Add for complex types

This commit is contained in:
Kinesin Data Technologies Incorporated
2022-10-04 09:06:20 -03:00
parent 388af82aaf
commit 26aa4dd4ea
2 changed files with 29 additions and 18 deletions
+28 -17
View File
@@ -20,20 +20,29 @@ impl ResourceInterval {
.merge(intervals); .merge(intervals);
} }
pub fn union(&mut self, other: ResourceInterval) { pub fn union(&self, other: &ResourceInterval) -> Self {
for (res, is) in other.iter() { let res: HashMap<Resource, IntervalSet> =
self.0 other.0.iter().fold(self.0.clone(), |mut acc, (res, is)| {
.entry(res.clone()) acc.entry(res.clone())
.or_insert(IntervalSet::new()) .or_insert(IntervalSet::new())
.merge(is); .merge(is);
} acc
});
ResourceInterval(res)
} }
pub fn subtract(&mut self, other: ResourceInterval) { pub fn difference(&mut self, other: &ResourceInterval) -> Self {
for (res, is) in other.iter() { let res: HashMap<Resource, IntervalSet> = self
let avail = self.0.entry(res.clone()).or_insert(IntervalSet::new()); .0
*avail = avail.difference(is); .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<Resource, IntervalSet>> for ResourceInterval {
} }
} }
/*
impl Add for &ResourceInterval { impl Add for &ResourceInterval {
type Output = ResourceInterval; type Output = ResourceInterval;
fn add(self, other: &ResourceInterval) -> Self::Output { fn add(self, other: &ResourceInterval) -> Self::Output {
@@ -93,11 +103,12 @@ impl Sub for &ResourceInterval {
} }
} }
impl AsRef<ResourceInterval> for ResourceInterval { impl AsRef<Self> for ResourceInterval {
fn as_ref(&self) -> &ResourceInterval { fn as_ref(&self) -> &Self {
self self
} }
} }
*/
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@@ -131,17 +142,17 @@ mod tests {
fn test_addition() { fn test_addition() {
let a = ri!("alpha", (13, 15)); 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] #[test]
fn test_subtraction() { fn test_subtraction() {
assert_eq!( 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)) ri!("alpha", (13, 15), (16, 18))
); );
assert_eq!( assert_eq!(
&ri!("alpha", (13, 18)) - &ResourceInterval::new(), ri!("alpha", (13, 18)).difference(&ResourceInterval::new()),
ri!("alpha", (13, 18)) ri!("alpha", (13, 18))
); );
} }
+1 -1
View File
@@ -207,7 +207,7 @@ impl Runner {
}; };
// Create queue // Create queue
let required = &runner.target - &runner.current; let required = runner.target.difference(&runner.current);
runner.queue = runner runner.queue = runner
.tasks .tasks
.iter() .iter()