Rivet API documentation

Rivet 4.1.3
RivetSTL.hh
1#ifndef RIVET_RivetSTL_HH
2#define RIVET_RivetSTL_HH
3
4#include <string>
5#include <array>
6#include <vector>
7#include <list>
8#include <set>
9#include <map>
10#include <memory>
11#include <functional>
12#include <ostream>
13#include <fstream>
14#include <sstream>
15#include <cmath>
16#include <limits>
17#include <complex>
18#include <cstdint>
19#include <type_traits>
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::vector;
35 using std::list;
36 using std::set;
37 using std::multiset;
38 using std::map;
39 using std::multimap;
40 using std::pair;
41 using std::make_pair;
42
43 using std::unique_ptr;
44 using std::shared_ptr;
45 using std::make_shared;
46 using std::make_unique;
47 using std::dynamic_pointer_cast;
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; rtn.reserve(c.size());
103 for (const typename CONT::value_type& x : c) {
104 rtn.push_back(static_cast<T>(x)); //< check narrowing conversion?
105 }
106 return rtn;
107 }
108
109 // /// Convert an initializer list of X to a vector of T via static casting
110 // template <typename T, typename X>
111 // inline vector<T> mkVecOf(const std::initializer_list<X>& il) {
112 // return mkVecOf<T>(vector<X>(il));
113 // }
114
115
117 //
118 template <typename T, typename CONT, typename FN = std::string(*)(typename std::remove_reference_t<CONT>::value_type),
119 typename = std::enable_if_t<std::is_convertible_v<
120 T, std::decay_t<std::invoke_result_t<
121 FN, typename std::remove_reference_t<CONT>::value_type>> >> >
122 vector<T> mkVecOf(CONT&& c, FN&& fn) {
123 vector<T> rtn;
124 rtn.reserve(std::distance(std::begin(c), std::end(c)));
125 for (auto&& x : std::forward<CONT>(c)) {
126 rtn.push_back(std::forward<FN>(fn)(std::forward<decltype(x)>(x))); //< check narrowing conversion?
127 }
128 return rtn;
129 }
130
131 // template <typename CONT, typename FN = std::string(*)(typename std::remove_reference_t<CONT>::value_type),
132 // typename T = std::decay_t<std::invoke_result_t<FN, typename std::remove_reference_t<CONT>::value_type> > >
133 // vector<T> mkVecOf(CONT&& c, FN&& fn) {
134 // vector<T> rtn;
135 // rtn.reserve(std::distance(std::begin(c), std::end(c)));
136 // for (auto&& x : std::forward<CONT>(c)) {
137 // rtn.push_back(std::forward<FN>(fn)(std::forward<decltype(x)>(x)));
138 // }
139 // return rtn;
140 // }
141
142
144 template <typename CONT>
146 mkStrings(const CONT& c) { return mkVecOf<string>(c, to_string); }
147
149 template <typename X>
151 mkStrings(const std::initializer_list<X>& il) { return mkVecOf<string>(il, to_string); }
152
153
155 template <typename CONT>
156 //inline typename std::enable_if_t<std::is_floating_point_v<typename CONT::value_type>, vector<double> >
157 inline vector<double>
158 mkDoubles(const CONT& c) { return mkVecOf<double>(c); }
159
161 template <typename X>
162 //inline typename std::enable_if_t<std::is_floating_point_v<X>, vector<double> >
163 inline vector<double>
164 mkDoubles(const std::initializer_list<X>& il) { return mkVecOf<double>(il); }
165
166
168 template <typename CONT>
169 inline typename std::enable_if_t<std::is_floating_point_v<typename CONT::value_type>, vector<float> >
170 // inline vector<float>
171 mkFloats(const CONT& c) { return mkVecOf<float>(c); }
172
174 template <typename X>
175 // inline typename std::enable_if_t<std::is_floating_point_v<X>, vector<float> >
176 inline vector<float>
177 mkFloats(const std::initializer_list<X>& il) { return mkVecOf<float>(il); }
178
179
181 template <typename CONT>
182 //inline typename std::enable_if_t<std::is_arithmetic_v<typename CONT::value_type>, vector<int> >
183 inline vector<int>
184 mkInts(const CONT& c) { return mkVecOf<int>(c); }
185
187 template <typename X>
188 //inline typename std::enable_if_t<std::is_arithmetic_v<X>, vector<int> >
189 inline vector<int>
190 mkInts(const std::initializer_list<X>& il) { return mkVecOf<int>(il); }
191
193
194
197
199
201 inline bool contains(const std::string& s, const std::string& sub) {
202 return s.find(sub) != string::npos;
203 }
204
206 template <typename T>
207 inline bool contains(const std::initializer_list<T>& il, const T& x) {
208 return find(begin(il), end(il), x) != end(il);
209 }
210
212 template <typename T>
213 inline bool contains(const std::vector<T>& v, const T& x) {
214 return find(begin(v), end(v), x) != end(v);
215 }
216
218 template <typename T>
219 inline bool contains(const std::list<T>& l, const T& x) {
220 return find(begin(l), end(l), x) != end(l);
221 }
222
224 template <typename T>
225 inline bool contains(const std::set<T>& s, const T& x) {
226 return find(begin(s), end(s), x) != end(s);
227 }
228
230 template <typename K, typename T>
231 inline bool has_key(const std::map<K, T>& m, const K& key) {
232 return m.find(key) != end(m);
233 }
234
236 template <typename K, typename T>
237 inline bool has_value(const std::map<K, T>& m, const T& val) {
238 for (typename std::map<K,T>::const_iterator it = begin(m); it != end(m); ++it) {
239 if (it->second == val) return true;
240 }
241 return false;
242 }
243
245 template <typename K, typename T>
246 inline const T& retrieve(const std::map<K, T>& m, const K& key, const T& fallback) {
247 return has_key(m, key) ? m[key] : fallback;
248 }
249
251 template <typename K>
252 inline const std::string& retrieve(const std::map<K, std::string>& m, const K& key, const std::string& fallback) {
253 return has_key(m, key) ? m.find(key)->second : fallback;
254 }
255
257 template <typename T>
258 inline const T& retrieve(const std::map<std::string, T>& m, const std::string& key, const T& fallback) {
259 return has_key(m, key) ? m.find(key)->second : fallback;
260 }
261
263 inline const std::string& retrieve(const std::map<std::string, std::string>& m, const std::string& key, const std::string& fallback) {
264 return has_key(m, key) ? m.find(key)->second : fallback;
265 }
266
268
269
270}
271
272
273namespace std {
274
275
278
280 template <typename T>
281 inline void operator += (std::vector<T>& v, const T& x) { v.push_back(x); }
282
284 template <typename T>
285 inline void operator += (std::vector<T>& v1, const std::vector<T>& v2) {
286 for (const auto& x : v2) v1.push_back(x);
287 }
288
290 template <typename T>
291 inline std::vector<T> operator + (const std::vector<T>& v1, const std::vector<T>& v2) {
292 std::vector<T> rtn(v1);
293 rtn += v2;
294 return rtn;
295 }
296
297
299 template <typename T>
300 inline void operator += (std::set<T>& s1, const std::set<T>& s2) {
301 for (const auto& x : s2) s1.insert(x);
302 }
303
305 template <typename T>
306 inline std::set<T> operator + (const std::set<T>& s1, const std::set<T>& s2) {
307 std::set<T> rtn(s1);
308 rtn += s2;
309 return rtn;
310 }
311
313
314
317
319 template<typename T, typename... U>
320 inline uintptr_t get_address(std::function<T(U...)> f) {
321 typedef T(fnType)(U...);
322 fnType ** fnPointer = f.template target<fnType*>();
323 return (fnPointer != nullptr) ? reinterpret_cast<uintptr_t>(*fnPointer) : 0;
324 }
325
327
328
329}
330
331#endif
STL iterator class.
STL class.
Definition MC_CENT_PPB_Projections.hh:10
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:246
bool has_value(const std::map< K, T > &m, const T &val)
Does the map m contain the value val?
Definition RivetSTL.hh:237
vector< double > mkDoubles(const CONT &c)
Convert a container to a vector of doubles.
Definition RivetSTL.hh:158
std::ostream & operator<<(std::ostream &os, const AnalysisInfo &ai)
Stream an AnalysisInfo as a text description.
Definition AnalysisInfo.hh:362
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:171
vector< std::string > mkStrings(const CONT &c)
Convert a container to a vector of strings.
Definition RivetSTL.hh:146
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:201
bool has_key(const std::map< K, T > &m, const K &key)
Does the map m contain the key key?
Definition RivetSTL.hh:231
vector< int > mkInts(const CONT &c)
Convert a container to a vector of ints.
Definition RivetSTL.hh:184
STL namespace.
uintptr_t get_address(std::function< T(U...)> f)
Get a function pointer / hash integer from an std::function.
Definition RivetSTL.hh:320
void operator+=(std::vector< T > &v, const T &x)
Append a single item to vector v.
Definition RivetSTL.hh:281
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:291