algorithm.hpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. #ifndef BOOST_MP11_ALGORITHM_HPP_INCLUDED
  2. #define BOOST_MP11_ALGORITHM_HPP_INCLUDED
  3. // Copyright 2015-2019 Peter Dimov
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. //
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. #include <boost/mp11/list.hpp>
  10. #include <boost/mp11/set.hpp>
  11. #include <boost/mp11/integral.hpp>
  12. #include <boost/mp11/utility.hpp>
  13. #include <boost/mp11/function.hpp>
  14. #include <boost/mp11/detail/mp_count.hpp>
  15. #include <boost/mp11/detail/mp_plus.hpp>
  16. #include <boost/mp11/detail/mp_map_find.hpp>
  17. #include <boost/mp11/detail/mp_with_index.hpp>
  18. #include <boost/mp11/detail/mp_fold.hpp>
  19. #include <boost/mp11/detail/mp_min_element.hpp>
  20. #include <boost/mp11/detail/mp_copy_if.hpp>
  21. #include <boost/mp11/detail/mp_remove_if.hpp>
  22. #include <boost/mp11/detail/config.hpp>
  23. #include <boost/mp11/integer_sequence.hpp>
  24. #include <type_traits>
  25. #include <utility>
  26. #if defined(_MSC_VER) || defined(__GNUC__)
  27. # pragma push_macro( "I" )
  28. # undef I
  29. #endif
  30. namespace boost
  31. {
  32. namespace mp11
  33. {
  34. // mp_transform<F, L...>
  35. namespace detail
  36. {
  37. template<template<class...> class F, class... L> struct mp_transform_impl
  38. {
  39. };
  40. template<template<class...> class F, template<class...> class L, class... T> struct mp_transform_impl<F, L<T...>>
  41. {
  42. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  43. template<class... U> struct f { using type = F<U...>; };
  44. using type = L<typename f<T>::type...>;
  45. #else
  46. using type = L<F<T>...>;
  47. #endif
  48. };
  49. template<template<class...> class F, template<class...> class L1, class... T1, template<class...> class L2, class... T2> struct mp_transform_impl<F, L1<T1...>, L2<T2...>>
  50. {
  51. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  52. template<class... U> struct f { using type = F<U...>; };
  53. using type = L1<typename f<T1, T2>::type...>;
  54. #else
  55. using type = L1<F<T1,T2>...>;
  56. #endif
  57. };
  58. template<template<class...> class F, template<class...> class L1, class... T1, template<class...> class L2, class... T2, template<class...> class L3, class... T3> struct mp_transform_impl<F, L1<T1...>, L2<T2...>, L3<T3...>>
  59. {
  60. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  61. template<class... U> struct f { using type = F<U...>; };
  62. using type = L1<typename f<T1, T2, T3>::type...>;
  63. #else
  64. using type = L1<F<T1,T2,T3>...>;
  65. #endif
  66. };
  67. #if defined(BOOST_MP11_HAS_TEMPLATE_AUTO)
  68. template<template<class...> class F, template<auto...> class L, auto... A> struct mp_transform_impl<F, L<A...>>
  69. {
  70. using type = L< F< mp_value<A> >::value... >;
  71. };
  72. template<template<class...> class F, template<auto...> class L1, auto... A1, template<auto...> class L2, auto... A2> struct mp_transform_impl<F, L1<A1...>, L2<A2...>>
  73. {
  74. using type = L1< F< mp_value<A1>, mp_value<A2> >::value... >;
  75. };
  76. template<template<class...> class F, template<auto...> class L1, auto... A1, template<auto...> class L2, auto... A2, template<auto...> class L3, auto... A3> struct mp_transform_impl<F, L1<A1...>, L2<A2...>, L3<A3...>>
  77. {
  78. using type = L1< F< mp_value<A1>, mp_value<A2>, mp_value<A3> >::value... >;
  79. };
  80. #endif
  81. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, == 1900 ) || BOOST_MP11_WORKAROUND( BOOST_MP11_GCC, < 40800 )
  82. template<class... L> using mp_same_size_1 = mp_same<mp_size<L>...>;
  83. template<class... L> struct mp_same_size_2: mp_defer<mp_same_size_1, L...> {};
  84. #endif
  85. struct list_size_mismatch
  86. {
  87. };
  88. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  89. template<template<class...> class F, class... L> struct mp_transform_cuda_workaround
  90. {
  91. using type = mp_if<mp_same<mp_size<L>...>, detail::mp_transform_impl<F, L...>, detail::list_size_mismatch>;
  92. };
  93. #endif
  94. } // namespace detail
  95. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, == 1900 ) || BOOST_MP11_WORKAROUND( BOOST_MP11_GCC, < 40800 )
  96. template<template<class...> class F, class... L> using mp_transform = typename mp_if<typename detail::mp_same_size_2<L...>::type, detail::mp_transform_impl<F, L...>, detail::list_size_mismatch>::type;
  97. #else
  98. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  99. template<template<class...> class F, class... L> using mp_transform = typename detail::mp_transform_cuda_workaround< F, L...>::type::type;
  100. #else
  101. template<template<class...> class F, class... L> using mp_transform = typename mp_if<mp_same<mp_size<L>...>, detail::mp_transform_impl<F, L...>, detail::list_size_mismatch>::type;
  102. #endif
  103. #endif
  104. template<class Q, class... L> using mp_transform_q = mp_transform<Q::template fn, L...>;
  105. namespace detail
  106. {
  107. template<template<class...> class F, template<class...> class L1, class... T1, template<class...> class L2, class... T2, template<class...> class L3, class... T3, template<class...> class L4, class... T4, class... L> struct mp_transform_impl<F, L1<T1...>, L2<T2...>, L3<T3...>, L4<T4...>, L...>
  108. {
  109. using A1 = L1<mp_list<T1, T2, T3, T4>...>;
  110. template<class V, class T> using _f = mp_transform<mp_push_back, V, T>;
  111. using A2 = mp_fold<mp_list<L...>, A1, _f>;
  112. template<class T> using _g = mp_apply<F, T>;
  113. using type = mp_transform<_g, A2>;
  114. };
  115. } // namespace detail
  116. // mp_transform_if<P, F, L...>
  117. namespace detail
  118. {
  119. template<template<class...> class P, template<class...> class F, class... L> struct mp_transform_if_impl
  120. {
  121. // the stupid quote-unquote dance avoids "pack expansion used as argument for non-pack parameter of alias template"
  122. using Qp = mp_quote<P>;
  123. using Qf = mp_quote<F>;
  124. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  125. template<class... U> struct _f_ { using type = mp_eval_if_q<mp_not<mp_invoke_q<Qp, U...>>, mp_first<mp_list<U...>>, Qf, U...>; };
  126. template<class... U> using _f = typename _f_<U...>::type;
  127. #else
  128. template<class... U> using _f = mp_eval_if_q<mp_not<mp_invoke_q<Qp, U...>>, mp_first<mp_list<U...>>, Qf, U...>;
  129. #endif
  130. using type = mp_transform<_f, L...>;
  131. };
  132. } // namespace detail
  133. template<template<class...> class P, template<class...> class F, class... L> using mp_transform_if = typename detail::mp_transform_if_impl<P, F, L...>::type;
  134. template<class Qp, class Qf, class... L> using mp_transform_if_q = typename detail::mp_transform_if_impl<Qp::template fn, Qf::template fn, L...>::type;
  135. // mp_filter<P, L...>
  136. namespace detail
  137. {
  138. template<template<class...> class P, class L1, class... L> struct mp_filter_impl
  139. {
  140. using Qp = mp_quote<P>;
  141. template<class T1, class... T> using _f = mp_if< mp_invoke_q<Qp, T1, T...>, mp_list<T1>, mp_list<> >;
  142. using _t1 = mp_transform<_f, L1, L...>;
  143. using _t2 = mp_apply<mp_append, _t1>;
  144. using type = mp_assign<L1, _t2>;
  145. };
  146. } // namespace detail
  147. template<template<class...> class P, class... L> using mp_filter = typename detail::mp_filter_impl<P, L...>::type;
  148. template<class Q, class... L> using mp_filter_q = typename detail::mp_filter_impl<Q::template fn, L...>::type;
  149. // mp_fill<L, V>
  150. namespace detail
  151. {
  152. template<class L, class V> struct mp_fill_impl
  153. {
  154. // An error "no type named 'type'" here means that the L argument of mp_fill is not a list
  155. };
  156. template<template<class...> class L, class... T, class V> struct mp_fill_impl<L<T...>, V>
  157. {
  158. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1900 )
  159. template<class...> struct _f { using type = V; };
  160. using type = L<typename _f<T>::type...>;
  161. #else
  162. template<class...> using _f = V;
  163. using type = L<_f<T>...>;
  164. #endif
  165. };
  166. #if defined(BOOST_MP11_HAS_TEMPLATE_AUTO)
  167. template<template<auto...> class L, auto... A, class V> struct mp_fill_impl<L<A...>, V>
  168. {
  169. using type = L<((void)A, V::value)...>;
  170. };
  171. #endif
  172. } // namespace detail
  173. template<class L, class V> using mp_fill = typename detail::mp_fill_impl<L, V>::type;
  174. // mp_contains<L, V>
  175. template<class L, class V> using mp_contains = mp_to_bool<mp_count<L, V>>;
  176. // mp_repeat(_c)<L, N>
  177. namespace detail
  178. {
  179. template<class L, std::size_t N> struct mp_repeat_c_impl
  180. {
  181. using _l1 = typename mp_repeat_c_impl<L, N/2>::type;
  182. using _l2 = typename mp_repeat_c_impl<L, N%2>::type;
  183. using type = mp_append<_l1, _l1, _l2>;
  184. };
  185. template<class L> struct mp_repeat_c_impl<L, 0>
  186. {
  187. using type = mp_clear<L>;
  188. };
  189. template<class L> struct mp_repeat_c_impl<L, 1>
  190. {
  191. using type = L;
  192. };
  193. } // namespace detail
  194. template<class L, std::size_t N> using mp_repeat_c = typename detail::mp_repeat_c_impl<L, N>::type;
  195. template<class L, class N> using mp_repeat = typename detail::mp_repeat_c_impl<L, std::size_t{ N::value }>::type;
  196. // mp_product<F, L...>
  197. namespace detail
  198. {
  199. template<template<class...> class F, class P, class... L> struct mp_product_impl_2
  200. {
  201. };
  202. template<template<class...> class F, class P> struct mp_product_impl_2<F, P>
  203. {
  204. using type = mp_list<mp_rename<P, F>>;
  205. };
  206. template<template<class...> class F, class P, template<class...> class L1, class... T1, class... L> struct mp_product_impl_2<F, P, L1<T1...>, L...>
  207. {
  208. using type = mp_append<typename mp_product_impl_2<F, mp_push_back<P, T1>, L...>::type...>;
  209. };
  210. template<template<class...> class F, class... L> struct mp_product_impl
  211. {
  212. };
  213. template<template<class...> class F> struct mp_product_impl<F>
  214. {
  215. using type = mp_list< F<> >;
  216. };
  217. template<template<class...> class F, class L1, class... L> struct mp_product_impl<F, L1, L...>
  218. {
  219. using type = mp_assign<L1, typename mp_product_impl_2<F, mp_list<>, L1, L...>::type>;
  220. };
  221. } // namespace detail
  222. template<template<class...> class F, class... L> using mp_product = typename detail::mp_product_impl<F, L...>::type;
  223. template<class Q, class... L> using mp_product_q = typename detail::mp_product_impl<Q::template fn, L...>::type;
  224. // mp_drop(_c)<L, N>
  225. namespace detail
  226. {
  227. template<class L, class L2, class En> struct mp_drop_impl;
  228. template<template<class...> class L, class... T, template<class...> class L2, class... U> struct mp_drop_impl<L<T...>, L2<U...>, mp_true>
  229. {
  230. template<class... W> static mp_identity<L<W...>> f( U*..., mp_identity<W>*... );
  231. using R = decltype( f( static_cast<mp_identity<T>*>(0) ... ) );
  232. using type = typename R::type;
  233. };
  234. } // namespace detail
  235. template<class L, std::size_t N> using mp_drop_c = mp_assign<L, typename detail::mp_drop_impl<mp_rename<L, mp_list>, mp_repeat_c<mp_list<void>, N>, mp_bool<N <= mp_size<L>::value>>::type>;
  236. template<class L, class N> using mp_drop = mp_drop_c<L, std::size_t{ N::value }>;
  237. // mp_from_sequence<S, F>
  238. namespace detail
  239. {
  240. template<class S, class F, bool Z> struct mp_from_sequence_impl;
  241. template<template<class T, T... I> class S, class U, U... J, class F> struct mp_from_sequence_impl<S<U, J...>, F, false>
  242. {
  243. using type = mp_list_c<U, (F::value + J)...>;
  244. };
  245. template<template<class T, T... I> class S, class U, U... J, class F> struct mp_from_sequence_impl<S<U, J...>, F, true>
  246. {
  247. using type = mp_list_c<U, J...>;
  248. };
  249. } // namespace detail
  250. template<class S, class F = mp_int<0>> using mp_from_sequence = typename detail::mp_from_sequence_impl<S, F, (F::value == 0)>::type;
  251. // mp_iota(_c)<N, F>
  252. template<std::size_t N, std::size_t F = 0> using mp_iota_c = mp_from_sequence<make_index_sequence<N>, mp_size_t<F>>;
  253. template<class N, class F = mp_int<0>> using mp_iota = mp_from_sequence<make_integer_sequence<typename std::remove_const<decltype(N::value)>::type, N::value>, F>;
  254. // mp_at(_c)<L, I>
  255. namespace detail
  256. {
  257. template<class L, std::size_t I> struct mp_at_c_impl;
  258. #if defined(BOOST_MP11_HAS_TYPE_PACK_ELEMENT)
  259. template<template<class...> class L, class... T, std::size_t I> struct mp_at_c_impl<L<T...>, I>
  260. {
  261. using type = __type_pack_element<I, T...>;
  262. };
  263. #if defined(BOOST_MP11_HAS_TEMPLATE_AUTO)
  264. template<template<auto...> class L, auto... A, std::size_t I> struct mp_at_c_impl<L<A...>, I>
  265. {
  266. using type = __type_pack_element<I, mp_value<A>...>;
  267. };
  268. #endif
  269. #else
  270. template<class L, std::size_t I> struct mp_at_c_impl
  271. {
  272. using _map = mp_transform<mp_list, mp_iota<mp_size<L> >, mp_rename<L, mp_list>>;
  273. using type = mp_second<mp_map_find<_map, mp_size_t<I> > >;
  274. };
  275. #endif
  276. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  277. template<class L, std::size_t I> struct mp_at_c_cuda_workaround
  278. {
  279. using type = mp_if_c<(I < mp_size<L>::value), detail::mp_at_c_impl<L, I>, void>;
  280. };
  281. #endif
  282. } // namespace detail
  283. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  284. template<class L, std::size_t I> using mp_at_c = typename detail::mp_at_c_cuda_workaround< L, I >::type::type;
  285. #else
  286. template<class L, std::size_t I> using mp_at_c = typename mp_if_c<(I < mp_size<L>::value), detail::mp_at_c_impl<L, I>, void>::type;
  287. #endif
  288. template<class L, class I> using mp_at = mp_at_c<L, std::size_t{ I::value }>;
  289. // mp_take(_c)<L, N>
  290. namespace detail
  291. {
  292. template<std::size_t N, class L, class E = void> struct mp_take_c_impl
  293. {
  294. };
  295. template<template<class...> class L, class... T>
  296. struct mp_take_c_impl<0, L<T...>>
  297. {
  298. using type = L<>;
  299. };
  300. template<template<class...> class L, class T1, class... T>
  301. struct mp_take_c_impl<1, L<T1, T...>>
  302. {
  303. using type = L<T1>;
  304. };
  305. template<template<class...> class L, class T1, class T2, class... T>
  306. struct mp_take_c_impl<2, L<T1, T2, T...>>
  307. {
  308. using type = L<T1, T2>;
  309. };
  310. template<template<class...> class L, class T1, class T2, class T3, class... T>
  311. struct mp_take_c_impl<3, L<T1, T2, T3, T...>>
  312. {
  313. using type = L<T1, T2, T3>;
  314. };
  315. template<template<class...> class L, class T1, class T2, class T3, class T4, class... T>
  316. struct mp_take_c_impl<4, L<T1, T2, T3, T4, T...>>
  317. {
  318. using type = L<T1, T2, T3, T4>;
  319. };
  320. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class... T>
  321. struct mp_take_c_impl<5, L<T1, T2, T3, T4, T5, T...>>
  322. {
  323. using type = L<T1, T2, T3, T4, T5>;
  324. };
  325. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class... T>
  326. struct mp_take_c_impl<6, L<T1, T2, T3, T4, T5, T6, T...>>
  327. {
  328. using type = L<T1, T2, T3, T4, T5, T6>;
  329. };
  330. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class... T>
  331. struct mp_take_c_impl<7, L<T1, T2, T3, T4, T5, T6, T7, T...>>
  332. {
  333. using type = L<T1, T2, T3, T4, T5, T6, T7>;
  334. };
  335. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class... T>
  336. struct mp_take_c_impl<8, L<T1, T2, T3, T4, T5, T6, T7, T8, T...>>
  337. {
  338. using type = L<T1, T2, T3, T4, T5, T6, T7, T8>;
  339. };
  340. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class... T>
  341. struct mp_take_c_impl<9, L<T1, T2, T3, T4, T5, T6, T7, T8, T9, T...>>
  342. {
  343. using type = L<T1, T2, T3, T4, T5, T6, T7, T8, T9>;
  344. };
  345. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class... T, std::size_t N>
  346. struct mp_take_c_impl<N, L<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T...>, typename std::enable_if<N >= 10>::type>
  347. {
  348. using type = mp_append<L<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>, typename mp_take_c_impl<N-10, L<T...>>::type>;
  349. };
  350. } // namespace detail
  351. template<class L, std::size_t N> using mp_take_c = mp_assign<L, typename detail::mp_take_c_impl<N, mp_rename<L, mp_list>>::type>;
  352. template<class L, class N> using mp_take = mp_take_c<L, std::size_t{ N::value }>;
  353. // mp_slice(_c)<L, I, J>
  354. template<class L, std::size_t I, std::size_t J> using mp_slice_c = mp_drop_c< mp_take_c<L, J>, I >;
  355. template<class L, class I, class J> using mp_slice = mp_drop< mp_take<L, J>, I >;
  356. // mp_back<L>
  357. template<class L> using mp_back = mp_at_c<L, mp_size<L>::value - 1>;
  358. // mp_pop_back<L>
  359. template<class L> using mp_pop_back = mp_take_c<L, mp_size<L>::value - 1>;
  360. // mp_replace<L, V, W>
  361. namespace detail
  362. {
  363. template<class L, class V, class W> struct mp_replace_impl;
  364. template<template<class...> class L, class... T, class V, class W> struct mp_replace_impl<L<T...>, V, W>
  365. {
  366. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  367. template<class A> struct _f { using type = mp_if<std::is_same<A, V>, W, A>; };
  368. using type = L<typename _f<T>::type...>;
  369. #else
  370. template<class A> using _f = mp_if<std::is_same<A, V>, W, A>;
  371. using type = L<_f<T>...>;
  372. #endif
  373. };
  374. } // namespace detail
  375. template<class L, class V, class W> using mp_replace = typename detail::mp_replace_impl<L, V, W>::type;
  376. // mp_replace_if<L, P, W>
  377. namespace detail
  378. {
  379. template<class L, template<class...> class P, class W> struct mp_replace_if_impl;
  380. template<template<class...> class L, class... T, template<class...> class P, class W> struct mp_replace_if_impl<L<T...>, P, W>
  381. {
  382. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  383. template<class U> struct _f { using type = mp_if<P<U>, W, U>; };
  384. using type = L<typename _f<T>::type...>;
  385. #else
  386. template<class U> using _f = mp_if<P<U>, W, U>;
  387. using type = L<_f<T>...>;
  388. #endif
  389. };
  390. } // namespace detail
  391. template<class L, template<class...> class P, class W> using mp_replace_if = typename detail::mp_replace_if_impl<L, P, W>::type;
  392. template<class L, class Q, class W> using mp_replace_if_q = mp_replace_if<L, Q::template fn, W>;
  393. // mp_copy_if<L, P>
  394. // in detail/mp_copy_if.hpp
  395. // mp_remove<L, V>
  396. namespace detail
  397. {
  398. template<class L, class V> struct mp_remove_impl;
  399. template<template<class...> class L, class... T, class V> struct mp_remove_impl<L<T...>, V>
  400. {
  401. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  402. template<class U> struct _f { using type = mp_if<std::is_same<U, V>, mp_list<>, mp_list<U>>; };
  403. using type = mp_append<L<>, typename _f<T>::type...>;
  404. #else
  405. template<class U> using _f = mp_if<std::is_same<U, V>, mp_list<>, mp_list<U>>;
  406. using type = mp_append<L<>, _f<T>...>;
  407. #endif
  408. };
  409. } // namespace detail
  410. template<class L, class V> using mp_remove = typename detail::mp_remove_impl<L, V>::type;
  411. // mp_remove_if<L, P>
  412. // in detail/mp_remove_if.hpp
  413. // mp_flatten<L, L2 = mp_clear<L>>
  414. namespace detail
  415. {
  416. template<class L2> struct mp_flatten_impl
  417. {
  418. template<class T> using fn = mp_if<mp_similar<L2, T>, T, mp_list<T>>;
  419. };
  420. } // namespace detail
  421. template<class L, class L2 = mp_clear<L>> using mp_flatten = mp_apply<mp_append, mp_push_front<mp_transform_q<detail::mp_flatten_impl<L2>, L>, mp_clear<L>>>;
  422. // mp_partition<L, P>
  423. namespace detail
  424. {
  425. template<class L, template<class...> class P> struct mp_partition_impl;
  426. template<template<class...> class L, class... T, template<class...> class P> struct mp_partition_impl<L<T...>, P>
  427. {
  428. using type = L<mp_copy_if<L<T...>, P>, mp_remove_if<L<T...>, P>>;
  429. };
  430. } // namespace detail
  431. template<class L, template<class...> class P> using mp_partition = typename detail::mp_partition_impl<L, P>::type;
  432. template<class L, class Q> using mp_partition_q = mp_partition<L, Q::template fn>;
  433. // mp_sort<L, P>
  434. namespace detail
  435. {
  436. template<class L, template<class...> class P> struct mp_sort_impl;
  437. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  438. template<template<class...> class L, class... T, template<class...> class P> struct mp_sort_impl<L<T...>, P>
  439. {
  440. static_assert( sizeof...(T) == 0, "T... must be empty" );
  441. using type = L<>;
  442. };
  443. #else
  444. template<template<class...> class L, template<class...> class P> struct mp_sort_impl<L<>, P>
  445. {
  446. using type = L<>;
  447. };
  448. #endif
  449. template<template<class...> class L, class T1, template<class...> class P> struct mp_sort_impl<L<T1>, P>
  450. {
  451. using type = L<T1>;
  452. };
  453. template<template<class...> class L, class T1, class... T, template<class...> class P> struct mp_sort_impl<L<T1, T...>, P>
  454. {
  455. template<class U> using F = P<U, T1>;
  456. using part = mp_partition<L<T...>, F>;
  457. using S1 = typename mp_sort_impl<mp_first<part>, P>::type;
  458. using S2 = typename mp_sort_impl<mp_second<part>, P>::type;
  459. using type = mp_append<mp_push_back<S1, T1>, S2>;
  460. };
  461. } // namespace detail
  462. template<class L, template<class...> class P> using mp_sort = typename detail::mp_sort_impl<L, P>::type;
  463. template<class L, class Q> using mp_sort_q = mp_sort<L, Q::template fn>;
  464. // mp_nth_element(_c)<L, I, P>
  465. namespace detail
  466. {
  467. template<class L, std::size_t I, template<class...> class P> struct mp_nth_element_impl;
  468. template<template<class...> class L, class T1, std::size_t I, template<class...> class P> struct mp_nth_element_impl<L<T1>, I, P>
  469. {
  470. static_assert( I == 0, "mp_nth_element index out of range" );
  471. using type = T1;
  472. };
  473. template<template<class...> class L, class T1, class... T, std::size_t I, template<class...> class P> struct mp_nth_element_impl<L<T1, T...>, I, P>
  474. {
  475. static_assert( I < 1 + sizeof...(T), "mp_nth_element index out of range" );
  476. template<class U> using F = P<U, T1>;
  477. using part = mp_partition<L<T...>, F>;
  478. using L1 = mp_first<part>;
  479. static std::size_t const N1 = mp_size<L1>::value;
  480. using L2 = mp_second<part>;
  481. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  482. struct detail
  483. {
  484. struct mp_nth_element_impl_cuda_workaround
  485. {
  486. using type = mp_cond<
  487. mp_bool<(I < N1)>, mp_nth_element_impl<L1, I, P>,
  488. mp_bool<(I == N1)>, mp_identity<T1>,
  489. mp_true, mp_nth_element_impl<L2, I - N1 - 1, P>
  490. >;
  491. };
  492. };
  493. using type = typename detail::mp_nth_element_impl_cuda_workaround::type::type;
  494. #else
  495. using type = typename mp_cond<
  496. mp_bool<(I < N1)>, mp_nth_element_impl<L1, I, P>,
  497. mp_bool<(I == N1)>, mp_identity<T1>,
  498. mp_true, mp_nth_element_impl<L2, I - N1 - 1, P>
  499. >::type;
  500. #endif
  501. };
  502. } // namespace detail
  503. template<class L, std::size_t I, template<class...> class P> using mp_nth_element_c = typename detail::mp_nth_element_impl<L, I, P>::type;
  504. template<class L, class I, template<class...> class P> using mp_nth_element = typename detail::mp_nth_element_impl<L, std::size_t{ I::value }, P>::type;
  505. template<class L, class I, class Q> using mp_nth_element_q = mp_nth_element<L, I, Q::template fn>;
  506. // mp_find<L, V>
  507. namespace detail
  508. {
  509. template<class L, class V> struct mp_find_impl;
  510. #if !defined( BOOST_MP11_NO_CONSTEXPR )
  511. template<template<class...> class L, class V> struct mp_find_impl<L<>, V>
  512. {
  513. using type = mp_size_t<0>;
  514. };
  515. #if defined( BOOST_MP11_HAS_CXX14_CONSTEXPR )
  516. constexpr std::size_t cx_find_index( bool const * first, bool const * last )
  517. {
  518. std::size_t m = 0;
  519. while( first != last && !*first )
  520. {
  521. ++m;
  522. ++first;
  523. }
  524. return m;
  525. }
  526. #else
  527. constexpr std::size_t cx_find_index( bool const * first, bool const * last )
  528. {
  529. return first == last || *first? 0: 1 + cx_find_index( first + 1, last );
  530. }
  531. #endif
  532. template<template<class...> class L, class... T, class V> struct mp_find_impl<L<T...>, V>
  533. {
  534. static constexpr bool _v[] = { std::is_same<T, V>::value... };
  535. using type = mp_size_t< cx_find_index( _v, _v + sizeof...(T) ) >;
  536. };
  537. #else
  538. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  539. template<template<class...> class L, class... T, class V> struct mp_find_impl<L<T...>, V>
  540. {
  541. static_assert( sizeof...(T) == 0, "T... must be empty" );
  542. using type = mp_size_t<0>;
  543. };
  544. #else
  545. template<template<class...> class L, class V> struct mp_find_impl<L<>, V>
  546. {
  547. using type = mp_size_t<0>;
  548. };
  549. #endif
  550. template<template<class...> class L, class... T, class V> struct mp_find_impl<L<V, T...>, V>
  551. {
  552. using type = mp_size_t<0>;
  553. };
  554. template<template<class...> class L, class T1, class... T, class V> struct mp_find_impl<L<T1, T...>, V>
  555. {
  556. using _r = typename mp_find_impl<mp_list<T...>, V>::type;
  557. using type = mp_size_t<1 + _r::value>;
  558. };
  559. #endif
  560. } // namespace detail
  561. template<class L, class V> using mp_find = typename detail::mp_find_impl<L, V>::type;
  562. // mp_find_if<L, P>
  563. namespace detail
  564. {
  565. template<class L, template<class...> class P> struct mp_find_if_impl;
  566. #if !defined( BOOST_MP11_NO_CONSTEXPR )
  567. template<template<class...> class L, template<class...> class P> struct mp_find_if_impl<L<>, P>
  568. {
  569. using type = mp_size_t<0>;
  570. };
  571. template<template<class...> class L, class... T, template<class...> class P> struct mp_find_if_impl<L<T...>, P>
  572. {
  573. static constexpr bool _v[] = { P<T>::value... };
  574. using type = mp_size_t< cx_find_index( _v, _v + sizeof...(T) ) >;
  575. };
  576. #else
  577. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  578. template<template<class...> class L, class... T, template<class...> class P> struct mp_find_if_impl<L<T...>, P>
  579. {
  580. static_assert( sizeof...(T) == 0, "T... must be empty" );
  581. using type = mp_size_t<0>;
  582. };
  583. #else
  584. template<template<class...> class L, template<class...> class P> struct mp_find_if_impl<L<>, P>
  585. {
  586. using type = mp_size_t<0>;
  587. };
  588. #endif
  589. template<class L, template<class...> class P> struct mp_find_if_impl_2
  590. {
  591. using _r = typename mp_find_if_impl<L, P>::type;
  592. using type = mp_size_t<1 + _r::value>;
  593. };
  594. template<template<class...> class L, class T1, class... T, template<class...> class P> struct mp_find_if_impl<L<T1, T...>, P>
  595. {
  596. using type = typename mp_if<P<T1>, mp_identity<mp_size_t<0>>, mp_find_if_impl_2<mp_list<T...>, P>>::type;
  597. };
  598. #endif
  599. } // namespace detail
  600. template<class L, template<class...> class P> using mp_find_if = typename detail::mp_find_if_impl<L, P>::type;
  601. template<class L, class Q> using mp_find_if_q = mp_find_if<L, Q::template fn>;
  602. // mp_reverse<L>
  603. namespace detail
  604. {
  605. template<class L> struct mp_reverse_impl;
  606. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  607. template<template<class...> class L, class... T> struct mp_reverse_impl<L<T...>>
  608. {
  609. static_assert( sizeof...(T) == 0, "T... must be empty" );
  610. using type = L<>;
  611. };
  612. #else
  613. template<template<class...> class L> struct mp_reverse_impl<L<>>
  614. {
  615. using type = L<>;
  616. };
  617. #endif
  618. template<template<class...> class L, class T1> struct mp_reverse_impl<L<T1>>
  619. {
  620. using type = L<T1>;
  621. };
  622. template<template<class...> class L, class T1, class T2> struct mp_reverse_impl<L<T1, T2>>
  623. {
  624. using type = L<T2, T1>;
  625. };
  626. template<template<class...> class L, class T1, class T2, class T3> struct mp_reverse_impl<L<T1, T2, T3>>
  627. {
  628. using type = L<T3, T2, T1>;
  629. };
  630. template<template<class...> class L, class T1, class T2, class T3, class T4> struct mp_reverse_impl<L<T1, T2, T3, T4>>
  631. {
  632. using type = L<T4, T3, T2, T1>;
  633. };
  634. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5> struct mp_reverse_impl<L<T1, T2, T3, T4, T5>>
  635. {
  636. using type = L<T5, T4, T3, T2, T1>;
  637. };
  638. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6> struct mp_reverse_impl<L<T1, T2, T3, T4, T5, T6>>
  639. {
  640. using type = L<T6, T5, T4, T3, T2, T1>;
  641. };
  642. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7> struct mp_reverse_impl<L<T1, T2, T3, T4, T5, T6, T7>>
  643. {
  644. using type = L<T7, T6, T5, T4, T3, T2, T1>;
  645. };
  646. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8> struct mp_reverse_impl<L<T1, T2, T3, T4, T5, T6, T7, T8>>
  647. {
  648. using type = L<T8, T7, T6, T5, T4, T3, T2, T1>;
  649. };
  650. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9> struct mp_reverse_impl<L<T1, T2, T3, T4, T5, T6, T7, T8, T9>>
  651. {
  652. using type = L<T9, T8, T7, T6, T5, T4, T3, T2, T1>;
  653. };
  654. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class... T> struct mp_reverse_impl<L<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T...>>
  655. {
  656. using type = mp_push_back<typename mp_reverse_impl<L<T...>>::type, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1>;
  657. };
  658. } // namespace detail
  659. template<class L> using mp_reverse = typename detail::mp_reverse_impl<L>::type;
  660. // mp_fold<L, V, F>
  661. // in detail/mp_fold.hpp
  662. // mp_reverse_fold<L, V, F>
  663. namespace detail
  664. {
  665. template<class L, class V, template<class...> class F> struct mp_reverse_fold_impl;
  666. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  667. template<template<class...> class L, class... T, class V, template<class...> class F> struct mp_reverse_fold_impl<L<T...>, V, F>
  668. {
  669. static_assert( sizeof...(T) == 0, "T... must be empty" );
  670. using type = V;
  671. };
  672. #else
  673. template<template<class...> class L, class V, template<class...> class F> struct mp_reverse_fold_impl<L<>, V, F>
  674. {
  675. using type = V;
  676. };
  677. #endif
  678. template<template<class...> class L, class T1, class... T, class V, template<class...> class F> struct mp_reverse_fold_impl<L<T1, T...>, V, F>
  679. {
  680. using rest = typename mp_reverse_fold_impl<L<T...>, V, F>::type;
  681. using type = F<T1, rest>;
  682. };
  683. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class... T, class V, template<class...> class F> struct mp_reverse_fold_impl<L<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T...>, V, F>
  684. {
  685. using rest = typename mp_reverse_fold_impl<L<T...>, V, F>::type;
  686. using type = F<T1, F<T2, F<T3, F<T4, F<T5, F<T6, F<T7, F<T8, F<T9, F<T10, rest> > > > > > > > > >;
  687. };
  688. } // namespace detail
  689. template<class L, class V, template<class...> class F> using mp_reverse_fold = typename detail::mp_reverse_fold_impl<mp_rename<L, mp_list>, V, F>::type;
  690. template<class L, class V, class Q> using mp_reverse_fold_q = mp_reverse_fold<L, V, Q::template fn>;
  691. // mp_unique<L>
  692. namespace detail
  693. {
  694. template<class L> struct mp_unique_impl;
  695. template<template<class...> class L, class... T> struct mp_unique_impl<L<T...>>
  696. {
  697. using type = mp_set_push_back<L<>, T...>;
  698. };
  699. } // namespace detail
  700. template<class L> using mp_unique = typename detail::mp_unique_impl<L>::type;
  701. // mp_unique_if<L, P>
  702. namespace detail
  703. {
  704. template<template<class...> class P> struct mp_unique_if_push_back
  705. {
  706. template<class...> struct impl
  707. {
  708. };
  709. template<template<class...> class L, class... Ts, class T>
  710. struct impl<L<Ts...>, T>
  711. {
  712. using type = mp_if<mp_any<P<Ts, T>...>, L<Ts...>, L<Ts..., T>>;
  713. };
  714. template<class... T> using fn = typename impl<T...>::type;
  715. };
  716. } // namespace detail
  717. template<class L, template<class...> class P>
  718. using mp_unique_if = mp_fold_q<L, mp_clear<L>, detail::mp_unique_if_push_back<P>>;
  719. template<class L, class Q> using mp_unique_if_q = mp_unique_if<L, Q::template fn>;
  720. // mp_all_of<L, P>
  721. template<class L, template<class...> class P> using mp_all_of = mp_bool< mp_count_if<L, P>::value == mp_size<L>::value >;
  722. template<class L, class Q> using mp_all_of_q = mp_all_of<L, Q::template fn>;
  723. // mp_none_of<L, P>
  724. template<class L, template<class...> class P> using mp_none_of = mp_bool< mp_count_if<L, P>::value == 0 >;
  725. template<class L, class Q> using mp_none_of_q = mp_none_of<L, Q::template fn>;
  726. // mp_any_of<L, P>
  727. template<class L, template<class...> class P> using mp_any_of = mp_bool< mp_count_if<L, P>::value != 0 >;
  728. template<class L, class Q> using mp_any_of_q = mp_any_of<L, Q::template fn>;
  729. // mp_replace_at_c<L, I, W>
  730. namespace detail
  731. {
  732. template<class L, class I, class W> struct mp_replace_at_impl
  733. {
  734. static_assert( I::value >= 0, "mp_replace_at<L, I, W>: I must not be negative" );
  735. template<class T1, class T2> using _p = std::is_same<T2, mp_size_t<I::value>>;
  736. template<class T1, class T2> using _f = W;
  737. using type = mp_transform_if<_p, _f, L, mp_iota<mp_size<L> > >;
  738. };
  739. } // namespace detail
  740. template<class L, class I, class W> using mp_replace_at = typename detail::mp_replace_at_impl<L, I, W>::type;
  741. template<class L, std::size_t I, class W> using mp_replace_at_c = typename detail::mp_replace_at_impl<L, mp_size_t<I>, W>::type;
  742. //mp_for_each<L>(f)
  743. namespace detail
  744. {
  745. template<class... T, class F> BOOST_MP11_CONSTEXPR F mp_for_each_impl( mp_list<T...>, F && f )
  746. {
  747. using A = int[sizeof...(T)];
  748. return (void)A{ ((void)f(T()), 0)... }, std::forward<F>(f);
  749. }
  750. template<class F> BOOST_MP11_CONSTEXPR F mp_for_each_impl( mp_list<>, F && f )
  751. {
  752. return std::forward<F>(f);
  753. }
  754. } // namespace detail
  755. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, >= 1900 )
  756. // msvc has a limit of 1024
  757. template<class L, class F> BOOST_MP11_CONSTEXPR mp_if_c<mp_size<L>::value <= 1024, F> mp_for_each( F && f )
  758. {
  759. return detail::mp_for_each_impl( mp_rename<L, mp_list>(), std::forward<F>(f) );
  760. }
  761. template<class L, class F> BOOST_MP11_CONSTEXPR mp_if_c<mp_size<L>::value >= 1025, F> mp_for_each( F && f )
  762. {
  763. using L2 = mp_rename<L, mp_list>;
  764. using L3 = mp_take_c<L2, 1024>;
  765. using L4 = mp_drop_c<L2, 1024>;
  766. return mp_for_each<L4>( mp_for_each<L3>( std::forward<F>(f) ) );
  767. }
  768. #else
  769. template<class L, class F> BOOST_MP11_CONSTEXPR F mp_for_each( F && f )
  770. {
  771. return detail::mp_for_each_impl( mp_rename<L, mp_list>(), std::forward<F>(f) );
  772. }
  773. #endif
  774. // mp_insert<L, I, T...>
  775. template<class L, class I, class... T> using mp_insert = mp_append<mp_take<L, I>, mp_push_front<mp_drop<L, I>, T...>>;
  776. // mp_insert_c<L, I, T...>
  777. template<class L, std::size_t I, class... T> using mp_insert_c = mp_append<mp_take_c<L, I>, mp_push_front<mp_drop_c<L, I>, T...>>;
  778. // mp_erase<L, I, J>
  779. template<class L, class I, class J> using mp_erase = mp_append<mp_take<L, I>, mp_drop<L, J>>;
  780. // mp_erase_c<L, I, J>
  781. template<class L, std::size_t I, std::size_t J> using mp_erase_c = mp_append<mp_take_c<L, I>, mp_drop_c<L, J>>;
  782. // mp_starts_with<L1, L2>
  783. // contributed by Glen Joseph Fernandes (glenjofe@gmail.com)
  784. namespace detail {
  785. template<class L1, class L2>
  786. struct mp_starts_with_impl { };
  787. template<template<class...> class L1, class... T1, template<class...> class L2,
  788. class... T2>
  789. struct mp_starts_with_impl<L1<T1...>, L2<T2...> > {
  790. template<class L>
  791. static mp_false check(L);
  792. template<class... T>
  793. static mp_true check(mp_list<T2..., T...>);
  794. using type = decltype(check(mp_list<T1...>()));
  795. };
  796. } // namespace detail
  797. template<class L1, class L2>
  798. using mp_starts_with = typename detail::mp_starts_with_impl<L1, L2>::type;
  799. // mp_rotate_left(_c)<L, N>
  800. namespace detail
  801. {
  802. // limit divisor to 1 to avoid division by 0 and give a rotation of 0 for lists containing 0 or 1 elements
  803. template<std::size_t Ln, std::size_t N> using canonical_left_rotation = mp_size_t<N % (Ln == 0? 1: Ln)>;
  804. // perform right rotation as a left rotation by inverting the number of elements to rotate
  805. template<std::size_t Ln, std::size_t N> using canonical_right_rotation = mp_size_t<Ln - N % (Ln == 0? 1: Ln)>;
  806. // avoid errors when rotating fixed-sized lists by using mp_list for the transformation
  807. template<class L, class N, class L2 = mp_rename<L, mp_list>> using mp_rotate_impl = mp_assign<L, mp_append< mp_drop<L2, N>, mp_take<L2, N> >>;
  808. } // namespace detail
  809. template<class L, std::size_t N> using mp_rotate_left_c = detail::mp_rotate_impl<L, detail::canonical_left_rotation<mp_size<L>::value, N>>;
  810. template<class L, class N> using mp_rotate_left = mp_rotate_left_c<L, std::size_t{ N::value }>;
  811. // mp_rotate_right(_c)<L, N>
  812. template<class L, std::size_t N> using mp_rotate_right_c = mp_rotate_left<L, detail::canonical_right_rotation<mp_size<L>::value, N>>;
  813. template<class L, class N> using mp_rotate_right = mp_rotate_right_c<L, std::size_t{ N::value }>;
  814. // mp_min_element<L, P>
  815. // mp_max_element<L, P>
  816. // in detail/mp_min_element.hpp
  817. // mp_power_set<L>
  818. namespace detail
  819. {
  820. template<class L> struct mp_power_set_impl;
  821. } // namespace detail
  822. template<class L> using mp_power_set = typename detail::mp_power_set_impl<L>::type;
  823. namespace detail
  824. {
  825. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  826. template<template<class...> class L, class... T> struct mp_power_set_impl< L<T...> >
  827. {
  828. static_assert( sizeof...(T) == 0, "T... must be empty" );
  829. using type = L< L<> >;
  830. };
  831. #else
  832. template<template<class...> class L> struct mp_power_set_impl< L<> >
  833. {
  834. using type = L< L<> >;
  835. };
  836. #endif
  837. template<template<class...> class L, class T1, class... T> struct mp_power_set_impl< L<T1, T...> >
  838. {
  839. using S1 = mp_power_set< L<T...> >;
  840. template<class L2> using _f = mp_push_front<L2, T1>;
  841. using S2 = mp_transform<_f, S1>;
  842. using type = mp_append< S1, S2 >;
  843. };
  844. } // namespace detail
  845. // mp_partial_sum<L, V, F>
  846. namespace detail
  847. {
  848. template<template<class...> class F> struct mp_partial_sum_impl_f
  849. {
  850. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1900 )
  851. template<class V, class T> using fn = mp_list<F<mp_first<V>, T>, mp_push_back<mp_second<V>, F<mp_first<V>, T>> >;
  852. #else
  853. template<class V, class T, class N = F<mp_first<V>, T>> using fn = mp_list<N, mp_push_back<mp_second<V>, N>>;
  854. #endif
  855. };
  856. } // namespace detail
  857. template<class L, class V, template<class...> class F> using mp_partial_sum = mp_second<mp_fold_q<L, mp_list<V, mp_clear<L>>, detail::mp_partial_sum_impl_f<F>> >;
  858. template<class L, class V, class Q> using mp_partial_sum_q = mp_partial_sum<L, V, Q::template fn>;
  859. // mp_iterate<V, F, R>
  860. namespace detail
  861. {
  862. template<class V, template<class...> class F, template<class...> class R, class N> struct mp_iterate_impl;
  863. } // namespace detail
  864. template<class V, template<class...> class F, template<class...> class R> using mp_iterate = typename detail::mp_iterate_impl<V, F, R, mp_valid<R, V>>::type;
  865. namespace detail
  866. {
  867. template<class V, template<class...> class F, template<class...> class R> struct mp_iterate_impl<V, F, R, mp_false>
  868. {
  869. template<class X> using _f = mp_list<F<X>>;
  870. using type = mp_eval_or<mp_list<>, _f, V>;
  871. };
  872. template<class V, template<class...> class F, template<class...> class R> struct mp_iterate_impl<V, F, R, mp_true>
  873. {
  874. using type = mp_push_front<mp_iterate<R<V>, F, R>, F<V>>;
  875. };
  876. } // namespace detail
  877. template<class V, class Qf, class Qr> using mp_iterate_q = mp_iterate<V, Qf::template fn, Qr::template fn>;
  878. // mp_pairwise_fold<L, F>
  879. namespace detail
  880. {
  881. template<class L, class Q> using mp_pairwise_fold_impl = mp_transform_q<Q, mp_pop_back<L>, mp_pop_front<L>>;
  882. } // namespace detail
  883. template<class L, class Q> using mp_pairwise_fold_q = mp_eval_if<mp_empty<L>, mp_clear<L>, detail::mp_pairwise_fold_impl, L, Q>;
  884. template<class L, template<class...> class F> using mp_pairwise_fold = mp_pairwise_fold_q<L, mp_quote<F>>;
  885. // mp_sliding_fold<L, N, F>
  886. namespace detail
  887. {
  888. template<class C, class L, class Q, class S> struct mp_sliding_fold_impl;
  889. template<class L, class N, class Q> struct mp_sliding_fold_impl<mp_true, L, N, Q>
  890. {
  891. static const std::size_t M = mp_size<L>::value - N::value + 1;
  892. template<class I> using F = mp_slice_c<L, I::value, I::value + M>;
  893. using J = mp_transform<F, mp_iota<N>>;
  894. using type = mp_apply<mp_transform_q, mp_push_front<J, Q>>;
  895. };
  896. template<class L, class N, class Q> struct mp_sliding_fold_impl<mp_false, L, N, Q>
  897. {
  898. using type = mp_clear<L>;
  899. };
  900. } // namespace detail
  901. template<class L, class N, class Q> using mp_sliding_fold_q = typename detail::mp_sliding_fold_impl<mp_bool<(mp_size<L>::value >= N::value)>, L, N, Q>::type;
  902. template<class L, class N, template<class...> class F> using mp_sliding_fold = mp_sliding_fold_q<L, N, mp_quote<F>>;
  903. // mp_intersperse<L, S>
  904. namespace detail
  905. {
  906. template<class L, class S> struct mp_intersperse_impl
  907. {
  908. };
  909. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  910. template<template<class...> class L, class... T, class S> struct mp_intersperse_impl<L<T...>, S>
  911. {
  912. static_assert( sizeof...(T) == 0, "T... must be empty" );
  913. using type = L<>;
  914. };
  915. #else
  916. template<template<class...> class L, class S> struct mp_intersperse_impl<L<>, S>
  917. {
  918. using type = L<>;
  919. };
  920. #endif
  921. template<template<class...> class L, class T1, class... T, class S> struct mp_intersperse_impl<L<T1, T...>, S>
  922. {
  923. using type = mp_append<L<T1>, L<S, T>...>;
  924. };
  925. } // namespace detail
  926. template<class L, class S> using mp_intersperse = typename detail::mp_intersperse_impl<L, S>::type;
  927. // mp_split<L, S>
  928. namespace detail
  929. {
  930. template<class L, class S, class J> struct mp_split_impl;
  931. } // namespace detail
  932. template<class L, class S> using mp_split = typename detail::mp_split_impl<L, S, mp_find<L, S>>::type;
  933. namespace detail
  934. {
  935. template<class L, class S, class J> using mp_split_impl_ = mp_push_front<mp_split<mp_drop_c<L, J::value + 1>, S>, mp_take<L, J>>;
  936. template<class L, class S, class J> struct mp_split_impl
  937. {
  938. using type = mp_eval_if_c<mp_size<L>::value == J::value, mp_push_back<mp_clear<L>, L>, mp_split_impl_, L, S, J>;
  939. };
  940. } // namespace detail
  941. // mp_join<L, S>
  942. template<class L, class S> using mp_join = mp_apply<mp_append, mp_intersperse<L, mp_list<S>>>;
  943. } // namespace mp11
  944. } // namespace boost
  945. #if defined(_MSC_VER) || defined(__GNUC__)
  946. # pragma pop_macro( "I" )
  947. #endif
  948. #endif // #ifndef BOOST_MP11_ALGORITHM_HPP_INCLUDED