Rivet API documentation

Rivet 4.1.3
RivetSTL.hh
1#ifndef RIVET_RivetSTL_HH
2#define RIVET_RivetSTL_HH
3
4#include <array>
5#include <cmath>
6#include <complex>
7#include <cstdint>
8#include <fstream>
9#include <functional>
10#include <limits>
11#include <list>
12#include <map>
13#include <memory>
14#include <ostream>
15#include <set>
16#include <sstream>
17#include <string>
18#include <type_traits>
19#include <vector>
20
21namespace Rivet {
22
23
25 // using namespace std;
26 using std::string;
27 using std::to_string;
28 using namespace std::string_literals;
29
30 using std::ifstream;
31 using std::ofstream;
32
33 using std::array;
34 using std::list;
35 using std::make_pair;
36 using std::map;
37 using std::multimap;
38 using std::multiset;
39 using std::pair;
40 using std::set;
41 using std::vector;
42
43 using std::dynamic_pointer_cast;
44 using std::make_shared;
45 using std::make_unique;
46 using std::shared_ptr;
47 using std::unique_ptr;
48
49 using std::initializer_list;
50
51 using std::function;
52
53 using std::isnan;
54
55 using std::complex;
56
62
64 template <typename T>
65 inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
66 os << "[ ";
67 for (size_t i = 0; i < vec.size(); ++i) {
68 os << vec[i] << " ";
69 }
70 os << "]";
71 return os;
72 }
73
75 template <typename T>
76 inline std::ostream& operator<<(std::ostream& os, const std::list<T>& vec) {
77 os << "[ ";
78 for (size_t i = 0; i < vec.size(); ++i) {
79 os << vec[i] << " ";
80 }
81 os << "]";
82 return os;
83 }
84
86
89 typedef vector<std::string> strings;
90 typedef vector<double> doubles;
91 typedef vector<float> floats;
92 typedef vector<int> ints;
94
95
98
100 template <typename T, typename CONT>
101 inline vector<T> mkVecOf(const CONT& c) {
102 vector<T> rtn;
103 rtn.reserve(c.size());
104 for (const typename CONT::value_type& x : c) {
105 rtn.push_back(static_cast<T>(x)); //< check narrowing conversion?
106 }
107 return rtn;
108 }
109
110 // /// Convert an initializer list of X to a vector of T via static casting
111 // template <typename T, typename X>
112 // inline vector<T> mkVecOf(const std::initializer_list<X>& il) {
113 // return mkVecOf<T>(vector<X>(il));
114 // }
115
116
118 //
119 template <typename T,
120 typename CONT,
121 typename FN = std::string (*)(typename std::remove_reference_t<CONT>::value_type),
122 typename = std::enable_if_t<std::is_convertible_v<
123 T,
124 std::decay_t<std::invoke_result_t<FN, typename std::remove_reference_t<CONT>::value_type>>>>>
125 vector<T> mkVecOf(CONT&& c, FN&& fn) {
126 vector<T> rtn;
127 rtn.reserve(std::distance(std::begin(c), std::end(c)));
128 for (auto&& x : std::forward<CONT>(c)) {
129 rtn.push_back(std::forward<FN>(fn)(std::forward<decltype(x)>(x))); //< check narrowing conversion?
130 }
131 return rtn;
132 }
133
134 // template <typename CONT, typename FN = std::string(*)(typename std::remove_reference_t<CONT>::value_type),
135 // typename T = std::decay_t<std::invoke_result_t<FN, typename std::remove_reference_t<CONT>::value_type> > >
136 // vector<T> mkVecOf(CONT&& c, FN&& fn) {
137 // vector<T> rtn;
138 // rtn.reserve(std::distance(std::begin(c), std::end(c)));
139 // for (auto&& x : std::forward<CONT>(c)) {
140 // rtn.push_back(std::forward<FN>(fn)(std::forward<decltype(x)>(x)));
141 // }
142 // return rtn;
143 // }
144
145
147 template <typename CONT>
148 inline vector<std::string> mkStrings(const CONT& c) {
149 return mkVecOf<string>(c, to_string);
150 }
151
153 template <typename X>
154 inline vector<std::string> mkStrings(const std::initializer_list<X>& il) {
155 return mkVecOf<string>(il, to_string);
156 }
157
158
160 template <typename CONT>
161 //inline typename std::enable_if_t<std::is_floating_point_v<typename CONT::value_type>, vector<double> >
162 inline vector<double> mkDoubles(const CONT& c) {
163 return mkVecOf<double>(c);
164 }
165
167 template <typename X>
168 //inline typename std::enable_if_t<std::is_floating_point_v<X>, vector<double> >
169 inline vector<double> mkDoubles(const std::initializer_list<X>& il) {
170 return mkVecOf<double>(il);
171 }
172
173
175 template <typename CONT>
176 inline typename std::enable_if_t<std::is_floating_point_v<typename CONT::value_type>, vector<float>>
177 // inline vector<float>
178 mkFloats(const CONT& c) {
179 return mkVecOf<float>(c);
180 }
181
183 template <typename X>
184 // inline typename std::enable_if_t<std::is_floating_point_v<X>, vector<float> >
185 inline vector<float> mkFloats(const std::initializer_list<X>& il) {
186 return mkVecOf<float>(il);
187 }
188
189
191 template <typename CONT>
192 //inline typename std::enable_if_t<std::is_arithmetic_v<typename CONT::value_type>, vector<int> >
193 inline vector<int> mkInts(const CONT& c) {
194 return mkVecOf<int>(c);
195 }
196
198 template <typename X>
199 //inline typename std::enable_if_t<std::is_arithmetic_v<X>, vector<int> >
200 inline vector<int> mkInts(const std::initializer_list<X>& il) {
201 return mkVecOf<int>(il);
202 }
203
205
206
209
211
213 inline bool contains(const std::string& s, const std::string& sub) {
214 return s.find(sub) != string::npos;
215 }
216
218 template <typename T>
219 inline bool contains(const std::initializer_list<T>& il, const T& x) {
220 return find(begin(il), end(il), x) != end(il);
221 }
222
224 template <typename T>
225 inline bool contains(const std::vector<T>& v, const T& x) {
226 return find(begin(v), end(v), x) != end(v);
227 }
228
230 template <typename T>
231 inline bool contains(const std::list<T>& l, const T& x) {
232 return find(begin(l), end(l), x) != end(l);
233 }
234
236 template <typename T>
237 inline bool contains(const std::set<T>& s, const T& x) {
238 return find(begin(s), end(s), x) != end(s);
239 }
240
242 template <typename K, typename T>
243 inline bool has_key(const std::map<K, T>& m, const K& key) {
244 return m.find(key) != end(m);
245 }
246
248 template <typename K, typename T>
249 inline bool has_value(const std::map<K, T>& m, const T& val) {
250 for (typename std::map<K, T>::const_iterator it = begin(m); it != end(m); ++it) {
251 if (it->second == val) return true;
252 }
253 return false;
254 }
255
257 template <typename K, typename T>
258 inline const T& retrieve(const std::map<K, T>& m, const K& key, const T& fallback) {
259 return has_key(m, key) ? m[key] : fallback;
260 }
261
263 template <typename K>
264 inline const std::string& retrieve(const std::map<K, std::string>& m,
265 const K& key,
266 const std::string& fallback) {
267 return has_key(m, key) ? m.find(key)->second : fallback;
268 }
269
271 template <typename T>
272 inline const T& retrieve(const std::map<std::string, T>& m, const std::string& key, const T& fallback) {
273 return has_key(m, key) ? m.find(key)->second : fallback;
274 }
275
277 inline const std::string& retrieve(const std::map<std::string, std::string>& m,
278 const std::string& key,
279 const std::string& fallback) {
280 return has_key(m, key) ? m.find(key)->second : fallback;
281 }
282
284
285
286}
287
288
289namespace std {
290
291
294
296 template <typename T>
297 inline void operator+=(std::vector<T>& v, const T& x) {
298 v.push_back(x);
299 }
300
302 template <typename T>
303 inline void operator+=(std::vector<T>& v1, const std::vector<T>& v2) {
304 for (const auto& x : v2) v1.push_back(x);
305 }
306
308 template <typename T>
309 inline std::vector<T> operator+(const std::vector<T>& v1, const std::vector<T>& v2) {
310 std::vector<T> rtn(v1);
311 rtn += v2;
312 return rtn;
313 }
314
315
317 template <typename T>
318 inline void operator+=(std::set<T>& s1, const std::set<T>& s2) {
319 for (const auto& x : s2) s1.insert(x);
320 }
321
323 template <typename T>
324 inline std::set<T> operator+(const std::set<T>& s1, const std::set<T>& s2) {
325 std::set<T> rtn(s1);
326 rtn += s2;
327 return rtn;
328 }
329
331
332
335
337 template <typename T, typename... U>
338 inline uintptr_t get_address(std::function<T(U...)> f) {
339 typedef T(fnType)(U...);
340 fnType** fnPointer = f.template target<fnType*>();
341 return (fnPointer != nullptr) ? reinterpret_cast<uintptr_t>(*fnPointer) : 0;
342 }
343
345
346
347}
348
349#endif
STL iterator class.
STL class.
Definition LHCbCommon.hh:9
const T & retrieve(const std::map< K, T > &m, const K &key, const T &fallback)
Get the value in map m with key key, or fall back to fallback.
Definition RivetSTL.hh:258
bool has_value(const std::map< K, T > &m, const T &val)
Does the map m contain the value val?
Definition RivetSTL.hh:249
vector< double > mkDoubles(const CONT &c)
Convert a container to a vector of doubles.
Definition RivetSTL.hh:162
std::ostream & operator<<(std::ostream &os, const AnalysisInfo &ai)
Stream an AnalysisInfo as a text description.
Definition AnalysisInfo.hh:463
std::enable_if_t< std::is_floating_point_v< typename CONT::value_type >, vector< float > > mkFloats(const CONT &c)
Convert a container to a vector of floats.
Definition RivetSTL.hh:178
vector< std::string > mkStrings(const CONT &c)
Convert a container to a vector of strings.
Definition RivetSTL.hh:148
vector< T > mkVecOf(const CONT &c)
Convert a container CONT to a vector of T via static casting.
Definition RivetSTL.hh:101
bool contains(const std::string &s, const std::string &sub)
Does s contain sub as a substring?
Definition RivetSTL.hh:213
bool has_key(const std::map< K, T > &m, const K &key)
Does the map m contain the key key?
Definition RivetSTL.hh:243
vector< int > mkInts(const CONT &c)
Convert a container to a vector of ints.
Definition RivetSTL.hh:193
STL namespace.
uintptr_t get_address(std::function< T(U...)> f)
Get a function pointer / hash integer from an std::function.
Definition RivetSTL.hh:338
void operator+=(std::vector< T > &v, const T &x)
Append a single item to vector v.
Definition RivetSTL.hh:297
std::vector< T > operator+(const std::vector< T > &v1, const std::vector< T > &v2)
Create a new vector from the concatenated items in vectors v1 and v2.
Definition RivetSTL.hh:309