1#ifndef RIVET_RivetSTL_HH
2#define RIVET_RivetSTL_HH
28 using namespace std::string_literals;
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;
49 using std::initializer_list;
65 inline std::ostream&
operator << (std::ostream& os,
const std::vector<T>& vec) {
67 for (
size_t i=0; i<vec.size(); ++i) {
76 inline std::ostream&
operator << (std::ostream& os,
const std::list<T>& vec) {
78 for (
size_t i=0; i<vec.size(); ++i) {
100 template <
typename T,
typename CONT>
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));
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>> >> >
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)));
144 template <
typename CONT>
149 template <
typename X>
155 template <
typename CONT>
161 template <
typename X>
168 template <
typename CONT>
169 inline typename std::enable_if_t<std::is_floating_point_v<typename CONT::value_type>,
vector<float> >
174 template <
typename X>
181 template <
typename CONT>
187 template <
typename X>
201 inline bool contains(
const std::string& s,
const std::string& sub) {
202 return s.find(sub) != string::npos;
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);
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);
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);
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);
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);
236 template <
typename K,
typename T>
237 inline bool has_value(
const std::map<K, T>& m,
const T& val) {
239 if (it->second == val)
return true;
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;
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;
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;
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;
280 template <
typename T>
281 inline void operator += (std::vector<T>& v,
const T& x) { v.push_back(x); }
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);
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);
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);
305 template <
typename T>
306 inline std::set<T>
operator + (
const std::set<T>& s1,
const std::set<T>& s2) {
319 template<
typename T,
typename... U>
321 typedef T(fnType)(U...);
322 fnType ** fnPointer = f.template target<fnType*>();
323 return (fnPointer !=
nullptr) ?
reinterpret_cast<uintptr_t
>(*fnPointer) : 0;
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
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