yat  0.13.2pre
utility.h
Go to the documentation of this file.
1 #ifndef _theplu_yat_utility_utility_
2 #define _theplu_yat_utility_utility_
3 
4 // $Id: utility.h 3417 2015-05-25 01:35:59Z peter $
5 
6 /*
7  Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér
8  Copyright (C) 2006 Jari Häkkinen
9  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
10  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Peter Johansson
11 
12  This file is part of the yat library, http://dev.thep.lu.se/yat
13 
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 3 of the
17  License, or (at your option) any later version.
18 
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with yat. If not, see <http://www.gnu.org/licenses/>.
26 */
27 
33 
34 #include "concept_check.h"
35 #include "deprecate.h"
36 #include "Exception.h"
37 #include "iterator_traits.h"
38 #include "WeightIterator.h"
39 #include "yat_assert.h"
40 
41 #include <gsl/gsl_math.h>
42 
43 #include <boost/iterator/iterator_concepts.hpp>
44 #include <boost/concept_check.hpp>
45 
46 #include <algorithm>
47 #include <cctype>
48 #include <cmath>
49 #include <functional>
50 #include <limits>
51 #include <locale>
52 #include <istream>
53 #include <numeric>
54 #include <string>
55 #include <stdexcept>
56 #include <sstream>
57 #include <utility>
58 #include <vector>
59 
60 namespace theplu {
61 namespace yat {
62 namespace utility {
63 
74  std::string basename(const std::string& fn);
75 
86  template<typename InputIterator, typename OutputIterator>
87  bool binary_weight(InputIterator first, InputIterator last,
88  OutputIterator result);
89 
100  void chdir(const std::string& dir);
101 
110  void chmod(const std::string& filename, mode_t mode);
111 
119  template<typename T>
120  std::string convert(T input);
121 
127  template<typename T>
128  T convert(const std::string& s);
129 
136  void copy_file(const std::string& source, const std::string& target);
137 
148  std::string dirname(const std::string& fn);
149 
160  bool fnmatch(const std::string& pattern, const std::string& str,
161  int flags=0);
162 
168  template<typename T>
169  bool is(const std::string& s);
170 
177  bool is_double(const std::string&) YAT_DEPRECATE;
178 
187  bool is_equal(std::string s, std::string other);
188 
195  bool is_float(const std::string&) YAT_DEPRECATE;
196 
203  bool is_int(const std::string&) YAT_DEPRECATE;
204 
208  bool is_nan(const std::string& s);
209 
245  template<typename T>
246  void load(std::istream& is, std::vector<std::vector<T> >& vec, char sep='\0',
247  char line_sep='\n', bool ignore_empty=false, bool rectangle=true);
248 
270  template<typename T>
271  void load(std::istream& is, std::vector<T>& vec, char sep='\0');
272 
278  // c++11 provides std::log2 so perhaps we should call that one if
279  // availalable (but a bit tricky since this is a public header)
280  template<typename T>
281  T log2(T x) { return std::log(x)/M_LN2; }
282 
292  void mkdir(const std::string& dir, mode_t mode=0777);
293 
301  void mkdir_p(const std::string& dir, mode_t mode=0777);
302 
310  void remove(const std::string& fn);
311 
320  void rename(const std::string& from, const std::string& to);
321 
328  void replace(std::string& full_str, std::string old_str, std::string new_str);
329 
330 
343  template<typename Iterator>
344  double sum_weight(Iterator first, Iterator last);
345 
347 
348 // private namespace
349 namespace detail {
350 
362  template<typename T>
363  bool convert(const std::string& s, T& t);
364 
368  template<typename T>
369  struct VectorPusher
370  {
376  void operator()(const std::string& element, std::vector<T>& vec)
377  {
378  if (!element.size())
379  vec.push_back(std::numeric_limits<T>::quiet_NaN());
380  else {
381  vec.push_back(theplu::yat::utility::convert<T>(element));
382  }
383  }
384  };
385 
391  template<>
392  struct VectorPusher<std::string>
393  {
397  void operator()(const std::string& element, std::vector<std::string>& vec)
398  {
399  vec.push_back(element);
400  }
401  };
402 
403 
404  template<typename Iterator>
405  double sum_weight(Iterator first, Iterator last, unweighted_iterator_tag tag)
406  {
407  return std::distance(first, last);
408  }
409 
410 
411  template<typename Iterator>
412  double sum_weight(Iterator first, Iterator last, weighted_iterator_tag tag)
413  {
414  return std::accumulate(weight_iterator(first), weight_iterator(last), 0);
415  }
416 
417 
418 } // end of namespace detail
419 
421 
422  // template implementations
423 
424  template<typename InputIterator, typename OutputIterator>
425  bool binary_weight(InputIterator first, InputIterator last,
426  OutputIterator result)
427  {
428  bool nan=false;
429  while (first!=last) {
430  if (std::isnan(*first)) {
431  *result=0;
432  nan=true;
433  }
434  else
435  *result = 1.0;
436  ++first;
437  ++result;
438  }
439  return nan;
440  }
441 
442 
443  template<typename T>
444  std::string convert(T input)
445  {
446  std::ostringstream ss;
447  ss << input;
448  return ss.str();
449  }
450 
451 
452  template<typename T>
453  T convert(const std::string& s)
454  {
455  T result;
456  if (!detail::convert(s, result))
457  throw runtime_error(std::string("yat::utility::convert(\"")+s+
458  std::string("\")"));
459  return result;
460  }
461 
462 
463  template<typename T>
464  bool is(const std::string& s)
465  {
466  T tmp;
467  return detail::convert(s, tmp);
468  }
469 
470 
471  template<typename T>
472  void load(std::istream& is, std::vector<std::vector<T> >& matrix,
473  char sep, char line_sep, bool ignore_empty,
474  bool rectangle)
475  {
476  size_t nof_columns=0;
477  std::string line;
478  while(getline(is, line, line_sep)){
479  if (line.empty() && ignore_empty)
480  continue;
481  matrix.push_back(std::vector<T>());
482  std::vector<T>& v=matrix.back();
483  v.reserve(nof_columns);
484  std::stringstream ss(line);
485  load(ss, v, sep);
486  // add NaN for final separator (or empty string if T=std::string)
487  detail::VectorPusher<T> pusher;
488  if(sep!='\0' && !line.empty() && line[line.size()-1]==sep)
489  pusher("", v);
490 
491  if (rectangle && nof_columns && v.size()!=nof_columns) {
492  std::ostringstream s;
493  s << "load stream error: "
494  << "line " << matrix.size() << " has " << v.size()
495  << " columns; expected " << nof_columns << " columns.";
496  throw utility::IO_error(s.str());
497  }
498  nof_columns = std::max(nof_columns, v.size());
499  }
500 
501  // manipulate the state of the stream to be good
502  is.clear(std::ios::goodbit);
503  }
504 
505  template<typename T>
506  void load(std::istream& is, std::vector<T>& vec, char sep)
507  {
508  detail::VectorPusher<T> pusher;
509  std::string element;
510  bool ok=true;
511  while(true) {
512  if(sep=='\0')
513  ok=(is>>element);
514  else
515  ok=getline(is, element, sep);
516  if(!ok)
517  break;
518  pusher(element, vec);
519  }
520  }
521 
522 
523  template<typename Iterator>
524  double sum_weight(Iterator first, Iterator last)
525  {
526  BOOST_CONCEPT_ASSERT((DataIteratorConcept<Iterator>));
527  BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<Iterator>));
528  BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<Iterator>));
530  return detail::sum_weight(first, last, tag);
531  }
532 
533 
535 namespace detail {
536  template<typename T>
537  bool convert(const std::string& s, T& result)
538  {
539  if (!std::numeric_limits<T>::is_signed) {
540  // first non-whitespace character
541  std::string::const_iterator iter = s.begin();
542  while (iter!=s.end() && std::isspace(*iter))
543  ++iter;
544  // unsigned int cannot start with a '-' and with some compilers
545  // operation ss >> result won't fail so catch it like this instead.
546  if (iter==s.end() || *iter=='-')
547  return false;
548  }
549  std::istringstream ss(s);
550  ss >> result;
551  if (ss.fail()) {
552  if (is_nan(s) || is_equal(s, "-nan")) {
553  result = std::numeric_limits<T>::quiet_NaN();
554  return true;
555  }
556  if (is_equal(s, "inf")) {
557  result = std::numeric_limits<T>::infinity();
558  return true;
559  }
560  if (is_equal(s, "-inf")) {
561  // unsigned types are caught in prologue
562  YAT_ASSERT(std::numeric_limits<T>::is_signed);
563  result = -std::numeric_limits<T>::infinity();
564  return true;
565  }
566  return false;
567  }
568  // Check that nothing is left on stream
569  std::string b;
570  ss >> b;
571  return b.empty();
572  }
573 } // of namespace detail
574 
576 
577 }}} // of namespace utility, yat, and theplu
578 
579 #endif
bool is(const std::string &s)
check if string is convertible to (numerical) type T
Definition: utility.h:464
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:226
double sum_weight(Iterator first, Iterator last)
Definition: utility.h:524
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:683
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:425
T log2(T x)
Definition: utility.h:281
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:472
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:444

Generated on Wed Jan 4 2017 02:23:07 for yat by  doxygen 1.8.5