1 #ifndef _theplu_yat_utility_utility_ 2 #define _theplu_yat_utility_utility_ 36 #include "concept_check.h" 38 #include "Exception.h" 39 #include "iterator_traits.h" 40 #include "WeightIterator.h" 41 #include "yat_assert.h" 43 #include <gsl/gsl_math.h> 45 #include <boost/iterator/iterator_concepts.hpp> 46 #include <boost/concept_check.hpp> 77 std::string
basename(
const std::string& fn);
98 template<
typename InputIterator,
typename OutputIterator>
100 OutputIterator result);
112 void chdir(
const std::string& dir);
122 void chmod(
const std::string& filename, mode_t mode);
140 T
convert(
const std::string& s);
161 void copy_file(
const std::string& source,
const std::string& target);
174 std::string
dirname(
const std::string& fn);
186 bool fnmatch(
const std::string& pattern,
const std::string& str,
195 bool is(
const std::string& s);
203 bool is_double(
const std::string&) YAT_DEPRECATE;
229 bool is_int(const
std::
string&) YAT_DEPRECATE;
272 void load(
std::istream&
is,
std::vector<
std::vector<T> >& vec,
char sep='\0',
273 char line_sep='\n',
bool ignore_empty=false,
bool rectangle=true);
297 void load(
std::istream& is,
std::vector<T>& vec,
char sep='\0');
310 T
log2(T x) {
return std::log(x)/M_LN2; }
322 void mkdir(
const std::string& dir, mode_t mode=0777);
331 void mkdir_p(
const std::string& dir, mode_t mode=0777);
340 void remove(
const std::string& fn);
350 void rename(
const std::string& from,
const std::string& to);
358 void replace(std::string& full_str, std::string old_str, std::string new_str);
373 template<
typename Iterator>
374 double sum_weight(Iterator first, Iterator last);
392 void operator()(
const std::string& element, std::vector<T>& vec)
395 vec.push_back(std::numeric_limits<T>::quiet_NaN());
397 vec.push_back(theplu::yat::utility::convert<T>(element));
408 struct VectorPusher<std::string>
413 void operator()(
const std::string& element, std::vector<std::string>& vec)
415 vec.push_back(element);
420 template<
typename Iterator>
423 return std::distance(first, last);
427 template<
typename Iterator>
430 return std::accumulate(weight_iterator(first), weight_iterator(last), 0);
440 template<
typename InputIterator,
typename OutputIterator>
442 OutputIterator result)
444 BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator>));
445 BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator>));
446 BOOST_CONCEPT_ASSERT((boost_concepts::IncrementableIterator<OutputIterator>));
447 BOOST_CONCEPT_ASSERT((boost_concepts::WritableIterator<OutputIterator, float>));
450 while (first!=last) {
451 if (std::isnan(*first)) {
467 std::ostringstream ss;
478 throw runtime_error(std::string(
"yat::utility::convert(\"")+s+
485 bool is(
const std::string& s)
493 void load(std::istream& is, std::vector<std::vector<T> >& matrix,
494 char sep,
char line_sep,
bool ignore_empty,
497 size_t nof_columns=0;
499 while(getline(is, line, line_sep)){
500 if (line.empty() && ignore_empty)
502 matrix.push_back(std::vector<T>());
503 std::vector<T>& v=matrix.back();
504 v.reserve(nof_columns);
505 std::stringstream ss(line);
508 detail::VectorPusher<T> pusher;
509 if(sep!=
'\0' && !line.empty() && line[line.size()-1]==sep)
512 if (rectangle && nof_columns && v.size()!=nof_columns) {
513 std::ostringstream s;
514 s <<
"load stream error: " 515 <<
"line " << matrix.size() <<
" has " << v.size()
516 <<
" columns; expected " << nof_columns <<
" columns.";
519 nof_columns =
std::max(nof_columns, v.size());
523 is.clear(std::ios::goodbit);
527 void load(std::istream& is, std::vector<T>& vec,
char sep)
529 detail::VectorPusher<T> pusher;
534 ok=!(is>>element).fail();
536 ok=!getline(is, element, sep).fail();
539 pusher(element, vec);
544 template<
typename Iterator>
548 BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<Iterator>));
549 BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<Iterator>));
559 if (!std::numeric_limits<T>::is_signed) {
561 std::string::const_iterator iter = s.begin();
562 while (iter!=s.end() && std::isspace(*iter))
566 if (iter==s.end() || *iter==
'-')
569 std::istringstream ss(s);
573 result = std::numeric_limits<T>::quiet_NaN();
577 result = std::numeric_limits<T>::infinity();
582 YAT_ASSERT(std::numeric_limits<T>::is_signed);
583 result = -std::numeric_limits<T>::infinity();
bool is(const std::string &s)
check if string is convertible to (numerical) type T
Definition: utility.h:485
bool is_int(const std::string &)
detail::weighted_iterator_traits_detail< value >::type type
Definition: iterator_traits.h:114
void chdir(const std::string &dir)
std::string dirname(const std::string &fn)
Concept check for Data Iterator.
Definition: concept_check.h:228
double sum_weight(Iterator first, Iterator last)
Definition: utility.h:545
void rename(const std::string &from, const std::string &to)
The Department of Theoretical Physics namespace as we define it.
void copy_file(const std::string &source, const std::string &target)
Copy file source to target.
Definition: iterator_traits.h:47
Definition: stl_utility.h:64
void replace(std::string &full_str, std::string old_str, std::string new_str)
bool is_double(const std::string &)
bool is_float(const std::string &)
void mkdir_p(const std::string &dir, mode_t mode=0777)
T max(const T &a, const T &b, const T &c)
Definition: stl_utility.h:697
Definition: iterator_traits.h:55
Class used for all runtime error detected within yat library.
Definition: Exception.h:38
void chmod(const std::string &filename, mode_t mode)
bool is_nan(const std::string &s)
bool is_equal(std::string s, std::string other)
bool fnmatch(const std::string &pattern, const std::string &str, int flags=0)
Class to report errors associated with IO operations.
Definition: Exception.h:109
bool binary_weight(InputIterator first, InputIterator last, OutputIterator result)
Definition: utility.h:441
T log2(T x)
Definition: utility.h:310
void load(std::istream &is, std::vector< std::vector< T > > &vec, char sep='\0', char line_sep='\n', bool ignore_empty=false, bool rectangle=true)
Definition: utility.h:493
bool convert_try(const std::string &s, T &t)
try to convert
Definition: utility.h:557
std::string basename(const std::string &fn)
void mkdir(const std::string &dir, mode_t mode=0777)
create a directory dir
std::string convert(T input)
convert T to a string
Definition: utility.h:465