rivet is hosted by Hepforge, IPPP Durham
RivetSTL.hh
Go to the documentation of this file.
00001 #ifndef RIVET_RivetSTL_HH
00002 #define RIVET_RivetSTL_HH
00003 
00004 #include <typeinfo>
00005 #include <set>
00006 #include <list>
00007 #include <map>
00008 #include <utility>
00009 #include <string>
00010 #include <sstream>
00011 #include <vector>
00012 #include <stdexcept>
00013 #include <iostream>
00014 #include <iomanip>
00015 #include <cmath>
00016 #include <limits>
00017 #include <cassert>
00018 #include <fstream>
00019 #include <algorithm>
00020 
00021 namespace Rivet {
00022 
00023   /// @name Convenient imports of common STL classes and functions
00024   //@{
00025 
00026   using std::set;
00027   using std::map;
00028   using std::multimap;
00029   using std::type_info;
00030   using std::string;
00031   using std::stringstream;
00032   using std::less;
00033   using std::list;
00034   using std::vector;
00035   using std::pair;
00036   using std::make_pair;
00037   using std::runtime_error;
00038   using std::min;
00039   using std::max;
00040   using std::abs;
00041   using std::numeric_limits;
00042   using std::ostream;
00043   using std::istream;
00044   using std::cout;
00045   using std::cin;
00046   using std::cerr;
00047   using std::setw;
00048   using std::pow;
00049   using std::endl;
00050 
00051   //@}
00052 
00053 
00054   /// @name Streaming containers as string reps
00055   /// @todo Make these named toStr rather than operator<<
00056   //@{
00057 
00058   /// Convenient function for streaming out vectors of any streamable object.
00059   template<typename T>
00060   inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
00061     os << "[ ";
00062     for (size_t i=0; i<vec.size(); ++i) {
00063       os << vec[i] << " ";
00064     }
00065     os << "]";
00066     return os;
00067   }
00068 
00069   /// Convenient function for streaming out lists of any streamable object.
00070   template<typename T>
00071   inline std::ostream& operator<<(std::ostream& os, const std::list<T>& vec) {
00072     os << "[ ";
00073     for (size_t i=0; i<vec.size(); ++i) {
00074       os << vec[i] << " ";
00075     }
00076     os << "]";
00077     return os;
00078   }
00079 
00080   //@}
00081 
00082 }
00083 
00084 \
00085 namespace std {
00086 
00087   /// @name Standard library enhancements
00088   /// @todo Merge in nice ideas from mili: https://code.google.com/p/mili/
00089   //@{
00090 
00091   /// @name Boolean-return container searching
00092   //@{
00093 
00094   /// Does @a s contain @a sub as a substring?
00095   inline bool contains(const string& s, const string& sub) {
00096     return s.find(sub) != string::npos;
00097   }
00098 
00099   /// Does the vector @a v contain @a x?
00100   template <typename T>
00101   inline bool contains(const vector<T>& v, const T& x) {
00102     return find(v.begin(), v.end(), x) != v.end();
00103   }
00104 
00105   /// Does the list @a l contain @a x?
00106   template <typename T>
00107   inline bool contains(const list<T>& l, const T& x) {
00108     return find(l.begin(), l.end(), x) != l.end();
00109   }
00110 
00111   /// Does the set @a s contain @a x?
00112   template <typename T>
00113   inline bool contains(const set<T>& s, const T& x) {
00114     return find(s.begin(), s.end(), x) != s.end();
00115   }
00116 
00117   /// Does the map @a m contain the key @a key?
00118   template <typename K, typename T>
00119   inline bool has_key(const map<K, T>& m, const K& key) {
00120     return m.find(key) != m.end();
00121   }
00122 
00123   /// Does the map @a m contain the value @a val?
00124   template <typename K, typename T>
00125   inline bool has_value(const map<K, T>& m, const T& val) {
00126     for (typename map<K,T>::const_iterator it = m.begin(); it != m.end(); ++it) {
00127       if (it->second == val) return true;
00128     }
00129     return false;
00130   }
00131 
00132   //@}
00133 
00134 
00135   /// @name Container filling and merging
00136   //@{
00137 
00138   /// Append all the items from vector @a v2 to vector @a v1
00139   template <typename T>
00140   inline void operator+=(vector<T>& v1, const vector<T>& v2) {
00141     for (typename vector<T>::const_iterator s = v2.begin(); s != v2.end(); ++s) {
00142       v1.push_back(*s);
00143     }
00144   }
00145 
00146   /// Create a new vector from the concatenated items in vectors @a v1 and @a v2
00147   template <typename T>
00148   inline vector<T> operator+(const vector<T>& v1, const vector<T>& v2) {
00149     vector<T> rtn(v1);
00150     rtn += v2;
00151     return rtn;
00152   }
00153 
00154 
00155   /// Merge the contents of set @a s2 into @a s1
00156   template <typename T>
00157   inline void operator+=(set<T>& s1, const set<T>& s2) {
00158     for (typename set<T>::const_iterator s = s2.begin(); s != s2.end(); ++s) {
00159       s1.insert(*s);
00160     }
00161   }
00162 
00163   /// Merge the contents of sets @a s1 and @a s2
00164   template <typename T>
00165   inline set<T> operator+(const set<T>& s1, const set<T>& s2) {
00166     set<T> rtn(s1);
00167     rtn += s2;
00168     return rtn;
00169   }
00170 
00171   //@}
00172 
00173   //@}
00174 
00175 
00176 }
00177 
00178 #endif