I think that this is better (fairer) code.

This commit is contained in:
Daniel Lemire 2020-09-03 16:47:05 -04:00 committed by John Keiser
parent b935544d65
commit 157604b3a5
2 changed files with 21 additions and 6 deletions

View File

@ -28,11 +28,10 @@ simdjson_really_inline bool OnDemand::Run(const padded_string &json) {
container.clear();
auto doc = parser.iterate(json);
// TODO this sucks, you should be able to just say for ( ... : doc)
// TODO you should be able to just say for ( ... : doc)
auto array = doc.get_array();
for (ondemand::object point_object : array) {
auto point = point_object.begin();
container.emplace_back(my_point{(*point).value(), (*++point).value(), (*++point).value()});
container.emplace_back(my_point{point_object["x"], point_object["y"], point_object["z"]});
}
return true;

View File

@ -33,7 +33,7 @@ using namespace SIMDJSON_IMPLEMENTATION::stage2;
struct sax_point_reader_visitor {
public:
std::vector<my_point> &points;
size_t idx{0};
size_t idx{4};
double buffer[3];
sax_point_reader_visitor(std::vector<my_point> &_points) : points(_points) {}
@ -43,7 +43,8 @@ public:
return SUCCESS;
}
simdjson_really_inline error_code visit_primitive(json_iterator &, const uint8_t *value) {
return numberparsing::parse_double(value).get(buffer[idx++]);
if(idx == 4) { return simdjson::SUCCESS; }
return numberparsing::parse_double(value).get(buffer[idx]);
}
simdjson_really_inline error_code visit_object_end(json_iterator &) {
points.emplace_back(my_point{buffer[0], buffer[1], buffer[2]});
@ -51,7 +52,22 @@ public:
}
simdjson_really_inline error_code visit_document_start(json_iterator &) { return SUCCESS; }
simdjson_really_inline error_code visit_key(json_iterator &, const uint8_t *) { return SUCCESS; }
simdjson_really_inline error_code visit_key(json_iterator &, const uint8_t * key) {
switch(key[1]) {
case 'x':
idx = 0;
break;
case 'y':
idx = 1;
break;
case 'z':
idx = 2;
break;
default:
idx = 4;
}
return SUCCESS;
}
simdjson_really_inline error_code visit_array_start(json_iterator &) { return SUCCESS; }
simdjson_really_inline error_code visit_array_end(json_iterator &) { return SUCCESS; }
simdjson_really_inline error_code visit_document_end(json_iterator &) { return SUCCESS; }