Changeset 1105
- Timestamp:
- Jun 20, 2010, 9:02:18 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/yat/utility.h
r1058 r1105 2 2 #define _theplu_yat_utility_utility_ 3 3 4 // $Id: utility.h 22 48 2010-04-22 00:57:13Z peter $4 // $Id: utility.h 2273 2010-06-15 02:11:47Z peter $ 5 5 6 6 /* … … 8 8 Copyright (C) 2006 Jari Häkkinen 9 9 Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson 10 Copyright (C) 2009 Peter Johansson10 Copyright (C) 2009, 2010 Peter Johansson 11 11 12 12 This file is part of the yat library, http://dev.thep.lu.se/yat … … 183 183 // private namespace 184 184 namespace detail { 185 186 /** 187 \brief convert s to t 188 189 used in function is<T> and convert<T> 190 191 \return true if conversion was successful 192 193 \internal 194 */ 195 template<typename T> 196 bool convert(const std::string& s, T& t); 197 185 198 /** 186 199 Functor used in load function … … 249 262 T convert(const std::string& s) 250 263 { 251 if (is_nan(s)) 252 return std::numeric_limits<T>::quiet_NaN(); 253 if (is_equal(s, "inf")) 254 return std::numeric_limits<T>::infinity(); 255 if (is_equal(s, "-inf")) { 256 if (std::numeric_limits<T>::is_signed) 257 return -std::numeric_limits<T>::infinity(); 258 else 259 throw runtime_error(std::string("yat::utility::convert(\"")+s+ 260 std::string("\"): type is unsigned") ); 261 } 262 std::stringstream ss(s); 263 T a; 264 ss >> a; 265 bool ok = true; 266 if(ss.fail()) 267 ok = false; 268 // Check that nothing is left on stream 269 std::string b; 270 ss >> b; 271 if (!b.empty() || !ok) 264 T result; 265 if (!detail::convert(s, result)) 272 266 throw runtime_error(std::string("yat::utility::convert(\"")+s+ 273 267 std::string("\")")); 274 return a; 275 } 268 return result; 269 } 270 276 271 277 272 template<typename T> 278 273 bool is(const std::string& s) 279 274 { 280 if (is_nan(s)) 281 return std::numeric_limits<T>::has_quiet_NaN; 282 if (is_equal(s, "inf")) 283 return std::numeric_limits<T>::has_infinity; 284 if (is_equal(s, "-inf")) 285 return std::numeric_limits<T>::has_infinity && 286 std::numeric_limits<T>::is_signed; 287 std::stringstream ss(s); 288 T a; 289 ss >> a; 290 if(ss.fail()) 291 return false; 292 // Check that nothing is left on stream 293 std::string b; 294 ss >> b; 295 return b.empty(); 296 } 275 T tmp; 276 return detail::convert(s, tmp); 277 } 278 297 279 298 280 template<typename T> … … 306 288 if (line.empty() && ignore_empty) 307 289 continue; 308 matrix. resize(matrix.size()+1);309 std::vector< double>& v=matrix.back();290 matrix.push_back(std::vector<T>()); 291 std::vector<T>& v=matrix.back(); 310 292 v.reserve(nof_columns); 311 293 std::stringstream ss(line); 312 294 load(ss, v, sep); 313 // add NaN for final separator 295 // add NaN for final separator (or empty string if T=std::string) 296 detail::VectorPusher<T> pusher; 314 297 if(sep!='\0' && !line.empty() && line[line.size()-1]==sep) 315 v.push_back(std::numeric_limits<T>::quiet_NaN());298 pusher("", v); 316 299 317 300 if (rectangle && nof_columns && v.size()!=nof_columns) { … … 342 325 if(!ok) 343 326 break; 344 345 327 pusher(element, vec); 346 328 } 347 329 } 348 330 331 332 namespace detail { 333 template<typename T> 334 bool convert(const std::string& s, T& result) 335 { 336 std::istringstream ss(s); 337 ss >> result; 338 if (ss.fail()) { 339 if (is_nan(s)) { 340 result = std::numeric_limits<T>::quiet_NaN(); 341 return true; 342 } 343 if (is_equal(s, "inf")) { 344 result = std::numeric_limits<T>::infinity(); 345 return true; 346 } 347 if (std::numeric_limits<T>::is_signed && is_equal(s, "-inf")) { 348 result = -std::numeric_limits<T>::infinity(); 349 return true; 350 } 351 return false; 352 } 353 // Check that nothing is left on stream 354 std::string b; 355 ss >> b; 356 return b.empty(); 357 } 358 } // of namespace detail 359 349 360 }}} // of namespace utility, yat, and theplu 350 361
Note: See TracChangeset
for help on using the changeset viewer.