Skip to main content

lichtung_plot/spec/
category_labels.rs

1use crate::{Error, Result};
2
3/// Full scientific-domain endpoints forced into a categorical label selection.
4#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
5pub enum LabelEndpoints {
6    /// No forced endpoints.
7    #[default]
8    None,
9    /// The first category.
10    First,
11    /// The last category.
12    Last,
13    /// Both endpoints, without duplicate occurrences.
14    Both,
15}
16
17/// Label eligibility only: categories, ticks, grid lines and marks remain complete.
18/// ```
19/// use lichtung_plot::{CategoryLabelSelection, LabelEndpoints};
20/// let months = CategoryLabelSelection::every(2)?;
21/// let shifted = months.offset(1)?.endpoints(LabelEndpoints::Both);
22/// # let _ = shifted;
23/// # Ok::<(), lichtung_plot::Error>(())
24/// ```
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct CategoryLabelSelection {
27    stride: usize,
28    offset: usize,
29    endpoints: LabelEndpoints,
30}
31
32impl Default for CategoryLabelSelection {
33    fn default() -> Self {
34        Self::all()
35    }
36}
37
38impl CategoryLabelSelection {
39    /// Make every category label eligible (the default).
40    pub const fn all() -> Self {
41        Self {
42            stride: 1,
43            offset: 0,
44            endpoints: LabelEndpoints::None,
45        }
46    }
47
48    /// Select every positive-stride label, initially at offset zero without endpoints.
49    pub fn every(stride: usize) -> Result<Self> {
50        if stride == 0 {
51            return Err(Error::Data("category label stride must be positive".into()));
52        }
53        Ok(Self {
54            stride,
55            ..Self::all()
56        })
57    }
58
59    /// Select the zero-based offset, strictly less than the configured stride.
60    pub fn offset(mut self, offset: usize) -> Result<Self> {
61        if offset >= self.stride {
62            return Err(Error::Data(
63                "category label offset must be smaller than its stride".into(),
64            ));
65        }
66        self.offset = offset;
67        Ok(self)
68    }
69
70    /// Union full-domain endpoints into the selected indices. All stays normalized.
71    #[must_use]
72    pub const fn endpoints(mut self, value: LabelEndpoints) -> Self {
73        if self.stride != 1 {
74            self.endpoints = value;
75        }
76        self
77    }
78
79    pub(crate) const fn is_all(&self) -> bool {
80        self.stride == 1
81    }
82
83    pub(crate) fn includes(&self, index: usize, count: usize) -> bool {
84        index < count
85            && (index % self.stride == self.offset
86                || (index == 0
87                    && matches!(self.endpoints, LabelEndpoints::First | LabelEndpoints::Both))
88                || (index == count - 1
89                    && matches!(self.endpoints, LabelEndpoints::Last | LabelEndpoints::Both)))
90    }
91
92    pub(crate) fn indices(
93        &self,
94        count: usize,
95    ) -> impl ExactSizeIterator<Item = usize> + Clone + '_ {
96        let base = if self.offset < count {
97            (count - 1 - self.offset) / self.stride + 1
98        } else {
99            0
100        };
101        let first = count > 0
102            && self.offset != 0
103            && matches!(self.endpoints, LabelEndpoints::First | LabelEndpoints::Both);
104        let last = count > 0
105            && (count - 1) % self.stride != self.offset
106            && matches!(self.endpoints, LabelEndpoints::Last | LabelEndpoints::Both)
107            && (count != 1 || !first);
108        let leading = usize::from(first);
109        (0..(base + leading + usize::from(last))).map(move |ordinal| {
110            if first && ordinal == 0 {
111                0
112            } else if ordinal - leading < base {
113                self.offset + (ordinal - leading) * self.stride
114            } else {
115                count - 1
116            }
117        })
118    }
119
120    pub(crate) fn observe(&self, round: &mut crate::layout::kernel::FeedbackRound) -> Result<()> {
121        round.observe_count(self.stride)?;
122        round.observe_count(self.offset)?;
123        round.observe_count(self.endpoints as usize)
124    }
125}