File indexing completed on 2025-01-18 09:28:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef BOOST_ALGORITHM_HPP
0018 #define BOOST_ALGORITHM_HPP
0019
0020 #include <functional> // for plus and multiplies
0021
0022 #include <boost/config.hpp>
0023 #include <boost/core/enable_if.hpp> // for boost::disable_if
0024 #include <boost/type_traits/is_integral.hpp>
0025
0026 namespace boost { namespace algorithm {
0027
0028 template <typename T>
0029 BOOST_CXX14_CONSTEXPR T identity_operation ( std::multiplies<T> ) { return T(1); }
0030
0031 template <typename T>
0032 BOOST_CXX14_CONSTEXPR T identity_operation ( std::plus<T> ) { return T(0); }
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 template <typename T, typename Integer>
0044 BOOST_CXX14_CONSTEXPR typename boost::enable_if<boost::is_integral<Integer>, T>::type
0045 power (T x, Integer n) {
0046 T y = 1;
0047 if (n == 0) return y;
0048 while (true) {
0049 if (n % 2 == 1) {
0050 y = x * y;
0051 if (n == 1)
0052 return y;
0053 }
0054 n = n / 2;
0055 x = x * x;
0056 }
0057 return y;
0058 }
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 template <typename T, typename Integer, typename Operation>
0071 BOOST_CXX14_CONSTEXPR typename boost::enable_if<boost::is_integral<Integer>, T>::type
0072 power (T x, Integer n, Operation op) {
0073 T y = identity_operation(op);
0074 if (n == 0) return y;
0075 while (true) {
0076 if (n % 2 == 1) {
0077 y = op(x, y);
0078 if (n == 1)
0079 return y;
0080 }
0081 n = n / 2;
0082 x = op(x, x);
0083 }
0084 return y;
0085 }
0086
0087 }}
0088
0089 #endif