lichtung_plot/spec/ticks/
function.rs1use std::sync::Arc;
4
5use crate::numeric::context::{DomainInfo, OwnerInfo};
6use crate::{
7 coordinates::CoordinateAxis,
8 formatting::{NumericDomain, NumericOwner},
9 Result,
10};
11
12#[derive(Debug, Clone)]
14pub struct TickContext<'a> {
15 pub(crate) axis: CoordinateAxis,
16 pub(crate) domain: &'a DomainInfo,
17 pub(crate) owner: &'a OwnerInfo,
18 pub(crate) span: f64,
19}
20impl<'a> TickContext<'a> {
21 pub const fn axis(&self) -> CoordinateAxis {
23 self.axis
24 }
25 pub fn domain(&self) -> NumericDomain<'a> {
27 NumericDomain(self.domain)
28 }
29 pub fn owner(&self) -> NumericOwner<'a> {
31 NumericOwner(self.owner)
32 }
33 pub const fn span(&self) -> f64 {
35 self.span
36 }
37}
38
39#[derive(Debug, Clone)]
41pub enum TickPositions {
42 Data(Vec<f64>),
44 Log10Exponents(Vec<f64>),
46}
47impl From<Vec<f64>> for TickPositions {
48 fn from(values: Vec<f64>) -> Self {
49 Self::Data(values)
50 }
51}
52
53type Callback = dyn for<'a> Fn(&TickContext<'a>) -> Result<TickPositions> + Send + Sync;
54
55#[derive(Clone)]
56pub(crate) struct Function(Arc<Callback>);
57impl Function {
58 pub(super) fn new<F>(function: F) -> Self
59 where
60 F: for<'a> Fn(&TickContext<'a>) -> Result<TickPositions> + Send + Sync + 'static,
61 {
62 Self(Arc::new(function))
63 }
64 fn call(&self, context: &TickContext<'_>) -> Result<TickPositions> {
65 crate::operation::checkpoint()?;
66 crate::operation::work(1)?;
67 let result = crate::operation::external(|| self.0(context))?;
68 crate::operation::checkpoint()?;
69 Ok(result)
70 }
71}
72impl std::fmt::Debug for Function {
73 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74 formatter.write_str("PositionFunction")
75 }
76}
77impl PartialEq for Function {
78 fn eq(&self, other: &Self) -> bool {
79 Arc::ptr_eq(&self.0, &other.0)
80 }
81}
82impl Eq for Function {}
83
84#[derive(Debug, PartialEq, Eq)]
85pub(crate) struct View {
86 pub(crate) owner: OwnerInfo,
87 pub(crate) domain: DomainInfo,
88 pub(crate) axis: CoordinateAxis,
89 pub(crate) span: u64,
90}
91
92#[derive(Debug)]
93struct Entry {
94 policy: crate::coordinates::TickGeneration,
95 view: View,
96 result: Arc<TickPositions>,
97}
98
99#[derive(Debug, Default)]
100pub(crate) struct PositionTable {
101 entries: Vec<Entry>,
102 sealed: bool,
103}
104impl PositionTable {
105 pub(crate) fn seal(&mut self) {
106 self.sealed = true;
107 self.entries.clear();
108 }
109
110 pub(crate) fn materialize(
111 table: &std::sync::Mutex<Self>,
112 policy: &crate::coordinates::TickGeneration,
113 view: View,
114 ) -> Result<Arc<TickPositions>> {
115 let synchronized = table
116 .lock()
117 .map_err(|_| crate::Error::Render("position table synchronization failed".into()))?;
118 if synchronized.sealed {
119 return Err(crate::Error::Render(
120 "tick positions requested after final root sealing".into(),
121 ));
122 }
123 if let Some(entry) = synchronized
124 .entries
125 .iter()
126 .find(|entry| entry.policy == *policy && entry.view == view)
127 {
128 return Ok(entry.result.clone());
129 }
130 drop(synchronized);
131 let function = policy
132 .position_function()
133 .ok_or_else(|| crate::Error::Render("position source lacks its function".into()))?;
134 let result = function
135 .call(&TickContext {
136 axis: view.axis,
137 domain: &view.domain,
138 owner: &view.owner,
139 span: f64::from_bits(view.span),
140 })
141 .map_err(|error| contextual(error, &view))?;
142 let result = crate::data::source::source_arc(result)?;
143 let mut synchronized = table
144 .lock()
145 .map_err(|_| crate::Error::Render("position table synchronization failed".into()))?;
146 crate::data::source::reserve_source(&mut synchronized.entries, 1)?;
147 synchronized.entries.push(Entry {
148 policy: policy.clone(),
149 view,
150 result: result.clone(),
151 });
152 Ok(result)
153 }
154}
155
156fn contextual(error: crate::Error, view: &View) -> crate::Error {
157 let context = format!("{:?} position function for {:?}", view.axis, view.owner);
158 error.with_prefix(&context)
159}