Skip to main content

mln_style/expression/
stops.rs

1// The stops of an `interpolate` or a `step`, plus the stops that cover a
2// zoom range.
3//
4// Keys are ordered with `<`: equivalent keys (-0 and +0) are one key.
5
6use std::cmp::Ordering;
7
8use crate::expression::Expr;
9use crate::range::Range;
10
11/// The stops of a curve: outputs by input key, in key order. The keys are
12/// kept apart from the outputs, so that finding a stop reads only keys.
13#[derive(Clone, Default, PartialEq, mln_wire::Wire)]
14pub struct Stops {
15    keys: Vec<f64>,
16    outputs: Vec<Expr>,
17}
18
19impl Stops {
20    #[must_use]
21    pub fn new() -> Self {
22        Stops::default()
23    }
24
25    /// The first index whose key is not less than `key`.
26    #[must_use]
27    pub(crate) fn lower_bound(&self, key: f64) -> usize {
28        self.keys.partition_point(|k| *k < key)
29    }
30
31    /// The first index whose key is greater than `key`.
32    #[must_use]
33    pub(crate) fn upper_bound(&self, key: f64) -> usize {
34        self.keys.partition_point(|k| key.partial_cmp(k) != Some(Ordering::Less))
35    }
36
37    /// Inserts `value` at `key` unless `key` is already present; returns
38    /// whether it inserted.
39    pub fn emplace(&mut self, key: f64, value: Expr) -> bool {
40        let i = self.lower_bound(key);
41        if i < self.keys.len() && key.partial_cmp(&self.keys[i]) != Some(Ordering::Less) {
42            return false;
43        }
44        self.keys.insert(i, key);
45        self.outputs.insert(i, value);
46        true
47    }
48
49    /// Inserts `value` at `key`, or replaces the output there.
50    #[cfg(any(test, feature = "dsl"))]
51    pub(crate) fn insert_or_assign(&mut self, key: f64, value: Expr) {
52        let i = self.lower_bound(key);
53        if i < self.keys.len() && key.partial_cmp(&self.keys[i]) != Some(Ordering::Less) {
54            self.outputs[i] = value;
55        } else {
56            self.keys.insert(i, key);
57            self.outputs.insert(i, value);
58        }
59    }
60
61    #[must_use]
62    pub(crate) fn len(&self) -> usize {
63        self.keys.len()
64    }
65
66    #[must_use]
67    pub(crate) fn is_empty(&self) -> bool {
68        self.keys.is_empty()
69    }
70
71    #[must_use]
72    pub(crate) fn key(&self, i: usize) -> f64 {
73        self.keys[i]
74    }
75
76    #[must_use]
77    pub(crate) fn output(&self, i: usize) -> &Expr {
78        &self.outputs[i]
79    }
80
81    pub(crate) fn iter(&self) -> impl Iterator<Item = (f64, &Expr)> {
82        self.keys.iter().copied().zip(&self.outputs)
83    }
84
85    pub(crate) fn values(&self) -> std::slice::Iter<'_, Expr> {
86        self.outputs.iter()
87    }
88}
89
90/// The smallest range of stops that covers the interval from `lower` to
91/// `upper`.
92#[must_use]
93pub(crate) fn covering_stops(stops: &Stops, lower: f64, upper: f64) -> Range<f64> {
94    debug_assert!(!stops.is_empty());
95    if stops.is_empty() {
96        return Range::new(f64::NAN, f64::NAN);
97    }
98
99    let mut min_it = stops.lower_bound(lower);
100    let max_it = stops.lower_bound(upper);
101    let end = stops.len();
102
103    // lower_bound yields the first stop >= `lower`, but we want the *last*
104    // stop <= `lower`, so if we found a stop > `lower`, back up by one.
105    if min_it != 0 && min_it != end && stops.key(min_it) > lower {
106        min_it -= 1;
107    }
108    let last = stops.key(end - 1);
109    Range::new(
110        if min_it == end { last } else { stops.key(min_it) },
111        if max_it == end { last } else { stops.key(max_it) },
112    )
113}