From 9693efb054750e02ef378f3e19e14317d72333e3 Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Wed, 24 Jan 2024 11:35:01 -0800 Subject: [PATCH] Unordered map seems to be faster than map --- mvt.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- mvt.hpp | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/mvt.cpp b/mvt.cpp index cc9b2496..81e809a4 100644 --- a/mvt.cpp +++ b/mvt.cpp @@ -498,6 +498,42 @@ bool mvt_value::operator<(const mvt_value &o) const { return false; } +bool mvt_value::operator==(const mvt_value &o) const { + if (type == o.type) { + switch (type) { + case mvt_string: + return string_value == o.string_value; + + case mvt_float: + return numeric_value.float_value == o.numeric_value.float_value; + + case mvt_double: + return numeric_value.double_value == o.numeric_value.double_value; + + case mvt_int: + return numeric_value.int_value == o.numeric_value.int_value; + + case mvt_uint: + return numeric_value.uint_value == o.numeric_value.uint_value; + + case mvt_sint: + return numeric_value.sint_value == o.numeric_value.sint_value; + + case mvt_bool: + return numeric_value.bool_value == o.numeric_value.bool_value; + + case mvt_null: + return numeric_value.null_value == o.numeric_value.null_value; + + default: + fprintf(stderr, "mvt_value::operator==: can't happen\n"); + exit(EXIT_IMPOSSIBLE); + } + } + + return false; +} + static std::string quote(std::string const &s) { std::string buf; @@ -557,8 +593,8 @@ std::string mvt_value::toString() const { void mvt_layer::tag(mvt_feature &feature, std::string key, mvt_value value) { size_t ko, vo; - std::map::iterator ki = key_map.find(key); - std::map::iterator vi = value_map.find(value); + std::unordered_map::iterator ki = key_map.find(key); + std::unordered_map::iterator vi = value_map.find(value); if (ki == key_map.end()) { ko = keys.size(); diff --git a/mvt.hpp b/mvt.hpp index c848cf6f..94512d1b 100644 --- a/mvt.hpp +++ b/mvt.hpp @@ -7,6 +7,8 @@ #include #include +#include "errors.hpp" + struct mvt_value; struct mvt_layer; @@ -82,6 +84,7 @@ struct mvt_value { } numeric_value; bool operator<(const mvt_value &o) const; + bool operator==(const mvt_value &o) const; std::string toString() const; mvt_value() { @@ -91,6 +94,45 @@ struct mvt_value { } }; +template <> +struct std::hash { + std::size_t operator()(const mvt_value &k) const { + using std::hash; + using std::size_t; + using std::string; + + switch (k.type) { + case mvt_string: + return std::hash()(k.string_value); + + case mvt_float: + return std::hash()(k.numeric_value.float_value); + + case mvt_double: + return std::hash()(k.numeric_value.double_value); + + case mvt_int: + return std::hash()(k.numeric_value.int_value); + + case mvt_uint: + return std::hash()(k.numeric_value.uint_value); + + case mvt_sint: + return std::hash()(k.numeric_value.sint_value); + + case mvt_bool: + return std::hash()(k.numeric_value.bool_value); + + case mvt_null: + return std::hash()(k.numeric_value.null_value); + + default: + fprintf(stderr, "mvt_value hash can't happen\n"); + exit(EXIT_IMPOSSIBLE); + } + } +}; + struct mvt_layer { int version = 0; std::string name = ""; @@ -103,8 +145,8 @@ struct mvt_layer { void tag(mvt_feature &feature, std::string key, mvt_value value); // For tracking the key-value constants already used in this layer - std::map key_map{}; - std::map value_map{}; + std::unordered_map key_map{}; + std::unordered_map value_map{}; }; struct mvt_tile {