-
Notifications
You must be signed in to change notification settings - Fork 0
/
astro_simd.hpp
573 lines (542 loc) · 15.6 KB
/
astro_simd.hpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#ifndef ASTRO_SIMD_HPP
#define ASTRO_SIMD_HPP
// Astro is new generation json dom api
#include "simdjson.h"
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <variant>
#include <vector>
namespace astro_simd
{
/*
@brief Type is a enum class that can hold any type of value
*/
enum class Type { Null, Bool, Number, String, Array, Object };
/*
@brief NumericType is a variant type that can hold any type of numeric value
*/
using NumericType = std::variant<int, float, uint64_t, double, long, long long, long double>;
/*
@brief Value is a variant type that can hold any type of value
*/
class Array;
class Number;
class Object;
class Value;
// !TODO NativeArray is not used yet
// class NativeArray : public std::vector<Value> {};
class Number final
{
public:
Number() = default;
Number(const Number&) = default;
Number(Number&&) = default;
Number(const NumericType& num) : value(num) {};
Number(const double& num) : value(num) {};
Number(const int& num) : value(num) {};
Number(const uint64_t& num) : value(num) {};
Number(const float& num) : value(num) {};
Number(const long& num) : value(num) {};
Number(const long long& num) : value(num) {};
Number& operator=(const Number&) = default;
Number& operator=(Number&&) = default;
Number& operator=(const NumericType& num)
{
value = num;
return *this;
};
Number& operator=(const double& num)
{
value = num;
return *this;
};
Number& operator=(const int& num)
{
value = num;
return *this;
};
Number& operator=(const float& num)
{
value = num;
return *this;
};
Number& operator=(const long& num)
{
value = num;
return *this;
};
Number& operator=(const long long& num)
{
value = num;
return *this;
};
~Number() = default;
NumericType value;
};
class String final
{
public:
String() = default;
String(const String&) = default;
String(String&&) = default;
String& operator=(const String&) = default;
String& operator=(String&&) = default;
String(const std::string& str) : value(str) {};
String(const std::string_view& str) : value(str) {};
String(const char *str) : value(str) {};
~String() = default;
String& operator=(const std::string& str)
{
value = str;
return *this;
};
String& operator=(const std::string_view& str)
{
value = str;
return *this;
};
String& operator=(const char *str)
{
value = str;
return *this;
};
std::string value;
};
class Array final
{
public:
Array() = default;
Array(const Array&) = default;
Array(Array&&) = default;
Array& operator=(const Array&) = default;
Array& operator=(Array&&) = default;
// consturct with std::vector
Array(const std::vector<Value>& vec) : Blocks(vec) { countElements = vec.size(); };
Array(simdjson::dom::element domy)
{
isParsed = false;
domElement = &domy;
};
/**
* @brief Construct a new Array object
* Example:
* Array arr = {1,2,3,4,5};
*/
Array(const std::initializer_list<Value>& vec) : Blocks(vec) { countElements = vec.size(); };
Array& operator=(const std::vector<Value>&& vec)
{
Blocks = vec;
countElements = vec.size();
return *this;
};
Array& operator=(const std::initializer_list<Value>& list)
{
Blocks = list;
countElements = list.size();
return *this;
};
~Array() = default;
// object start end around string values
std::vector<Value> Blocks;
std::size_t countElements = 0;
private:
bool isParsed = false;
simdjson::dom::element *domElement = nullptr;
void inline parse();
};
class Object final
{
public:
Object() = default;
Object(const Object&) = default;
Object(Object&&) = default;
Object& operator=(const Object&) = default;
Object& operator=(Object&&) = default;
// consturct with std::map
Object(const std::map<std::string, Value>& map) : Blocks(map) { countElements = map.size(); };
Object(simdjson::dom::element map)
{
isParsed = false;
domElement = ↦
countElements = map.get_object().size();
};
Object& operator=(const std::map<std::string, Value> map)
{
Blocks = map;
countElements = map.size();
return *this;
};
Object& operator=(std::map<std::string, Value>&& map)
{
Blocks = map;
countElements = map.size();
return *this;
};
Object& operator=(std::map<std::string, Value>& map)
{
Blocks = map;
countElements = map.size();
return *this;
};
~Object() = default;
// operators
// object start end around string values
std::map<std::string, Value> Blocks;
std::size_t countElements = 0;
private:
bool isParsed = false;
simdjson::dom::element *domElement = nullptr;
void inline parse()
{
if( isParsed ) return;
isParsed = true;
if( domElement == nullptr ) return;
if( !domElement->is_array() ) return;
for( auto&& element : domElement->get_object() )
{
auto key = element.key;
switch( element.value.type() )
{
case simdjson::dom::element_type::INT64:
Blocks.emplace(key, element.value.get_int64());
break;
case simdjson::dom::element_type::UINT64:
Blocks.emplace(key, element.value.get_uint64());
break;
case simdjson::dom::element_type::DOUBLE:
Blocks.emplace(key, element.value.get_double());
break;
case simdjson::dom::element_type::STRING:
Blocks.emplace(key, element.value.get_c_str());
break;
case simdjson::dom::element_type::BOOL: Blocks.emplace(key, element.value.get_bool()); break;
case simdjson::dom::element_type::ARRAY: Blocks.emplace(key, Array {element.value}); break;
case simdjson::dom::element_type::OBJECT: Blocks.emplace(key, Object {element.value}); break;
case simdjson::dom::element_type::NULL_VALUE: Blocks.emplace(key, nullptr); break;
default: break;
}
};
};
};
// phony answer for forward declaration bug
void inline Array::parse()
{
if( isParsed ) return;
isParsed = true;
if( domElement == nullptr ) return;
if( !domElement->is_array() ) return;
for( simdjson::dom::element&& element : domElement->get_array() )
{
switch( element.type() )
{
case simdjson::dom::element_type::INT64: Blocks.emplace_back(element.get_int64()); break;
case simdjson::dom::element_type::UINT64: Blocks.emplace_back(element.get_uint64()); break;
case simdjson::dom::element_type::DOUBLE: Blocks.emplace_back(element.get_double()); break;
case simdjson::dom::element_type::STRING: Blocks.emplace_back(element.get_c_str()); break;
case simdjson::dom::element_type::BOOL: Blocks.emplace_back(element.get_bool()); break;
case simdjson::dom::element_type::ARRAY:
Blocks.emplace_back(Array {
element,
});
break;
case simdjson::dom::element_type::OBJECT: Blocks.emplace_back(Object {element}); break;
case simdjson::dom::element_type::NULL_VALUE: Blocks.emplace_back(nullptr); break;
default: break;
}
}
}
class Value final
{
public:
Value() = default;
Value(const Value&) = default;
Value(Value&&) = default;
Value& operator=(const Value&) = default;
Value& operator=(Value&&) = default;
// Member functions
Value(String str) : type(Type::String) { value = str; };
// from string
Value(const std::string& str) : type(Type::String)
{
type = Type::String;
value = String {str};
};
// from char *
Value(const char *str) : type(Type::String)
{
type = Type::String;
value = String {str};
};
// = string
Value& operator=(const std::string& str)
{
type = Type::String;
value = String {str};
return *this;
};
// = char *
Value& operator=(const char *str)
{
type = Type::String;
value = String {str};
return *this;
};
// from bool
Value(const bool& b) : type(Type::Bool)
{
type = Type::Bool;
value = b;
};
// = bool
Value& operator=(const bool& b)
{
type = Type::Bool;
value = b;
return *this;
};
// from number
Value(const Number& num) : type(Type::Number)
{
type = Type::Number;
value = num;
};
Value(const double& num) : type(Type::Number)
{
type = Type::Number;
value = Number {num};
};
Value(const int& num) : type(Type::Number)
{
type = Type::Number;
value = Number {num};
};
Value(const uint64_t& num) : type(Type::Number)
{
type = Type::Number;
value = Number {num};
};
Value(const float& num) : type(Type::Number)
{
type = Type::Number;
value = Number {num};
};
Value(const long& num) : type(Type::Number)
{
type = Type::Number;
value = Number {num};
};
Value(const long long& num) : type(Type::Number)
{
type = Type::Number;
value = Number {num};
};
// = number
Value& operator=(const double& num)
{
type = Type::Number;
value = Number {num};
return *this;
};
// from array
Value(const Array& arr) : type(Type::Array)
{
type = Type::Array;
value = arr;
};
// = array
Value& operator=(const Array& arr)
{
type = Type::Array;
value = arr;
return *this;
};
Value& operator=(const std::vector<Value>& arr)
{
type = Type::Array;
value = Array {arr};
return *this;
};
Value& operator=(std::vector<Value> arr)
{
type = Type::Array;
value = Array {arr};
return *this;
};
// from object
Value(const Object& obj) : type(Type::Object)
{
type = Type::Object;
value = obj;
};
// = object
Value& operator=(const Object& obj)
{
type = Type::Object;
value = obj;
return *this;
};
Value& operator=(const std::map<std::string, Value>& obj)
{
type = Type::Object;
value = Object {obj};
return *this;
};
Value& operator=(std::map<std::string, Value> obj)
{
type = Type::Object;
value = Object {obj};
return *this;
};
// from null
Value(const nullptr_t& null) : type(Type::Null) { value = null; };
// = null
Value& operator=(const nullptr_t& null)
{
type = Type::Null;
value = null;
return *this;
};
~Value() = default;
Type type;
std::variant<nullptr_t, bool, Number, String, Array, Object> value {};
// isRules
inline bool isNull() const { return std::holds_alternative<nullptr_t>(value); };
inline bool isBool() const { return std::holds_alternative<bool>(value); };
inline bool isNumber() const { return std::holds_alternative<Number>(value); };
inline bool isString() const { return std::holds_alternative<String>(value); };
inline bool isArray() const { return std::holds_alternative<Array>(value); };
inline bool isObject() const { return std::holds_alternative<Object>(value); }
inline nullptr_t& getNull() { return std::get<nullptr_t>(value); };
inline bool & getBool() { return std::get<bool>(value); };
inline Number & getNumber() { return std::get<Number>(value); };
inline String & getString() { return std::get<String>(value); };
inline Array & getArray() { return std::get<Array>(value); };
inline Object & getObject() { return std::get<Object>(value); };
// get any value
template<class T> T& as() { return std::get<T>(value); };
inline void simdLoad(std::string path)
{
simdjson::dom::parser parser;
simdjson::dom::element dom;
auto error = parser.load(path).get(dom);
if( error ) throw std::runtime_error("Error loading file");
domElement = &dom;
parse();
}
// Parse With SimdJSON
private:
bool isParsed = false;
simdjson::dom::element *domElement = nullptr;
void inline parse()
{
if( isParsed ) return;
isParsed = true;
if( domElement == nullptr ) return;
switch( type )
{
case Type::Null: break;
case Type::Bool: value = domElement->get_bool().take_value(); break;
case Type::Number:
if( domElement->is_int64() ) { value = domElement->get_int64().take_value(); }
else if( domElement->is_uint64() ) { value = domElement->get_uint64().value(); }
else if( domElement->is_double() ) { value = domElement->get_double().take_value(); }
break;
case Type::String: value = domElement->get_string().value(); break;
case Type::Array: value = Array {*domElement}; break;
case Type::Object: value = Object {*domElement}; break;
default: break;
}
}
};
// struct Parser final {
// Parser() = default;
// Parser(const Parser &) = default;
// Parser(Parser &&) = default;
// Parser &operator=(const Parser &) = default;
// Parser &operator=(Parser &&) = default;
// ~Parser() = default;
// std::string::const_iterator begin;
// std::string::const_iterator end;
// Value parse() = {};
// Value parse_next() = {};
// void consume_ws();
// Value parse_string() = {};
// Value parse_number() = {};
// Value parse_object() = {};
// Value parse_array() = {};
// Value parse_bool() = {};
// Value parse_null() = {};
// };
struct Stringifier final
{
Stringifier() = default;
Stringifier(const Stringifier&) = default;
Stringifier(Stringifier&&) = default;
Stringifier& operator=(const Stringifier&) = default;
Stringifier& operator=(Stringifier&&) = default;
~Stringifier() = default;
std::string stringify(const Value& value)
{
switch( value.type )
{
case Type::String: return stringify_string(value);
case Type::Number: return stringify_number(value);
case Type::Object: return stringify_object(value);
case Type::Array: return stringify_array(value);
case Type::Bool: return stringify_bool(value);
case Type::Null:
// return stringify_null(value);
return stringify_null();
default: return "";
}
};
std::string stringify_string(const Value& value)
{
return "\"" + std::get<String>(value.value).value + "\"";
};
std::string stringify_number(const Value& value)
{
auto vv = std::get<Number>(value.value).value;
if( std::holds_alternative<int>(vv) ) { return std::to_string(std::get<int>(vv)); }
else if( std::holds_alternative<float>(vv) ) { return std::to_string(std::get<float>(vv)); }
else if( std::holds_alternative<long>(vv) ) { return std::to_string(std::get<long>(vv)); }
else if( std::holds_alternative<long long>(vv) )
{
return std::to_string(std::get<long long>(vv));
}
else if( std::holds_alternative<double>(vv) ) { return std::to_string(std::get<double>(vv)); }
else if( std::holds_alternative<long double>(vv) )
{
return std::to_string(std::get<long double>(vv));
}
else { return "0"; }
};
std::string stringify_object(const Value& value)
{
std::string result = "{";
for( auto& pair : std::get<Object>(value.value).Blocks )
{
result += "\"" + pair.first + "\":" + stringify(pair.second) + ",";
}
if( result.back() == ',' ) { result.pop_back(); }
result += "}";
return result;
};
std::string stringify_array(const Value& value)
{
std::string result = "[";
for( auto& item : std::get<Array>(value.value).Blocks ) { result += stringify(item) + ","; }
if( result.back() == ',' ) { result.pop_back(); }
result += "]";
return result;
};
std::string stringify_bool(const Value& value)
{
return std::get<bool>(value.value) ? "true" : "false";
};
std::string stringify_null() { return "null"; };
};
} // namespace astro
#endif