46 template<
typename T,
typename U>
54 }
catch (
const std::exception& e) {
61 inline std::string
to_str(
const T& val) {
67 inline std::string
to_str(
const std::vector<T>& vec) {
69 for (
size_t i = 0; i < vec.size(); ++i) {
71 if (i < vec.size()-1) rtn +=
", ";
80 ss << setfill(
'0') << setw(nchars) << val;
85 inline std::string
join(
const std::vector<std::string>& svec,
const std::string& sep) {
87 for (
size_t i = 0; i < svec.size(); ++i) {
89 if (i < svec.size()-1) rtn += sep;
95 inline std::vector<std::string>
split(
const std::string& s,
const std::string& sep) {
99 const size_t delim_pos = tmp.find(sep);
100 if (delim_pos == string::npos)
break;
101 const string stmp = tmp.substr(0, delim_pos);
102 if (!stmp.empty()) rtn.push_back(stmp);
103 tmp.replace(0, delim_pos+1,
"");
105 if (!tmp.empty()) rtn.push_back(tmp);
110 inline bool contains(
const std::string& s,
const std::string& sub) {
111 return s.find(sub) != string::npos;
115 inline bool startswith(
const std::string& s,
const std::string& sub) {
116 return s.find(sub) == 0;
120 inline bool endswith(
const std::string& s,
const std::string& sub) {
121 return s.find(sub) == s.length()-sub.length();
125 inline size_t countchar(
const std::string& s,
const char c) {
126 return std::count(s.begin(), s.end(), c);
130 inline std::string
trim(
const std::string& s) {
131 const size_t firstnonspacepos = s.find_first_not_of(
" ");
132 const size_t lastnonspacepos = s.find_last_not_of(
" ");
133 if (firstnonspacepos == std::string::npos)
return "";
134 return s.substr(firstnonspacepos, lastnonspacepos-firstnonspacepos+1);
138 inline std::string
to_lower(
const std::string& s) {
140 transform(rtn.begin(), rtn.end(), rtn.begin(), (int(*)(int)) tolower);
145 inline std::string
to_upper(
const std::string& s) {
147 transform(rtn.begin(), rtn.end(), rtn.begin(), (int(*)(int)) toupper);
164 bool dir_exists(
const std::string& p,
int mode=0);
167 inline std::string
operator / (
const std::string& a,
const std::string& b) {
169 const string anorm = (a.find(
"/") != std::string::npos) ? a.substr(0, a.find_last_not_of(
"/")+1) : a;
170 const string bnorm = (b.find(
"/") != std::string::npos) ? b.substr(b.find_first_not_of(
"/")) : b;
171 return anorm +
"/" + bnorm;
175 inline std::string
basename(
const std::string& p) {
177 return p.substr(p.rfind(
"/")+1);
181 inline std::string
dirname(
const std::string& p) {
183 return p.substr(0, p.rfind(
"/"));
189 return f.substr(0, f.rfind(
"."));
195 return f.substr(f.rfind(
".")+1);
207 template <
typename N>
208 inline N
sqr(
const N& x) {
return x*x; }
211 template <
typename N>
212 inline int sgn(N val) {
return (N(0) < val) - (val < N(0)); }
215 inline int in_range(
double x,
double low,
double high) {
return x >= low && x < high; }
218 inline int in_closed_range(
double x,
double low,
double high) {
return x >= low && x <= high; }
221 inline int in_open_range(
double x,
double low,
double high) {
return x > low && x < high; }
238 template <
typename T>
239 inline bool contains(
const std::vector<T>& container,
const T& item) {
240 return find(container.begin(), container.end(), item) != container.end();
250 template <
typename K,
typename T>
251 inline bool has_key(
const std::map<K,T>& container,
const K& key) {
252 return container.find(key) != container.end();
N sqr(const N &x)
Convenience function for squaring (of any type)
Definition: Utils.h:208
int sgn(N val)
Get the sign of a number.
Definition: Utils.h:212
bool endswith(const std::string &s, const std::string &sub)
Does a string s end with the sub substring?
Definition: Utils.h:120
std::string file_stem(const std::string &f)
Get the stem (i.e. part without a file extension) from a filename f.
Definition: Utils.h:187
int in_closed_range(double x, double low, double high)
Check if a number is in a range (closed-closed)
Definition: Utils.h:218
bool contains(const std::string &s, const std::string &sub)
Does a string s contain the sub substring?
Definition: Utils.h:110
double norm_quantile(double p)
Quantiles of the standard normal probability distribution function.
std::string file_extn(const std::string &f)
Get the file extension from a filename f.
Definition: Utils.h:193
bool dir_exists(const std::string &p, int mode=0)
Check if a dir p exists.
std::string trim(const std::string &s)
Strip leading and trailing spaces (not in-place)
Definition: Utils.h:130
std::string operator/(const std::string &a, const std::string &b)
Operator for joining strings a and b with filesystem separators.
Definition: Utils.h:167
double chisquared_quantile(double p, double ndf)
Quantiles of the chi-squared probability distribution function.
std::string dirname(const std::string &p)
Get the dirname (i.e. path to the penultimate directory) from a path p.
Definition: Utils.h:181
bool has_key(const std::map< K, T > &container, const K &key)
Does the map<K,T> container have a key K key?
Definition: Utils.h:251
When lexical_cast goes bad.
Definition: Utils.h:40
std::string to_lower(const std::string &s)
Convert a string to lower-case (not in-place)
Definition: Utils.h:138
int in_range(double x, double low, double high)
Check if a number is in a range (closed-open)
Definition: Utils.h:215
bool file_exists(const std::string &p, int mode=0)
Check if a file p exists.
std::string to_str_zeropad(int val, size_t nchars=4)
Format an integer val as a zero-padded string of length nchars.
Definition: Utils.h:78
bool startswith(const std::string &s, const std::string &sub)
Does a string s start with the sub substring?
Definition: Utils.h:115
std::string to_str(const T &val)
Make a string representation of val.
Definition: Utils.h:61
std::string basename(const std::string &p)
Get the basename (i.e. terminal file name) from a path p.
Definition: Utils.h:175
std::string to_upper(const std::string &s)
Convert a string to upper-case (not in-place)
Definition: Utils.h:145
bad_lexical_cast(const std::string &what)
Constructor.
Definition: Utils.h:42
T lexical_cast(const U &in)
Convert between types via stringstream.
Definition: Utils.h:47
std::vector< std::string > split(const std::string &s, const std::string &sep)
Split a string by a given separator.
Definition: Utils.h:95
size_t countchar(const std::string &s, const char c)
How many times does a string s contain the character c?
Definition: Utils.h:125
bool path_exists(const std::string &p, int mode=0)
Check if a path p (either file or dir) exists.
int in_open_range(double x, double low, double high)
Check if a number is in a range (open-open)
Definition: Utils.h:221
std::string join(const std::vector< std::string > &svec, const std::string &sep)
Concatenate strings with separator strings between each element.
Definition: Utils.h:85