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