Skip to main content

Plan

Struct Plan 

Source
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 Plan

Source

pub const fn new() -> Self

Creates an empty heap plan.

Source§

impl<P: PlanStorage> Plan<P>

Source

pub const fn new_in() -> Self

Creates an empty plan with storage P.

Source

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).

Source

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).

Source

pub fn timelines(&self) -> &[Timeline<P::Line>]

The numeric timelines, in the order they were added.

Source

pub fn states(&self) -> &[StateTimeline<P::Line>]

The state timelines, in the order they were added.

Source

pub fn timeline(&self, id: TimelineId) -> Option<&Timeline<P::Line>>

The numeric timeline behind id, if it belongs to this plan.

Source

pub fn state(&self, id: StateId) -> Option<&StateTimeline<P::Line>>

The state timeline behind id, if it belongs to this plan.

Source

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.

Source

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

The plan is unchanged on error.

Source

pub fn remove(&mut self, activity: ActivityId) -> usize

Removes every impact of activity from every timeline and returns how many there were.

Source

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.

Source

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
Source

pub fn fits_with( &self, demands: &[Demand], start: Tick, scratch: &mut SearchScratch<P::Line>, ) -> Result<bool, Error>

fits with borrowed working memory instead of a stack buffer.

§Errors

As for fits.

Source

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
Source

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.

Trait Implementations§

Source§

impl<P: Clone + PlanStorage> Clone for Plan<P>
where P::Timelines: Clone, P::States: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P: Debug + PlanStorage> Debug for Plan<P>
where P::Timelines: Debug, P::States: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<P: Default + PlanStorage> Default for Plan<P>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<P: Eq + PlanStorage> Eq for Plan<P>
where P::Timelines: Eq, P::States: Eq,

Source§

impl<P: Hash + PlanStorage> Hash for Plan<P>
where P::Timelines: Hash, P::States: Hash,

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<P: PartialEq + PlanStorage> PartialEq for Plan<P>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<P: PartialEq + PlanStorage> StructuralPartialEq for Plan<P>

Auto Trait Implementations§

§

impl<P> Forget for Plan<P>

§

impl<P> Freeze for Plan<P>

§

impl<P> Move for Plan<P>

§

impl<P> RefUnwindSafe for Plan<P>

§

impl<P> Send for Plan<P>
where <P as PlanStorage>::Timelines: Send, <P as PlanStorage>::States: Send,

§

impl<P> Sync for Plan<P>
where <P as PlanStorage>::Timelines: Sync, <P as PlanStorage>::States: Sync,

§

impl<P> Unpin for Plan<P>

§

impl<P> UnsafeUnpin for Plan<P>

§

impl<P> UnwindSafe for Plan<P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.