Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:06:54

0001 #ifndef RIVET_RivetSTL_HH
0002 #define RIVET_RivetSTL_HH
0003 
0004 #include <string>
0005 #include <array>
0006 #include <vector>
0007 #include <list>
0008 #include <set>
0009 #include <map>
0010 #include <memory>
0011 #include <functional>
0012 #include <ostream>
0013 #include <fstream>
0014 #include <sstream>
0015 #include <cmath>
0016 #include <limits>
0017 #include <complex>
0018 
0019 namespace Rivet {
0020 
0021 
0022   /// We implicitly use STL entities in the Rivet namespace
0023   // using namespace std;
0024   using std::string;
0025   using std::to_string;
0026 
0027   using std::ifstream;
0028   using std::ofstream;
0029 
0030   using std::array;
0031   using std::vector;
0032   using std::list;
0033   using std::set;
0034   using std::multiset;
0035   using std::map;
0036   using std::multimap;
0037   using std::pair;
0038   using std::make_pair;
0039 
0040   using std::unique_ptr;
0041   using std::shared_ptr;
0042   using std::make_shared;
0043   using std::make_unique;
0044   using std::dynamic_pointer_cast;
0045 
0046   using std::initializer_list;
0047 
0048   using std::function;
0049 
0050   using std::isnan;
0051 
0052   using std::complex;
0053 
0054   /// @name Streaming containers as string reps
0055   /// @todo Make these named toStr rather than operator<<
0056   /// @todo Make these generic to any iterable
0057   /// @{
0058 
0059   /// Convenient function for streaming out vectors of any streamable object.
0060   template<typename T>
0061   inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
0062     os << "[ ";
0063     for (size_t i=0; i<vec.size(); ++i) {
0064       os << vec[i] << " ";
0065     }
0066     os << "]";
0067     return os;
0068   }
0069 
0070   /// Convenient function for streaming out lists of any streamable object.
0071   template<typename T>
0072   inline std::ostream& operator<<(std::ostream& os, const std::list<T>& vec) {
0073     os << "[ ";
0074     for (size_t i=0; i<vec.size(); ++i) {
0075       os << vec[i] << " ";
0076     }
0077     os << "]";
0078     return os;
0079   }
0080 
0081   /// @}
0082 
0083   /// @name Convenience container typedefs
0084   /// @{
0085   typedef vector<std::string> strings;
0086   typedef vector<double> doubles;
0087   typedef vector<float> floats;
0088   typedef vector<int> ints;
0089   /// @}
0090 
0091   /// @name Boolean-return container searching
0092   /// @{
0093 
0094   /// @todo Use SFINAE, Boost.Range, or other template trickery for more generic container matching?
0095 
0096   /// Does @a s contain @a sub as a substring?
0097   inline bool contains(const std::string& s, const std::string& sub) {
0098     return s.find(sub) != string::npos;
0099   }
0100 
0101   /// Does the init list @a il contain @a x?
0102   template <typename T>
0103   inline bool contains(const std::initializer_list<T>& il, const T& x) {
0104     return find(begin(il), end(il), x) != end(il);
0105   }
0106 
0107   /// Does the vector @a v contain @a x?
0108   template <typename T>
0109   inline bool contains(const std::vector<T>& v, const T& x) {
0110     return find(begin(v), end(v), x) != end(v);
0111   }
0112 
0113   /// Does the list @a l contain @a x?
0114   template <typename T>
0115   inline bool contains(const std::list<T>& l, const T& x) {
0116     return find(begin(l), end(l), x) != end(l);
0117   }
0118 
0119   /// Does the set @a s contain @a x?
0120   template <typename T>
0121   inline bool contains(const std::set<T>& s, const T& x) {
0122     return find(begin(s), end(s), x) != end(s);
0123   }
0124 
0125   /// Does the map @a m contain the key @a key?
0126   template <typename K, typename T>
0127   inline bool has_key(const std::map<K, T>& m, const K& key) {
0128     return m.find(key) != end(m);
0129   }
0130 
0131   /// Does the map @a m contain the value @a val?
0132   template <typename K, typename T>
0133   inline bool has_value(const std::map<K, T>& m, const T& val) {
0134     for (typename std::map<K,T>::const_iterator it = begin(m); it != end(m); ++it) {
0135       if (it->second == val) return true;
0136     }
0137     return false;
0138   }
0139 
0140   /// Get the value in map @a m with key @a key, or fall back to @a fallback
0141   template <typename K, typename T>
0142   inline const T& retrieve(const std::map<K, T>& m, const K& key, const T& fallback) {
0143     return has_key(m, key) ? m[key] : fallback;
0144   }
0145 
0146   /// Get the value in map @a m with key @a key, or fall back to @a fallback (string-value specialisation)
0147   template <typename K>
0148   inline const std::string& retrieve(const std::map<K, std::string>& m, const K& key, const std::string& fallback) {
0149     return has_key(m, key) ? m.find(key)->second : fallback;
0150   }
0151 
0152   /// Get the value in map @a m with key @a key, or fall back to @a fallback (string-key specialisation)
0153   template <typename T>
0154   inline const T& retrieve(const std::map<std::string, T>& m, const std::string& key, const T& fallback) {
0155     return has_key(m, key) ? m.find(key)->second : fallback;
0156   }
0157 
0158   /// Get the value in map @a m with key @a key, or fall back to @a fallback (string-key+value specialisation)
0159   inline const std::string& retrieve(const std::map<std::string, std::string>& m, const std::string& key, const std::string& fallback) {
0160     return has_key(m, key) ? m.find(key)->second : fallback;
0161   }  
0162   
0163   /// @}
0164 
0165 
0166 }
0167 
0168 
0169 namespace std {
0170 
0171 
0172   /// @name Container filling and merging
0173   /// @{
0174 
0175   /// Append a single item to vector @a v
0176   template <typename T>
0177   inline void operator += (std::vector<T>& v, const T& x) { v.push_back(x); }
0178 
0179   /// Append all the items from vector @a v2 to vector @a v1
0180   template <typename T>
0181   inline void operator += (std::vector<T>& v1, const std::vector<T>& v2) {
0182     for (const auto& x : v2) v1.push_back(x);
0183   }
0184 
0185   /// Create a new vector from the concatenated items in vectors @a v1 and @a v2
0186   template <typename T>
0187   inline std::vector<T> operator + (const std::vector<T>& v1, const std::vector<T>& v2) {
0188     std::vector<T> rtn(v1);
0189     rtn += v2;
0190     return rtn;
0191   }
0192 
0193 
0194   /// Merge the contents of set @a s2 into @a s1
0195   template <typename T>
0196   inline void operator += (std::set<T>& s1, const std::set<T>& s2) {
0197     for (const auto& x : s2) s1.insert(x);
0198   }
0199 
0200   /// Merge the contents of sets @a s1 and @a s2
0201   template <typename T>
0202   inline std::set<T> operator + (const std::set<T>& s1, const std::set<T>& s2) {
0203     std::set<T> rtn(s1);
0204     rtn += s2;
0205     return rtn;
0206   }
0207 
0208   /// @}
0209 
0210 
0211   /// @name Function helpers
0212   /// @{
0213 
0214   /// Get a function pointer / hash integer from an std::function
0215   template<typename T, typename... U>
0216   inline uintptr_t get_address(std::function<T(U...)> f) {
0217     typedef T(fnType)(U...);
0218     fnType ** fnPointer = f.template target<fnType*>();
0219     return (fnPointer != nullptr) ? reinterpret_cast<uintptr_t>(*fnPointer) : 0;
0220   }
0221 
0222   /// @}
0223 
0224 
0225 }
0226 
0227 #endif