pub struct ParameterRange { /* private fields */ }Expand description
A continuous or discrete range for a single tunable parameter.
When step is Some, the parameter is treated as discrete: values are
quantized to the nearest grid point anchored at min. When step is None
the parameter is treated as continuous and generators fall back to an internal
default step count (typically 20 divisions).
Invariants enforced by ParameterRange::new:
min < max(both must be finite)min <= default <= max(defaultmust be finite)step, whenSome, must be finite and positive
§Examples
use zeph_experiments::{ParameterRange, ParameterKind};
let range = ParameterRange::new(ParameterKind::Temperature, 0.0, 1.0, Some(0.1), 0.7).unwrap();
assert_eq!(range.step_count(), Some(11));
assert!((range.clamp(2.0) - 1.0).abs() < f64::EPSILON);
assert!((range.quantize(0.73) - 0.7).abs() < 1e-10);Implementations§
Source§impl ParameterRange
impl ParameterRange
Sourcepub fn new(
kind: ParameterKind,
min: f64,
max: f64,
step: Option<f64>,
default: f64,
) -> Result<Self, EvalError>
pub fn new( kind: ParameterKind, min: f64, max: f64, step: Option<f64>, default: f64, ) -> Result<Self, EvalError>
Construct a validated ParameterRange.
§Errors
Returns EvalError::InvalidRange if min >= max or either bound is non-finite.
Returns EvalError::DefaultOutOfRange if default is outside [min, max].
step is not validated by this constructor; non-positive or non-finite values
are treated as None by step_count and quantize.
§Examples
use zeph_experiments::{ParameterRange, ParameterKind, EvalError};
let r = ParameterRange::new(ParameterKind::Temperature, 0.0, 1.0, Some(0.1), 0.7).unwrap();
assert!((r.min() - 0.0).abs() < f64::EPSILON);
assert!((r.max() - 1.0).abs() < f64::EPSILON);
assert!((r.default_value() - 0.7).abs() < f64::EPSILON);
assert!(matches!(
ParameterRange::new(ParameterKind::Temperature, 1.0, 0.0, None, 0.5),
Err(EvalError::InvalidRange { .. })
));
assert!(matches!(
ParameterRange::new(ParameterKind::Temperature, 0.0, 1.0, None, 2.0),
Err(EvalError::DefaultOutOfRange { .. })
));Sourcepub fn kind(&self) -> ParameterKind
pub fn kind(&self) -> ParameterKind
Return the ParameterKind this range applies to.
Sourcepub fn step(&self) -> Option<f64>
pub fn step(&self) -> Option<f64>
Return the discrete step size, or None for a continuous range.
Sourcepub fn default_value(&self) -> f64
pub fn default_value(&self) -> f64
Return the default (baseline) value.
Named default_value to avoid shadowing the Default trait keyword.
Sourcepub fn step_count(&self) -> Option<usize>
pub fn step_count(&self) -> Option<usize>
Number of discrete grid points in this range, or None if step is not set or ≤ 0.
The count is floor((max - min) / step) + 1.
§Examples
use zeph_experiments::{ParameterRange, ParameterKind};
let r = ParameterRange::new(ParameterKind::Temperature, 0.0, 1.0, Some(0.5), 0.5).unwrap();
assert_eq!(r.step_count(), Some(3)); // 0.0, 0.5, 1.0
let r_continuous = ParameterRange::new(ParameterKind::Temperature, 0.0, 1.0, None, 0.5).unwrap();
assert_eq!(r_continuous.step_count(), None);Sourcepub fn clamp(&self, value: f64) -> f64
pub fn clamp(&self, value: f64) -> f64
Clamp value to [min, max].
§Examples
use zeph_experiments::{ParameterRange, ParameterKind};
let r = ParameterRange::new(ParameterKind::TopP, 0.1, 1.0, Some(0.1), 0.9).unwrap();
assert!((r.clamp(2.0) - 1.0).abs() < f64::EPSILON);
assert!((r.clamp(-1.0) - 0.1).abs() < f64::EPSILON);Sourcepub fn contains(&self, value: f64) -> bool
pub fn contains(&self, value: f64) -> bool
Return true if value lies within [min, max] (inclusive).
§Examples
use zeph_experiments::{ParameterRange, ParameterKind};
let r = ParameterRange::new(ParameterKind::Temperature, 0.0, 1.0, Some(0.1), 0.7).unwrap();
assert!(r.contains(0.5));
assert!(!r.contains(1.1));Trait Implementations§
Source§impl Clone for ParameterRange
impl Clone for ParameterRange
Source§fn clone(&self) -> ParameterRange
fn clone(&self) -> ParameterRange
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ParameterRange
impl Debug for ParameterRange
Source§impl<'de> Deserialize<'de> for ParameterRange
impl<'de> Deserialize<'de> for ParameterRange
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for ParameterRange
impl RefUnwindSafe for ParameterRange
impl Send for ParameterRange
impl Sync for ParameterRange
impl Unpin for ParameterRange
impl UnsafeUnpin for ParameterRange
impl UnwindSafe for ParameterRange
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<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request