Skip to main content

mln_expression/
stops.rs

1// The `std::map<double, std::unique_ptr<Expression>>` that Interpolate and
2// Step hold, plus src/mln/style/expression/get_covering_stops.cpp.
3//
4// Keys are ordered with `<` like std::map<double>: equivalent keys (e.g. -0
5// and +0) are one key.
6
7use crate::expression::Expression;
8use crate::range::Range;
9
10#[derive(Default)]
11pub struct Stops {
12    stops: Vec<(f64, Box<dyn Expression>)>,
13}
14
15impl Stops {
16    pub fn new() -> Self {
17        Stops { stops: Vec::new() }
18    }
19
20    /// `std::map::lower_bound`: the first index whose key is not less than
21    /// `key`.
22    pub fn lower_bound(&self, key: f64) -> usize {
23        self.stops.partition_point(|(k, _)| *k < key)
24    }
25
26    /// `std::map::upper_bound`: the first index whose key is greater than
27    /// `key`.
28    pub fn upper_bound(&self, key: f64) -> usize {
29        self.stops.partition_point(|(k, _)| !(key < *k))
30    }
31
32    /// `std::map::emplace`: inserts unless an equivalent key exists.
33    pub fn emplace(&mut self, key: f64, value: Box<dyn Expression>) -> bool {
34        let i = self.lower_bound(key);
35        if i < self.stops.len() && !(key < self.stops[i].0) {
36            return false;
37        }
38        self.stops.insert(i, (key, value));
39        true
40    }
41
42    /// `stops[key] = value`: inserts or replaces.
43    pub fn insert_or_assign(&mut self, key: f64, value: Box<dyn Expression>) {
44        let i = self.lower_bound(key);
45        if i < self.stops.len() && !(key < self.stops[i].0) {
46            self.stops[i].1 = value;
47        } else {
48            self.stops.insert(i, (key, value));
49        }
50    }
51
52    pub fn len(&self) -> usize {
53        self.stops.len()
54    }
55
56    pub fn is_empty(&self) -> bool {
57        self.stops.is_empty()
58    }
59
60    pub fn get(&self, i: usize) -> (f64, &dyn Expression) {
61        let (k, v) = &self.stops[i];
62        (*k, &**v)
63    }
64
65    pub fn iter(&self) -> impl Iterator<Item = (f64, &dyn Expression)> {
66        self.stops.iter().map(|(k, v)| (*k, &**v))
67    }
68
69    pub fn values(&self) -> impl Iterator<Item = &Box<dyn Expression>> {
70        self.stops.iter().map(|(_, v)| v)
71    }
72
73    /// `Expression::childrenEqual` over two stop maps.
74    pub fn children_equal(&self, rhs: &Stops) -> bool {
75        if self.stops.len() != rhs.stops.len() {
76            return false;
77        }
78        self.stops
79            .iter()
80            .zip(&rhs.stops)
81            .all(|(l, r)| l.0 == r.0 && l.1.equals(&*r.1))
82    }
83}
84
85/// `getCoveringStops`: the smallest range of stops that covers the interval
86/// [lower, upper].
87pub fn get_covering_stops(stops: &Stops, lower: f64, upper: f64) -> Range<f32> {
88    debug_assert!(!stops.is_empty());
89    if stops.is_empty() {
90        return Range::new(f32::NAN, f32::NAN);
91    }
92
93    let mut min_it = stops.lower_bound(lower);
94    let max_it = stops.lower_bound(upper);
95    let end = stops.len();
96
97    // lower_bound yields first element >= lowerZoom, but we want the *last*
98    // element <= lowerZoom, so if we found a stop > lowerZoom, back up by one.
99    if min_it != 0 && min_it != end && stops.get(min_it).0 > lower {
100        min_it -= 1;
101    }
102    let last = stops.get(end - 1).0;
103    Range::new(
104        (if min_it == end { last } else { stops.get(min_it).0 }) as f32,
105        (if max_it == end { last } else { stops.get(max_it).0 }) as f32,
106    )
107}