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/utility/enable_if.hpp> // for boost::disable_if
23 #include <boost/type_traits/is_integral.hpp>
25 namespace boost { namespace algorithm {
28 T identity_operation ( std::multiplies<T> ) { return T(1); }
31 T identity_operation ( std::plus<T> ) { return T(0); }
34 /// \fn power ( T x, Integer n )
35 /// \return the value "x" raised to the power "n"
37 /// \param x The value to be exponentiated
38 /// \param n The exponent (must be >= 0)
40 // \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
41 // Seminumerical Algorithms, Section 4.6.3
42 template <typename T, typename Integer>
43 typename boost::enable_if<boost::is_integral<Integer>, T>::type
44 power (T x, Integer n) {
45 T y = 1; // Should be "T y{1};"
59 /// \fn power ( T x, Integer n, Operation op )
60 /// \return the value "x" raised to the power "n"
61 /// using the operation "op".
63 /// \param x The value to be exponentiated
64 /// \param n The exponent (must be >= 0)
65 /// \param op The operation used
67 // \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
68 // Seminumerical Algorithms, Section 4.6.3
69 template <typename T, typename Integer, typename Operation>
70 typename boost::enable_if<boost::is_integral<Integer>, T>::type
71 power (T x, Integer n, Operation op) {
72 T y = identity_operation(op);
88 #endif // BOOST_ALGORITHM_HPP