foldl1.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*!
  2. @file
  3. Defines `boost::hana::detail::variadic::foldl1`.
  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_DETAIL_VARIADIC_FOLDL1_HPP
  9. #define BOOST_HANA_DETAIL_VARIADIC_FOLDL1_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN namespace detail { namespace variadic {
  13. //! @cond
  14. template <unsigned int n, typename = when<true>>
  15. struct foldl1_impl;
  16. template <>
  17. struct foldl1_impl<1> {
  18. template <typename F, typename X1>
  19. static constexpr X1 apply(F&&, X1&& x1)
  20. { return static_cast<X1&&>(x1); }
  21. };
  22. template <>
  23. struct foldl1_impl<2> {
  24. template <typename F, typename X1, typename X2>
  25. static constexpr decltype(auto) apply(F&& f, X1&& x1, X2&& x2) {
  26. return static_cast<F&&>(f)(static_cast<X1&&>(x1),
  27. static_cast<X2&&>(x2));
  28. }
  29. };
  30. template <>
  31. struct foldl1_impl<3> {
  32. template <typename F, typename X1, typename X2, typename X3>
  33. static constexpr decltype(auto) apply(F&& f, X1&& x1, X2&& x2, X3&& x3) {
  34. return f(f(static_cast<X1&&>(x1),
  35. static_cast<X2&&>(x2)),
  36. static_cast<X3&&>(x3));
  37. }
  38. };
  39. template <>
  40. struct foldl1_impl<4> {
  41. template <typename F, typename X1, typename X2, typename X3, typename X4>
  42. static constexpr decltype(auto) apply(F&& f, X1&& x1, X2&& x2, X3&& x3, X4&& x4) {
  43. return f(f(f(static_cast<X1&&>(x1),
  44. static_cast<X2&&>(x2)),
  45. static_cast<X3&&>(x3)),
  46. static_cast<X4&&>(x4));
  47. }
  48. };
  49. template <>
  50. struct foldl1_impl<5> {
  51. template <typename F, typename X1, typename X2, typename X3, typename X4, typename X5>
  52. static constexpr decltype(auto) apply(F&& f, X1&& x1, X2&& x2, X3&& x3, X4&& x4, X5&& x5) {
  53. return f(f(f(f(static_cast<X1&&>(x1),
  54. static_cast<X2&&>(x2)),
  55. static_cast<X3&&>(x3)),
  56. static_cast<X4&&>(x4)),
  57. static_cast<X5&&>(x5));
  58. }
  59. };
  60. template <>
  61. struct foldl1_impl<6> {
  62. template <typename F, typename X1, typename X2, typename X3, typename X4, typename X5, typename X6>
  63. static constexpr decltype(auto) apply(F&& f, X1&& x1, X2&& x2, X3&& x3, X4&& x4, X5&& x5, X6&& x6) {
  64. return f(f(f(f(f(static_cast<X1&&>(x1),
  65. static_cast<X2&&>(x2)),
  66. static_cast<X3&&>(x3)),
  67. static_cast<X4&&>(x4)),
  68. static_cast<X5&&>(x5)),
  69. static_cast<X6&&>(x6));
  70. }
  71. };
  72. template <unsigned int n>
  73. struct foldl1_impl<n, when<(n >= 7) && (n < 14)>> {
  74. template <typename F, typename X1, typename X2, typename X3, typename X4, typename X5, typename X6, typename X7, typename ...Xn>
  75. static constexpr decltype(auto)
  76. apply(F&& f
  77. , X1&& x1, X2&& x2, X3&& x3, X4&& x4, X5&& x5, X6&& x6, X7&& x7
  78. , Xn&& ...xn)
  79. {
  80. return foldl1_impl<sizeof...(xn) + 1>::apply(
  81. f,
  82. f(f(f(f(f(f(static_cast<X1&&>(x1),
  83. static_cast<X2&&>(x2)),
  84. static_cast<X3&&>(x3)),
  85. static_cast<X4&&>(x4)),
  86. static_cast<X5&&>(x5)),
  87. static_cast<X6&&>(x6)),
  88. static_cast<X7&&>(x7)),
  89. static_cast<Xn&&>(xn)...
  90. );
  91. }
  92. };
  93. template <unsigned int n>
  94. struct foldl1_impl<n, when<(n >= 14) && (n < 28)>> {
  95. template <
  96. typename F
  97. , typename X1, typename X2, typename X3, typename X4, typename X5, typename X6, typename X7
  98. , typename X8, typename X9, typename X10, typename X11, typename X12, typename X13, typename X14
  99. , typename ...Xn
  100. >
  101. static constexpr decltype(auto)
  102. apply(F&& f
  103. , X1&& x1, X2&& x2, X3&& x3, X4&& x4, X5&& x5, X6&& x6, X7&& x7
  104. , X8&& x8, X9&& x9, X10&& x10, X11&& x11, X12&& x12, X13&& x13, X14&& x14
  105. , Xn&& ...xn)
  106. {
  107. return foldl1_impl<sizeof...(xn) + 1>::apply(
  108. f,
  109. f(f(f(f(f(f(f(f(f(f(f(f(f(
  110. static_cast<X1&&>(x1), static_cast<X2&&>(x2)), static_cast<X3&&>(x3)), static_cast<X4&&>(x4)), static_cast<X5&&>(x5)), static_cast<X6&&>(x6)), static_cast<X7&&>(x7)),
  111. static_cast<X8&&>(x8)), static_cast<X9&&>(x9)), static_cast<X10&&>(x10)), static_cast<X11&&>(x11)), static_cast<X12&&>(x12)), static_cast<X13&&>(x13)), static_cast<X14&&>(x14))
  112. , static_cast<Xn&&>(xn)...);
  113. }
  114. };
  115. template <unsigned int n>
  116. struct foldl1_impl<n, when<(n >= 28) && (n < 56)>> {
  117. template <
  118. typename F
  119. , typename X1, typename X2, typename X3, typename X4, typename X5, typename X6, typename X7
  120. , typename X8, typename X9, typename X10, typename X11, typename X12, typename X13, typename X14
  121. , typename X15, typename X16, typename X17, typename X18, typename X19, typename X20, typename X21
  122. , typename X22, typename X23, typename X24, typename X25, typename X26, typename X27, typename X28
  123. , typename ...Xn
  124. >
  125. static constexpr decltype(auto)
  126. apply(F&& f
  127. , X1&& x1, X2&& x2, X3&& x3, X4&& x4, X5&& x5, X6&& x6, X7&& x7
  128. , X8&& x8, X9&& x9, X10&& x10, X11&& x11, X12&& x12, X13&& x13, X14&& x14
  129. , X15&& x15, X16&& x16, X17&& x17, X18&& x18, X19&& x19, X20&& x20, X21&& x21
  130. , X22&& x22, X23&& x23, X24&& x24, X25&& x25, X26&& x26, X27&& x27, X28&& x28
  131. , Xn&& ...xn)
  132. {
  133. return foldl1_impl<sizeof...(xn) + 1>::apply(
  134. f,
  135. f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(
  136. static_cast<X1&&>(x1), static_cast<X2&&>(x2)), static_cast<X3&&>(x3)), static_cast<X4&&>(x4)), static_cast<X5&&>(x5)), static_cast<X6&&>(x6)), static_cast<X7&&>(x7)),
  137. static_cast<X8&&>(x8)), static_cast<X9&&>(x9)), static_cast<X10&&>(x10)), static_cast<X11&&>(x11)), static_cast<X12&&>(x12)), static_cast<X13&&>(x13)), static_cast<X14&&>(x14)),
  138. static_cast<X15&&>(x15)), static_cast<X16&&>(x16)), static_cast<X17&&>(x17)), static_cast<X18&&>(x18)), static_cast<X19&&>(x19)), static_cast<X20&&>(x20)), static_cast<X21&&>(x21)),
  139. static_cast<X22&&>(x22)), static_cast<X23&&>(x23)), static_cast<X24&&>(x24)), static_cast<X25&&>(x25)), static_cast<X26&&>(x26)), static_cast<X27&&>(x27)), static_cast<X28&&>(x28))
  140. , static_cast<Xn&&>(xn)...);
  141. }
  142. };
  143. template <unsigned int n>
  144. struct foldl1_impl<n, when<(n >= 56)>> {
  145. template <
  146. typename F
  147. , typename X1, typename X2, typename X3, typename X4, typename X5, typename X6, typename X7
  148. , typename X8, typename X9, typename X10, typename X11, typename X12, typename X13, typename X14
  149. , typename X15, typename X16, typename X17, typename X18, typename X19, typename X20, typename X21
  150. , typename X22, typename X23, typename X24, typename X25, typename X26, typename X27, typename X28
  151. , typename X29, typename X30, typename X31, typename X32, typename X33, typename X34, typename X35
  152. , typename X36, typename X37, typename X38, typename X39, typename X40, typename X41, typename X42
  153. , typename X43, typename X44, typename X45, typename X46, typename X47, typename X48, typename X49
  154. , typename X50, typename X51, typename X52, typename X53, typename X54, typename X55, typename X56
  155. , typename ...Xn
  156. >
  157. static constexpr decltype(auto)
  158. apply(F&& f
  159. , X1&& x1, X2&& x2, X3&& x3, X4&& x4, X5&& x5, X6&& x6, X7&& x7
  160. , X8&& x8, X9&& x9, X10&& x10, X11&& x11, X12&& x12, X13&& x13, X14&& x14
  161. , X15&& x15, X16&& x16, X17&& x17, X18&& x18, X19&& x19, X20&& x20, X21&& x21
  162. , X22&& x22, X23&& x23, X24&& x24, X25&& x25, X26&& x26, X27&& x27, X28&& x28
  163. , X29&& x29, X30&& x30, X31&& x31, X32&& x32, X33&& x33, X34&& x34, X35&& x35
  164. , X36&& x36, X37&& x37, X38&& x38, X39&& x39, X40&& x40, X41&& x41, X42&& x42
  165. , X43&& x43, X44&& x44, X45&& x45, X46&& x46, X47&& x47, X48&& x48, X49&& x49
  166. , X50&& x50, X51&& x51, X52&& x52, X53&& x53, X54&& x54, X55&& x55, X56&& x56
  167. , Xn&& ...xn)
  168. {
  169. return foldl1_impl<sizeof...(xn) + 1>::apply(
  170. f,
  171. f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(
  172. static_cast<X1&&>(x1), static_cast<X2&&>(x2)), static_cast<X3&&>(x3)), static_cast<X4&&>(x4)), static_cast<X5&&>(x5)), static_cast<X6&&>(x6)), static_cast<X7&&>(x7)),
  173. static_cast<X8&&>(x8)), static_cast<X9&&>(x9)), static_cast<X10&&>(x10)), static_cast<X11&&>(x11)), static_cast<X12&&>(x12)), static_cast<X13&&>(x13)), static_cast<X14&&>(x14)),
  174. static_cast<X15&&>(x15)), static_cast<X16&&>(x16)), static_cast<X17&&>(x17)), static_cast<X18&&>(x18)), static_cast<X19&&>(x19)), static_cast<X20&&>(x20)), static_cast<X21&&>(x21)),
  175. static_cast<X22&&>(x22)), static_cast<X23&&>(x23)), static_cast<X24&&>(x24)), static_cast<X25&&>(x25)), static_cast<X26&&>(x26)), static_cast<X27&&>(x27)), static_cast<X28&&>(x28)),
  176. static_cast<X29&&>(x29)), static_cast<X30&&>(x30)), static_cast<X31&&>(x31)), static_cast<X32&&>(x32)), static_cast<X33&&>(x33)), static_cast<X34&&>(x34)), static_cast<X35&&>(x35)),
  177. static_cast<X36&&>(x36)), static_cast<X37&&>(x37)), static_cast<X38&&>(x38)), static_cast<X39&&>(x39)), static_cast<X40&&>(x40)), static_cast<X41&&>(x41)), static_cast<X42&&>(x42)),
  178. static_cast<X43&&>(x43)), static_cast<X44&&>(x44)), static_cast<X45&&>(x45)), static_cast<X46&&>(x46)), static_cast<X47&&>(x47)), static_cast<X48&&>(x48)), static_cast<X49&&>(x49)),
  179. static_cast<X50&&>(x50)), static_cast<X51&&>(x51)), static_cast<X52&&>(x52)), static_cast<X53&&>(x53)), static_cast<X54&&>(x54)), static_cast<X55&&>(x55)), static_cast<X56&&>(x56))
  180. , static_cast<Xn&&>(xn)...);
  181. }
  182. };
  183. //! @endcond
  184. struct foldl1_t {
  185. template <typename F, typename X1, typename ...Xn>
  186. constexpr decltype(auto) operator()(F&& f, X1&& x1, Xn&& ...xn) const {
  187. return foldl1_impl<sizeof...(xn) + 1>::apply(
  188. static_cast<F&&>(f), static_cast<X1&&>(x1), static_cast<Xn&&>(xn)...
  189. );
  190. }
  191. };
  192. constexpr foldl1_t foldl1{};
  193. constexpr auto foldl = foldl1;
  194. }} BOOST_HANA_NAMESPACE_END
  195. #endif // !BOOST_HANA_DETAIL_VARIADIC_FOLDL1_HPP