use super::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SamplingCoordinate(pub(super) Vec<usize>);
impl SamplingCoordinate {
pub fn new(values: Vec<usize>) -> Self {
Self(values)
}
pub fn as_slice(&self) -> &[usize] {
&self.0
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SamplingSchedule {
pub(super) grid: Vec<usize>,
pub(super) coordinates: Vec<SamplingCoordinate>,
pub(super) repeated_coordinates: bool,
pub(super) declaration: Option<std::sync::Arc<crate::SamplingDeclaration>>,
}
impl SamplingSchedule {
pub fn new(
grid: Vec<usize>,
coordinates: Vec<SamplingCoordinate>,
) -> Result<Self, ValidationError> {
if grid.contains(&0) {
return Err(ValidationError::ZeroAxis);
}
if coordinates.is_empty() {
return Err(ValidationError::EmptySchedule);
}
for coordinate in &coordinates {
if coordinate.0.len() != grid.len() {
return Err(ValidationError::SamplingRankMismatch);
}
if coordinate
.0
.iter()
.enumerate()
.any(|(axis, &value)| value >= grid[axis])
{
return Err(ValidationError::SamplingOutOfBounds);
}
}
let requested_bytes = coordinates
.len()
.checked_mul(std::mem::size_of::<&SamplingCoordinate>())
.filter(|&bytes| bytes <= isize::MAX as usize)
.ok_or(ValidationError::SizeOverflow)?;
let mut sorted = Vec::new();
sorted
.try_reserve_exact(coordinates.len())
.map_err(|_| ValidationError::Allocation { requested_bytes })?;
sorted.extend(coordinates.iter());
sorted.sort_unstable_by(|left, right| left.0.cmp(&right.0));
let repeated_coordinates = sorted.windows(2).any(|pair| pair[0] == pair[1]);
drop(sorted);
Ok(Self {
grid,
coordinates,
repeated_coordinates,
declaration: None,
})
}
pub fn grid(&self) -> &[usize] {
&self.grid
}
pub fn coordinates(&self) -> &[SamplingCoordinate] {
&self.coordinates
}
pub fn has_repeated_coordinates(&self) -> bool {
self.repeated_coordinates
}
pub fn declaration(&self) -> Option<&crate::SamplingDeclaration> {
self.declaration.as_deref()
}
pub(crate) fn set_declaration(&mut self, value: std::sync::Arc<crate::SamplingDeclaration>) {
self.declaration = Some(value);
}
pub(crate) fn is_complete_unique(&self) -> Result<bool, ValidationError> {
if self.has_repeated_coordinates() {
return Ok(false);
}
Ok(self.coordinates.len()
== checked_product(&self.grid).ok_or(ValidationError::SizeOverflow)?)
}
}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct ObservationOrdinal(usize);
impl ObservationOrdinal {
pub const fn new(value: usize) -> Self {
Self(value)
}
pub const fn get(self) -> usize {
self.0
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct SparseTrace {
pub(super) ordinal: ObservationOrdinal,
pub(super) coordinate: SamplingCoordinate,
pub(super) samples: Vec<Complex64>,
}
impl SparseTrace {
pub fn new(
ordinal: ObservationOrdinal,
coordinate: SamplingCoordinate,
samples: Vec<Complex64>,
) -> Self {
Self {
ordinal,
coordinate,
samples,
}
}
pub fn ordinal(&self) -> ObservationOrdinal {
self.ordinal
}
pub fn coordinate(&self) -> &SamplingCoordinate {
&self.coordinate
}
pub fn samples(&self) -> &[Complex64] {
&self.samples
}
}
impl ObservationOrdinal {
#[allow(clippy::type_complexity)]
pub(crate) fn model_parts(&self) -> (&usize,) {
(&self.0,)
}
#[allow(clippy::type_complexity)]
pub(crate) fn from_model_parts(parts: (usize,)) -> Result<Self, crate::internal::ModelError> {
let (f0,) = parts;
let value = Self(f0);
Ok(value)
}
}
impl SamplingCoordinate {
#[allow(clippy::type_complexity)]
pub(crate) fn model_parts(&self) -> (&Vec<usize>,) {
(&self.0,)
}
#[allow(clippy::type_complexity)]
pub(crate) fn from_model_parts(
parts: (Vec<usize>,),
) -> Result<Self, crate::internal::ModelError> {
let (f0,) = parts;
let value = Self(f0);
Ok(value)
}
}
impl SamplingSchedule {
pub(crate) fn model_parts(
&self,
) -> (
&Vec<usize>,
&Vec<SamplingCoordinate>,
&bool,
&Option<std::sync::Arc<crate::SamplingDeclaration>>,
) {
(
&self.grid,
&self.coordinates,
&self.repeated_coordinates,
&self.declaration,
)
}
}
impl SparseTrace {
#[allow(clippy::type_complexity)]
pub(crate) fn model_parts(
&self,
) -> (&ObservationOrdinal, &SamplingCoordinate, &Vec<Complex64>) {
(&self.ordinal, &self.coordinate, &self.samples)
}
#[allow(clippy::type_complexity)]
pub(crate) fn from_model_parts(
parts: (ObservationOrdinal, SamplingCoordinate, Vec<Complex64>),
) -> Result<Self, crate::internal::ModelError> {
let (ordinal, coordinate, samples) = parts;
let value = Self {
ordinal,
coordinate,
samples,
};
Ok(value)
}
}