algorithm.hpp 2.4 KB

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