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>
impl<T: Debug + Clone + Ord, V> StepSchedule<T, V>
Sourcepub fn new<I>(steps: I) -> Result<Self, OverlappingStepsError<T>>where
I: IntoIterator<Item = Step<T, V>>,
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.
Sourcepub fn try_push(
&mut self,
step: Step<T, V>,
) -> Result<(), OverlappingStepsError<T>>
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.
Sourcepub fn value_at(&self, time: &T) -> Option<&V>
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>
impl<T: Clone, V: Clone> Clone for StepSchedule<T, V>
Source§fn clone(&self) -> StepSchedule<T, V>
fn clone(&self) -> StepSchedule<T, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Default, V: Default> Default for StepSchedule<T, V>
impl<T: Default, V: Default> Default for StepSchedule<T, V>
Source§fn default() -> StepSchedule<T, V>
fn default() -> StepSchedule<T, V>
impl<T: Eq, V: Eq> Eq for StepSchedule<T, V>
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>where
V: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, V> Send for StepSchedule<T, V>
impl<T, V> Sync for StepSchedule<T, V>
impl<T, V> Unpin for StepSchedule<T, V>
impl<T, V> UnwindSafe for StepSchedule<T, V>where
V: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.