Skip to main content

mln_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    pub(crate) fn insert_or_assign(&mut self, key: f64, value: Expr) {
51        let i = self.lower_bound(key);
52        if i < self.keys.len() && key.partial_cmp(&self.keys[i]) != Some(Ordering::Less) {
53            self.outputs[i] = value;
54        } else {
55            self.keys.insert(i, key);
56            self.outputs.insert(i, value);
57        }
58    }
59
60    #[must_use]
61    pub(crate) fn len(&self) -> usize {
62        self.keys.len()
63    }
64
65    #[must_use]
66    pub(crate) fn is_empty(&self) -> bool {
67        self.keys.is_empty()
68    }
69
70    #[must_use]
71    pub(crate) fn key(&self, i: usize) -> f64 {
72        self.keys[i]
73    }
74
75    #[must_use]
76    pub(crate) fn output(&self, i: usize) -> &Expr {
77        &self.outputs[i]
78    }
79
80    pub(crate) fn iter(&self) -> impl Iterator<Item = (f64, &Expr)> {
81        self.keys.iter().copied().zip(&self.outputs)
82    }
83
84    pub(crate) fn values(&self) -> std::slice::Iter<'_, Expr> {
85        self.outputs.iter()
86    }
87}
88
89/// The smallest range of stops that covers the interval from `lower` to
90/// `upper`.
91#[must_use]
92pub(crate) fn get_covering_stops(stops: &Stops, lower: f64, upper: f64) -> Range<f32> {
93    debug_assert!(!stops.is_empty());
94    if stops.is_empty() {
95        return Range::new(f32::NAN, f32::NAN);
96    }
97
98    let mut min_it = stops.lower_bound(lower);
99    let max_it = stops.lower_bound(upper);
100    let end = stops.len();
101
102    // lower_bound yields first element >= lowerZoom, but we want the *last*
103    // element <= lowerZoom, so if we found a stop > lowerZoom, back up by one.
104    if min_it != 0 && min_it != end && stops.key(min_it) > lower {
105        min_it -= 1;
106    }
107    let last = stops.key(end - 1);
108    Range::new(
109        (if min_it == end { last } else { stops.key(min_it) }) as f32,
110        (if max_it == end { last } else { stops.key(max_it) }) as f32,
111    )
112}