Adding resource interval

This commit is contained in:
Kinesin Data Technologies Incorporated
2022-09-29 11:41:45 -03:00
parent 00bf07db2b
commit 9cd2be5255
3 changed files with 69 additions and 1 deletions
+1 -1
View File
@@ -91,7 +91,7 @@ impl IntervalSet {
}
pub fn merge(&mut self, other: &IntervalSet) {
self.0.extend(other.0.clone());
self.0.extend(other.0.iter().cloned());
self.coalesce();
}
+2
View File
@@ -14,6 +14,7 @@ use crate::executors::*;
use crate::interval::*;
use crate::interval_set::*;
use crate::requirement::*;
use crate::resource_interval::*;
use crate::schedule::*;
use crate::storage::*;
use crate::task::*;
@@ -27,6 +28,7 @@ pub mod executors;
pub mod interval;
pub mod interval_set;
pub mod requirement;
pub mod resource_interval;
pub mod schedule;
pub mod storage;
pub mod task;
+66
View File
@@ -0,0 +1,66 @@
use super::*;
use std::ops::{Add, Deref, DerefMut, Sub};
/// Contains a map of resource and intervals. The intervals could
/// represent where a resource is available, or where it's required
/// Resources are independent, so overlaps between the
/// interval sets are possible.
pub struct ResourceInterval(HashMap<Resource, IntervalSet>);
impl ResourceInterval {
fn new() -> Self {
ResourceInterval(HashMap::new())
}
}
impl Deref for ResourceInterval {
type Target = HashMap<Resource, IntervalSet>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ResourceInterval {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl From<HashMap<Resource, IntervalSet>> for ResourceInterval {
fn from(hm: HashMap<Resource, IntervalSet>) -> Self {
ResourceInterval(hm)
}
}
impl From<&HashMap<Resource, IntervalSet>> for ResourceInterval {
fn from(hm: &HashMap<Resource, IntervalSet>) -> Self {
ResourceInterval(hm.clone())
}
}
impl Add for &ResourceInterval {
type Output = ResourceInterval;
fn add(self, other: &ResourceInterval) -> Self::Output {
let res: HashMap<Resource, IntervalSet> =
other.0.iter().fold(self.0.clone(), |mut acc, (res, is)| {
acc.entry(res.clone())
.or_insert(IntervalSet::new())
.merge(is);
acc
});
ResourceInterval(res)
}
}
impl Sub for &ResourceInterval {
type Output = ResourceInterval;
fn sub(self, other: &ResourceInterval) -> Self::Output {
let res: HashMap<Resource, IntervalSet> =
other.0.iter().fold(self.0.clone(), |mut acc, (res, is)| {
acc.entry(res.clone())
.or_insert(IntervalSet::new())
.difference(is);
acc
});
ResourceInterval(res)
}
}