00001 #ifndef _theplu_yat_utility_utility_
00002 #define _theplu_yat_utility_utility_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00033
00034 #include "deprecate.h"
00035 #include "Exception.h"
00036
00037 #include <cmath>
00038 #include <limits>
00039 #include <istream>
00040 #include <string>
00041 #include <stdexcept>
00042 #include <sstream>
00043 #include <utility>
00044 #include <vector>
00045
00046 namespace theplu {
00047 namespace yat {
00048 namespace utility {
00049
00060 template<typename InputIterator, typename OutputIterator>
00061 bool binary_weight(InputIterator first, InputIterator last,
00062 OutputIterator result);
00063
00064
00070 template<typename T>
00071 T convert(const std::string& s);
00072
00078 template<typename T>
00079 bool is(const std::string& s);
00080
00087 bool is_double(const std::string&) YAT_DEPRECATE;
00088
00097 bool is_equal(std::string s, std::string other);
00098
00105 bool is_float(const std::string&) YAT_DEPRECATE;
00106
00113 bool is_int(const std::string&) YAT_DEPRECATE;
00114
00118 bool is_nan(const std::string& s);
00119
00154 template<typename T>
00155 void load(std::istream& is, std::vector<std::vector<T> >& vec, char sep='\0',
00156 char line_sep='\n', bool ignore_empty=false, bool rectangle=true);
00157
00178 template<typename T>
00179 void load(std::istream& is, std::vector<T>& vec, char sep='\0');
00180
00181
00182
00183 template<typename InputIterator, typename OutputIterator>
00184 bool binary_weight(InputIterator first, InputIterator last,
00185 OutputIterator result)
00186 {
00187 bool nan=false;
00188 while (first!=last) {
00189 if (std::isnan(*first)) {
00190 *result=0;
00191 nan=true;
00192 }
00193 else
00194 *result = 1.0;
00195 ++first;
00196 ++result;
00197 }
00198 return nan;
00199 }
00200
00201
00202
00203 template<typename T>
00204 T convert(const std::string& s)
00205 {
00206 if (is_nan(s))
00207 return std::numeric_limits<T>::quiet_NaN();
00208 if (is_equal(s, "inf"))
00209 return std::numeric_limits<T>::infinity();
00210 if (is_equal(s, "-inf")) {
00211 if (std::numeric_limits<T>::is_signed)
00212 return -std::numeric_limits<T>::infinity();
00213 else
00214 throw std::runtime_error(std::string("yat::utility::convert(\"")+s+
00215 std::string("\"): type is unsigned") );
00216 }
00217 std::stringstream ss(s);
00218 T a;
00219 ss >> a;
00220 bool ok = true;
00221 if(ss.fail())
00222 ok = false;
00223
00224 std::string b;
00225 ss >> b;
00226 if (!b.empty() || !ok)
00227 throw std::runtime_error(std::string("yat::utility::convert(\"")+s+
00228 std::string("\")"));
00229 return a;
00230 }
00231
00232 template<typename T>
00233 bool is(const std::string& s)
00234 {
00235 if (is_nan(s))
00236 return std::numeric_limits<T>::has_quiet_NaN;
00237 if (is_equal(s, "inf"))
00238 return std::numeric_limits<T>::has_infinity;
00239 if (is_equal(s, "-inf"))
00240 return std::numeric_limits<T>::has_infinity &&
00241 std::numeric_limits<T>::is_signed;
00242 std::stringstream ss(s);
00243 T a;
00244 ss >> a;
00245 if(ss.fail())
00246 return false;
00247
00248 std::string b;
00249 ss >> b;
00250 return b.empty();
00251 }
00252
00253 template<typename T>
00254 void load(std::istream& is, std::vector<std::vector<T> >& matrix,
00255 char sep, char line_sep, bool ignore_empty,
00256 bool rectangle)
00257 {
00258 size_t nof_columns=0;
00259 std::string line;
00260 while(getline(is, line, line_sep)){
00261 if (line.empty() && ignore_empty)
00262 continue;
00263 matrix.resize(matrix.size()+1);
00264 std::vector<T>& v=matrix.back();
00265 v.reserve(nof_columns);
00266 std::stringstream ss(line);
00267 load(ss, v, sep);
00268
00269 if(sep!='\0' && !line.empty() && line[line.size()-1]==sep)
00270 v.push_back(std::numeric_limits<T>::quiet_NaN());
00271
00272 if (rectangle && nof_columns && v.size()!=nof_columns) {
00273 std::ostringstream s;
00274 s << "load data file error: "
00275 << "line " << matrix.size() << " has " << v.size()
00276 << " columns; expected " << nof_columns << " columns.";
00277 throw utility::IO_error(s.str());
00278 }
00279 nof_columns = std::max(nof_columns, v.size());
00280 }
00281
00282
00283 is.clear(std::ios::goodbit);
00284 }
00285
00286 template<typename T>
00287 void load(std::istream& is, std::vector<T>& vec, char sep='\0')
00288 {
00289 std::string element;
00290 bool ok=true;
00291 while(ok) {
00292 if(sep=='\0')
00293 ok=(is>>element);
00294 else
00295 ok=getline(is, element, sep);
00296 if(!ok)
00297 break;
00298
00299 if (!element.size())
00300 vec.push_back(std::numeric_limits<T>::quiet_NaN());
00301 else {
00302 vec.push_back(convert<T>(element));
00303 }
00304 }
00305 }
00306
00307 }}}
00308
00309 #endif