algorithm.hpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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