2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2015-01-10 10:13:57 +00:00
|
|
|
Copyright (c) 2011-2015 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2012-04-19 03:29:48 +00:00
|
|
|
#ifndef SPROUT_DETAIL_INT_HPP
|
|
|
|
#define SPROUT_DETAIL_INT_HPP
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2012-05-26 15:43:38 +00:00
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
2014-03-15 03:06:51 +00:00
|
|
|
#include <sprout/type_traits/is_sint.hpp>
|
2013-08-09 11:04:27 +00:00
|
|
|
#include <sprout/type_traits/is_uint.hpp>
|
2012-04-19 03:29:48 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
2015-12-09 10:54:35 +00:00
|
|
|
//
|
|
|
|
// int_digits_mf
|
|
|
|
//
|
|
|
|
template<typename IntType, IntType Val, int Base = 10, bool = Val != 0>
|
|
|
|
struct int_digits_mf_impl;
|
|
|
|
template<typename IntType, IntType Val, int Base>
|
|
|
|
struct int_digits_mf_impl<IntType, Val, Base, false>
|
|
|
|
: public sprout::integral_constant<IntType, 0>
|
|
|
|
{};
|
|
|
|
template<typename IntType, IntType Val, int Base>
|
|
|
|
struct int_digits_mf_impl<IntType, Val, Base, true>
|
|
|
|
: public sprout::integral_constant<IntType, 1 + sprout::detail::int_digits_mf_impl<IntType, Val / Base, Base>::value>
|
|
|
|
{};
|
|
|
|
template<typename IntType, IntType Val, int Base = 10, bool = Val != 0>
|
|
|
|
struct int_digits_mf;
|
|
|
|
template<typename IntType, IntType Val, int Base>
|
|
|
|
struct int_digits_mf<IntType, Val, Base, false>
|
|
|
|
: public sprout::integral_constant<IntType, 1>
|
|
|
|
{};
|
|
|
|
template<typename IntType, IntType Val, int Base>
|
|
|
|
struct int_digits_mf<IntType, Val, Base, true>
|
|
|
|
: public sprout::integral_constant<IntType, 1 + sprout::detail::int_digits_mf_impl<IntType, Val / Base, Base>::value>
|
|
|
|
{};
|
|
|
|
|
2012-04-19 03:29:48 +00:00
|
|
|
//
|
|
|
|
// int_pow
|
|
|
|
//
|
2012-10-05 15:58:56 +00:00
|
|
|
template<
|
|
|
|
typename IntType, int Base = 10,
|
|
|
|
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-04-19 03:29:48 +00:00
|
|
|
inline SPROUT_CONSTEXPR IntType
|
|
|
|
int_pow(int exponent) {
|
|
|
|
return exponent ? Base * sprout::detail::int_pow<IntType, Base>(exponent - 1)
|
|
|
|
: 1
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// int_digits
|
|
|
|
//
|
|
|
|
template<int Base = 10, typename IntType>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
int_digits_impl(IntType val) {
|
|
|
|
return val ? 1 + sprout::detail::int_digits_impl<Base>(val / Base)
|
|
|
|
: 0
|
|
|
|
;
|
|
|
|
}
|
2012-10-05 15:58:56 +00:00
|
|
|
template<
|
|
|
|
int Base = 10, typename IntType,
|
|
|
|
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-04-19 03:29:48 +00:00
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
int_digits(IntType val) {
|
|
|
|
return val ? 1 + sprout::detail::int_digits_impl<Base>(val / Base)
|
|
|
|
: 1
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// int_digit_at
|
|
|
|
//
|
2012-10-05 15:58:56 +00:00
|
|
|
template<
|
|
|
|
int Base = 10, typename IntType,
|
2014-03-15 03:06:51 +00:00
|
|
|
typename sprout::enabler_if<sprout::is_sint<IntType>::value>::type = sprout::enabler
|
2012-10-05 15:58:56 +00:00
|
|
|
>
|
2012-04-19 03:29:48 +00:00
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
int_digit_at(IntType val, int digits) {
|
|
|
|
return val < 0 ? -((val / sprout::detail::int_pow<IntType, Base>(digits)) % Base)
|
|
|
|
: (val / sprout::detail::int_pow<IntType, Base>(digits)) % Base
|
|
|
|
;
|
|
|
|
}
|
2013-08-09 11:04:27 +00:00
|
|
|
template<
|
|
|
|
int Base = 10, typename IntType,
|
|
|
|
typename sprout::enabler_if<sprout::is_uint<IntType>::value>::type = sprout::enabler
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
int_digit_at(IntType val, int digits) {
|
|
|
|
return (val / sprout::detail::int_pow<IntType, Base>(digits)) % Base;
|
|
|
|
}
|
2012-04-19 03:29:48 +00:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_DETAIL_INT_HPP
|