mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-02 16:35:40 +02:00
Replace duplicated code with template
This commit is contained in:
+20
-134
@@ -88,139 +88,17 @@ void set_attribute_accum(std::unordered_map<std::string, attribute_op> &attribut
|
||||
set_attribute_accum(attribute_accum, name, type);
|
||||
}
|
||||
|
||||
void preserve_attribute(attribute_op const &op, std::string const &key, serial_val const &val, std::vector<std::string> &full_keys, std::vector<serial_val> &full_values, std::unordered_map<std::string, accum_state> &attribute_accum_state) {
|
||||
template <class T>
|
||||
static void preserve_attribute1(attribute_op const &op, std::string const &key, T const &val, std::vector<std::string> &full_keys, std::vector<T> &full_values, std::unordered_map<std::string, accum_state> &attribute_accum_state) {
|
||||
for (size_t i = 0; i < full_keys.size(); i++) {
|
||||
if (key == full_keys[i]) {
|
||||
switch (op) {
|
||||
case op_sum:
|
||||
full_values[i].s = milo::dtoa_milo(atof(full_values[i].s.c_str()) + atof(val.s.c_str()));
|
||||
full_values[i].type = mvt_double;
|
||||
full_values[i] = (full_values[i].to_double() + val.to_double());
|
||||
return;
|
||||
|
||||
case op_product:
|
||||
full_values[i].s = milo::dtoa_milo(atof(full_values[i].s.c_str()) * atof(val.s.c_str()));
|
||||
full_values[i].type = mvt_double;
|
||||
return;
|
||||
|
||||
case op_max: {
|
||||
double existing = atof(full_values[i].s.c_str());
|
||||
double maybe = atof(val.s.c_str());
|
||||
if (maybe > existing) {
|
||||
full_values[i].s = val.s.c_str();
|
||||
full_values[i].type = mvt_double;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
case op_min: {
|
||||
double existing = atof(full_values[i].s.c_str());
|
||||
double maybe = atof(val.s.c_str());
|
||||
if (maybe < existing) {
|
||||
full_values[i].s = val.s.c_str();
|
||||
full_values[i].type = mvt_double;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
case op_mean: {
|
||||
auto state = attribute_accum_state.find(key);
|
||||
if (state == attribute_accum_state.end()) {
|
||||
accum_state s;
|
||||
s.sum = atof(full_values[i].s.c_str()) + atof(val.s.c_str());
|
||||
s.count = 2;
|
||||
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));
|
||||
|
||||
full_values[i].s = milo::dtoa_milo(s.sum / s.count);
|
||||
full_values[i].type = mvt_double;
|
||||
} else {
|
||||
state->second.sum += atof(val.s.c_str());
|
||||
state->second.count += 1;
|
||||
|
||||
full_values[i].s = milo::dtoa_milo(state->second.sum / state->second.count);
|
||||
full_values[i].type = mvt_double;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
case op_concat:
|
||||
full_values[i].s += val.s;
|
||||
full_values[i].type = mvt_string;
|
||||
return;
|
||||
|
||||
case op_comma:
|
||||
full_values[i].s += std::string(",") + val.s;
|
||||
full_values[i].type = mvt_string;
|
||||
return;
|
||||
|
||||
case op_count: {
|
||||
auto state = attribute_accum_state.find(key);
|
||||
if (state == attribute_accum_state.end()) { // not already present
|
||||
accum_state s;
|
||||
s.count = 2;
|
||||
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));
|
||||
|
||||
full_values[i].type = mvt_double;
|
||||
full_values[i].s = std::to_string(s.count);
|
||||
} else { // already present, incrementing
|
||||
state->second.count += 1;
|
||||
full_values[i].type = mvt_double;
|
||||
full_values[i].s = std::to_string(state->second.count);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// not found, so we are making a new value
|
||||
|
||||
serial_val sv;
|
||||
switch (op) {
|
||||
case op_sum:
|
||||
case op_max:
|
||||
case op_min:
|
||||
sv.s = val.s;
|
||||
sv.type = mvt_double;
|
||||
break;
|
||||
|
||||
case op_count: {
|
||||
auto state = attribute_accum_state.find(key);
|
||||
if (state == attribute_accum_state.end()) { // not already present
|
||||
accum_state s;
|
||||
s.count = 1;
|
||||
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));
|
||||
|
||||
sv.s = std::to_string(s.count);
|
||||
} else { // already present, incrementing
|
||||
fprintf(stderr, "preserve_attribute: can't happen (count)\n");
|
||||
exit(EXIT_IMPOSSIBLE);
|
||||
}
|
||||
sv.type = mvt_double;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
fprintf(stderr, "can't happen: operation that isn't used by --accumulate-numeric-attributes\n");
|
||||
exit(EXIT_IMPOSSIBLE);
|
||||
}
|
||||
|
||||
full_keys.push_back(key);
|
||||
full_values.push_back(sv);
|
||||
}
|
||||
|
||||
// This is exactly the same logic as preserve_attribute above,
|
||||
// but with mvt_value instead of serial_val, since overzoom doesn't
|
||||
// need anything in serial_val form.
|
||||
void preserve_attribute(attribute_op const &op, std::string const &key, mvt_value const &val, std::vector<std::string> &full_keys, std::vector<mvt_value> &full_values, std::unordered_map<std::string, accum_state> &attribute_accum_state) {
|
||||
for (size_t i = 0; i < full_keys.size(); i++) {
|
||||
if (key == full_keys[i]) {
|
||||
switch (op) {
|
||||
case op_sum:
|
||||
full_values[i] = mvt_value(full_values[i].to_double() + val.to_double());
|
||||
return;
|
||||
|
||||
case op_product:
|
||||
full_values[i] = mvt_value(full_values[i].to_double() * val.to_double());
|
||||
full_values[i] = (full_values[i].to_double() * val.to_double());
|
||||
return;
|
||||
|
||||
case op_max: {
|
||||
@@ -249,22 +127,22 @@ void preserve_attribute(attribute_op const &op, std::string const &key, mvt_valu
|
||||
s.count = 2;
|
||||
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));
|
||||
|
||||
full_values[i] = mvt_value(s.sum / s.count);
|
||||
full_values[i] = (s.sum / s.count);
|
||||
} else {
|
||||
state->second.sum += val.to_double();
|
||||
state->second.count += 1;
|
||||
|
||||
full_values[i] = mvt_value(state->second.sum / state->second.count);
|
||||
full_values[i] = (state->second.sum / state->second.count);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
case op_concat:
|
||||
full_values[i].set_string_value(full_values[i].toString() + val.toString());
|
||||
full_values[i].set_string_value(full_values[i].get_string_value() + val.get_string_value());
|
||||
return;
|
||||
|
||||
case op_comma:
|
||||
full_values[i].set_string_value(full_values[i].toString() + "," + val.toString());
|
||||
full_values[i].set_string_value(full_values[i].get_string_value() + "," + val.get_string_value());
|
||||
return;
|
||||
|
||||
case op_count: {
|
||||
@@ -274,10 +152,10 @@ void preserve_attribute(attribute_op const &op, std::string const &key, mvt_valu
|
||||
s.count = 2;
|
||||
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));
|
||||
|
||||
full_values[i] = mvt_value(s.count);
|
||||
full_values[i] = (s.count);
|
||||
} else { // already present, incrementing
|
||||
state->second.count += 1;
|
||||
full_values[i] = mvt_value(state->second.count);
|
||||
full_values[i] = (state->second.count);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -287,7 +165,7 @@ void preserve_attribute(attribute_op const &op, std::string const &key, mvt_valu
|
||||
|
||||
// not found, so we are making a new value
|
||||
|
||||
mvt_value v;
|
||||
T v;
|
||||
switch (op) {
|
||||
case op_sum:
|
||||
case op_max:
|
||||
@@ -302,7 +180,7 @@ void preserve_attribute(attribute_op const &op, std::string const &key, mvt_valu
|
||||
s.count = 1;
|
||||
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));
|
||||
|
||||
v = mvt_value(s.count);
|
||||
v = (s.count);
|
||||
} else { // already present, incrementing
|
||||
fprintf(stderr, "preserve_attribute: can't happen (count)\n");
|
||||
exit(EXIT_IMPOSSIBLE);
|
||||
@@ -318,3 +196,11 @@ void preserve_attribute(attribute_op const &op, std::string const &key, mvt_valu
|
||||
full_keys.push_back(key);
|
||||
full_values.push_back(v);
|
||||
}
|
||||
|
||||
void preserve_attribute(attribute_op const &op, std::string const &key, mvt_value const &val, std::vector<std::string> &full_keys, std::vector<mvt_value> &full_values, std::unordered_map<std::string, accum_state> &attribute_accum_state) {
|
||||
preserve_attribute1(op, key, val, full_keys, full_values, attribute_accum_state);
|
||||
}
|
||||
|
||||
void preserve_attribute(attribute_op const &op, std::string const &key, serial_val const &val, std::vector<std::string> &full_keys, std::vector<serial_val> &full_values, std::unordered_map<std::string, accum_state> &attribute_accum_state) {
|
||||
preserve_attribute1(op, key, val, full_keys, full_values, attribute_accum_state);
|
||||
}
|
||||
|
||||
+3
-1
@@ -5,6 +5,7 @@
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include "mvt.hpp"
|
||||
#include "milo/dtoa_milo.h"
|
||||
|
||||
enum attribute_op {
|
||||
op_sum,
|
||||
@@ -26,7 +27,8 @@ struct serial_val;
|
||||
|
||||
void set_attribute_accum(std::unordered_map<std::string, attribute_op> &attribute_accum, std::string name, std::string type);
|
||||
void set_attribute_accum(std::unordered_map<std::string, attribute_op> &attribute_accum, const char *arg, char **argv);
|
||||
void preserve_attribute(attribute_op const &op, const std::string &key, serial_val const &val, std::vector<std::string> &full_keys, std::vector<serial_val> &full_values, std::unordered_map<std::string, accum_state> &attribute_accum_state);
|
||||
|
||||
void preserve_attribute(attribute_op const &op, std::string const &key, serial_val const &val, std::vector<std::string> &full_keys, std::vector<serial_val> &full_values, std::unordered_map<std::string, accum_state> &attribute_accum_state);
|
||||
void preserve_attribute(attribute_op const &op, std::string const &key, mvt_value const &val, std::vector<std::string> &full_keys, std::vector<mvt_value> &full_values, std::unordered_map<std::string, accum_state> &attribute_accum_state);
|
||||
|
||||
extern std::map<std::string, attribute_op> numeric_operations;
|
||||
|
||||
@@ -98,7 +98,11 @@ struct mvt_value {
|
||||
} numeric_value;
|
||||
|
||||
std::string get_string_value() const {
|
||||
return std::string(*s, numeric_value.string_value.off, numeric_value.string_value.len);
|
||||
if (type == mvt_string) {
|
||||
return std::string(*s, numeric_value.string_value.off, numeric_value.string_value.len);
|
||||
} else {
|
||||
return toString();
|
||||
}
|
||||
}
|
||||
|
||||
std::string_view get_string_view() const {
|
||||
|
||||
+20
@@ -49,6 +49,26 @@ struct serial_val {
|
||||
serial_val(int t, const std::string &val)
|
||||
: type(t), s(val) {
|
||||
}
|
||||
|
||||
// These functions for interface compatibility with mvt_value:
|
||||
|
||||
serial_val(double val) {
|
||||
type = mvt_double;
|
||||
s = milo::dtoa_milo(val);
|
||||
}
|
||||
|
||||
double to_double() const {
|
||||
return atof(s.c_str());
|
||||
}
|
||||
|
||||
std::string get_string_value() const {
|
||||
return s;
|
||||
}
|
||||
|
||||
void set_string_value(std::string const &val) {
|
||||
type = mvt_string;
|
||||
s = val;
|
||||
}
|
||||
};
|
||||
|
||||
struct serial_feature {
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user