fold_left.hpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*!
  2. @file
  3. Defines `boost::hana::fold_left`.
  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_FOLD_LEFT_HPP
  9. #define BOOST_HANA_FOLD_LEFT_HPP
  10. #include <boost/hana/fwd/fold_left.hpp>
  11. #include <boost/hana/concept/foldable.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/detail/variadic/foldl1.hpp>
  15. #include <boost/hana/functional/partial.hpp>
  16. #include <boost/hana/unpack.hpp>
  17. BOOST_HANA_NAMESPACE_BEGIN
  18. //! @cond
  19. template <typename Xs, typename State, typename F>
  20. constexpr decltype(auto) fold_left_t::operator()(Xs&& xs, State&& state, F&& f) const {
  21. using S = typename hana::tag_of<Xs>::type;
  22. using FoldLeft = BOOST_HANA_DISPATCH_IF(fold_left_impl<S>,
  23. hana::Foldable<S>::value
  24. );
  25. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  26. static_assert(hana::Foldable<S>::value,
  27. "hana::fold_left(xs, state, f) requires 'xs' to be Foldable");
  28. #endif
  29. return FoldLeft::apply(static_cast<Xs&&>(xs),
  30. static_cast<State&&>(state),
  31. static_cast<F&&>(f));
  32. }
  33. template <typename Xs, typename F>
  34. constexpr decltype(auto) fold_left_t::operator()(Xs&& xs, F&& f) const {
  35. using S = typename hana::tag_of<Xs>::type;
  36. using FoldLeft = BOOST_HANA_DISPATCH_IF(fold_left_impl<S>,
  37. hana::Foldable<S>::value
  38. );
  39. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  40. static_assert(hana::Foldable<S>::value,
  41. "hana::fold_left(xs, f) requires 'xs' to be Foldable");
  42. #endif
  43. return FoldLeft::apply(static_cast<Xs&&>(xs), static_cast<F&&>(f));
  44. }
  45. //! @endcond
  46. namespace detail {
  47. template <typename F, typename State>
  48. struct variadic_foldl1 {
  49. F& f;
  50. State& state;
  51. template <typename ...T>
  52. constexpr decltype(auto) operator()(T&& ...t) const {
  53. return detail::variadic::foldl1(
  54. static_cast<F&&>(f),
  55. static_cast<State&&>(state),
  56. static_cast<T&&>(t)...
  57. );
  58. }
  59. };
  60. }
  61. template <typename T, bool condition>
  62. struct fold_left_impl<T, when<condition>> : default_ {
  63. // with state
  64. template <typename Xs, typename S, typename F>
  65. static constexpr decltype(auto) apply(Xs&& xs, S&& s, F&& f) {
  66. return hana::unpack(static_cast<Xs&&>(xs),
  67. detail::variadic_foldl1<F, S>{f, s}
  68. );
  69. }
  70. // without state
  71. template <typename Xs, typename F>
  72. static constexpr decltype(auto) apply(Xs&& xs, F&& f) {
  73. return hana::unpack(static_cast<Xs&&>(xs),
  74. hana::partial(
  75. detail::variadic::foldl1,
  76. static_cast<F&&>(f)
  77. )
  78. );
  79. }
  80. };
  81. BOOST_HANA_NAMESPACE_END
  82. #endif // !BOOST_HANA_FOLD_LEFT_HPP