StepSchedule

Struct StepSchedule 

Source
pub struct StepSchedule<T, V> { /* private fields */ }
Expand description

Associates values with distinct, non-overlapping time ranges.

A StepSchedule is a collection of Steps, each mapping a value to a non-empty, non-overlapping half-open range [start, end).

The range type T must implement Ord, and usually represents time, though any ordered type (e.g., numbers or indices) is supported.

§Examples

use twine_components::schedule::step_schedule::{Step, StepSchedule};

let schedule = StepSchedule::new([
    Step::new(0..10, "low").unwrap(),
    Step::new(10..20, "medium").unwrap(),
    Step::new(20..30, "high").unwrap(),
]).unwrap();

assert_eq!(schedule.value_at(&-1), None);
assert_eq!(schedule.value_at(&0), Some(&"low"));
assert_eq!(schedule.value_at(&15), Some(&"medium"));
assert_eq!(schedule.value_at(&25), Some(&"high"));
assert_eq!(schedule.value_at(&30), None);

Implementations§

Source§

impl<T: Debug + Clone + Ord, V> StepSchedule<T, V>

Source

pub fn new<I>(steps: I) -> Result<Self, OverlappingStepsError<T>>
where I: IntoIterator<Item = Step<T, V>>,

Creates a new StepSchedule from an iterator over Steps.

Accepts any type convertible into an iterator, such as a vector or array. The resulting schedule is ordered by increasing start time.

§Errors

Returns an OverlappingStepsError if any steps have overlapping ranges. Steps are sorted by start time before validation, so the existing step will always have an earlier start than the incoming one in the error.

Source

pub fn try_push( &mut self, step: Step<T, V>, ) -> Result<(), OverlappingStepsError<T>>

Attempts to add a step to the schedule.

§Errors

Returns an OverlappingStepsError if the new step’s range overlaps with any existing steps in the schedule.

Source

pub fn steps(&self) -> &[Step<T, V>]

Returns a slice of the steps in this schedule.

Source

pub fn value_at(&self, time: &T) -> Option<&V>

Returns the value associated with the given time, if any.

Searches for the Step whose range contains time and returns a reference to its associated value. Returns None if no such step exists.

For schedules with fewer than [LINEAR_SEARCH_THRESHOLD] steps, a linear scan is used. Otherwise, binary search is performed.

§Examples
use twine_components::schedule::step_schedule::{Step, StepSchedule};

let schedule = StepSchedule::new(vec![
    Step::new(0..10, 1).unwrap(),
    Step::new(10..20, 2).unwrap(),
]).unwrap();

assert_eq!(schedule.value_at(&5), Some(&1));
assert_eq!(schedule.value_at(&15), Some(&2));
assert_eq!(schedule.value_at(&20), None);

Trait Implementations§

Source§

impl<T: Clone, V: Clone> Clone for StepSchedule<T, V>

Source§

fn clone(&self) -> StepSchedule<T, V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, V: Debug> Debug for StepSchedule<T, V>

Source§

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

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

impl<T: Default, V: Default> Default for StepSchedule<T, V>

Source§

fn default() -> StepSchedule<T, V>

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

impl<T: PartialEq, V: PartialEq> PartialEq for StepSchedule<T, V>

Source§

fn eq(&self, other: &StepSchedule<T, V>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq, V: Eq> Eq for StepSchedule<T, V>

Source§

impl<T, V> StructuralPartialEq for StepSchedule<T, V>

Auto Trait Implementations§

§

impl<T, V> Freeze for StepSchedule<T, V>

§

impl<T, V> RefUnwindSafe for StepSchedule<T, V>

§

impl<T, V> Send for StepSchedule<T, V>
where V: Send, T: Send,

§

impl<T, V> Sync for StepSchedule<T, V>
where V: Sync, T: Sync,

§

impl<T, V> Unpin for StepSchedule<T, V>
where V: Unpin, T: Unpin,

§

impl<T, V> UnwindSafe for StepSchedule<T, V>
where V: UnwindSafe, T: UnwindSafe,

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.