rivet is hosted by Hepforge, IPPP Durham
Rivet 4.0.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
20namespace Rivet {
21
22
24 // using namespace std;
25 using std::string;
26 using std::to_string;
27
28 using std::ifstream;
29 using std::ofstream;
30
31 using std::array;
32 using std::vector;
33 using std::list;
34 using std::set;
35 using std::multiset;
36 using std::map;
37 using std::multimap;
38 using std::pair;
39 using std::make_pair;
40
41 using std::unique_ptr;
42 using std::shared_ptr;
43 using std::make_shared;
44 using std::make_unique;
45 using std::dynamic_pointer_cast;
46
47 using std::initializer_list;
48
49 using std::function;
50
51 using std::isnan;
52
53 using std::complex;
54
59
61 template<typename T>
62 inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
63 os << "[ ";
64 for (size_t i=0; i<vec.size(); ++i) {
65 os << vec[i] << " ";
66 }
67 os << "]";
68 return os;
69 }
70
72 template<typename T>
73 inline std::ostream& operator<<(std::ostream& os, const std::list<T>& vec) {
74 os << "[ ";
75 for (size_t i=0; i<vec.size(); ++i) {
76 os << vec[i] << " ";
77 }
78 os << "]";
79 return os;
80 }
81
83
86 typedef vector<std::string> strings;
87 typedef vector<double> doubles;
88 typedef vector<float> floats;
89 typedef vector<int> ints;
91
94
96
98 inline bool contains(const std::string& s, const std::string& sub) {
99 return s.find(sub) != string::npos;
100 }
101
103 template <typename T>
104 inline bool contains(const std::initializer_list<T>& il, const T& x) {
105 return find(begin(il), end(il), x) != end(il);
106 }
107
109 template <typename T>
110 inline bool contains(const std::vector<T>& v, const T& x) {
111 return find(begin(v), end(v), x) != end(v);
112 }
113
115 template <typename T>
116 inline bool contains(const std::list<T>& l, const T& x) {
117 return find(begin(l), end(l), x) != end(l);
118 }
119
121 template <typename T>
122 inline bool contains(const std::set<T>& s, const T& x) {
123 return find(begin(s), end(s), x) != end(s);
124 }
125
127 template <typename K, typename T>
128 inline bool has_key(const std::map<K, T>& m, const K& key) {
129 return m.find(key) != end(m);
130 }
131
133 template <typename K, typename T>
134 inline bool has_value(const std::map<K, T>& m, const T& val) {
135 for (typename std::map<K,T>::const_iterator it = begin(m); it != end(m); ++it) {
136 if (it->second == val) return true;
137 }
138 return false;
139 }
140
142 template <typename K, typename T>
143 inline const T& retrieve(const std::map<K, T>& m, const K& key, const T& fallback) {
144 return has_key(m, key) ? m[key] : fallback;
145 }
146
148 template <typename K>
149 inline const std::string& retrieve(const std::map<K, std::string>& m, const K& key, const std::string& fallback) {
150 return has_key(m, key) ? m.find(key)->second : fallback;
151 }
152
154 template <typename T>
155 inline const T& retrieve(const std::map<std::string, T>& m, const std::string& key, const T& fallback) {
156 return has_key(m, key) ? m.find(key)->second : fallback;
157 }
158
160 inline const std::string& retrieve(const std::map<std::string, std::string>& m, const std::string& key, const std::string& fallback) {
161 return has_key(m, key) ? m.find(key)->second : fallback;
162 }
163
165
166
167}
168
169
170namespace std {
171
172
175
177 template <typename T>
178 inline void operator += (std::vector<T>& v, const T& x) { v.push_back(x); }
179
181 template <typename T>
182 inline void operator += (std::vector<T>& v1, const std::vector<T>& v2) {
183 for (const auto& x : v2) v1.push_back(x);
184 }
185
187 template <typename T>
188 inline std::vector<T> operator + (const std::vector<T>& v1, const std::vector<T>& v2) {
189 std::vector<T> rtn(v1);
190 rtn += v2;
191 return rtn;
192 }
193
194
196 template <typename T>
197 inline void operator += (std::set<T>& s1, const std::set<T>& s2) {
198 for (const auto& x : s2) s1.insert(x);
199 }
200
202 template <typename T>
203 inline std::set<T> operator + (const std::set<T>& s1, const std::set<T>& s2) {
204 std::set<T> rtn(s1);
205 rtn += s2;
206 return rtn;
207 }
208
210
211
214
216 template<typename T, typename... U>
217 inline uintptr_t get_address(std::function<T(U...)> f) {
218 typedef T(fnType)(U...);
219 fnType ** fnPointer = f.template target<fnType*>();
220 return (fnPointer != nullptr) ? reinterpret_cast<uintptr_t>(*fnPointer) : 0;
221 }
222
224
225
226}
227
228#endif
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:143
bool has_value(const std::map< K, T > &m, const T &val)
Does the map m contain the value val?
Definition RivetSTL.hh:134
std::ostream & operator<<(std::ostream &os, const AnalysisInfo &ai)
Stream an AnalysisInfo as a text description.
Definition AnalysisInfo.hh:362
bool contains(const std::string &s, const std::string &sub)
Does s contain sub as a substring?
Definition RivetSTL.hh:98
bool has_key(const std::map< K, T > &m, const K &key)
Does the map m contain the key key?
Definition RivetSTL.hh:128
STL namespace.
uintptr_t get_address(std::function< T(U...)> f)
Get a function pointer / hash integer from an std::function.
Definition RivetSTL.hh:217
void operator+=(std::vector< T > &v, const T &x)
Append a single item to vector v.
Definition RivetSTL.hh:178
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:188