1 #pragma once
2 
3 #ifdef SIMDJSON_COMPETITION_RAPIDJSON
4 
5 #include "large_random.h"
6 
7 namespace large_random {
8 
9 using namespace rapidjson;
10 
11 struct rapidjson_sax {
12     static constexpr diff_flags DiffFlags = diff_flags::NONE;
13 
14     struct Handler {
15         size_t k{0};
16         double buffer[3];
17         std::vector<point>& result;
18 
Handlerrapidjson_sax::Handler19         Handler(std::vector<point> &r) : result(r) { }
20 
Keyrapidjson_sax::Handler21         bool Key(const char* key, SizeType length, bool copy) {
22             switch(key[0]) {
23             case 'x':
24                 k = 0;
25                 break;
26             case 'y':
27                 k = 1;
28                 break;
29             case 'z':
30                 k = 2;
31                 break;
32             }
33             return true;
34         }
Doublerapidjson_sax::Handler35         bool Double(double d) {
36             buffer[k] = d;
37             if (k == 2) {
38                 result.emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
39                 k = 0;
40             }
41             return true;
42         }
Uintrapidjson_sax::Handler43         bool Uint(unsigned i) { return Double(i); }   // Need this event because coordinate value can be equal to 1
44         // Irrelevant events
Nullrapidjson_sax::Handler45         bool Null() { return true; }
Boolrapidjson_sax::Handler46         bool Bool(bool b) { return true; }
Intrapidjson_sax::Handler47         bool Int(int i) { return true; }
Int64rapidjson_sax::Handler48         bool Int64(int64_t i) { return true; }
Uint64rapidjson_sax::Handler49         bool Uint64(uint64_t i) { return true; }
RawNumberrapidjson_sax::Handler50         bool RawNumber(const char* str, SizeType length, bool copy) { return true; }
Stringrapidjson_sax::Handler51         bool String(const char* str, SizeType length, bool copy) { return true; }
StartObjectrapidjson_sax::Handler52         bool StartObject() { return true; }
EndObjectrapidjson_sax::Handler53         bool EndObject(SizeType memberCount) { return true; }
StartArrayrapidjson_sax::Handler54         bool StartArray() { return true; }
EndArrayrapidjson_sax::Handler55         bool EndArray(SizeType elementCount) { return true; }
56     }; // handler
57 
runrapidjson_sax58     bool run(simdjson::padded_string &json, std::vector<point> &result) {
59         Reader reader;
60         Handler handler(result);
61         InsituStringStream ss(json.data());
62         reader.Parse<kParseInsituFlag | kParseValidateEncodingFlag | kParseFullPrecisionFlag>(ss,handler);
63         return true;
64     }
65 }; // rapid_jason_sax
66 BENCHMARK_TEMPLATE(large_random, rapidjson_sax)->UseManualTime();
67 } // namespace large_random
68 
69 #endif // SIMDJSON_COMPETITION_RAPIDJSON