123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #ifndef BOOST_GEOMETRY_UTIL_RATIONAL_HPP
- #define BOOST_GEOMETRY_UTIL_RATIONAL_HPP
- #include <boost/rational.hpp>
- #include <boost/geometry/util/bounds.hpp>
- #include <boost/geometry/util/coordinate_cast.hpp>
- #include <boost/geometry/util/numeric_cast.hpp>
- #include <boost/geometry/util/select_most_precise.hpp>
- namespace boost{ namespace geometry
- {
- namespace detail
- {
- template <typename T>
- struct coordinate_cast<rational<T> >
- {
- static inline void split_parts(std::string const& source, std::string::size_type p,
- T& before, T& after, bool& negate, std::string::size_type& len)
- {
- std::string before_part = source.substr(0, p);
- std::string const after_part = source.substr(p + 1);
- negate = false;
- if (before_part.size() > 0 && before_part[0] == '-')
- {
- negate = true;
- before_part.erase(0, 1);
- }
- before = atol(before_part.c_str());
- after = atol(after_part.c_str());
- len = after_part.length();
- }
- static inline rational<T> apply(std::string const& source)
- {
- T before, after;
- bool negate;
- std::string::size_type len;
-
-
- std::string::size_type p = source.find('.');
- if (p == std::string::npos)
- {
- p = source.find('/');
- if (p == std::string::npos)
- {
- return rational<T>(atol(source.c_str()));
- }
- split_parts(source, p, before, after, negate, len);
- return negate
- ? -rational<T>(before, after)
- : rational<T>(before, after)
- ;
- }
- split_parts(source, p, before, after, negate, len);
- T den = 1;
- for (std::string::size_type i = 0; i < len; i++)
- {
- den *= 10;
- }
- return negate
- ? -rational<T>(before) - rational<T>(after, den)
- : rational<T>(before) + rational<T>(after, den)
- ;
- }
- };
- }
- template <typename T1, typename T2>
- struct select_most_precise<boost::rational<T1>, boost::rational<T2> >
- {
- typedef typename boost::rational
- <
- typename select_most_precise<T1, T2>::type
- > type;
- };
- template <typename T>
- struct select_most_precise<boost::rational<T>, double>
- {
- typedef typename boost::rational<T> type;
- };
- namespace util
- {
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail
- {
- template <typename Target, typename T>
- struct numeric_caster<Target, rational<T>>
- {
- static inline Target apply(rational<T> const& source)
- {
- return boost::rational_cast<Target>(source);
- }
- };
- }
- #endif
- template<class T>
- struct bounds<rational<T> >
- {
- static inline rational<T> lowest()
- {
- return rational<T>(bounds<T>::lowest(), 1);
- }
- static inline rational<T> highest()
- {
- return rational<T>(bounds<T>::highest(), 1);
- }
- };
- }
- }}
- #endif
|