File: car_helpers.h

package info (click to toggle)
simdjson 4.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 31,396 kB
  • sloc: cpp: 195,760; ansic: 20,954; sh: 1,126; python: 885; makefile: 47; ruby: 25; javascript: 13
file content (146 lines) | stat: -rw-r--r-- 3,663 bytes parent folder | download | duplicates (6)
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
#ifndef CAR_HELPERS_H
#define CAR_HELPERS_H

#include <iomanip>
#include <random>
#include <sstream>
#include <vector>

#include <simdjson.h>

struct Car {
  std::string make;
  std::string model;
  int64_t year; // We deliberately do not include the tire pressure.
};

using namespace simdjson;

std::string random_car_json() {
  static const std::vector<std::string> makes = {"Toyota", "Honda", "Ford",
                                                 "BMW", "Mazda"};
  static const std::vector<std::string> models = {"Camry", "Civic", "Focus",
                                                  "320i", "3"};
  static thread_local std::mt19937 rng{std::random_device{}()};
  std::uniform_int_distribution<int> make_dist(0, makes.size() - 1);
  std::uniform_int_distribution<int> model_dist(0, models.size() - 1);
  std::uniform_int_distribution<int> year_dist(2000, 2025);
  std::uniform_real_distribution<double> pressure_dist(30.0, 45.0);

  std::ostringstream oss;
  oss << R"({ "make": ")" << makes[make_dist(rng)] << R"(", "model": ")"
      << models[model_dist(rng)] << R"(",  "year": )" << year_dist(rng)
      << R"(, "tire_pressure": [ )" << std::fixed << std::setprecision(1)
      << pressure_dist(rng) << ", " << pressure_dist(rng) << R"( ] })";
  return oss.str();
}

std::string generate_car_json_stream(size_t N) {
  std::ostringstream oss;
  for (size_t i = 0; i < N; ++i) {
    oss << random_car_json();
    oss << "\n";
  }
  return oss.str();
}

std::string generate_car_json_array(size_t N) {
  std::ostringstream oss;
  oss << "[\n";
  for (size_t i = 0; i < N; ++i) {
    oss << random_car_json();
    if (i < N - 1) {
      oss << ",";
    }
    oss << "\n";
  }
  oss << "]";
  return oss.str();
}

// We want to maximize C++ portability, but this is not needed with static
// reflection (C++26).
template <>
simdjson_inline simdjson_result<Car>
simdjson::ondemand::document::get() & noexcept {
  ondemand::object obj;
  auto error = get_object().get(obj);
  if (error) {
    return error;
  }
  Car car;
  if ((error = obj["make"].get_string(car.make))) {
    return error;
  }
  if ((error = obj["model"].get_string(car.model))) {
    return error;
  }
  if ((error = obj["year"].get_int64().get(car.year))) {
    return error;
  }
  return car;
}

template <>
simdjson_inline simdjson_result<Car> simdjson::ondemand::value::get() noexcept {
  ondemand::object obj;
  auto error = get_object().get(obj);
  if (error) {
    return error;
  }
  Car car;
  if ((error = obj["make"].get_string(car.make))) {
    return error;
  }
  if ((error = obj["model"].get_string(car.model))) {
    return error;
  }
  if ((error = obj["year"].get_int64().get(car.year))) {
    return error;
  }
  return car;
}

template <>
simdjson_inline simdjson_result<Car>
simdjson::ondemand::document_reference::get() & noexcept {
  ondemand::object obj;
  auto error = get_object().get(obj);
  if (error) {
    return error;
  }
  Car car;
  if ((error = obj["make"].get_string(car.make))) {
    return error;
  }
  if ((error = obj["model"].get_string(car.model))) {
    return error;
  }
  if ((error = obj["year"].get_int64().get(car.year))) {
    return error;
  }
  return car;
}

template <>
simdjson_inline simdjson_result<Car>
simdjson::ondemand::document_reference::get() && noexcept {
  ondemand::object obj;
  auto error = get_object().get(obj);
  if (error) {
    return error;
  }
  Car car;
  if ((error = obj["make"].get_string(car.make))) {
    return error;
  }
  if ((error = obj["model"].get_string(car.model))) {
    return error;
  }
  if ((error = obj["year"].get_int64().get(car.year))) {
    return error;
  }
  return car;
}

#endif