use crate::interval::*;
use crate::intervals::*;
pub struct IntervalsCache<Is>
where
Is: Intervals,
{
intervals: Is,
pos: Option<Option<LeftT<Is::Endpoint>>>,
value: Option<Interval<Is::Endpoint, Is::Value>>,
}
impl<Is> IntervalsCache<Is>
where
Is: Intervals,
{
pub fn new(intervals: Is) -> IntervalsCache<Is> {
IntervalsCache {
intervals,
pos: None,
value: None,
}
}
pub fn uncache(self) -> Is {
self.intervals
}
}
impl<Is> Intervals for IntervalsCache<Is>
where
Is: Intervals,
Is::Endpoint: Clone + EndpointToggle,
Is::Value: Clone,
LeftT<Is::Endpoint>: Ord,
{
type Endpoint = Is::Endpoint;
type Value = Is::Value;
fn head(
&mut self,
pos: Option<LeftT<Self::Endpoint>>,
) -> Option<Interval<Self::Endpoint, Self::Value>> {
if let Some(cached_pos) = &self.pos {
if cached_pos == &pos {
return self.value.clone();
}
}
self.value = self.intervals.head(pos.clone());
self.pos = Some(pos);
self.value.clone()
}
}