mln-rs-expression 0.0.7

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
// The `std::map<double, std::unique_ptr<Expression>>` that Interpolate and
// Step hold, plus src/mln/style/expression/get_covering_stops.cpp.
//
// Keys are ordered with `<` like std::map<double>: equivalent keys (e.g. -0
// and +0) are one key.

use crate::expression::Expression;
use crate::range::Range;

#[derive(Default)]
pub struct Stops {
    stops: Vec<(f64, Box<dyn Expression>)>,
}

impl Stops {
    pub fn new() -> Self {
        Stops { stops: Vec::new() }
    }

    /// `std::map::lower_bound`: the first index whose key is not less than
    /// `key`.
    pub fn lower_bound(&self, key: f64) -> usize {
        self.stops.partition_point(|(k, _)| *k < key)
    }

    /// `std::map::upper_bound`: the first index whose key is greater than
    /// `key`.
    pub fn upper_bound(&self, key: f64) -> usize {
        self.stops.partition_point(|(k, _)| !(key < *k))
    }

    /// `std::map::emplace`: inserts unless an equivalent key exists.
    pub fn emplace(&mut self, key: f64, value: Box<dyn Expression>) -> bool {
        let i = self.lower_bound(key);
        if i < self.stops.len() && !(key < self.stops[i].0) {
            return false;
        }
        self.stops.insert(i, (key, value));
        true
    }

    /// `stops[key] = value`: inserts or replaces.
    pub fn insert_or_assign(&mut self, key: f64, value: Box<dyn Expression>) {
        let i = self.lower_bound(key);
        if i < self.stops.len() && !(key < self.stops[i].0) {
            self.stops[i].1 = value;
        } else {
            self.stops.insert(i, (key, value));
        }
    }

    pub fn len(&self) -> usize {
        self.stops.len()
    }

    pub fn is_empty(&self) -> bool {
        self.stops.is_empty()
    }

    pub fn get(&self, i: usize) -> (f64, &dyn Expression) {
        let (k, v) = &self.stops[i];
        (*k, &**v)
    }

    pub fn iter(&self) -> impl Iterator<Item = (f64, &dyn Expression)> {
        self.stops.iter().map(|(k, v)| (*k, &**v))
    }

    pub fn values(&self) -> impl Iterator<Item = &Box<dyn Expression>> {
        self.stops.iter().map(|(_, v)| v)
    }

    /// `Expression::childrenEqual` over two stop maps.
    pub fn children_equal(&self, rhs: &Stops) -> bool {
        if self.stops.len() != rhs.stops.len() {
            return false;
        }
        self.stops
            .iter()
            .zip(&rhs.stops)
            .all(|(l, r)| l.0 == r.0 && l.1.equals(&*r.1))
    }
}

/// `getCoveringStops`: the smallest range of stops that covers the interval
/// [lower, upper].
pub fn get_covering_stops(stops: &Stops, lower: f64, upper: f64) -> Range<f32> {
    debug_assert!(!stops.is_empty());
    if stops.is_empty() {
        return Range::new(f32::NAN, f32::NAN);
    }

    let mut min_it = stops.lower_bound(lower);
    let max_it = stops.lower_bound(upper);
    let end = stops.len();

    // lower_bound yields first element >= lowerZoom, but we want the *last*
    // element <= lowerZoom, so if we found a stop > lowerZoom, back up by one.
    if min_it != 0 && min_it != end && stops.get(min_it).0 > lower {
        min_it -= 1;
    }
    let last = stops.get(end - 1).0;
    Range::new(
        (if min_it == end { last } else { stops.get(min_it).0 }) as f32,
        (if max_it == end { last } else { stops.get(max_it).0 }) as f32,
    )
}