diff options
Diffstat (limited to 'include/llmr/style')
-rw-r--r-- | include/llmr/style/filter_expression.hpp | 6 | ||||
-rw-r--r-- | include/llmr/style/filter_expression_private.hpp | 87 | ||||
-rw-r--r-- | include/llmr/style/value.hpp | 130 | ||||
-rw-r--r-- | include/llmr/style/value_comparison.hpp | 100 |
4 files changed, 202 insertions, 121 deletions
diff --git a/include/llmr/style/filter_expression.hpp b/include/llmr/style/filter_expression.hpp index 5c7a400d80..61e0fae3c8 100644 --- a/include/llmr/style/filter_expression.hpp +++ b/include/llmr/style/filter_expression.hpp @@ -11,8 +11,6 @@ namespace llmr { -class VectorTileTagExtractor; - class FilterComparison { public: enum class Operator : uint8_t { @@ -50,7 +48,7 @@ public: FilterComparison(const std::string &field) : field(field) {}; const std::string &getField() const; - inline bool compare(const VectorTileTagExtractor &extractor) const; + template <typename Extractor> inline bool compare(const Extractor &extractor) const; template <typename ...Args> inline void add(Args&& ...args) { @@ -95,7 +93,7 @@ public: bool empty() const; - bool compare(const VectorTileTagExtractor &extractor) const; + template <typename Extractor> bool compare(const Extractor &extractor) const; void add(const FilterComparison &comparison); void add(const FilterExpression &expression); void setGeometryType(GeometryType g); diff --git a/include/llmr/style/filter_expression_private.hpp b/include/llmr/style/filter_expression_private.hpp new file mode 100644 index 0000000000..9a1f9fed30 --- /dev/null +++ b/include/llmr/style/filter_expression_private.hpp @@ -0,0 +1,87 @@ +#ifndef LLMR_STYLE_FILTER_EXPRESSION_PRIVATE +#define LLMR_STYLE_FILTER_EXPRESSION_PRIVATE + +#include "filter_expression.hpp" + +namespace llmr { + +template <typename Extractor> +inline bool FilterComparison::compare(const Extractor &extractor) const { + const std::forward_list<Value> values = extractor.getValues(field); + + // All instances are ANDed together. + for (const Instance &instance : instances) { + if (!instance.compare(values)) { + return false; + } + } + return true; +} + +template <typename Extractor> +bool FilterExpression::compare(const Extractor &extractor) const { + if (type != GeometryType::Any && extractor.getType() != type && extractor.getType() != GeometryType::Any) { + return false; + } + + switch (op) { + case Operator::And: + for (const FilterComparison &comparison : comparisons) { + if (!comparison.compare(extractor)) { + return false; + } + } + for (const FilterExpression &expression: expressions) { + if (!expression.compare(extractor)) { + return false; + } + } + return true; + case Operator::Or: + for (const FilterComparison &comparison : comparisons) { + if (comparison.compare(extractor)) { + return true; + } + } + for (const FilterExpression &expression: expressions) { + if (expression.compare(extractor)) { + return true; + } + } + return false; + case Operator::Xor: { + int count = 0; + for (const FilterComparison &comparison : comparisons) { + count += comparison.compare(extractor); + if (count > 1) { + return false; + } + } + for (const FilterExpression &expression: expressions) { + count += expression.compare(extractor); + if (count > 1) { + return false; + } + } + return count == 1; + } + case Operator::Nor: + for (const FilterComparison &comparison : comparisons) { + if (comparison.compare(extractor)) { + return false; + } + } + for (const FilterExpression &expression: expressions) { + if (expression.compare(extractor)) { + return false; + } + } + return true; + default: + return true; + } +} + +} + +#endif diff --git a/include/llmr/style/value.hpp b/include/llmr/style/value.hpp index fac316d3d5..ed14c233e1 100644 --- a/include/llmr/style/value.hpp +++ b/include/llmr/style/value.hpp @@ -4,6 +4,9 @@ #include <llmr/util/variant.hpp> #include <llmr/util/pbf.hpp> +#include <cstdlib> +#include <cerrno> + namespace llmr { typedef util::variant<bool, int64_t, uint64_t, double, std::string> Value; @@ -13,128 +16,21 @@ std::string toString(const Value &value); Value parseValue(pbf data); namespace util { - -namespace detail { - -inline int string_to_bool(std::string str) { - std::transform(str.begin(), str.end(), str.begin(), ::tolower); - if (str == "0" || str == "0.0" || str == "false" || str == "null" || !str.length()) return 0; - else if (str == "true") return 1; - else return -1; +inline bool parseNumericString(const std::string &str, double &result) { + char *end = nullptr; + const char *begin = str.c_str(); + result = std::strtod(begin, &end); + while (*end != '\0' && isspace(*end)) end++; // eat whitespace after the end + return errno == 0 && end - begin == long(str.size()); } - -template <typename T> -struct string_to_number {}; - -template <> struct string_to_number<bool> { - bool operator()(const std::string &str) const { - // Converts 0 => false, 1 => true and -1 => true. - return string_to_bool(str); - } -}; - -template <> struct string_to_number<double> { - double operator()(std::string const &str) const { - int val = string_to_bool(str); - return val < 0 ? std::stod(str) : val; - } -}; - -template <> struct string_to_number<int64_t> { - int64_t operator()(std::string const &str) const { - int val = string_to_bool(str); - return val < 0 ? std::stoll(str) : val; - } -}; - -template <> struct string_to_number<uint64_t> { - uint64_t operator()(std::string const &str) const { - int val = string_to_bool(str); - return val < 0 ? std::stoull(str) : val; - } -}; - -template <typename Operator> -struct relaxed_operator_visitor { - typedef bool result_type; - - bool operator()(bool lhs, bool rhs) const { return Operator()(lhs, rhs); } - - template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> - bool operator()(bool lhs, T rhs) const { return Operator()(lhs, bool(rhs)); } - - template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> - bool operator()(T lhs, bool rhs) const { return Operator()(bool(lhs), rhs); } - - bool operator()(int64_t lhs, uint64_t rhs) const { return lhs < 0 ? false : Operator()(uint64_t(lhs), rhs); } - bool operator()(uint64_t lhs, int64_t rhs) const { return rhs < 0 ? false : Operator()(lhs, uint64_t(rhs)); } - - template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> - bool operator()(const std::string &lhs, T rhs) const try { return Operator()(string_to_number<T>()(lhs), rhs); } - catch(...) { return false; } - - template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> - bool operator()(T lhs, const std::string &rhs) const try { return Operator()(lhs, string_to_number<T>()(rhs)); } - catch(...) { return false; } - - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return Operator()(lhs, rhs); } -}; - -struct relaxed_equal_operator { - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return lhs == rhs; } -}; - -struct relaxed_greater_operator { - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return lhs > rhs; } -}; - -struct relaxed_greater_equal_operator { - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return lhs >= rhs; } -}; - -struct relaxed_less_operator { - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return lhs < rhs; } -}; - -struct relaxed_less_equal_operator { - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return lhs <= rhs; } -}; - - -} // end namespace detail - -inline bool relaxed_equal(Value const &lhs, Value const &rhs) { - return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_equal_operator>(), lhs, rhs); -} - -inline bool relaxed_greater(Value const &lhs, Value const &rhs) { - return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_greater_operator>(), lhs, rhs); } -inline bool relaxed_greater_equal(Value const &lhs, Value const &rhs) { - return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_greater_equal_operator>(), lhs, rhs); -} - -inline bool relaxed_less(Value const &lhs, Value const &rhs) { - return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_less_operator>(), lhs, rhs); -} - -inline bool relaxed_less_equal(Value const &lhs, Value const &rhs) { - return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_less_equal_operator>(), lhs, rhs); -} - -} - - template <typename T> T toNumber(const Value &value) { - if (value.is<std::string>()) return util::detail::string_to_number<T>()(value.get<std::string>()); + if (value.is<std::string>()) { + double val; + return util::parseNumericString(value.get<std::string>(), val) ? val : 0; + } else if (value.is<bool>()) return value.get<bool>(); else if (value.is<int64_t>()) return value.get<int64_t>(); else if (value.is<uint64_t>()) return value.get<uint64_t>(); diff --git a/include/llmr/style/value_comparison.hpp b/include/llmr/style/value_comparison.hpp new file mode 100644 index 0000000000..d69142bc5b --- /dev/null +++ b/include/llmr/style/value_comparison.hpp @@ -0,0 +1,100 @@ +#ifndef LLMR_STYLE_VALUE_COMPARISON +#define LLMR_STYLE_VALUE_COMPARISON + +#include "value.hpp" +#include <cstdlib> +#include <cerrno> + +namespace llmr { + +namespace util { + +namespace detail { + +template <typename Operator> +struct relaxed_operator_visitor { + typedef bool result_type; + + inline bool operator()(bool lhs, bool rhs) const { return Operator()(lhs, rhs); } + + template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> + inline bool operator()(bool lhs, T rhs) const { return Operator()(T(lhs), rhs); } + + template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> + inline bool operator()(T lhs, bool rhs) const { return Operator()(lhs, T(rhs)); } + + inline bool operator()(int64_t lhs, uint64_t rhs) const { + return lhs < 0 ? false : Operator()(uint64_t(lhs), rhs); + } + inline bool operator()(uint64_t lhs, int64_t rhs) const { + return rhs < 0 ? false : Operator()(lhs, uint64_t(rhs)); + } + + template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> + inline bool operator()(const std::string &lhs, T rhs) const { + double value; + return parseNumericString(lhs, value) ? Operator()(value, double(rhs)) : false; + } + + template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> + inline bool operator()(T lhs, const std::string &rhs) const { + double value; + return parseNumericString(rhs, value) ? Operator()(double(lhs), value) : false; + } + + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return Operator()(lhs, rhs); } +}; + +struct relaxed_equal_operator { + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return lhs == rhs; } +}; + +struct relaxed_greater_operator { + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return lhs > rhs; } +}; + +struct relaxed_greater_equal_operator { + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return lhs >= rhs; } +}; + +struct relaxed_less_operator { + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return lhs < rhs; } +}; + +struct relaxed_less_equal_operator { + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return lhs <= rhs; } +}; + +} // end namespace detail + +inline bool relaxed_equal(Value const &lhs, Value const &rhs) { + return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_equal_operator>(), lhs, rhs); +} + +inline bool relaxed_greater(Value const &lhs, Value const &rhs) { + return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_greater_operator>(), lhs, rhs); +} + +inline bool relaxed_greater_equal(Value const &lhs, Value const &rhs) { + return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_greater_equal_operator>(), lhs, rhs); +} + +inline bool relaxed_less(Value const &lhs, Value const &rhs) { + return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_less_operator>(), lhs, rhs); +} + +inline bool relaxed_less_equal(Value const &lhs, Value const &rhs) { + return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_less_equal_operator>(), lhs, rhs); +} + +} + +} + +#endif |