ppap4lmp  0.7.2
fil_comparison.cpp
Go to the documentation of this file.
1 
9 #include "fil_comparison.h"
10 #include "../utils/runtime_error.h"
11 
12 namespace ut = utils;
13 
14 /* ------------------------------------------------------------------ */
15 
17  const std::tuple<Str,Str,Json> &compare_expr_)
18 {
19  compare_expr_list = {compare_expr_};
20 }
21 
22 /* ------------------------------------------------------------------ */
23 
25  const Vec<std::tuple<Str,Str,Json>> &compare_expr_list_)
26 {
27  compare_expr_list = compare_expr_list_;
28 }
29 
30 /* ------------------------------------------------------------------ */
31 
33  const Str &oper,
34  const Json &rval)
35 {
36  if (oper == "<")
37  {
38  return [rval](const Json &j) {
39  return j < rval;
40  };
41  }
42  else if (oper == ">")
43  {
44  return [rval](const Json &j) {
45  return j > rval;
46  };
47  }
48  else if (oper == "<=")
49  {
50  return [rval](const Json &j) {
51  return j <= rval;
52  };
53  }
54  else if (oper == ">=")
55  {
56  return [rval](const Json &j) {
57  return j >= rval;
58  };
59  }
60  else if (oper == "==")
61  {
62  return [rval](const Json &j) {
63  return j == rval;
64  };
65  }
66  else if (oper == "!=")
67  {
68  return [rval](const Json &j) {
69  return j != rval;
70  };
71  }
72  else
73  {
75  "Supported operators for comparison are "
76  "'<', '>', '<=', '>=', '==' and '!='");
77  }
78 }
79 
80 /* ------------------------------------------------------------------ */
81 
83 {
84  CompareFuncs compare_func_list;
85 
86  for (const auto &item : compare_expr_list)
87  {
88  auto &datakey = std::get<0>(item);
89  auto &compare_oper = std::get<1>(item);
90  auto &target_val = std::get<2>(item);
91 
92  compare_func_list.push_back(std::make_pair(
93  datakey, make_compare_func(compare_oper, target_val)));
94  }
95 
96  return compare_func_list;
97 }
98 
99 /* ------------------------------------------------------------------ */
100 
102  const Json &elem_in_data,
103  const CompareFuncs &compare_func_list)
104 {
105  for (const auto &item : compare_func_list)
106  {
107  if (!item.second(elem_in_data[item.first])) return false;
108  }
109 
110  return true;
111 }
112 
113 /* ------------------------------------------------------------------ */
114 
116  Json &data,
117  JsonToVoidFunc check_required_keys,
118  JsonToBoolFunc check_optional_keys)
119 {
120  const auto compare_func_list = make_compare_func_list();
121 
122  for (const auto &item : compare_func_list)
123  {
124  check_required_keys(item.first);
125  }
126 
127  Json passing_data = Json::array();
128 
129  for (const auto &d : data)
130  {
131  if (check_if_pass_data_elem(d, compare_func_list))
132  {
133  passing_data.push_back(d);
134  }
135  }
136 
137  data.swap(passing_data);
138 }
FilComparison(const std::tuple< Str, Str, Json > &compare_expr_)
Constructor of FilComparison class with one criterion.
virtual void compute_impl(Json &data, JsonToVoidFunc check_required_keys, JsonToBoolFunc check_optional_keys) override
This method overrides Updater::compute_impl.
This file has a definition of FilComparison class, which is a subclass of Filter class.
std::function< bool(const Json &)> JsonToBoolFunc
An alias for a function accepts a Json object and returns a bool.
Definition: updater.h:20
std::string Str
Str is an alias for string.
Definition: std.h:21
const JsonToBoolFunc make_compare_func(const Str &oper, const Json &rval)
Make a JsonToBoolFunc object from a comparison operator and a right side value.
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
const CompareFuncs make_compare_func_list()
Convert tuples in compare_expr_list to pairs of a string key and JsonToBoolFunc.
const bool check_if_pass_data_elem(const Json &elem_in_data, const CompareFuncs &compare_func_list)
Check if an element in data array can pass this filter.
Vec< std::tuple< Str, Str, Json > > compare_expr_list
void runtime_error(const Str &msg)
Raise (for Python) and throw (for C++) a runtime error.
Namespace for utility functions.
Definition: join.h:14
std::function< void(const Json &)> JsonToVoidFunc
An alias for a function accepts a Json object.
Definition: updater.h:18
Vec< std::pair< Str, JsonToBoolFunc > > CompareFuncs
An alias for Vec containing comparing functions.