Rivet 3.1.9
Utils.hh
1// -*- C++ -*-
2#ifndef RIVET_Utils_HH
3#define RIVET_Utils_HH
4
5#include "Rivet/Tools/RivetSTL.hh"
6#include "Rivet/Tools/PrettyPrint.hh"
7#include "Rivet/Tools/Exceptions.hh"
8#include <ostream>
9#include <cctype>
10#include <cerrno>
11#include <stdexcept>
12#include <numeric>
13#include <limits>
14#include <climits>
15#include <cfloat>
16#include <cmath>
17#include <sstream>
18#include <functional>
19
21
22
24#ifndef DEPRECATED
25#if __GNUC__ && __cplusplus && RIVET_NO_DEPRECATION_WARNINGS == 0
26 #define DEPRECATED(x) __attribute__((deprecated(x)))
27#else
28 #define DEPRECATED(x)
29#endif
30#endif
31
32
33namespace Rivet {
34
35
37 static constexpr double DBL_NAN = std::numeric_limits<double>::quiet_NaN();
38
39
42
44 struct bad_lexical_cast : public std::runtime_error {
45 bad_lexical_cast(const std::string& what) : std::runtime_error(what) {}
46 };
47
49 template<typename T, typename U>
50 T lexical_cast(const U& in) {
51 try {
52 std::stringstream ss;
53 ss << in;
54 T out;
55 ss >> out;
56 return out;
57 } catch (const std::exception& e) {
58 throw bad_lexical_cast(e.what());
59 }
60 }
61
65 template <typename T>
66 inline string to_str(const T& x) {
67 return lexical_cast<string>(x);
68 }
69
73 template <typename T>
74 inline string toString(const T& x) {
75 return lexical_cast<string>(x);
76 }
77
79 inline string& replace_first(string& str, const string& patt, const string& repl) {
80 if (!contains(str, patt)) return str; //< contains from RivetSTL
81 str.replace(str.find(patt), patt.size(), repl);
82 return str;
83 }
84
90 inline string& replace_all(string& str, const string& patt, const string& repl) {
91 if (!contains(str, patt)) return str; //< contains from RivetSTL
92 while (true) {
93 string::size_type it = str.find(patt);
94 if (it == string::npos) break;
95 str.replace(it, patt.size(), repl);
96 }
97 return str;
98 }
99
100
102 inline int nocase_cmp(const string& s1, const string& s2) {
103 string::const_iterator it1 = s1.begin();
104 string::const_iterator it2 = s2.begin();
105 while ( (it1 != s1.end()) && (it2 != s2.end()) ) {
106 if(::toupper(*it1) != ::toupper(*it2)) { // < Letters differ?
107 // Return -1 to indicate smaller than, 1 otherwise
108 return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1;
109 }
110 // Proceed to the next character in each string
111 ++it1;
112 ++it2;
113 }
114 size_t size1 = s1.size(), size2 = s2.size(); // Cache lengths
115 // Return -1,0 or 1 according to strings' lengths
116 if (size1 == size2) return 0;
117 return (size1 < size2) ? -1 : 1;
118 }
119
120
122 inline bool nocase_equals(const string& s1, const string& s2) {
123 return nocase_cmp(s1, s2) == 0;
124 }
125
126
128 inline string toLower(const string& s) {
129 string out = s;
130 std::transform(out.begin(), out.end(), out.begin(), (int(*)(int)) std::tolower);
131 return out;
132 }
133
134
136 inline string toUpper(const string& s) {
137 string out = s;
138 std::transform(out.begin(), out.end(), out.begin(), (int(*)(int)) std::toupper);
139 return out;
140 }
141
142
144 inline bool startsWith(const string& s, const string& start) {
145 if (s.length() < start.length()) return false;
146 return s.substr(0, start.length()) == start;
147 }
148
149
151 inline bool endsWith(const string& s, const string& end) {
152 if (s.length() < end.length()) return false;
153 return s.substr(s.length() - end.length()) == end;
154 }
155
156
157 // Terminating version of strjoin, for empty fargs list
158 inline string strcat() { return ""; }
160 template<typename T, typename... Ts>
161 inline string strcat(T value, Ts... fargs) {
162 const string strthis = lexical_cast<string>(value);
163 const string strnext = strcat(fargs...);
164 return strnext.empty() ? strthis : strthis + strnext;
165 }
166
167
169 template <typename T>
170 inline string join(const vector<T>& v, const string& sep=" ") {
171 string rtn;
172 for (size_t i = 0; i < v.size(); ++i) {
173 if (i != 0) rtn += sep;
174 rtn += to_str(v[i]);
175 }
176 return rtn;
177 }
178
180 template <>
181 inline string join(const vector<string>& v, const string& sep) {
182 string rtn;
183 for (size_t i = 0; i < v.size(); ++i) {
184 if (i != 0) rtn += sep;
185 rtn += v[i];
186 }
187 return rtn;
188 }
189
191 template <typename T>
192 inline string join(const set<T>& s, const string& sep=" ") {
193 string rtn;
194 for (const T& x : s) {
195 if (rtn.size() > 0) rtn += sep;
196 rtn += to_str(x);
197 }
198 return rtn;
199 }
200
202 template <>
203 inline string join(const set<string>& s, const string& sep) {
204 string rtn;
205 for (const string & x : s) {
206 if (rtn.size() > 0) rtn += sep;
207 rtn += x;
208 }
209 return rtn;
210 }
211
213 inline vector<string> split(const string& s, const string& sep) {
214 vector<string> dirs;
215 string tmp = s;
216 while (true) {
217 const size_t delim_pos = tmp.find(sep);
218 if (delim_pos == string::npos) break;
219 const string dir = tmp.substr(0, delim_pos);
220 if (dir.length()) dirs.push_back(dir); // Don't insert "empties"
221 tmp.replace(0, delim_pos+1, "");
222 }
223 if (tmp.length()) dirs.push_back(tmp); // Don't forget the trailing component!
224 return dirs;
225 }
226
228 inline string lpad(const string& s, size_t width, const string& padchar=" ") {
229 if (s.size() >= width) return s;
230 return string(width - s.size(), padchar[0]) + s;
231 }
232
234 inline string rpad(const string& s, size_t width, const string& padchar=" ") {
235 if (s.size() >= width) return s;
236 return s + string(width - s.size(), padchar[0]);
237 }
238
240
241
242
245
249 inline vector<string> pathsplit(const string& path) {
250 return split(path, ":");
251 }
252
257 inline string pathjoin(const vector<string>& paths) {
258 return join(paths, ":");
259 }
260
262 inline string operator / (const string& a, const string& b) {
263 // Ensure that a doesn't end with a slash, and b doesn't start with one, to avoid "//"
264 const string anorm = (a.find("/") != string::npos) ? a.substr(0, a.find_last_not_of("/")+1) : a;
265 const string bnorm = (b.find("/") != string::npos) ? b.substr(b.find_first_not_of("/")) : b;
266 return anorm + "/" + bnorm;
267 }
268
270 inline string basename(const string& p) {
271 if (!contains(p, "/")) return p;
272 return p.substr(p.rfind("/")+1);
273 }
274
276 inline string dirname(const string& p) {
277 if (!contains(p, "/")) return "";
278 return p.substr(0, p.rfind("/"));
279 }
280
282 inline string file_stem(const string& f) {
283 if (!contains(f, ".")) return f;
284 return f.substr(0, f.rfind("."));
285 }
286
288 inline string file_extn(const string& f) {
289 if (!contains(f, ".")) return "";
290 return f.substr(f.rfind(".")+1);
291 }
292
294
295
296
299
301 template <typename CONTAINER>
302 inline unsigned int count(const CONTAINER& c) {
303 // return std::count_if(std::begin(c), std::end(c), [](const typename CONTAINER::value_type& x){return bool(x);});
304 unsigned int rtn = 0;
305 for (const auto& x : c) if (bool(x)) rtn += 1;
306 return rtn;
307 }
308
309 // /// Return number of elements in the container @a c for which @c f(x) is true.
310 // template <typename CONTAINER>
311 // inline unsigned int count(const CONTAINER& c, const std::function<bool(typename CONTAINER::value_type)>& f) {
312 // return std::count_if(std::begin(c), std::end(c), f);
313 // }
314
316 template <typename CONTAINER, typename FN>
317 inline unsigned int count(const CONTAINER& c, const FN& f) {
318 return std::count_if(std::begin(c), std::end(c), f);
319 }
320
321
322
324 template <typename CONTAINER>
325 inline bool any(const CONTAINER& c) {
326 // return std::any_of(std::begin(c), std::end(c), [](const auto& x){return bool(x);});
327 for (const auto& x : c) if (bool(x)) return true;
328 return false;
329 }
330
331 // /// Return true if f(x) is true for any x in container c, otherwise false.
332 // template <typename CONTAINER>
333 // inline bool any(const CONTAINER& c, const std::function<bool(typename CONTAINER::value_type)>& f) {
334 // return std::any_of(std::begin(c), std::end(c), f);
335 // }
336
338 template <typename CONTAINER, typename FN>
339 inline bool any(const CONTAINER& c, const FN& f) {
340 return std::any_of(std::begin(c), std::end(c), f);
341 }
342
343
344
346 template <typename CONTAINER>
347 inline bool all(const CONTAINER& c) {
348 // return std::all_of(std::begin(c), std::end(c), [](const auto& x){return bool(x);});
349 for (const auto& x : c) if (!bool(x)) return false;
350 return true;
351 }
352
353 // /// Return true if @a f(x) is true for all @c x in container @a c, otherwise false.
354 // template <typename CONTAINER>
355 // inline bool all(const CONTAINER& c, const std::function<bool(typename CONTAINER::value_type)>& f) {
356 // return std::all_of(std::begin(c), std::end(c), f);
357 // }
358
360 template <typename CONTAINER, typename FN>
361 inline bool all(const CONTAINER& c, const FN& f) {
362 return std::all_of(std::begin(c), std::end(c), f);
363 }
364
365
366
368 template <typename CONTAINER>
369 inline bool none(const CONTAINER& c) {
370 // return std::none_of(std::begin(c), std::end(c), [](){});
371 for (const auto& x : c) if (bool(x)) return false;
372 return true;
373 }
374
375 // /// Return true if @a f(x) is false for all @c x in container @a c, otherwise false.
376 // template <typename C>
377 // inline bool none(const C& c, const std::function<bool(typename C::value_type)>& f) {
378 // return std::none_of(std::begin(c), std::end(c), f);
379 // }
380
382 template <typename CONTAINER, typename FN>
383 inline bool none(const CONTAINER& c, const FN& f) {
384 return std::none_of(std::begin(c), std::end(c), f);
385 }
386
387
388 // /// A single-container-arg version of std::transform, aka @c map
389 // template <typename CONTAINER1, typename CONTAINER2>
390 // inline const CONTAINER2& transform(const CONTAINER1& in, CONTAINER2& out,
391 // const std::function<typename CONTAINER2::value_type(typename CONTAINER1::value_type)>& f) {
392 // out.clear(); out.resize(in.size());
393 // std::transform(in.begin(), in.end(), out.begin(), f);
394 // return out;
395 // }
396
398 template <typename CONTAINER1, typename CONTAINER2, typename FN>
399 inline const CONTAINER2& transform(const CONTAINER1& in, CONTAINER2& out, const FN& f) {
400 out.clear(); out.resize(in.size());
401 std::transform(in.begin(), in.end(), out.begin(), f);
402 return out;
403 }
404
407 template <typename CONTAINER1, typename T2>
408 inline std::vector<T2> transform(const CONTAINER1& in, const std::function<T2(typename CONTAINER1::value_type)>& f) {
409 std::vector<T2> out(in.size());
410 transform(in, out, f);
411 return out;
412 }
413
414
415
416 // /// A single-container-arg version of std::accumulate, aka @c reduce
417 // template <typename CONTAINER1, typename T>
418 // inline T accumulate(const CONTAINER1& in, const T& init, const std::function<T(typename CONTAINER1::value_type)>& f) {
419 // const T rtn = std::accumulate(in.begin(), in.end(), init, f);
420 // return rtn;
421 // }
422
424 template <typename CONTAINER1, typename T, typename FN>
425 inline T accumulate(const CONTAINER1& in, const T& init, const FN& f) {
426 const T rtn = std::accumulate(in.begin(), in.end(), init, f);
427 return rtn;
428 }
429
430
431
435 template <typename CONTAINER>
436 inline typename CONTAINER::value_type sum(const CONTAINER& c) {
437 typename CONTAINER::value_type rtn; //< default construct return type
438 for (const auto& x : c) rtn += x;
439 return rtn;
440 }
441
445 template <typename CONTAINER, typename T>
446 inline T sum(const CONTAINER& c, const T& start) {
447 T rtn = start;
448 for (const auto& x : c) rtn += x;
449 return rtn;
450 }
451
453 template <typename CONTAINER, typename FN, typename T>
454 inline T sum(const CONTAINER& c, const FN& f, const T& start=T()) {
455 T rtn = start;
456 for (const auto& x : c) rtn += f(x);
457 return rtn;
458 }
459
460
461
465 template <typename CONTAINER, typename T>
466 inline T& isum(const CONTAINER& c, T& out) {
467 for (const auto& x : c) out += x;
468 return out;
469 }
470
474 template <typename CONTAINER, typename FN, typename T>
475 inline T& isum(const CONTAINER& c, const FN& f, T& out) {
476 for (const auto& x : c) out += f(x);
477 return out;
478 }
479
480
481
487 template <typename CONTAINER, typename FN>
488 inline CONTAINER& ifilter_discard(CONTAINER& c, const FN& f) {
489 const auto newend = std::remove_if(std::begin(c), std::end(c), f);
490 c.erase(newend, c.end());
491 return c;
492 }
494 template <typename CONTAINER, typename FN>
495 inline CONTAINER& idiscard(CONTAINER& c, const FN& f) {
496 return ifilter_discard(c, f);
497 }
498
502 template <typename CONTAINER>
503 inline CONTAINER& ifilter_discard(CONTAINER& c, const typename CONTAINER::value_type& y) {
504 return ifilter_discard(c, [&](typename CONTAINER::value_type& x){ return x == y; });
505 }
507 template <typename CONTAINER>
508 inline CONTAINER& idiscard(CONTAINER& c, const typename CONTAINER::value_type& y) {
509 return idiscard(c, [&](typename CONTAINER::value_type& x){ return x == y; });
510 }
511
515 template <typename CONTAINER>
516 inline CONTAINER& ifilter_discard_if_any(CONTAINER& c, const CONTAINER& ys) {
517 return ifilter_discard(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
518 }
520 template <typename CONTAINER>
521 inline CONTAINER& idiscard_if_any(CONTAINER& c, const CONTAINER& ys) {
522 return idiscard(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
523 }
524
525
531 template <typename CONTAINER, typename FN>
532 inline CONTAINER filter_discard(const CONTAINER& c, const FN& f) {
533 CONTAINER rtn = c;
534 return ifilter_discard(rtn, f);
535 }
537 template <typename CONTAINER, typename FN>
538 inline CONTAINER discard(const CONTAINER& c, const FN& f) {
539 return filter_discard(c, f);
540 }
541
545 template <typename CONTAINER>
546 inline CONTAINER filter_discard(const CONTAINER& c, const typename CONTAINER::value_type& y) {
547 return filter_discard(c, [&](typename CONTAINER::value_type& x){ return x == y; });
548 }
550 template <typename CONTAINER>
551 inline CONTAINER discard(const CONTAINER& c, const typename CONTAINER::value_type& y) {
552 return discard(c, [&](typename CONTAINER::value_type& x){ return x == y; });
553 }
554
558 template <typename CONTAINER>
559 inline CONTAINER filter_discard_if_any(const CONTAINER& c, const CONTAINER& ys) {
560 return filter_discard(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
561 }
563 template <typename CONTAINER>
564 inline CONTAINER discard_if_any(const CONTAINER& c, const CONTAINER& ys) {
565 return discard(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
566 }
567
568
576 template <typename CONTAINER, typename FN>
577 inline CONTAINER& filter_discard(const CONTAINER& c, const FN& f, CONTAINER& out) {
578 out = filter_discard(c, f);
579 return out;
580 }
582 template <typename CONTAINER, typename FN>
583 inline CONTAINER& discard(const CONTAINER& c, const FN& f, CONTAINER& out) {
584 return filter_discard(c, f, out);
585 }
586
590 template <typename CONTAINER>
591 inline CONTAINER& filter_discard(const CONTAINER& c, const typename CONTAINER::value_type& y, CONTAINER& out) {
592 return filter_discard(c, [&](typename CONTAINER::value_type& x){ return x == y; }, out);
593 }
595 template <typename CONTAINER>
596 inline CONTAINER& discard(const CONTAINER& c, const typename CONTAINER::value_type& y, CONTAINER& out) {
597 return discard(c, [&](typename CONTAINER::value_type& x){ return x == y; }, out);
598 }
599
603 template <typename CONTAINER>
604 inline CONTAINER& filter_discard_if_any(const CONTAINER& c, const CONTAINER& ys, CONTAINER& out) {
605 return filter_discard(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); }, out);
606 }
608 template <typename CONTAINER>
609 inline CONTAINER& discard_if_any(const CONTAINER& c, const CONTAINER& ys, CONTAINER& out) {
610 return discard(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); }, out);
611 }
612
613
614
620 template <typename CONTAINER, typename FN>
621 inline CONTAINER& ifilter_select(CONTAINER& c, const FN& f) {
622 //using value_type = typename std::remove_reference<decltype(*std::begin(std::declval<typename std::add_lvalue_reference<CONTAINER>::type>()))>::type;
623 auto invf = [&](const typename CONTAINER::value_type& x){ return !f(x); };
624 return ifilter_discard(c, invf); //< yes, intentional!
625 }
627 template <typename CONTAINER, typename FN>
628 inline CONTAINER& iselect(CONTAINER& c, const FN& f) {
629 return ifilter_select(c, f);
630 }
631
632 // No single equality-comparison version for select, since that would be silly!
633
637 template <typename CONTAINER>
638 inline CONTAINER& ifilter_select_if_any(CONTAINER& c, const CONTAINER& ys) {
639 return ifilter_select(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
640 }
642 template <typename CONTAINER>
643 inline CONTAINER& iselect_if_any(CONTAINER& c, const CONTAINER& ys) {
644 return iselect(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
645 }
646
647
653 template <typename CONTAINER, typename FN>
654 inline CONTAINER filter_select(const CONTAINER& c, const FN& f) {
655 CONTAINER rtn = c;
656 return ifilter_select(rtn, f);
657 }
659 template <typename CONTAINER, typename FN>
660 inline CONTAINER select(const CONTAINER& c, const FN& f) {
661 return filter_select(c, f);
662 }
663
664 // No single equality-comparison version for select, since that would be silly!
665
669 template <typename CONTAINER>
670 inline CONTAINER filter_select_if_any(const CONTAINER& c, const CONTAINER& ys) {
671 return filter_select(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
672 }
674 template <typename CONTAINER>
675 inline CONTAINER select_if_any(const CONTAINER& c, const CONTAINER& ys) {
676 return select(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); });
677 }
678
679
687 template <typename CONTAINER, typename FN>
688 inline CONTAINER& filter_select(const CONTAINER& c, const FN& f, CONTAINER& out) {
689 out = filter_select(c, f);
690 return out;
691 }
693 template <typename CONTAINER, typename FN>
694 inline CONTAINER& select(const CONTAINER& c, const FN& f, CONTAINER& out) {
695 return filter_select(c, f, out);
696 }
697
698 // No single equality-comparison version for select, since that would be silly!
699
703 template <typename CONTAINER>
704 inline CONTAINER& filter_select_if_any(const CONTAINER& c, const CONTAINER& ys, CONTAINER& out) {
705 return filter_select(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); }, out);
706 }
708 template <typename CONTAINER>
709 inline CONTAINER& select_if_any(const CONTAINER& c, const CONTAINER& ys, CONTAINER& out) {
710 return select(c, [&](typename CONTAINER::value_type& x){ return contains(ys, x); }, out);
711 }
712
713
714
719 template <typename CONTAINER>
720 inline CONTAINER slice(const CONTAINER& c, int i, int j) {
721 CONTAINER rtn;
722 const size_t off1 = (i >= 0) ? i : c.size() + i;
723 const size_t off2 = (j >= 0) ? j : c.size() + j;
724 if (off1 > c.size() || off2 > c.size()) throw RangeError("Attempting to slice beyond requested offsets");
725 if (off2 < off1) throw RangeError("Requested offsets in invalid order");
726 rtn.resize(off2 - off1);
727 std::copy(c.begin()+off1, c.begin()+off2, rtn.begin());
728 return rtn;
729 }
730
734 template <typename CONTAINER>
735 inline CONTAINER slice(const CONTAINER& c, int i) {
736 return slice(c, i, c.size());
737 }
738
742 template <typename CONTAINER>
743 inline CONTAINER head(const CONTAINER& c, int n) {
744 // if (n > c.size()) throw RangeError("Requested head longer than container");
745 if (n < 0) n = std::max(0, (int)c.size()+n);
746 n = std::min(n, (int)c.size());
747 return slice(c, 0, n);
748 }
749
753 template <typename CONTAINER>
754 inline CONTAINER tail(const CONTAINER& c, int n) {
755 // if (n > c.size()) throw RangeError("Requested tail longer than container");
756 if (n < 0) n = std::max(0, (int)c.size()+n);
757 n = std::min(n, (int)c.size());
758 return slice(c, c.size()-n);
759 }
760
761
762 using std::min;
763 using std::max;
764
766 inline double min(const vector<double>& in, double errval=DBL_NAN) {
767 const auto e = std::min_element(in.begin(), in.end());
768 return e != in.end() ? *e : errval;
769 }
770
772 inline double max(const vector<double>& in, double errval=DBL_NAN) {
773 const auto e = std::max_element(in.begin(), in.end());
774 return e != in.end() ? *e : errval;
775 }
776
778 inline pair<double,double> minmax(const vector<double>& in, double errval=DBL_NAN) {
779 const auto e = std::minmax_element(in.begin(), in.end());
780 const double rtnmin = e.first != in.end() ? *e.first : errval;
781 const double rtnmax = e.second != in.end() ? *e.first : errval;
782 return std::make_pair(rtnmin, rtnmax);
783 }
784
785
787 inline int min(const vector<int>& in, int errval=-1) {
788 const auto e = std::min_element(in.begin(), in.end());
789 return e != in.end() ? *e : errval;
790 }
791
793 inline int max(const vector<int>& in, int errval=-1) {
794 const auto e = std::max_element(in.begin(), in.end());
795 return e != in.end() ? *e : errval;
796 }
797
799 inline pair<int,int> minmax(const vector<int>& in, int errval=-1) {
800 const auto e = std::minmax_element(in.begin(), in.end());
801 const double rtnmin = e.first != in.end() ? *e.first : errval;
802 const double rtnmax = e.second != in.end() ? *e.first : errval;
803 return std::make_pair(rtnmin, rtnmax);
804 }
805
807
808
814 template <typename T>
815 T getEnvParam(const std::string name, const T& fallback) {
816 char* env = getenv(name.c_str());
817 return env ? lexical_cast<T>(env) : fallback;
818 }
819
820
821
825 template<class T>
826 vector<T> slice(const vector<T>& v, int startidx, int endidx) {
827
828 if (startidx < 0 || endidx <= startidx || endidx >= v.size())
829 throw RangeError("Requested start or end indices incompatible with given vector");
830
831 auto start = v.begin() + startidx;
832 auto end = v.begin() + endidx;
833
834 vector<T> output(endidx - startidx);
835
836 copy(start, end, output.begin());
837
838 return output;
839 }
840
841
842}
843
844#endif
bool all(const CONTAINER &c)
Return true if x is true for all x in container c, otherwise false.
Definition Utils.hh:347
CONTAINER & idiscard_if_any(CONTAINER &c, const CONTAINER &ys)
Version with element-equality comparisons in place of a function.
Definition Utils.hh:521
CONTAINER filter_discard_if_any(const CONTAINER &c, const CONTAINER &ys)
Definition Utils.hh:559
T & isum(const CONTAINER &c, T &out)
Definition Utils.hh:466
pair< double, double > minmax(const vector< double > &in, double errval=DBL_NAN)
Find the minimum and maximum values in the vector.
Definition Utils.hh:778
CONTAINER select_if_any(const CONTAINER &c, const CONTAINER &ys)
Version with element-equality comparisons in place of a function.
Definition Utils.hh:675
CONTAINER filter_select_if_any(const CONTAINER &c, const CONTAINER &ys)
Definition Utils.hh:670
CONTAINER head(const CONTAINER &c, int n)
Head slice of the n first container elements.
Definition Utils.hh:743
CONTAINER discard_if_any(const CONTAINER &c, const CONTAINER &ys)
Version with element-equality comparisons in place of a function.
Definition Utils.hh:564
CONTAINER slice(const CONTAINER &c, int i, int j)
Slice of the container elements cf. Python's [i:j] syntax.
Definition Utils.hh:720
CONTAINER::value_type sum(const CONTAINER &c)
Generic sum function, adding x for all x in container c.
Definition Utils.hh:436
bool none(const CONTAINER &c)
Return true if x is false for all x in container c, otherwise false.
Definition Utils.hh:369
CONTAINER tail(const CONTAINER &c, int n)
Tail slice of the n last container elements.
Definition Utils.hh:754
CONTAINER & ifilter_discard_if_any(CONTAINER &c, const CONTAINER &ys)
Definition Utils.hh:516
unsigned int count(const CONTAINER &c)
Return number of true elements in the container c .
Definition Utils.hh:302
CONTAINER & ifilter_select_if_any(CONTAINER &c, const CONTAINER &ys)
Definition Utils.hh:638
T accumulate(const CONTAINER1 &in, const T &init, const FN &f)
A single-container-arg version of std::accumulate, aka reduce.
Definition Utils.hh:425
CONTAINER & iselect_if_any(CONTAINER &c, const CONTAINER &ys)
Version with element-equality comparisons in place of a function.
Definition Utils.hh:643
bool any(const CONTAINER &c)
Return true if x is true for any x in container c, otherwise false.
Definition Utils.hh:325
Jets & idiscard(Jets &jets, const Cut &c)
New alias for ifilter_discard.
Definition JetUtils.hh:184
Jets select(const Jets &jets, const Cut &c)
New alias for filter_select.
Definition JetUtils.hh:165
Jets & iselect(Jets &jets, const Cut &c)
New alias for ifilter_select.
Definition JetUtils.hh:153
Jets filter_discard(const Jets &jets, const Cut &c)
Filter a jet collection in-place to the subset that fails the supplied Cut.
Definition JetUtils.hh:188
Jets discard(const Jets &jets, const Cut &c)
New alias for filter_discard.
Definition JetUtils.hh:193
Jets filter_select(const Jets &jets, const Cut &c)
Filter a jet collection in-place to the subset that passes the supplied Cut.
Definition JetUtils.hh:157
Jets & ifilter_select(Jets &jets, const Cut &c)
Filter a jet collection in-place to the subset that passes the supplied Cut.
Jets & ifilter_discard(Jets &jets, const Cut &c)
Filter a jet collection in-place to the subset that fails the supplied Cut.
double p(const ParticleBase &p)
Unbound function access to p.
Definition ParticleBaseUtils.hh:684
string dirname(const string &p)
Get the dirname (i.e. path to the penultimate directory) from a path p.
Definition Utils.hh:276
string file_extn(const string &f)
Get the file extension from a filename f.
Definition Utils.hh:288
string pathjoin(const vector< string > &paths)
Join several filesystem paths together with the standard ':' delimiter.
Definition Utils.hh:257
vector< string > pathsplit(const string &path)
Split a path string with colon delimiters.
Definition Utils.hh:249
string basename(const string &p)
Get the basename (i.e. terminal file name) from a path p.
Definition Utils.hh:270
string file_stem(const string &f)
Get the stem (i.e. part without a file extension) from a filename f.
Definition Utils.hh:282
bool endsWith(const string &s, const string &end)
Check whether a string end is found at the end of s.
Definition Utils.hh:151
bool nocase_equals(const string &s1, const string &s2)
Case-insensitive string equality function.
Definition Utils.hh:122
bool startsWith(const string &s, const string &start)
Check whether a string start is found at the start of s.
Definition Utils.hh:144
string toUpper(const string &s)
Convert a string to upper-case.
Definition Utils.hh:136
string rpad(const string &s, size_t width, const string &padchar=" ")
Right-pad the given string s to width width.
Definition Utils.hh:234
string to_str(const T &x)
Convert any object to a string.
Definition Utils.hh:66
string lpad(const string &s, size_t width, const string &padchar=" ")
Left-pad the given string s to width width.
Definition Utils.hh:228
string toLower(const string &s)
Convert a string to lower-case.
Definition Utils.hh:128
T lexical_cast(const U &in)
Convert between any types via stringstream.
Definition Utils.hh:50
vector< string > split(const string &s, const string &sep)
Split a string on a specified separator string.
Definition Utils.hh:213
string & replace_all(string &str, const string &patt, const string &repl)
Replace all instances of patt with repl.
Definition Utils.hh:90
string join(const vector< T > &v, const string &sep=" ")
Make a string containing the string representations of each item in v, separated by sep.
Definition Utils.hh:170
string & replace_first(string &str, const string &patt, const string &repl)
Replace the first instance of patt with repl.
Definition Utils.hh:79
int nocase_cmp(const string &s1, const string &s2)
Case-insensitive string comparison function.
Definition Utils.hh:102
T getEnvParam(const std::string name, const T &fallback)
Get a parameter from a named environment variable, with automatic type conversion.
Definition Utils.hh:815
Definition MC_Cent_pPb.hh:10
std::enable_if< std::is_arithmetic< N1 >::value &&std::is_arithmetic< N2 >::value, typenamestd::common_type< N1, N2 >::type >::type max(N1 a, N2 b)
Get the maximum of two numbers.
Definition MathUtils.hh:111
std::enable_if< std::is_arithmetic< N1 >::value &&std::is_arithmetic< N2 >::value, typenamestd::common_type< N1, N2 >::type >::type min(N1 a, N2 b)
Get the minimum of two numbers.
Definition MathUtils.hh:102
static constexpr double DBL_NAN
Convenient const for getting the double NaN value.
Definition Utils.hh:37
bool contains(const std::string &s, const std::string &sub)
Does s contain sub as a substring?
Definition RivetSTL.hh:98
std::string toString(const AnalysisInfo &ai)
String representation.
Error for e.g. use of invalid bin ranges.
Definition Exceptions.hh:22
Exception class for throwing from lexical_cast when a parse goes wrong.
Definition Utils.hh:44