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
+38 -2
View File
@@ -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<std::string, size_t>::iterator ki = key_map.find(key);
std::map<mvt_value, size_t>::iterator vi = value_map.find(value);
std::unordered_map<std::string, size_t>::iterator ki = key_map.find(key);
std::unordered_map<mvt_value, size_t>::iterator vi = value_map.find(value);
if (ki == key_map.end()) {
ko = keys.size();
+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 {