ppap4lmp  0.7.2
starter.cpp
Go to the documentation of this file.
1 
9 #include "starter.h"
10 #include "../utils/map_to_index.h"
11 #include "../utils/runtime_error.h"
12 
13 namespace ut = utils;
14 
15 /* ------------------------------------------------------------------ */
16 
18  Json &data)
19 {
20  auto map = ut::map_to_index(data, "id");
21  auto vec = Vec<std::pair<int,int>>(map.begin(), map.end());
22 
23  std::sort(vec.begin(), vec.end());
24 
25  auto tmp = Json::array();
26  tmp.get_ref<Json::array_t&>().reserve(vec.size());
27 
28  for (const auto &pair : vec)
29  {
30  tmp.push_back(data[pair.second]);
31  }
32 
33  data.swap(tmp);
34 }
35 
36 /* ------------------------------------------------------------------ */
37 
39  const ElPtr &elem,
40  Json &data)
41 {
42  if (data != nullptr)
43  {
44  ut::runtime_error("Starter accepts empty data only");
45  }
46 
47  compute_common(elem, data);
48 
49  elem->update_keys();
50 
51  if (data.is_array() && elem->check_optional_keys("id") && do_sorting_by_id)
52  {
53  sort_by_id(data);
54  }
55 }
void sort_by_id(Json &data)
Definition: starter.cpp:17
ShPtr< Element > ElPtr
An alias for a shared pointer of Element class.
Definition: element.h:378
This file has a definition of Starter class, which is a subclass of Updater class.
nlohmann::json Json
Json is an alias for nlohmann::json.
Definition: json.h:22
std::vector< T > Vec
Vec is an alias for vector (same as list in Python).
Definition: std.h:27
static const bool do_sorting_by_id
Definition: starter.h:39
void runtime_error(const Str &msg)
Raise (for Python) and throw (for C++) a runtime error.
Namespace for utility functions.
Definition: join.h:14
virtual void compute_body(const ElPtr &elem, Json &data) override
Compute or parse properties and set them to Element::data given as a mutable reference.
Definition: starter.cpp:38
virtual void compute_common(const ElPtr &elem, Json &data)
Common part of compute_body.
Definition: updater.cpp:47
Map< Json, int > map_to_index(const Json &data, const Str &key)
Create a Map object from values of selected property in an array Json object to corresponding index i...