Chris@102
|
1 /*
|
Chris@102
|
2 Copyright (c) Marshall Clow 2014.
|
Chris@102
|
3
|
Chris@102
|
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@102
|
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
6
|
Chris@102
|
7 Revision history:
|
Chris@102
|
8 2 Dec 2014 mtc First version; power
|
Chris@102
|
9
|
Chris@102
|
10 */
|
Chris@102
|
11
|
Chris@102
|
12 /// \file algorithm.hpp
|
Chris@102
|
13 /// \brief Misc Algorithms
|
Chris@102
|
14 /// \author Marshall Clow
|
Chris@102
|
15 ///
|
Chris@102
|
16
|
Chris@102
|
17 #ifndef BOOST_ALGORITHM_HPP
|
Chris@102
|
18 #define BOOST_ALGORITHM_HPP
|
Chris@102
|
19
|
Chris@102
|
20 #include <boost/utility/enable_if.hpp> // for boost::disable_if
|
Chris@102
|
21 #include <boost/type_traits/is_integral.hpp>
|
Chris@102
|
22
|
Chris@102
|
23 namespace boost { namespace algorithm {
|
Chris@102
|
24
|
Chris@102
|
25 template <typename T>
|
Chris@102
|
26 T identity_operation ( std::multiplies<T> ) { return T(1); }
|
Chris@102
|
27
|
Chris@102
|
28 template <typename T>
|
Chris@102
|
29 T identity_operation ( std::plus<T> ) { return T(0); }
|
Chris@102
|
30
|
Chris@102
|
31
|
Chris@102
|
32 /// \fn power ( T x, Integer n )
|
Chris@102
|
33 /// \return the value "x" raised to the power "n"
|
Chris@102
|
34 ///
|
Chris@102
|
35 /// \param x The value to be exponentiated
|
Chris@102
|
36 /// \param n The exponent (must be >= 0)
|
Chris@102
|
37 ///
|
Chris@102
|
38 // \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
|
Chris@102
|
39 // Seminumerical Algorithms, Section 4.6.3
|
Chris@102
|
40 template <typename T, typename Integer>
|
Chris@102
|
41 typename boost::enable_if<boost::is_integral<Integer>, T>::type
|
Chris@102
|
42 power (T x, Integer n) {
|
Chris@102
|
43 T y = 1; // Should be "T y{1};"
|
Chris@102
|
44 if (n == 0) return y;
|
Chris@102
|
45 while (true) {
|
Chris@102
|
46 if (n % 2 == 1) {
|
Chris@102
|
47 y = x * y;
|
Chris@102
|
48 if (n == 1)
|
Chris@102
|
49 return y;
|
Chris@102
|
50 }
|
Chris@102
|
51 n = n / 2;
|
Chris@102
|
52 x = x * x;
|
Chris@102
|
53 }
|
Chris@102
|
54 return y;
|
Chris@102
|
55 }
|
Chris@102
|
56
|
Chris@102
|
57 /// \fn power ( T x, Integer n, Operation op )
|
Chris@102
|
58 /// \return the value "x" raised to the power "n"
|
Chris@102
|
59 /// using the operaton "op".
|
Chris@102
|
60 ///
|
Chris@102
|
61 /// \param x The value to be exponentiated
|
Chris@102
|
62 /// \param n The exponent (must be >= 0)
|
Chris@102
|
63 /// \param op The operation used
|
Chris@102
|
64 ///
|
Chris@102
|
65 // \remark Taken from Knuth, The Art of Computer Programming, Volume 2:
|
Chris@102
|
66 // Seminumerical Algorithms, Section 4.6.3
|
Chris@102
|
67 template <typename T, typename Integer, typename Operation>
|
Chris@102
|
68 typename boost::enable_if<boost::is_integral<Integer>, T>::type
|
Chris@102
|
69 power (T x, Integer n, Operation op) {
|
Chris@102
|
70 T y = identity_operation(op);
|
Chris@102
|
71 if (n == 0) return y;
|
Chris@102
|
72 while (true) {
|
Chris@102
|
73 if (n % 2 == 1) {
|
Chris@102
|
74 y = op(x, y);
|
Chris@102
|
75 if (n == 1)
|
Chris@102
|
76 return y;
|
Chris@102
|
77 }
|
Chris@102
|
78 n = n / 2;
|
Chris@102
|
79 x = op(x, x);
|
Chris@102
|
80 }
|
Chris@102
|
81 return y;
|
Chris@102
|
82 }
|
Chris@102
|
83
|
Chris@102
|
84 }}
|
Chris@102
|
85
|
Chris@102
|
86 #endif // BOOST_ALGORITHM_HPP
|