rivet is hosted by Hepforge, IPPP Durham
Rivet 4.0.0
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
19namespace Rivet {
20
21
23 // using namespace std;
24 using std::string;
25 using std::to_string;
26
27 using std::ifstream;
28 using std::ofstream;
29
30 using std::array;
31 using std::vector;
32 using std::list;
33 using std::set;
34 using std::multiset;
35 using std::map;
36 using std::multimap;
37 using std::pair;
38 using std::make_pair;
39
40 using std::unique_ptr;
41 using std::shared_ptr;
42 using std::make_shared;
43 using std::make_unique;
44 using std::dynamic_pointer_cast;
45
46 using std::initializer_list;
47
48 using std::function;
49
50 using std::isnan;
51
52 using std::complex;
53
58
60 template<typename T>
61 inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
62 os << "[ ";
63 for (size_t i=0; i<vec.size(); ++i) {
64 os << vec[i] << " ";
65 }
66 os << "]";
67 return os;
68 }
69
71 template<typename T>
72 inline std::ostream& operator<<(std::ostream& os, const std::list<T>& vec) {
73 os << "[ ";
74 for (size_t i=0; i<vec.size(); ++i) {
75 os << vec[i] << " ";
76 }
77 os << "]";
78 return os;
79 }
80
82
85 typedef vector<std::string> strings;
86 typedef vector<double> doubles;
87 typedef vector<float> floats;
88 typedef vector<int> ints;
90
93
95
97 inline bool contains(const std::string& s, const std::string& sub) {
98 return s.find(sub) != string::npos;
99 }
100
102 template <typename T>
103 inline bool contains(const std::initializer_list<T>& il, const T& x) {
104 return find(begin(il), end(il), x) != end(il);
105 }
106
108 template <typename T>
109 inline bool contains(const std::vector<T>& v, const T& x) {
110 return find(begin(v), end(v), x) != end(v);
111 }
112
114 template <typename T>
115 inline bool contains(const std::list<T>& l, const T& x) {
116 return find(begin(l), end(l), x) != end(l);
117 }
118
120 template <typename T>
121 inline bool contains(const std::set<T>& s, const T& x) {
122 return find(begin(s), end(s), x) != end(s);
123 }
124
126 template <typename K, typename T>
127 inline bool has_key(const std::map<K, T>& m, const K& key) {
128 return m.find(key) != end(m);
129 }
130
132 template <typename K, typename T>
133 inline bool has_value(const std::map<K, T>& m, const T& val) {
134 for (typename std::map<K,T>::const_iterator it = begin(m); it != end(m); ++it) {
135 if (it->second == val) return true;
136 }
137 return false;
138 }
139
141 template <typename K, typename T>
142 inline const T& retrieve(const std::map<K, T>& m, const K& key, const T& fallback) {
143 return has_key(m, key) ? m[key] : fallback;
144 }
145
147 template <typename K>
148 inline const std::string& retrieve(const std::map<K, std::string>& m, const K& key, const std::string& fallback) {
149 return has_key(m, key) ? m.find(key)->second : fallback;
150 }
151
153 template <typename T>
154 inline const T& retrieve(const std::map<std::string, T>& m, const std::string& key, const T& fallback) {
155 return has_key(m, key) ? m.find(key)->second : fallback;
156 }
157
159 inline const std::string& retrieve(const std::map<std::string, std::string>& m, const std::string& key, const std::string& fallback) {
160 return has_key(m, key) ? m.find(key)->second : fallback;
161 }
162
164
165
166}
167
168
169namespace std {
170
171
174
176 template <typename T>
177 inline void operator += (std::vector<T>& v, const T& x) { v.push_back(x); }
178
180 template <typename T>
181 inline void operator += (std::vector<T>& v1, const std::vector<T>& v2) {
182 for (const auto& x : v2) v1.push_back(x);
183 }
184
186 template <typename T>
187 inline std::vector<T> operator + (const std::vector<T>& v1, const std::vector<T>& v2) {
188 std::vector<T> rtn(v1);
189 rtn += v2;
190 return rtn;
191 }
192
193
195 template <typename T>
196 inline void operator += (std::set<T>& s1, const std::set<T>& s2) {
197 for (const auto& x : s2) s1.insert(x);
198 }
199
201 template <typename T>
202 inline std::set<T> operator + (const std::set<T>& s1, const std::set<T>& s2) {
203 std::set<T> rtn(s1);
204 rtn += s2;
205 return rtn;
206 }
207
209
210
213
215 template<typename T, typename... U>
216 inline uintptr_t get_address(std::function<T(U...)> f) {
217 typedef T(fnType)(U...);
218 fnType ** fnPointer = f.template target<fnType*>();
219 return (fnPointer != nullptr) ? reinterpret_cast<uintptr_t>(*fnPointer) : 0;
220 }
221
223
224
225}
226
227#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:142
bool has_value(const std::map< K, T > &m, const T &val)
Does the map m contain the value val?
Definition RivetSTL.hh:133
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:97
bool has_key(const std::map< K, T > &m, const K &key)
Does the map m contain the key key?
Definition RivetSTL.hh:127
STL namespace.
uintptr_t get_address(std::function< T(U...)> f)
Get a function pointer / hash integer from an std::function.
Definition RivetSTL.hh:216
void operator+=(std::vector< T > &v, const T &x)
Append a single item to vector v.
Definition RivetSTL.hh:177
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:187