pub struct Plan<P: PlanStorage = Heap> { /* private fields */ }Expand description
A set of numeric and state timelines, one per resource, checked together.
The storage P is storage::Heap by default. With
FixedPlan<T, S, N> the plan holds at most T numeric and S
state timelines with Fixed<N> storage each, and placement,
checks and the earliest-start search never allocate.
§Examples
use resource_timeline::{ActivityId, Demand, Impact, Plan, Timeline, Window};
let mut plan = Plan::new();
let transmitter = plan.add_timeline(Timeline::claimable(0, 1, 16)?)?;
let downlink = [Demand::Level {
timeline: transmitter,
impact: Impact::Claim { start: 0, end: 8, amount: 1 },
}];
plan.place(ActivityId(1), 5, &downlink)?;
let window = Window::new(0, 100)?;
// Before tick 5 there is no room for 8 ticks; the transmitter is free again at 13.
assert_eq!(plan.earliest_start(&downlink, window, 1_000)?, Some(13));The same plan without allocation, for at most two numeric and one state timeline with room for 16 impacts each:
use resource_timeline::storage::{Fixed, FixedPlan};
use resource_timeline::{ActivityId, Demand, Impact, Plan, Timeline, Window};
let mut plan = Plan::<FixedPlan<2, 1, 16>>::new_in();
let transmitter = plan.add_timeline(Timeline::<Fixed<16>>::claimable_in(0, 1, 16)?)?;
let downlink = [Demand::Level {
timeline: transmitter,
impact: Impact::Claim { start: 0, end: 8, amount: 1 },
}];
plan.place(ActivityId(1), 5, &downlink)?;
assert_eq!(plan.earliest_start(&downlink, Window::new(0, 100)?, 1_000)?, Some(13));Implementations§
Source§impl<P: PlanStorage> Plan<P>
impl<P: PlanStorage> Plan<P>
Sourcepub fn add_timeline(
&mut self,
timeline: Timeline<P::Line>,
) -> Result<TimelineId, Error>
pub fn add_timeline( &mut self, timeline: Timeline<P::Line>, ) -> Result<TimelineId, Error>
Adds a numeric timeline and returns its handle.
§Errors
Error::OutOfMemory if the timeline list cannot grow (heap storage),
Error::CapacityExceeded naming Container::Timelines if it is full (fixed
storage).
Sourcepub fn add_state(
&mut self,
timeline: StateTimeline<P::Line>,
) -> Result<StateId, Error>
pub fn add_state( &mut self, timeline: StateTimeline<P::Line>, ) -> Result<StateId, Error>
Adds a state timeline and returns its handle.
§Errors
Error::OutOfMemory if the timeline list cannot grow (heap storage),
Error::CapacityExceeded naming Container::States if it is full (fixed storage).
Sourcepub fn timelines(&self) -> &[Timeline<P::Line>]
pub fn timelines(&self) -> &[Timeline<P::Line>]
The numeric timelines, in the order they were added.
Sourcepub fn states(&self) -> &[StateTimeline<P::Line>]
pub fn states(&self) -> &[StateTimeline<P::Line>]
The state timelines, in the order they were added.
Sourcepub fn timeline(&self, id: TimelineId) -> Option<&Timeline<P::Line>>
pub fn timeline(&self, id: TimelineId) -> Option<&Timeline<P::Line>>
The numeric timeline behind id, if it belongs to this plan.
Sourcepub fn state(&self, id: StateId) -> Option<&StateTimeline<P::Line>>
pub fn state(&self, id: StateId) -> Option<&StateTimeline<P::Line>>
The state timeline behind id, if it belongs to this plan.
Sourcepub fn add_target(
&mut self,
id: TimelineId,
target: Target,
) -> Result<(), Error>
pub fn add_target( &mut self, id: TimelineId, target: Target, ) -> Result<(), Error>
Requires the level of numeric timeline id at target.at to be at least target.min.
See Timeline::add_target. fits and
earliest_start then reject starts that would miss it.
§Errors
Error::UnknownResource if id is not part of this plan, and the errors of
Timeline::add_target.
Sourcepub fn place(
&mut self,
activity: ActivityId,
start: Tick,
demands: &[Demand],
) -> Result<(), Error>
pub fn place( &mut self, activity: ActivityId, start: Tick, demands: &[Demand], ) -> Result<(), Error>
Adds every demand of activity, shifted to start, to its timeline.
The placement is all or nothing. It does not check the bounds; use
fits before or first_violation after.
Placing the same activity id twice is allowed; remove then removes both.
§Errors
Error::UnknownResourceif a demand names a timeline outside this plan.Error::Overflowif a shifted tick does not fit aTick.- The validation errors of
Timeline::addandStateTimeline::add, includingError::BudgetExhaustedand, with fixed storage,Error::CapacityExceeded. Both are detected before anything is added. Error::OutOfMemoryif an impact list cannot grow.
The plan is unchanged on error.
Sourcepub fn remove(&mut self, activity: ActivityId) -> usize
pub fn remove(&mut self, activity: ActivityId) -> usize
Removes every impact of activity from every timeline and returns how many there were.
Sourcepub fn first_violation(&self) -> Result<Option<PlanViolation<P::Line>>, Error>
pub fn first_violation(&self) -> Result<Option<PlanViolation<P::Line>>, Error>
The earliest violation over all timelines.
Ties at the same tick go to numeric timelines before state timelines, then to the timeline added first.
§Errors
The errors of Timeline::first_violation and StateTimeline::first_violation.
Sourcepub fn fits(&self, demands: &[Demand], start: Tick) -> Result<bool, Error>
pub fn fits(&self, demands: &[Demand], start: Tick) -> Result<bool, Error>
Whether the activity described by demands could start at start without violating
any timeline it touches.
“Without violating” means the touched timelines have no violation at all once the
demands are added, so a timeline that is already violated admits nothing. Timelines the
demands do not touch are not checked. A demand that would precede its timeline’s start
does not fit. The impact budgets are not consulted; place does that.
§Errors
Error::UnknownResourceif a demand names a timeline outside this plan.Error::Overflowif a shifted tick or a level does not fit.- The validation errors of
Timeline::addandStateTimeline::addother thanError::BeforeStart, which mean the demands are malformed at every start. Error::OutOfMemoryif the scratch buffers cannot be allocated (heap storage).Error::CapacityExceedednamingContainer::Scratchif more demands address one timeline than its fixed capacity holds (fixed storage); such an activity could not be placed either.
Sourcepub fn fits_with(
&self,
demands: &[Demand],
start: Tick,
scratch: &mut SearchScratch<P::Line>,
) -> Result<bool, Error>
pub fn fits_with( &self, demands: &[Demand], start: Tick, scratch: &mut SearchScratch<P::Line>, ) -> Result<bool, Error>
Sourcepub fn earliest_start(
&self,
demands: &[Demand],
window: Window,
max_checks: usize,
) -> Result<Option<Tick>, Error>
pub fn earliest_start( &self, demands: &[Demand], window: Window, max_checks: usize, ) -> Result<Option<Tick>, Error>
The earliest start in window at which fits holds, or None.
The answer can only change where a demand tick meets a change tick or target of its
timeline. The search walks the pieces between consecutive such candidates, O(n k) of
them for n change ticks and k demands, without collecting them. Without rates the
levels are piecewise constant and one check at the piece start decides the whole piece.
With rates (in the demands or a touched timeline) the levels are affine in the start
within a piece, and the earliest fitting start of the piece is solved in closed form
from two sweeps, so the cost does not grow with the window length. Each piece costs
O(n k); nothing is allocated beyond the scratch reserved up front.
max_checks caps the number of pieces examined.
§Errors
Error::SearchExhaustedifmax_checkspieces were examined without an answer.- The errors of
fits.
Sourcepub fn earliest_start_with(
&self,
demands: &[Demand],
window: Window,
max_checks: usize,
scratch: &mut SearchScratch<P::Line>,
) -> Result<Option<Tick>, Error>
pub fn earliest_start_with( &self, demands: &[Demand], window: Window, max_checks: usize, scratch: &mut SearchScratch<P::Line>, ) -> Result<Option<Tick>, Error>
earliest_start with borrowed working memory instead of a
stack buffer; see SearchScratch for sizes and an example.
§Errors
As for earliest_start.