1 #ifndef _theplu_yat_utility_utility_
2 #define _theplu_yat_utility_utility_
35 #include "concept_check.h"
37 #include "Exception.h"
38 #include "iterator_traits.h"
39 #include "WeightIterator.h"
40 #include "yat_assert.h"
42 #include <gsl/gsl_math.h>
44 #include <boost/iterator/iterator_concepts.hpp>
45 #include <boost/concept_check.hpp>
76 std::string
basename(
const std::string& fn);
97 template<
typename InputIterator,
typename OutputIterator>
99 OutputIterator result);
111 void chdir(
const std::string& dir);
121 void chmod(
const std::string& filename, mode_t mode);
139 T
convert(
const std::string& s);
147 void copy_file(
const std::string& source,
const std::string& target);
160 std::string
dirname(
const std::string& fn);
172 bool fnmatch(
const std::string& pattern,
const std::string& str,
181 bool is(
const std::string& s);
189 bool is_double(
const std::string&) YAT_DEPRECATE;
199 bool is_equal(std::
string s, std::
string other);
207 bool is_float(const std::
string&) YAT_DEPRECATE;
215 bool is_int(const std::
string&) YAT_DEPRECATE;
220 bool is_nan(const std::
string& s);
258 void load(std::istream&
is, std::vector<std::vector<T> >& vec,
char sep='\0',
259 char line_sep='\n',
bool ignore_empty=false,
bool rectangle=true);
283 void load(std::istream& is, std::vector<T>& vec,
char sep='\0');
295 T
log2(T x) {
return std::log(x)/M_LN2; }
306 void mkdir(
const std::string& dir, mode_t mode=0777);
315 void mkdir_p(
const std::string& dir, mode_t mode=0777);
324 void remove(
const std::string& fn);
334 void rename(
const std::string& from,
const std::string& to);
342 void replace(std::string& full_str, std::string old_str, std::string new_str);
357 template<
typename Iterator>
358 double sum_weight(Iterator first, Iterator last);
377 bool convert(
const std::string& s, T& t);
390 void operator()(
const std::string& element, std::vector<T>& vec)
393 vec.push_back(std::numeric_limits<T>::quiet_NaN());
395 vec.push_back(theplu::yat::utility::convert<T>(element));
406 struct VectorPusher<std::string>
411 void operator()(
const std::string& element, std::vector<std::string>& vec)
413 vec.push_back(element);
418 template<
typename Iterator>
419 double sum_weight(Iterator first, Iterator last, unweighted_iterator_tag tag)
421 return std::distance(first, last);
425 template<
typename Iterator>
426 double sum_weight(Iterator first, Iterator last, weighted_iterator_tag tag)
428 return std::accumulate(weight_iterator(first), weight_iterator(last), 0);
438 template<
typename InputIterator,
typename OutputIterator>
440 OutputIterator result)
442 BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator>));
443 BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator>));
444 BOOST_CONCEPT_ASSERT((boost_concepts::IncrementableIterator<OutputIterator>));
445 BOOST_CONCEPT_ASSERT((boost_concepts::WritableIterator<OutputIterator, float>));
448 while (first!=last) {
449 if (std::isnan(*first)) {
465 std::ostringstream ss;
476 throw runtime_error(std::string(
"yat::utility::convert(\"")+s+
483 bool is(
const std::string& s)
491 void load(std::istream&
is, std::vector<std::vector<T> >& matrix,
492 char sep,
char line_sep,
bool ignore_empty,
495 size_t nof_columns=0;
497 while(getline(is, line, line_sep)){
498 if (line.empty() && ignore_empty)
500 matrix.push_back(std::vector<T>());
501 std::vector<T>& v=matrix.back();
502 v.reserve(nof_columns);
503 std::stringstream ss(line);
506 detail::VectorPusher<T> pusher;
507 if(sep!=
'\0' && !line.empty() && line[line.size()-1]==sep)
510 if (rectangle && nof_columns && v.size()!=nof_columns) {
511 std::ostringstream s;
512 s <<
"load stream error: "
513 <<
"line " << matrix.size() <<
" has " << v.size()
514 <<
" columns; expected " << nof_columns <<
" columns.";
517 nof_columns =
std::max(nof_columns, v.size());
521 is.clear(std::ios::goodbit);
525 void load(std::istream&
is, std::vector<T>& vec,
char sep)
527 detail::VectorPusher<T> pusher;
532 ok=!(is>>element).fail();
534 ok=!getline(is, element, sep).fail();
537 pusher(element, vec);
542 template<
typename Iterator>
546 BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<Iterator>));
547 BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<Iterator>));
556 bool convert(
const std::string& s, T& result)
558 if (!std::numeric_limits<T>::is_signed) {
560 std::string::const_iterator iter = s.begin();
561 while (iter!=s.end() && std::isspace(*iter))
565 if (iter==s.end() || *iter==
'-')
568 std::istringstream ss(s);
572 result = std::numeric_limits<T>::quiet_NaN();
576 result = std::numeric_limits<T>::infinity();
581 YAT_ASSERT(std::numeric_limits<T>::is_signed);
582 result = -std::numeric_limits<T>::infinity();
bool is(const std::string &s)
check if string is convertible to (numerical) type T
Definition: utility.h:483
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:543
void rename(const std::string &from, const std::string &to)
void copy_file(const std::string &source, const std::string &target)
Copy file source to target.
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
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:439
T log2(T x)
Definition: utility.h:295
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:491
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:463