Changeset 2270


Ignore:
Timestamp:
Jun 14, 2010, 6:05:59 AM (11 years ago)
Author:
Peter
Message:

is<T> and convert<T> is very similar so rather than duplicating the code let them call a private function. Optimize this function by avoiding calls to is_nan and friends. There is no need to check for is_nan (and friends) if the conversion works. Therefore, and since conversion is expected to most of the time, it is better to try conversion first and then only at failure check if the string is nan, inf or similar.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/utility.h

    r2248 r2270  
    88  Copyright (C) 2006 Jari Häkkinen
    99  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
    10   Copyright (C) 2009 Peter Johansson
     10  Copyright (C) 2009, 2010 Peter Johansson
    1111
    1212  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    183183// private namespace
    184184namespace 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
    185198  /**
    186199     Functor used in load function
     
    249262  T convert(const std::string& s)
    250263  {
    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))
    272266      throw runtime_error(std::string("yat::utility::convert(\"")+s+
    273267                          std::string("\")"));
    274     return a;
    275   }
     268    return result;
     269  }
     270
    276271
    277272  template<typename T>
    278273  bool is(const std::string& s)
    279274  {
    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
    297279
    298280  template<typename T>
     
    347329  }           
    348330
     331
     332namespace 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
    349360}}} // of namespace utility, yat, and theplu
    350361
Note: See TracChangeset for help on using the changeset viewer.