1 #ifndef _theplu_yat_utility_utility_
2 #define _theplu_yat_utility_utility_
35 #include "Exception.h"
36 #include "yat_assert.h"
65 template<
typename InputIterator,
typename OutputIterator>
67 OutputIterator result);
85 T
convert(
const std::string& s);
93 bool is(
const std::string& s);
101 bool is_double(
const std::string&) YAT_DEPRECATE;
111 bool is_equal(std::
string s, std::
string other);
119 bool is_float(const std::
string&) YAT_DEPRECATE;
127 bool is_int(const std::
string&) YAT_DEPRECATE;
132 bool is_nan(const std::
string& s);
170 void load(std::istream&
is, std::vector<std::vector<T> >& vec,
char sep='\0',
171 char line_sep='\n',
bool ignore_empty=false,
bool rectangle=true);
195 void load(std::istream& is, std::vector<T>& vec,
char sep='\0');
210 bool convert(
const std::string& s, T& t);
223 void operator()(
const std::string& element, std::vector<T>& vec)
226 vec.push_back(std::numeric_limits<T>::quiet_NaN());
228 vec.push_back(theplu::yat::utility::convert<T>(element));
244 void operator()(
const std::string& element, std::vector<std::string>& vec)
246 vec.push_back(element);
255 template<
typename InputIterator,
typename OutputIterator>
257 OutputIterator result)
260 while (first!=last) {
261 if (std::isnan(*first)) {
278 std::ostringstream ss;
289 throw runtime_error(std::string(
"yat::utility::convert(\"")+s+
296 bool is(
const std::string& s)
304 void load(std::istream&
is, std::vector<std::vector<T> >& matrix,
305 char sep,
char line_sep,
bool ignore_empty,
308 size_t nof_columns=0;
310 while(getline(is, line, line_sep)){
311 if (line.empty() && ignore_empty)
313 matrix.push_back(std::vector<T>());
314 std::vector<T>& v=matrix.back();
315 v.reserve(nof_columns);
316 std::stringstream ss(line);
320 if(sep!=
'\0' && !line.empty() && line[line.size()-1]==sep)
323 if (rectangle && nof_columns && v.size()!=nof_columns) {
324 std::ostringstream s;
325 s <<
"load stream error: "
326 <<
"line " << matrix.size() <<
" has " << v.size()
327 <<
" columns; expected " << nof_columns <<
" columns.";
330 nof_columns =
std::max(nof_columns, v.size());
334 is.clear(std::ios::goodbit);
338 void load(std::istream&
is, std::vector<T>& vec,
char sep)
347 ok=getline(is, element, sep);
350 pusher(element, vec);
357 bool convert(
const std::string& s, T& result)
359 if (!std::numeric_limits<T>::is_signed) {
361 std::string::const_iterator iter = s.begin();
362 while (iter!=s.end() && std::isspace(*iter))
366 if (iter==s.end() || *iter==
'-')
369 std::istringstream ss(s);
373 result = std::numeric_limits<T>::quiet_NaN();
377 result = std::numeric_limits<T>::infinity();
382 YAT_ASSERT(std::numeric_limits<T>::is_signed);
383 result = -std::numeric_limits<T>::infinity();