use std::sync::Arc;
use crate::numeric::context::{DomainInfo, OwnerInfo};
use crate::{
coordinates::AxisRef,
formatting::{NumericDomain, NumericOwner},
Result,
};
#[derive(Debug, Clone)]
pub struct TickContext<'a> {
pub(crate) axis: AxisRef,
pub(crate) domain: &'a DomainInfo,
pub(crate) owner: &'a OwnerInfo,
pub(crate) span: f64,
}
impl<'a> TickContext<'a> {
pub const fn axis(&self) -> AxisRef {
self.axis
}
pub fn domain(&self) -> NumericDomain<'a> {
NumericDomain(self.domain)
}
pub fn owner(&self) -> NumericOwner<'a> {
NumericOwner(self.owner)
}
pub const fn span(&self) -> f64 {
self.span
}
}
#[derive(Debug, Clone)]
pub enum TickPositions {
Data(Vec<f64>),
Log10Exponents(Vec<f64>),
}
impl From<Vec<f64>> for TickPositions {
fn from(values: Vec<f64>) -> Self {
Self::Data(values)
}
}
type Callback = dyn for<'a> Fn(&TickContext<'a>) -> Result<TickPositions> + Send + Sync;
#[derive(Clone)]
pub(crate) struct Function(Arc<Callback>);
impl Function {
pub(super) fn new<F>(function: F) -> Self
where
F: for<'a> Fn(&TickContext<'a>) -> Result<TickPositions> + Send + Sync + 'static,
{
Self(Arc::new(function))
}
fn call(&self, context: &TickContext<'_>) -> Result<TickPositions> {
crate::operation::checkpoint()?;
let result = crate::operation::external(|| self.0(context))?;
crate::operation::checkpoint()?;
Ok(result)
}
}
impl std::fmt::Debug for Function {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("PositionFunction")
}
}
impl PartialEq for Function {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for Function {}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct View {
pub(crate) owner: OwnerInfo,
pub(crate) domain: DomainInfo,
pub(crate) axis: AxisRef,
pub(crate) span: u64,
}
#[derive(Debug)]
struct Entry {
policy: crate::coordinates::TickGeneration,
view: View,
result: Arc<TickPositions>,
}
#[derive(Debug, Default)]
pub(crate) struct PositionTable {
entries: Vec<Entry>,
sealed: bool,
}
impl PositionTable {
pub(crate) fn seal(&mut self) {
self.sealed = true;
self.entries.clear();
}
pub(crate) fn materialize(
table: &std::sync::Mutex<Self>,
policy: &crate::coordinates::TickGeneration,
view: View,
) -> Result<Arc<TickPositions>> {
let synchronized = table
.lock()
.map_err(|_| crate::Error::Render("position table synchronization failed".into()))?;
if synchronized.sealed {
return Err(crate::Error::Render(
"tick positions requested after final root sealing".into(),
));
}
if let Some(entry) = synchronized
.entries
.iter()
.find(|entry| entry.policy == *policy && entry.view == view)
{
return Ok(entry.result.clone());
}
drop(synchronized);
let function = policy
.position_function()
.ok_or_else(|| crate::Error::Render("position source lacks its function".into()))?;
let result = function
.call(&TickContext {
axis: view.axis,
domain: &view.domain,
owner: &view.owner,
span: f64::from_bits(view.span),
})
.map_err(|error| contextual(error, &view))?;
let result = crate::data::source::source_arc(result)?;
let mut synchronized = table
.lock()
.map_err(|_| crate::Error::Render("position table synchronization failed".into()))?;
crate::data::source::reserve_source(&mut synchronized.entries, 1)?;
synchronized.entries.push(Entry {
policy: policy.clone(),
view,
result: result.clone(),
});
Ok(result)
}
}
fn contextual(error: crate::Error, view: &View) -> crate::Error {
let context = format!("{:?} position function for {:?}", view.axis, view.owner);
error.with_prefix(&context)
}