Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-23 08:35:27

0001 // -*- C++ -*-
0002 #ifndef RIVET_Utils_HH
0003 #define RIVET_Utils_HH
0004 
0005 #include "Rivet/Tools/RivetSTL.hh"
0006 #include "Rivet/Tools/TypeTraits.hh"
0007 #include "Rivet/Tools/PrettyPrint.hh"
0008 #include "Rivet/Tools/Exceptions.hh"
0009 #include <ostream>
0010 #include <cctype>
0011 #include <cerrno>
0012 #include <stdexcept>
0013 #include <numeric>
0014 #include <limits>
0015 #include <climits>
0016 #include <cfloat>
0017 #include <cmath>
0018 #include <sstream>
0019 #include <functional>
0020 
0021 /// @defgroup utils Other utilities
0022 
0023 
0024 /// Macro to help mark code as deprecated to produce compiler warnings
0025 #ifndef DEPRECATED
0026 #if __GNUC__ && __cplusplus && RIVET_NO_DEPRECATION_WARNINGS == 0
0027   #define DEPRECATED(x) __attribute__((deprecated(x)))
0028 #else
0029   #define DEPRECATED(x)
0030 #endif
0031 #endif
0032 
0033 
0034 namespace Rivet {
0035 
0036 
0037   /// Convenient const for getting the double NaN value
0038   static constexpr double DBL_NAN = std::numeric_limits<double>::quiet_NaN();
0039 
0040 
0041   /// @defgroup strutils String utils
0042   /// @{
0043 
0044   /// @brief Exception class for throwing from lexical_cast when a parse goes wrong
0045   struct bad_lexical_cast : public std::runtime_error {
0046     bad_lexical_cast(const std::string& what) : std::runtime_error(what) {}
0047   };
0048 
0049   /// @brief Convert between any types via stringstream
0050   template<typename T, typename U>
0051   T lexical_cast(const U& in) {
0052     try {
0053       std::stringstream ss;
0054       ss << in;
0055       T out;
0056       ss >> out;
0057       return out;
0058     } catch (const std::exception& e) {
0059       throw bad_lexical_cast(e.what());
0060     }
0061   }
0062 
0063   /// @brief Convert any object to a string
0064   ///
0065   /// Just a convenience wrapper for the more general Boost lexical_cast
0066   template <typename T>
0067   inline string to_str(const T& x) {
0068     return lexical_cast<string>(x);
0069   }
0070 
0071   /// @brief Convert any object to a string
0072   ///
0073   /// An alias for to_str() with a more "Rivety" mixedCase name.
0074   template <typename T>
0075   inline string toString(const T& x) {
0076     return lexical_cast<string>(x);
0077   }
0078 
0079   /// Replace the first instance of patt with repl
0080   inline string& replace_first(string& str, const string& patt, const string& repl) {
0081     if (!contains(str, patt)) return str; //< contains from RivetSTL
0082     str.replace(str.find(patt), patt.size(), repl);
0083     return str;
0084   }
0085 
0086   /// @brief Replace all instances of patt with repl
0087   ///
0088   /// @note Finding is interleaved with replacement, so the second search happens after
0089   /// first replacement, etc. This could lead to infinite loops and other counterintuitive
0090   /// behaviours if not careful.
0091   inline string& replace_all(string& str, const string& patt, const string& repl) {
0092     if (!contains(str, patt)) return str; //< contains from RivetSTL
0093     while (true) {
0094       string::size_type it = str.find(patt);
0095       if (it == string::npos) break;
0096       str.replace(it, patt.size(), repl);
0097     }
0098     return str;
0099   }
0100 
0101 
0102   /// Case-insensitive string comparison function
0103   inline int nocase_cmp(const string& s1, const string& s2) {
0104     string::const_iterator it1 = s1.begin();
0105     string::const_iterator it2 = s2.begin();
0106     while ( (it1 != s1.end()) && (it2 != s2.end()) ) {
0107       if(::toupper(*it1) != ::toupper(*it2)) { // < Letters differ?
0108         // Return -1 to indicate smaller than, 1 otherwise
0109         return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1;
0110       }
0111       // Proceed to the next character in each string
0112       ++it1;
0113       ++it2;
0114     }
0115     size_t size1 = s1.size(), size2 = s2.size(); // Cache lengths
0116     // Return -1,0 or 1 according to strings' lengths
0117     if (size1 == size2) return 0;
0118     return (size1 < size2) ? -1 : 1;
0119   }
0120 
0121 
0122   /// Case-insensitive string equality function
0123   inline bool nocase_equals(const string& s1, const string& s2) {
0124     return nocase_cmp(s1, s2) == 0;
0125   }
0126 
0127 
0128   /// Convert a string to lower-case
0129   inline string toLower(const string& s) {
0130     string out = s;
0131     std::transform(out.begin(), out.end(), out.begin(), (int(*)(int)) std::tolower);
0132     return out;
0133   }
0134 
0135 
0136   /// Convert a string to upper-case
0137   inline string toUpper(const string& s) {
0138     string out = s;
0139     std::transform(out.begin(), out.end(), out.begin(), (int(*)(int)) std::toupper);
0140     return out;
0141   }
0142 
0143 
0144   /// Check whether a string @a start is found at the start of @a s
0145   inline bool startsWith(const string& s, const string& start) {
0146     if (s.length() < start.length()) return false;
0147     return s.substr(0, start.length()) == start;
0148   }
0149 
0150 
0151   /// Check whether a string @a end is found at the end of @a s
0152   inline bool endsWith(const string& s, const string& end) {
0153     if (s.length() < end.length()) return false;
0154     return s.substr(s.length() - end.length()) == end;
0155   }
0156 
0157 
0158   // Terminating version of strjoin, for empty fargs list
0159   inline string strcat() { return ""; }
0160   /// Make a string containing the concatenated string representations of each item in the variadic list
0161   template<typename T, typename... Ts>
0162   inline string strcat(T value, Ts... fargs) {
0163     const string strthis = lexical_cast<string>(value);
0164     const string strnext = strcat(fargs...);
0165     return strnext.empty() ? strthis : strthis + strnext;
0166   }
0167 
0168 
0169   /// Make a string containing the string representations of each item in v, separated by sep
0170   template <typename T>
0171   inline string join(const vector<T>& v, const string& sep=" ") {
0172     string rtn;
0173     for (size_t i = 0; i < v.size(); ++i) {
0174       if (i != 0) rtn += sep;
0175       rtn += to_str(v[i]);
0176     }
0177     return rtn;
0178   }
0179 
0180   /// Make a string containing the string representations of each item in v, separated by sep
0181   template <>
0182   inline string join(const vector<string>& v, const string& sep) {
0183     string rtn;
0184     for (size_t i = 0; i < v.size(); ++i) {
0185       if (i != 0) rtn += sep;
0186       rtn += v[i];
0187     }
0188     return rtn;
0189   }
0190 
0191   /// Make a string containing the string representations of each item in s, separated by sep
0192   template <typename T>
0193   inline string join(const set<T>& s, const string& sep=" ") {
0194     string rtn;
0195     for (const T& x : s) {
0196       if (rtn.size() > 0) rtn += sep;
0197       rtn += to_str(x);
0198     }
0199     return rtn;
0200   }
0201 
0202   /// Make a string containing the string representations of each item in s, separated by sep
0203   template <>
0204   inline string join(const set<string>& s, const string& sep) {
0205     string rtn;
0206     for (const string & x : s) {
0207       if (rtn.size() > 0) rtn += sep;
0208       rtn += x;
0209     }
0210     return rtn;
0211   }
0212 
0213   /// @brief Split a string on a specified separator string
0214   inline vector<string> split(const string& s, const string& sep) {
0215     vector<string> dirs;
0216     string tmp = s;
0217     while (true) {
0218       const size_t delim_pos = tmp.find(sep);
0219       if (delim_pos == string::npos) break;
0220       const string dir = tmp.substr(0, delim_pos);
0221       if (dir.length()) dirs.push_back(dir); // Don't insert "empties"
0222       tmp.replace(0, delim_pos+1, "");
0223     }
0224     if (tmp.length()) dirs.push_back(tmp); // Don't forget the trailing component!
0225     return dirs;
0226   }
0227 
0228   /// Left-pad the given string @a s to width @a width
0229   inline string lpad(const string& s, size_t width, const string& padchar=" ") {
0230     if (s.size() >= width) return s;
0231     return string(width - s.size(), padchar[0]) + s;
0232   }
0233 
0234   /// Right-pad the given string @a s to width @a width
0235   inline string rpad(const string& s, size_t width, const string& padchar=" ") {
0236     if (s.size() >= width) return s;
0237     return s + string(width - s.size(), padchar[0]);
0238   }
0239 
0240   /// @}
0241 
0242 
0243 
0244   /// @defgroup pathutils Path utils
0245   /// @{
0246 
0247   /// @brief Split a path string with colon delimiters
0248   ///
0249   /// Ignores zero-length substrings. Designed for getting elements of filesystem paths, naturally.
0250   inline vector<string> pathsplit(const string& path) {
0251     return split(path, ":");
0252   }
0253 
0254   /// @brief Join several filesystem paths together with the standard ':' delimiter
0255   ///
0256   /// Note that this does NOT join path elements together with a platform-portable
0257   /// directory delimiter, cf. the Python @c {os.path.join}!
0258   inline string pathjoin(const vector<string>& paths) {
0259     return join(paths, ":");
0260   }
0261 
0262   /// Operator for joining strings @a a and @a b with filesystem separators
0263   inline string operator / (const string& a, const string& b) {
0264     // Ensure that a doesn't end with a slash, and b doesn't start with one, to avoid "//"
0265     const string anorm = (a.find("/") != string::npos) ? a.substr(0, a.find_last_not_of("/")+1) : a;
0266     const string bnorm = (b.find("/") != string::npos) ? b.substr(b.find_first_not_of("/")) : b;
0267     return anorm + "/" + bnorm;
0268   }
0269 
0270   /// Get the basename (i.e. terminal file name) from a path @a p
0271   inline string basename(const string& p) {
0272     if (!contains(p, "/")) return p;
0273     return p.substr(p.rfind("/")+1);
0274   }
0275 
0276   /// Get the dirname (i.e. path to the penultimate directory) from a path @a p
0277   inline string dirname(const string& p) {
0278     if (!contains(p, "/")) return "";
0279     return p.substr(0, p.rfind("/"));
0280   }
0281 
0282   /// Get the stem (i.e. part without a file extension) from a filename @a f
0283   inline string file_stem(const string& f) {
0284     if (!contains(f, ".")) return f;
0285     return f.substr(0, f.rfind("."));
0286   }
0287 
0288   /// Get the file extension from a filename @a f
0289   inline string file_extn(const string& f) {
0290     if (!contains(f, ".")) return "";
0291     return f.substr(f.rfind(".")+1);
0292   }
0293 
0294   /// @}
0295 
0296 
0297 
0298   /// @defgroup contutils Container utils
0299   /// @{
0300 
0301   /// Return number of true elements in the container @a c .
0302   template <typename CONTAINER,
0303             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0304   inline unsigned int count(const CONTAINER& c) {
0305     // return std::count_if(std::begin(c), std::end(c), [](const typename CONTAINER::value_type& x){return bool(x);});
0306     unsigned int rtn = 0;
0307     for (const auto& x : c) if (bool(x)) rtn += 1;
0308     return rtn;
0309   }
0310 
0311   // /// Return number of elements in the container @a c for which @c f(x) is true.
0312   // template <typename CONTAINER>
0313   // inline unsigned int count(const CONTAINER& c, const std::function<bool(typename CONTAINER::value_type)>& f) {
0314   //   return std::count_if(std::begin(c), std::end(c), f);
0315   // }
0316 
0317   /// Return number of elements in the container @a c for which @c f(x) is true.
0318   template <typename CONTAINER, typename FN,
0319             //typename FN = bool(const typename std::decay_t<CONTAINER>::value_type&, const std::decay_t<T>&),
0320             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0321   inline unsigned int count(const CONTAINER& c, const FN& f) {
0322     return std::count_if(std::begin(c), std::end(c), f);
0323   }
0324 
0325 
0326 
0327   /// Return true if x is true for any x in container c, otherwise false.
0328   template <typename CONTAINER,
0329             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0330   inline bool any(const CONTAINER& c) {
0331     // return std::any_of(std::begin(c), std::end(c), [](const auto& x){return bool(x);});
0332     for (const auto& x : c) if (bool(x)) return true;
0333     return false;
0334   }
0335 
0336   // /// Return true if f(x) is true for any x in container c, otherwise false.
0337   // template <typename CONTAINER>
0338   // inline bool any(const CONTAINER& c, const std::function<bool(typename CONTAINER::value_type)>& f) {
0339   //   return std::any_of(std::begin(c), std::end(c), f);
0340   // }
0341 
0342   /// Return true if f(x) is true for any x in container c, otherwise false.
0343   template <typename CONTAINER, typename FN,
0344             //typename FN = double(const typename std::decay_t<CONTAINER>::value_type&, const std::decay_t<T>&),
0345             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0346   inline bool any(const CONTAINER& c, const FN& f) {
0347     return std::any_of(std::begin(c), std::end(c), f);
0348   }
0349 
0350 
0351 
0352   /// Return true if @a x is true for all @c x in container @a c, otherwise false.
0353   template <typename CONTAINER,
0354             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0355   inline bool all(const CONTAINER& c) {
0356     // return std::all_of(std::begin(c), std::end(c), [](const auto& x){return bool(x);});
0357     for (const auto& x : c) if (!bool(x)) return false;
0358     return true;
0359   }
0360 
0361   // /// Return true if @a f(x) is true for all @c x in container @a c, otherwise false.
0362   // template <typename CONTAINER>
0363   // inline bool all(const CONTAINER& c, const std::function<bool(typename CONTAINER::value_type)>& f) {
0364   //   return std::all_of(std::begin(c), std::end(c), f);
0365   // }
0366 
0367   /// Return true if @a f(x) is true for all @c x in container @a c, otherwise false.
0368   template <typename CONTAINER, typename FN,
0369             //typename FN = double(const typename std::decay_t<CONTAINER>::value_type&, const std::decay_t<T>&),
0370             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0371   inline bool all(const CONTAINER& c, const FN& f) {
0372     return std::all_of(std::begin(c), std::end(c), f);
0373   }
0374 
0375 
0376   /// Return true if @a x is false for all @c x in container @a c, otherwise false.
0377   template <typename CONTAINER,
0378             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0379   inline bool none(const CONTAINER& c) {
0380     // return std::none_of(std::begin(c), std::end(c), [](){});
0381     for (const auto& x : c) if (bool(x)) return false;
0382     return true;
0383   }
0384 
0385   // /// Return true if @a f(x) is false for all @c x in container @a c, otherwise false.
0386   // template <typename C>
0387   // inline bool none(const C& c, const std::function<bool(typename C::value_type)>& f) {
0388   //   return std::none_of(std::begin(c), std::end(c), f);
0389   // }
0390 
0391   /// Return true if @a f(x) is false for all @c x in container @a c, otherwise false.
0392   template <typename CONTAINER, typename FN,
0393             //typename FN = double(const typename std::decay_t<CONTAINER>::value_type&, const std::decay_t<T>&),
0394             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0395   inline bool none(const CONTAINER& c, const FN& f) {
0396     return std::none_of(std::begin(c), std::end(c), f);
0397   }
0398 
0399 
0400   // /// A single-container-arg version of std::transform, aka @c map
0401   // template <typename CONTAINER1, typename CONTAINER2>
0402   // inline const CONTAINER2& transform(const CONTAINER1& in, CONTAINER2& out,
0403   //                            const std::function<typename CONTAINER2::value_type(typename CONTAINER1::value_type)>& f) {
0404   //   out.clear(); out.resize(in.size());
0405   //   std::transform(in.begin(), in.end(), out.begin(), f);
0406   //   return out;
0407   // }
0408 
0409   /// A single-container-arg version of std::transform
0410   template <typename CONTAINER1, typename CONTAINER2,
0411             typename FN = typename std::decay_t<CONTAINER2>::value_type(
0412                           const typename std::decay_t<CONTAINER1>::value_type::ParticleBase&),
0413             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER1>> &&
0414                                          is_citerable_v<std::decay_t<CONTAINER2>> >>
0415   inline const CONTAINER2& transform(const CONTAINER1& in, CONTAINER2& out, FN&& f) {
0416     out.clear(); out.resize(in.size());
0417     std::transform(in.begin(), in.end(), out.begin(), std::forward<FN>(f));
0418     return out;
0419   }
0420 
0421   /// A single-container-arg, return-value version of std::transform, aka @c map
0422   ///
0423   /// @todo Make the function template polymorphic... or specific to ParticleBase
0424   template <typename CONTAINER1, typename RTN,
0425             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER1>> >>
0426   inline std::vector<RTN> transform(const CONTAINER1& in,
0427                                     const std::function<RTN(typename CONTAINER1::value_type::ParticleBase)>& f) {
0428     std::vector<RTN> out;
0429     transform(in, out, f);
0430     return out;
0431   }
0432 
0433   // /// A single-container-arg version of std::accumulate, aka @c reduce
0434   // template <typename CONTAINER1, typename T>
0435   // inline T accumulate(const CONTAINER1& in, const T& init, const std::function<T(typename CONTAINER1::value_type)>& f) {
0436   //   const T rtn = std::accumulate(in.begin(), in.end(), init, f);
0437   //   return rtn;
0438   // }
0439 
0440   /// A single-container-arg version of std::accumulate, aka @c reduce
0441   template <typename CONTAINER1, typename T, typename FN,
0442             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER1>> >>
0443   inline T accumulate(const CONTAINER1& in, const T& init, const FN& f) {
0444     const T rtn = std::accumulate(in.begin(), in.end(), init, f);
0445     return rtn;
0446   }
0447 
0448 
0449   /// @brief Generic sum function, adding @c x for all @c x in container @a c
0450   ///
0451   /// @note Default-constructs the return type -- not always possible! Supply an explicit start value if necessary.
0452   template <typename CONTAINER,
0453             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0454   inline typename CONTAINER::value_type sum(const CONTAINER& c) {
0455     typename CONTAINER::value_type rtn; //< default construct return type
0456     for (const auto& x : c) rtn += x;
0457     return rtn;
0458   }
0459 
0460   /// Generic sum function, adding @c x for all @c x in container @a c, starting with @a start
0461   ///
0462   /// @note It's more more flexible here to not use CONTAINER::value_type, allowing implicit casting to T.
0463   template <typename CONTAINER, typename T,
0464             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0465   inline T sum(const CONTAINER& c, const T& start) {
0466     T rtn = start;
0467     for (const auto& x : c) rtn += x;
0468     return rtn;
0469   }
0470 
0471   /// Generic sum function, adding @a fn(@c x) for all @c x in container @a c, starting with @a start
0472   template <typename CONTAINER, typename T,
0473             typename FN = T(const typename std::decay_t<CONTAINER>::value_type::ParticleBase&),
0474             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0475   inline T sum(const CONTAINER& c, FN&& fn, const T& start=T()) {
0476     auto f = std::function(std::forward<FN>(fn));
0477     T rtn = start;
0478     for (const auto& x : c) rtn += fn(x);
0479     return rtn;
0480   }
0481 
0482 
0483   /// In-place generic sum function, adding @c x on to container @a out for all @c x in container @a c
0484   ///
0485   /// @note It's more more flexible here to not use CONTAINER::value_type, allowing implicit casting to T.
0486   template <typename CONTAINER, typename T,
0487             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0488   inline T& isum(const CONTAINER& c, T& out) {
0489     for (const auto& x : c) out += x;
0490     return out;
0491   }
0492 
0493   /// In-place generic sum function, adding @a fn(@c x) on to container @a out for all @c x in container @a c
0494   ///
0495   /// @note It's more more flexible here to not use CONTAINER::value_type, allowing implicit casting to T.
0496   template <typename CONTAINER, typename FN, typename T,
0497             //typename FN = double(const typename std::decay_t<CONTAINER>::value_type&, const std::decay_t<T>&),
0498             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0499   inline T& isum(const CONTAINER& c, const FN& f, T& out) {
0500     for (const auto& x : c) out += f(x);
0501     return out;
0502   }
0503 
0504 
0505 
0506   /// Filter a collection in-place, removing the subset that passes the supplied function
0507   ///
0508   /// @todo Use const std::function<bool(typename CONTAINER::value_type)>... but need polymorphism for ParticleBase
0509   template <typename CONTAINER, typename FN,
0510             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0511   inline CONTAINER& idiscard(CONTAINER& c, const FN& f) {
0512     const auto newend = std::remove_if(std::begin(c), std::end(c), f);
0513     c.erase(newend, c.end());
0514     return c;
0515   }
0516 
0517   /// Version with element-equality comparison in place of a function
0518   template <typename CONTAINER,
0519             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0520   inline CONTAINER& idiscard(CONTAINER& c, const typename CONTAINER::value_type& y) {
0521     return idiscard(c, [&](typename CONTAINER::value_type& x){ return x == y; });
0522   }
0523 
0524   /// Version with several element-equality comparisons in place of a function
0525   template <typename CONTAINER,
0526             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0527   inline CONTAINER& idiscard_if_any(CONTAINER& c, const CONTAINER& ys) {
0528     return idiscard(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
0529   }
0530 
0531 
0532   /// Filter a collection by copy, removing the subset that passes the supplied function
0533   ///
0534   /// @todo Use const std::function<bool(typename CONTAINER::value_type)>... but need polymorphism for ParticleBase
0535   template <typename CONTAINER, typename FN,
0536             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0537   inline CONTAINER discard(const CONTAINER& c, const FN& f) {
0538     CONTAINER rtn = c;
0539     return idiscard(rtn, f); ///< @todo More efficient would be copy_if with back_inserter...
0540   }
0541 
0542   /// Version with element-equality comparison in place of a function
0543   template <typename CONTAINER,
0544             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0545   inline CONTAINER discard(const CONTAINER& c, const typename CONTAINER::value_type& y) {
0546     return discard(c, [&](typename CONTAINER::value_type& x){ return x == y; });
0547   }
0548 
0549   /// Version with several element-equality comparisons in place of a function
0550   template <typename CONTAINER,
0551             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0552   inline CONTAINER discard_if_any(const CONTAINER& c, const CONTAINER& ys) {
0553     return discard(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
0554   }
0555 
0556 
0557   /// Filter a collection by copy into a supplied container, removing the subset that passes the supplied function
0558   ///
0559   /// @note New container will be replaced, not appended to
0560   ///
0561   /// @todo Use const std::function<bool(typename CONTAINER::value_type)>... but need polymorphism for ParticleBase
0562   template <typename CONTAINER, typename FN,
0563             //typename FN = bool(const typename std::decay_t<CONTAINER>::value_type&, const std::decay_t<T>&),
0564             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0565   inline CONTAINER& discard(const CONTAINER& c, const FN& f, CONTAINER& out) {
0566     out = discard(c, f);
0567     return out;
0568   }
0569 
0570   /// Version with element-equality comparison in place of a function
0571   template <typename CONTAINER,
0572             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0573   inline CONTAINER& discard(const CONTAINER& c, const typename CONTAINER::value_type& y, CONTAINER& out) {
0574     return discard(c, [&](typename CONTAINER::value_type& x){ return x == y; }, out);
0575   }
0576 
0577   /// Version with several element-equality comparisons in place of a function
0578   template <typename CONTAINER,
0579             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0580   inline CONTAINER& discard_if_any(const CONTAINER& c, const CONTAINER& ys, CONTAINER& out) {
0581     return discard(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); }, out);
0582   }
0583 
0584 
0585 
0586   /// Filter a collection in-place, keeping the subset that passes the supplied function
0587   ///
0588   /// @todo Use const std::function<bool(typename CONTAINER::value_type)>... but need polymorphism for ParticleBase
0589   template <typename CONTAINER, typename FN,
0590             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0591       inline CONTAINER& iselect(CONTAINER& c, const FN& f) {
0592     auto invf = [&](const typename CONTAINER::value_type& x){ return !f(x); };
0593     return idiscard(c, invf); //< yes, intentional!
0594   }
0595 
0596   // No single equality-comparison version for select, since that would be silly!
0597 
0598   /// Version with several element-equality comparisons in place of a function
0599   template <typename CONTAINER,
0600             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0601   inline CONTAINER& iselect_if_any(CONTAINER& c, const CONTAINER& ys) {
0602     return iselect(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
0603   }
0604 
0605 
0606   /// Filter a collection by copy, keeping the subset that passes the supplied function
0607   ///
0608   /// @todo Use const std::function<bool(typename CONTAINER::value_type)>... but need polymorphism for ParticleBase
0609   template <typename CONTAINER, typename FN,
0610             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0611   inline CONTAINER select(const CONTAINER& c, const FN& f) {
0612     CONTAINER rtn = c;
0613     return iselect(rtn, f); ///< @todo More efficient would be copy_if with back_inserter ... but is that equally container agnostic?
0614   }
0615 
0616   // No single equality-comparison version for select, since that would be silly!
0617 
0618   /// Version with several element-equality comparisons in place of a function
0619   template <typename CONTAINER,
0620             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0621   inline CONTAINER select_if_any(const CONTAINER& c, const CONTAINER& ys) {
0622     return select(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
0623   }
0624 
0625 
0626   /// Filter a collection by copy into a supplied container, keeping the subset that passes the supplied function
0627   ///
0628   /// @note New container will be replaced, not appended to
0629   ///
0630   /// @todo Use const std::function<bool(typename CONTAINER::value_type)>... but need polymorphism for ParticleBase
0631   template <typename CONTAINER, typename FN,
0632             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0633   inline CONTAINER& select(const CONTAINER& c, const FN& f, CONTAINER& out) {
0634     out = select(c, f);
0635     return out;
0636   }
0637 
0638   // No single equality-comparison version for select, since that would be silly!
0639 
0640   /// Version with several element-equality comparisons in place of a function
0641   template <typename CONTAINER,
0642             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0643   inline CONTAINER& select_if_any(const CONTAINER& c, const CONTAINER& ys, CONTAINER& out) {
0644     return select(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); }, out);
0645   }
0646 
0647 
0648 
0649   /// @brief Slice of the container elements cf. Python's [i:j] syntax
0650   ///
0651   /// The element at the @a j index is not included in the returned container.
0652   /// @a i and @a j can be negative, treated as backward offsets from the end of the container.
0653   template <typename CONTAINER,
0654             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0655   inline CONTAINER slice(const CONTAINER& c, int i, int j) {
0656     CONTAINER rtn;
0657     const size_t off1 = (i >= 0) ? i : c.size() + i;
0658     const size_t off2 = (j >= 0) ? j : c.size() + j;
0659     if (off1 > c.size() || off2 > c.size()) throw RangeError("Attempting to slice beyond requested offsets");
0660     if (off2 < off1) throw RangeError("Requested offsets in invalid order");
0661     rtn.resize(off2 - off1);
0662     std::copy(c.begin()+off1, c.begin()+off2, rtn.begin());
0663     return rtn;
0664   }
0665 
0666   /// @brief Tail slice of the container elements cf. Python's [i:] syntax
0667   ///
0668   /// Single-index specialisation of @c slice(c, i, j)
0669   template <typename CONTAINER,
0670             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0671   inline CONTAINER slice(const CONTAINER& c, int i) {
0672     return slice(c, i, c.size());
0673   }
0674 
0675   /// @brief Head slice of the @a n first container elements
0676   ///
0677   /// Negative @a n means to take the head excluding the @a n -element tail
0678   template <typename CONTAINER,
0679             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0680   inline CONTAINER head(const CONTAINER& c, int n) {
0681     // if (n > c.size()) throw RangeError("Requested head longer than container");
0682     if (n < 0) n = std::max(0, (int)c.size()+n);
0683     n = std::min(n, (int)c.size());
0684     return slice(c, 0, n);
0685   }
0686 
0687   /// @brief Tail slice of the @a n last container elements
0688   ///
0689   /// Negative @a n means to take the tail from after the @a n th element
0690   template <typename CONTAINER,
0691             typename = std::enable_if_t< is_citerable_v<std::decay_t<CONTAINER>> >>
0692   inline CONTAINER tail(const CONTAINER& c, int n) {
0693     // if (n > c.size()) throw RangeError("Requested tail longer than container");
0694     if (n < 0) n = std::max(0, (int)c.size()+n);
0695     n = std::min(n, (int)c.size());
0696     return slice(c, c.size()-n);
0697   }
0698 
0699 
0700   using std::min;
0701   using std::max;
0702 
0703   /// Find the minimum value in the vector
0704   inline double min(const vector<double>& in, double errval=DBL_NAN) {
0705     const auto e = std::min_element(in.begin(), in.end());
0706     return e != in.end() ? *e : errval;
0707   }
0708 
0709   /// Find the maximum value in the vector
0710   inline double max(const vector<double>& in, double errval=DBL_NAN) {
0711     const auto e = std::max_element(in.begin(), in.end());
0712     return e != in.end() ? *e : errval;
0713   }
0714 
0715   /// Find the minimum and maximum values in the vector
0716   inline pair<double,double> minmax(const vector<double>& in, double errval=DBL_NAN) {
0717     const auto e = std::minmax_element(in.begin(), in.end());
0718     const double rtnmin = e.first != in.end() ? *e.first : errval;
0719     const double rtnmax = e.second != in.end() ? *e.first : errval;
0720     return std::make_pair(rtnmin, rtnmax);
0721   }
0722 
0723 
0724   /// Find the minimum value in the vector
0725   inline int min(const vector<int>& in, int errval=-1) {
0726     const auto e = std::min_element(in.begin(), in.end());
0727     return e != in.end() ? *e : errval;
0728   }
0729 
0730   /// Find the maximum value in the vector
0731   inline int max(const vector<int>& in, int errval=-1) {
0732     const auto e = std::max_element(in.begin(), in.end());
0733     return e != in.end() ? *e : errval;
0734   }
0735 
0736   /// Find the minimum and maximum values in the vector
0737   inline pair<int,int> minmax(const vector<int>& in, int errval=-1) {
0738     const auto e = std::minmax_element(in.begin(), in.end());
0739     const double rtnmin = e.first != in.end() ? *e.first : errval;
0740     const double rtnmax = e.second != in.end() ? *e.first : errval;
0741     return std::make_pair(rtnmin, rtnmax);
0742   }
0743 
0744   /// @}
0745 
0746 
0747   /// @defgroup contcombutils Container-combinatorics utils
0748   /// @{
0749 
0750   /// @brief Return the index from a vector which best matches fn(c[i]) to the target value
0751   ///
0752   /// A NaN return from the function will be counted as a no-match, as will combinations giving
0753   /// a value outside the given @a minval .. @a maxval range. A -1 index is returned in the
0754   /// case that no valid match is found.
0755   template <typename CONTAINER,
0756             typename FN = double(const typename std::decay_t<CONTAINER>::value_type::ParticleBase&),
0757             typename = isCIterable<CONTAINER>>
0758   inline int closestMatchIndex(const CONTAINER& c, FN&& fn,
0759                    double target, double minval=-DBL_MAX, double maxval=DBL_MAX) {
0760     auto f = std::function(std::forward<FN>(fn));
0761     int ibest = -1;
0762     double best = DBL_NAN;
0763     for (size_t i = 0; i < c.size(); ++i) {
0764       const double val = f(c[i]);
0765       if (isnan(val)) continue;
0766       if (val < minval || val > maxval) continue;
0767       if (isnan(best) || fabs(val-target) < fabs(best-target)) {
0768         best = val;
0769         ibest = i;
0770       }
0771     }
0772     return ibest;
0773   }
0774 
0775   /// @brief Return the indices from two vectors which best match fn(c1[i], c2[j]) to the target value
0776   ///
0777   /// A NaN return from the function will be counted as a no-match, as will combinations giving
0778   /// a value outside the given @a minval .. @a maxval range. A {-1,-1} pair is returned in the
0779   /// case that no valid match is found.
0780   ///
0781   /// @note No attempt is made to avoid duplication: the input lists are assumed independent and
0782   ///   the user should check the sanity of the result, e.g. c1[i] and c2[j] are not the same object.
0783   template <typename CONTAINER1, typename CONTAINER2,
0784             typename FN = double(const typename std::decay_t<CONTAINER1>::value_type::ParticleBase&,
0785                                  const typename std::decay_t<CONTAINER2>::value_type::ParticleBase&),
0786             typename = isCIterable<CONTAINER1, CONTAINER2>>
0787   inline pair<int,int> closestMatchIndices(const CONTAINER1& c1, const CONTAINER2& c2, FN&& fn,
0788                        double target, double minval=-DBL_MAX, double maxval=DBL_MAX) {
0789     auto f = std::function(std::forward<FN>(fn));
0790     pair<int,int> ijbest{-1,-1};
0791     double best = DBL_NAN;
0792     for (size_t i = 0; i < c1.size(); ++i) {
0793       for (size_t j = 0; j < c2.size(); ++j) {
0794         const double val = f(c1[i], c2[j]);
0795         if (isnan(val)) continue;
0796         if (val < minval || val > maxval) continue;
0797         if (isnan(best) || fabs(val-target) < fabs(best-target)) {
0798           best = val;
0799           ijbest = {i,j};
0800         }
0801       }
0802     }
0803     return ijbest;
0804   }
0805 
0806   /// @brief Return the index from a vector which best matches fn(c[i], x) to the target value
0807   ///
0808   /// A NaN return from the function will be counted as a no-match, as will combinations giving
0809   /// a value outside the given @a minval .. @a maxval range. A -1 index is returned in the
0810   /// case that no valid match is found.
0811   ///
0812   /// @note No attempt is made to avoid duplication: the inputs are assumed independent and
0813   ///   the user should check the sanity of the result, e.g. c[i] and x are not the same object.
0814   template <typename CONTAINER, typename T,
0815             typename FN = double(const typename std::decay_t<CONTAINER>::value_type::ParticleBase&, const std::decay_t<T>&),
0816             typename = isCIterable<CONTAINER>>
0817   inline int closestMatchIndex(const CONTAINER& c, const T& x, FN&& fn,
0818                    double target, double minval=-DBL_MAX, double maxval=DBL_MAX) {
0819     pair<int,int> ijbest = closestMatchIndices(c, vector<T>{x}, std::forward<FN>(fn), target, minval, maxval);
0820     return ijbest.first;
0821   }
0822 
0823   /// @brief Return the index from a vector which best matches fn(x, c[j]) to the target value
0824   ///
0825   /// A NaN return from the function will be counted as a no-match, as will combinations giving
0826   /// a value outside the given @a minval .. @a maxval range. A -1 index is returned in the
0827   /// case that no valid match is found.
0828   ///
0829   /// @note No attempt is made to avoid duplication: the inputs are assumed independent and
0830   ///   the user should check the sanity of the result, e.g. c[i] and x are not the same object.
0831   template <typename CONTAINER, typename T, typename FN, typename = isCIterable<CONTAINER>>
0832   inline int closestMatchIndex(const T& x, const CONTAINER& c, FN&& fn,
0833                    double target, double minval=-DBL_MAX, double maxval=DBL_MAX) {
0834     return closestMatchIndex(c, x, std::forward<FN>(fn), target, minval, maxval);
0835   }
0836 
0837   /// @}
0838 
0839 
0840   /// @brief Get a parameter from a named environment variable, with automatic type conversion
0841   ///
0842   /// @note Return @a fallback if the variable is not defined, otherwise convert its string to the template type
0843   /// @todo Should the param name have to be specific to an analysis? Can specialise as an Analysis member fn.
0844   /// @ingroup utils
0845   template <typename T>
0846   T getEnvParam(const std::string name, const T& fallback) {
0847     char* env = getenv(name.c_str());
0848     return env ? lexical_cast<T>(env) : fallback;
0849   }
0850 
0851 
0852 
0853   /// @brief Get the contents of a vector between two indices
0854   ///
0855   /// @ingroup utils
0856   template<class T>
0857   vector<T> slice(const vector<T>& v, int startidx, int endidx) {
0858 
0859     if (startidx < 0 || endidx <= startidx || endidx >= v.size())
0860       throw RangeError("Requested start or end indices incompatible with given vector");
0861 
0862     auto start = v.begin() + startidx;
0863     auto end = v.begin() + endidx;
0864 
0865     vector<T> output(endidx - startidx);
0866 
0867     copy(start, end, output.begin());
0868 
0869     return output;
0870   }
0871 
0872 
0873 }
0874 
0875 #endif