2 Copyright (c) Marshall Clow 2014.
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 2 Dec 2014 mtc First version; power
12 /// \file algorithm.hpp
13 /// \brief Misc Algorithms
14 /// \author Marshall Clow
17 #ifndef BOOST_ALGORITHM_HPP
18 #define BOOST_ALGORITHM_HPP
20 #include <functional> // for plus and multiplies
22 #include <boost/config.hpp>
23 #include <boost/utility/enable_if.hpp> // for boost::disable_if
24 #include <boost/type_traits/is_integral.hpp>
26 namespace boost { namespace algorithm {
29 BOOST_CXX14_CONSTEXPR T identity_operation ( std::multiplies<T> ) { return T(1); }
32 BOOST_CXX14_CONSTEXPR T identity_operation ( std::plus<T> ) { return T(0); }
35 /// \fn power ( T x, Integer n )
36 /// \return the value "x" raised to the power "n"
38 /// \param x The value to be exponentiated
39 /// \param n The exponent (must be >= 0)
41 // \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
42 // Seminumerical Algorithms, Section 4.6.3
43 template <typename T, typename Integer>
44 BOOST_CXX14_CONSTEXPR typename boost::enable_if<boost::is_integral<Integer>, T>::type
45 power (T x, Integer n) {
46 T y = 1; // Should be "T y{1};"
60 /// \fn power ( T x, Integer n, Operation op )
61 /// \return the value "x" raised to the power "n"
62 /// using the operation "op".
64 /// \param x The value to be exponentiated
65 /// \param n The exponent (must be >= 0)
66 /// \param op The operation used
68 // \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
69 // Seminumerical Algorithms, Section 4.6.3
70 template <typename T, typename Integer, typename Operation>
71 BOOST_CXX14_CONSTEXPR typename boost::enable_if<boost::is_integral<Integer>, T>::type
72 power (T x, Integer n, Operation op) {
73 T y = identity_operation(op);
89 #endif // BOOST_ALGORITHM_HPP