Unordered map seems to be faster than map

This commit is contained in:
Erica Fischer
2024-01-24 11:35:01 -08:00
parent bdc0f207ae
commit 9693efb054
2 changed files with 82 additions and 4 deletions
+44 -2
View File
@@ -7,6 +7,8 @@
#include <set>
#include <vector>
#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<mvt_value> {
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<string>()(k.string_value);
case mvt_float:
return std::hash<float>()(k.numeric_value.float_value);
case mvt_double:
return std::hash<double>()(k.numeric_value.double_value);
case mvt_int:
return std::hash<long long>()(k.numeric_value.int_value);
case mvt_uint:
return std::hash<unsigned long long>()(k.numeric_value.uint_value);
case mvt_sint:
return std::hash<long long>()(k.numeric_value.sint_value);
case mvt_bool:
return std::hash<bool>()(k.numeric_value.bool_value);
case mvt_null:
return std::hash<int>()(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<std::string, size_t> key_map{};
std::map<mvt_value, size_t> value_map{};
std::unordered_map<std::string, size_t> key_map{};
std::unordered_map<mvt_value, size_t> value_map{};
};
struct mvt_tile {