-
Notifications
You must be signed in to change notification settings - Fork 256
/
test_rapid_sax.cpp
191 lines (165 loc) · 3.81 KB
/
test_rapid_sax.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <libnotify.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/reader.h>
#include <sstream>
#ifdef __clang__
static constexpr auto COMPILER = "clang++";
#else
static constexpr auto COMPILER = "g++";
#endif
using namespace std;
using namespace rapidjson;
struct coordinate_t {
double x;
double y;
double z;
auto operator<=>(const coordinate_t &) const = default;
friend ostream &operator<<(ostream &out, const coordinate_t &point) {
out << "coordinate_t {x: " << point.x << ", y: " << point.y
<< ", z: " << point.z << "}";
return out;
}
};
using TCallback = function<void(const coordinate_t &)>;
class CoordinateHandler : public BaseReaderHandler<UTF8<>, CoordinateHandler> {
public:
CoordinateHandler(const TCallback &callback)
: state_(kStart), x_(), y_(), z_(), callback_(callback) {}
bool Double(double d) {
switch (state_) {
case kX:
x_ += d;
state_ = kCoordinatesElement;
break;
case kY:
y_ += d;
state_ = kCoordinatesElement;
break;
case kZ:
z_ += d;
state_ = kCoordinatesElement;
break;
default:
break;
}
return true;
}
bool StartObject() {
switch (state_) {
case kStart:
state_ = kRoot;
break;
case kCoordinatesArray:
state_ = kCoordinatesElement;
break;
default:
break;
}
return true;
}
bool Key(const Ch *str, SizeType len, bool) {
switch (state_) {
case kRoot:
if (len == sizeof("coordinates") - 1 &&
memcmp(str, "coordinates", sizeof("coordinates") - 1) == 0)
state_ = kCoordinates;
break;
case kCoordinatesElement:
if (len == 1 && str[0] >= 'x' && str[0] <= 'z')
state_ = static_cast<State>(kX + (str[0] - 'x'));
break;
default:
break;
}
return true;
}
bool EndObject(SizeType) {
switch (state_) {
case kCoordinatesElement:
state_ = kCoordinatesArray;
break;
default:
break;
}
return true;
}
bool StartArray() {
switch (state_) {
case kCoordinates:
state_ = kCoordinatesArray;
break;
default:
break;
}
return true;
}
bool EndArray(SizeType len) {
switch (state_) {
case kCoordinatesArray:
callback_(coordinate_t{x_ / len, y_ / len, z_ / len});
state_ = kCoordinates;
break;
default:
break;
}
return true;
}
private:
enum State {
kStart,
kRoot,
kCoordinates,
kCoordinatesArray,
kCoordinatesElement,
kX,
kY,
kZ
} state_;
double x_, y_, z_;
TCallback callback_;
};
void read_file(const string &filename, stringstream &buffer) {
ifstream file{filename};
if (file.good()) {
buffer << file.rdbuf();
}
}
void calc(stringstream &ss, const TCallback &callback) {
IStreamWrapper isw(ss);
Reader reader;
CoordinateHandler handler(callback);
#ifdef PRECISED
reader.Parse<kParseFullPrecisionFlag>(isw, handler);
#else
reader.Parse(isw, handler);
#endif
}
int main() {
auto right = coordinate_t{2.0, 0.5, 0.25};
for (auto v : {R"({"coordinates":[{"x":2.0,"y":0.5,"z":0.25}]})",
R"({"coordinates":[{"y":0.5,"x":2.0,"z":0.25}]})"}) {
auto json = stringstream(v);
calc(json, [right](const coordinate_t &left) {
if (left != right) {
cerr << left << " != " << right << endl;
exit(EXIT_FAILURE);
}
});
}
stringstream ss;
read_file("/tmp/1.json", ss);
#ifdef PRECISED
static constexpr auto SUFFIX = " Precise";
#else
static constexpr auto SUFFIX = "";
#endif
coordinate_t results;
notifying_invoke(
[&]() { calc(ss, [&](const coordinate_t &v) { results = v; }); },
"C++/{} (RapidJSON SAX{})", COMPILER, SUFFIX);
cout << results << endl;
}