9
3

mod.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*!
  2. @file
  3. Defines `boost::hana::mod`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_MOD_HPP
  9. #define BOOST_HANA_MOD_HPP
  10. #include <boost/hana/fwd/mod.hpp>
  11. #include <boost/hana/concept/constant.hpp>
  12. #include <boost/hana/concept/euclidean_ring.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/to.hpp>
  15. #include <boost/hana/core/dispatch.hpp>
  16. #include <boost/hana/detail/canonical_constant.hpp>
  17. #include <boost/hana/detail/has_common_embedding.hpp>
  18. #include <boost/hana/value.hpp>
  19. #include <type_traits>
  20. BOOST_HANA_NAMESPACE_BEGIN
  21. //! @cond
  22. template <typename X, typename Y>
  23. constexpr decltype(auto) mod_t::operator()(X&& x, Y&& y) const {
  24. using T = typename hana::tag_of<X>::type;
  25. using U = typename hana::tag_of<Y>::type;
  26. using Mod = BOOST_HANA_DISPATCH_IF(decltype(mod_impl<T, U>{}),
  27. hana::EuclideanRing<T>::value &&
  28. hana::EuclideanRing<U>::value &&
  29. !is_default<mod_impl<T, U>>::value
  30. );
  31. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  32. static_assert(hana::EuclideanRing<T>::value,
  33. "hana::mod(x, y) requires 'x' to be an EuclideanRing");
  34. static_assert(hana::EuclideanRing<U>::value,
  35. "hana::mod(x, y) requires 'y' to be an EuclideanRing");
  36. static_assert(!is_default<mod_impl<T, U>>::value,
  37. "hana::mod(x, y) requires 'x' and 'y' to be embeddable "
  38. "in a common EuclideanRing");
  39. #endif
  40. return Mod::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
  41. }
  42. //! @endcond
  43. template <typename T, typename U, bool condition>
  44. struct mod_impl<T, U, when<condition>> : default_ {
  45. template <typename ...Args>
  46. static constexpr auto apply(Args&& ...) = delete;
  47. };
  48. // Cross-type overload
  49. template <typename T, typename U>
  50. struct mod_impl<T, U, when<
  51. detail::has_nontrivial_common_embedding<EuclideanRing, T, U>::value
  52. >> {
  53. using C = typename common<T, U>::type;
  54. template <typename X, typename Y>
  55. static constexpr decltype(auto) apply(X&& x, Y&& y) {
  56. return hana::mod(hana::to<C>(static_cast<X&&>(x)),
  57. hana::to<C>(static_cast<Y&&>(y)));
  58. }
  59. };
  60. //////////////////////////////////////////////////////////////////////////
  61. // Model for integral data types
  62. //////////////////////////////////////////////////////////////////////////
  63. template <typename T>
  64. struct mod_impl<T, T, when<std::is_integral<T>::value &&
  65. !std::is_same<T, bool>::value>> {
  66. template <typename X, typename Y>
  67. static constexpr decltype(auto) apply(X&& x, Y&& y)
  68. { return static_cast<X&&>(x) % static_cast<Y&&>(y); }
  69. };
  70. //////////////////////////////////////////////////////////////////////////
  71. // Model for Constants over an EuclideanRing
  72. //////////////////////////////////////////////////////////////////////////
  73. namespace detail {
  74. template <typename C, typename X, typename Y>
  75. struct constant_from_mod {
  76. static constexpr auto value = hana::mod(hana::value<X>(), hana::value<Y>());
  77. using hana_tag = detail::CanonicalConstant<typename C::value_type>;
  78. };
  79. }
  80. template <typename C>
  81. struct mod_impl<C, C, when<
  82. hana::Constant<C>::value &&
  83. EuclideanRing<typename C::value_type>::value
  84. >> {
  85. template <typename X, typename Y>
  86. static constexpr decltype(auto) apply(X const&, Y const&)
  87. { return hana::to<C>(detail::constant_from_mod<C, X, Y>{}); }
  88. };
  89. BOOST_HANA_NAMESPACE_END
  90. #endif // !BOOST_HANA_MOD_HPP