Vector.hh
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <boost/range/algorithm/find.hpp>
7 #include <boost/range/algorithm/min_element.hpp>
8 #include <boost/range/algorithm_ext/push_back.hpp>
9 #include <boost/range/algorithm/transform.hpp>
10 #include <boost/range/adaptors.hpp>
11 
12 namespace boca
13 {
14 
22 // For generic types, directly use the result of the signature of its 'operator()'
23 template <typename Function_>
24 struct function_traits : public function_traits<decltype(&Function_::operator())> {
25 };
26 
27 // we specialize for pointers to member function
28 template <typename Class_, typename Result_, typename... Arguments_>
29 struct function_traits<Result_(Class_::*)(Arguments_...) const> {
30  // arity is the number of arguments.
31  enum { arity = sizeof...(Arguments_) };
32  using result_type = Result_;
33 
34  template <std::size_t number>
35  struct arg {
36  // the i-th argument is equivalent to the i-th tuple element of a tuple composed of those arguments.
37  using type = typename std::tuple_element<number, std::tuple<Arguments_...>>::type;
38  };
39 };
40 
41 template <typename Input_, typename Function_>
42 auto Transform(Input_ const &inputs, Function_ function)
43 {
44  using Result_ = typename function_traits<decltype(function)>::result_type;
45  auto results = std::vector<Result_> {};
46  if (inputs.empty()) return results;
47  results.reserve(inputs.size());
48  boost::range::transform(inputs, std::back_inserter(results), function);
49 // boost::range::push_back(results, inputs | boost::adaptors::transformed(function));
50  return results;
51 }
52 
53 template<typename Result_, typename Input_, typename Function_>
54 std::vector<Result_> TransformIf(Input_ const &inputs, Function_ const &function_1, Function_ const &function_2)
55 {
56  auto results = std::vector<Result_> {};
57  if (inputs.empty()) return results;
58  boost::range::push_back(results, inputs | boost::adaptors::filtered(function_1) | boost::adaptors::transformed(function_2));
59  return results;
60 }
61 
62 template<typename Elements_, typename Function_>
63 std::vector<Elements_> CopyIf(std::vector<Elements_> const &inputs, Function_ function)
64 {
65  std::vector<Elements_> results;
66  return inputs.empty() ? results : boost::range::push_back(results, inputs | boost::adaptors::filtered(std::ref(function))); // http://stackoverflow.com/a/25907302
67 }
68 
69 template <typename Element_>
70 std::size_t Position(std::vector<Element_> const &vector, Element_ const &element)
71 {
72  return std::addressof(element) - vector.data();
73 }
74 
75 template <typename Element_, unsigned long size_>
76 std::size_t Position(std::array<Element_, size_> const &array, Element_ const &element)
77 {
78  return std::addressof(element) - array.data();
79 }
80 
81 
82 template <typename Element_>
83 bool FindInVector(const std::vector<Element_> vector, const Element_ element)
84 {
85  return boost::range::find(vector, element) != vector.end();
86 }
87 
88 template <typename Multiplet_1_, typename Multiplet_2_>
89 Multiplet_1_ ClosestJet(std::vector<Multiplet_1_> const &multiplets, Multiplet_2_ const &multiplet)
90 {
91  return *boost::range::min_element(multiplets, [&](Multiplet_1_ const & multiplet_1, Multiplet_1_ const & multiplet_2) {
92  return multiplet.DeltaRTo(multiplet_1) < multiplet.DeltaRTo(multiplet_2);
93  });
94 }
95 
100 template <typename Element_>
101 std::vector<Element_> Combine(std::vector<Element_> const &vector_1, std::vector<Element_> const &vector_2)
102 {
103  auto combined = std::vector<Element_> {};
104  combined.reserve(vector_1.size() + vector_2.size());
105  if (!vector_1.empty()) combined.insert(combined.end(), vector_1.begin(), vector_1.end());
106  if (!vector_2.empty()) combined.insert(combined.end(), vector_2.begin(), vector_2.end());
107  return combined;
108 }
109 
114 template <typename Element_>
115 std::vector<Element_> Combine(std::vector<Element_> const &vector_1, std::vector<Element_> const &vector_2, std::vector<Element_> const &vector_3)
116 {
117  auto combined = std::vector<Element_> {};
118  combined.reserve(vector_1.size() + vector_2.size() + vector_3.size());
119  if (!vector_1.empty()) combined.insert(combined.end(), vector_1.begin(), vector_1.end());
120  if (!vector_2.empty()) combined.insert(combined.end(), vector_2.begin(), vector_2.end());
121  if (!vector_3.empty()) combined.insert(combined.end(), vector_3.begin(), vector_3.end());
122  return combined;
123 }
124 
129 template <typename Element_>
130 void Insert(std::vector<Element_> &vector_1, std::vector<Element_> const &vector_2)
131 {
132  if (vector_2.empty()) return;
133  vector_1.reserve(vector_1.size() + vector_2.size());
134  vector_1.insert(vector_1.end(), vector_2.begin(), vector_2.end());
135 }
136 
141 template <typename Element_>
142 void Insert(std::vector<Element_> &vector_1, std::vector<Element_> const &vector_2, std::vector<Element_> const &vector_3)
143 {
144  vector_1.reserve(vector_1.size() + vector_2.size() + vector_3.size());
145  if (!vector_2.empty()) vector_1.insert(vector_1.end(), vector_2.begin(), vector_2.end());
146  if (!vector_3.empty()) vector_1.insert(vector_1.end(), vector_3.begin(), vector_3.end());
147 }
148 
153 template < typename Element_,
154  typename Function,
155  typename Result = typename std::result_of<Function&(Element_, Element_)>::type >
156 auto OrderedPairs(std::vector<Element_> const &container, Function function)
157 {
158  auto results = std::vector<Result> {};
159  for (auto element_1 = container.begin(); element_1 != container.end(); ++element_1) {
160  for (auto element_2 = std::next(element_1); element_2 != container.end(); ++element_2) {
161  try {
162  results.emplace_back(function(*element_1, *element_2));
163  } catch (std::exception const &) {}
164  try {
165  results.emplace_back(function(*element_2, *element_1));
166  } catch (std::exception const &) {}
167  }
168  }
169  return results;
170 }
171 
176 template < typename Element_, typename Function_, typename Result_ = typename std::result_of<Function_&(Element_, Element_)>::type >
177 auto UnorderedPairs(std::vector<Element_> const &container, Function_ function)
178 {
179  auto results = std::vector<Result_> {};
180  for (auto element_1 = container.begin(); element_1 != container.end(); ++element_1) {
181  for (auto element_2 = std::next(element_1); element_2 != container.end(); ++element_2)
182  try {
183  results.emplace_back(function(*element_1, *element_2));
184  } catch (std::exception const &) {}
185  }
186  return results;
187 }
188 
193 template < typename Element_1_, typename Element_2_, typename Function_, typename Result_ = typename std::result_of<Function_&(Element_1_, Element_2_)>::type >
194 auto Pairs(std::vector<Element_1_> const &container_1, std::vector<Element_2_> const &container_2, Function_ function)
195 {
196  auto results = std::vector<Result_> {};
197  for (auto const &element_1 : container_1) {
198  for (auto const &element_2 : container_2) {
199  try {
200  results.emplace_back(function(element_1, element_2));
201  } catch (std::exception const &) {}
202  }
203  }
204  return results;
205 }
206 
211 template < typename Element_1_,
212  typename Element_2_,
213  typename Function_,
214  typename Result_ = typename std::result_of<Function_&(Element_1_, Element_1_, Element_2_)>::type >
215 auto Triples(std::vector<Element_1_> const &container_1, std::vector<Element_2_> const &container_2, Function_ function)
216 {
217  auto results = std::vector<Result_> {};
218  for (auto element_1 = container_1.begin(); element_1 != container_1.end(); ++element_1) {
219  for (auto element_2 = std::next(element_1); element_2 != container_1.end(); ++element_2)
220  for (auto &element_3 : container_2) {
221  try {
222  results.emplace_back(function(*element_1, *element_2, element_3));
223  } catch (std::exception const &) {}
224  }
225  }
226  return results;
227 }
228 
233 template < typename Element_1_,
234  typename Element_2_,
235  typename Element_3_,
236  typename Function_,
237  typename Result_ = typename std::result_of<Function_&(Element_1_, Element_2_, Element_3_)>::type >
238 auto Triples(std::vector<Element_1_> const &container_1, std::vector<Element_2_> const &container_2, std::vector<Element_3_> const &container_3, Function_ function)
239 {
240  auto results = std::vector<Result_> {};
241  for (auto const &element_1 : container_1) {
242  for (auto const &element_2 : container_2) {
243  for (auto const &element_3 : container_3) {
244  try {
245  results.emplace_back(function(element_1, element_2, element_3));
246  } catch (std::exception const &) {}
247  }
248  }
249  }
250  return results;
251 }
252 
257 template < typename Element_1_,
258  typename Element_2_,
259  typename Element_3_,
260  typename Function_1_,
261  typename Function_2_,
262  typename Result_1_ = typename std::result_of<Function_1_&(Element_1_, Element_2_)>::type,
263  typename Result_2_ = typename std::result_of<Function_2_&(Result_1_, Element_3_)>::type >
264 auto Triples(std::vector<Element_1_> const &container_1, std::vector<Element_2_> const &container_2, std::vector<Element_3_> const &container_3, Function_1_ function_1, Function_2_ function_2)
265 {
266  auto results = std::vector<Result_2_> {};
267  for (auto const &element_1 : container_1) {
268  for (auto const &element_2 : container_2) {
269  try {
270  auto pair = function_1(element_1, element_2);
271  for (auto const &element_3 : container_3) {
272  try {
273  results.emplace_back(function_2(pair, element_3));
274  } catch (std::exception const &) {}
275  }
276  } catch (std::exception const &) {}
277  }
278  }
279  return results;
280 }
281 
282 // @}
283 
284 }
Definition: Vector.hh:24
std::size_t Position(std::vector< Element_ > const &vector, Element_ const &element)
Definition: Vector.hh:70
bool FindInVector(const std::vector< Element_ > vector, const Element_ element)
Definition: Vector.hh:83
auto Triples(std::vector< Element_1_ > const &container_1, std::vector< Element_2_ > const &container_2, Function_ function)
forms all triples, applies to them the function and returns a vector of its results ...
Definition: Vector.hh:215
typename std::tuple_element< number, std::tuple< Arguments_... >>::type type
Definition: Vector.hh:37
auto Pairs(std::vector< Element_1_ > const &container_1, std::vector< Element_2_ > const &container_2, Function_ function)
forms all pairs of the elements in the two containers, applies the function and returns a vector of ...
Definition: Vector.hh:194
auto UnorderedPairs(std::vector< Element_ > const &container, Function_ function)
forms all unordered pairs, applies to them the function and returns a vector of its results ...
Definition: Vector.hh:177
std::vector< Element_ > Combine(std::vector< Element_ > const &vector_1, std::vector< Element_ > const &vector_2)
Combine two std::vector.
Definition: Vector.hh:101
std::vector< Result_ > TransformIf(Input_ const &inputs, Function_ const &function_1, Function_ const &function_2)
Definition: Vector.hh:54
Boosted Collider Analysis.
Definition: Analysis.hh:15
Multiplet_1_ ClosestJet(std::vector< Multiplet_1_ > const &multiplets, Multiplet_2_ const &multiplet)
Definition: Vector.hh:89
void Insert(std::vector< Element_ > &vector_1, std::vector< Element_ > const &vector_2)
Insert two std::vector.
Definition: Vector.hh:130
auto Transform(Input_ const &inputs, Function_ function)
Definition: Vector.hh:42
auto OrderedPairs(std::vector< Element_ > const &container, Function function)
forms all ordered pairs of vector elements, applies to them the function and returns a vector of its...
Definition: Vector.hh:156
std::vector< Elements_ > CopyIf(std::vector< Elements_ > const &inputs, Function_ function)
Definition: Vector.hh:63
Definition: Result.hh:17