diff options
Diffstat (limited to 'include/mbgl/style/function/interval_stops.hpp')
-rw-r--r-- | include/mbgl/style/function/interval_stops.hpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/include/mbgl/style/function/interval_stops.hpp b/include/mbgl/style/function/interval_stops.hpp new file mode 100644 index 0000000000..cf879d730b --- /dev/null +++ b/include/mbgl/style/function/interval_stops.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include <mbgl/util/feature.hpp> + +#include <map> + +namespace mbgl { +namespace style { + +template <class T> +class IntervalStops { +public: + using Stops = std::map<float, T>; + Stops stops; + + IntervalStops() = default; + IntervalStops(Stops stops_) + : stops(std::move(stops_)) { + } + + T evaluate(const Value& value) const { + if (stops.empty()) { + assert(false); + return T(); + } + + optional<float> z = numericValue<float>(value); + if (!z) { + return T(); + } + + auto it = stops.upper_bound(*z); + if (it == stops.end()) { + return stops.rbegin()->second; + } else if (it == stops.begin()) { + return stops.begin()->second; + } else { + return std::prev(it)->second; + } + } + + friend bool operator==(const IntervalStops& lhs, + const IntervalStops& rhs) { + return lhs.stops == rhs.stops; + } +}; + +} // namespace style +} // namespace mbgl |