algorithm.hpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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> 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>
  242. {
  243. using type = mp_list_c<U, (F::value + J)...>;
  244. };
  245. } // namespace detail
  246. template<class S, class F = mp_int<0>> using mp_from_sequence = typename detail::mp_from_sequence_impl<S, F>::type;
  247. // mp_iota(_c)<N, F>
  248. 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>>;
  249. 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>;
  250. // mp_at(_c)<L, I>
  251. namespace detail
  252. {
  253. template<class L, std::size_t I> struct mp_at_c_impl;
  254. #if defined(BOOST_MP11_HAS_TYPE_PACK_ELEMENT)
  255. template<template<class...> class L, class... T, std::size_t I> struct mp_at_c_impl<L<T...>, I>
  256. {
  257. using type = __type_pack_element<I, T...>;
  258. };
  259. #if defined(BOOST_MP11_HAS_TEMPLATE_AUTO)
  260. template<template<auto...> class L, auto... A, std::size_t I> struct mp_at_c_impl<L<A...>, I>
  261. {
  262. using type = __type_pack_element<I, mp_value<A>...>;
  263. };
  264. #endif
  265. #else
  266. template<class L, std::size_t I> struct mp_at_c_impl
  267. {
  268. using _map = mp_transform<mp_list, mp_iota<mp_size<L> >, mp_rename<L, mp_list>>;
  269. using type = mp_second<mp_map_find<_map, mp_size_t<I> > >;
  270. };
  271. #endif
  272. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  273. template<class L, std::size_t I> struct mp_at_c_cuda_workaround
  274. {
  275. using type = mp_if_c<(I < mp_size<L>::value), detail::mp_at_c_impl<L, I>, void>;
  276. };
  277. #endif
  278. } // namespace detail
  279. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  280. template<class L, std::size_t I> using mp_at_c = typename detail::mp_at_c_cuda_workaround< L, I >::type::type;
  281. #else
  282. 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;
  283. #endif
  284. template<class L, class I> using mp_at = mp_at_c<L, std::size_t{ I::value }>;
  285. // mp_take(_c)<L, N>
  286. namespace detail
  287. {
  288. template<std::size_t N, class L, class E = void> struct mp_take_c_impl
  289. {
  290. };
  291. template<template<class...> class L, class... T>
  292. struct mp_take_c_impl<0, L<T...>>
  293. {
  294. using type = L<>;
  295. };
  296. template<template<class...> class L, class T1, class... T>
  297. struct mp_take_c_impl<1, L<T1, T...>>
  298. {
  299. using type = L<T1>;
  300. };
  301. template<template<class...> class L, class T1, class T2, class... T>
  302. struct mp_take_c_impl<2, L<T1, T2, T...>>
  303. {
  304. using type = L<T1, T2>;
  305. };
  306. template<template<class...> class L, class T1, class T2, class T3, class... T>
  307. struct mp_take_c_impl<3, L<T1, T2, T3, T...>>
  308. {
  309. using type = L<T1, T2, T3>;
  310. };
  311. template<template<class...> class L, class T1, class T2, class T3, class T4, class... T>
  312. struct mp_take_c_impl<4, L<T1, T2, T3, T4, T...>>
  313. {
  314. using type = L<T1, T2, T3, T4>;
  315. };
  316. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class... T>
  317. struct mp_take_c_impl<5, L<T1, T2, T3, T4, T5, T...>>
  318. {
  319. using type = L<T1, T2, T3, T4, T5>;
  320. };
  321. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class... T>
  322. struct mp_take_c_impl<6, L<T1, T2, T3, T4, T5, T6, T...>>
  323. {
  324. using type = L<T1, T2, T3, T4, T5, T6>;
  325. };
  326. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class... T>
  327. struct mp_take_c_impl<7, L<T1, T2, T3, T4, T5, T6, T7, T...>>
  328. {
  329. using type = L<T1, T2, T3, T4, T5, T6, T7>;
  330. };
  331. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class... T>
  332. struct mp_take_c_impl<8, L<T1, T2, T3, T4, T5, T6, T7, T8, T...>>
  333. {
  334. using type = L<T1, T2, T3, T4, T5, T6, T7, T8>;
  335. };
  336. 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>
  337. struct mp_take_c_impl<9, L<T1, T2, T3, T4, T5, T6, T7, T8, T9, T...>>
  338. {
  339. using type = L<T1, T2, T3, T4, T5, T6, T7, T8, T9>;
  340. };
  341. 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>
  342. 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>
  343. {
  344. 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>;
  345. };
  346. } // namespace detail
  347. 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>;
  348. template<class L, class N> using mp_take = mp_take_c<L, std::size_t{ N::value }>;
  349. // mp_slice(_c)<L, I, J>
  350. template<class L, std::size_t I, std::size_t J> using mp_slice_c = mp_drop_c< mp_take_c<L, J>, I >;
  351. template<class L, class I, class J> using mp_slice = mp_drop< mp_take<L, J>, I >;
  352. // mp_back<L>
  353. template<class L> using mp_back = mp_at_c<L, mp_size<L>::value - 1>;
  354. // mp_pop_back<L>
  355. template<class L> using mp_pop_back = mp_take_c<L, mp_size<L>::value - 1>;
  356. // mp_replace<L, V, W>
  357. namespace detail
  358. {
  359. template<class L, class V, class W> struct mp_replace_impl;
  360. template<template<class...> class L, class... T, class V, class W> struct mp_replace_impl<L<T...>, V, W>
  361. {
  362. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  363. template<class A> struct _f { using type = mp_if<std::is_same<A, V>, W, A>; };
  364. using type = L<typename _f<T>::type...>;
  365. #else
  366. template<class A> using _f = mp_if<std::is_same<A, V>, W, A>;
  367. using type = L<_f<T>...>;
  368. #endif
  369. };
  370. } // namespace detail
  371. template<class L, class V, class W> using mp_replace = typename detail::mp_replace_impl<L, V, W>::type;
  372. // mp_replace_if<L, P, W>
  373. namespace detail
  374. {
  375. template<class L, template<class...> class P, class W> struct mp_replace_if_impl;
  376. template<template<class...> class L, class... T, template<class...> class P, class W> struct mp_replace_if_impl<L<T...>, P, W>
  377. {
  378. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  379. template<class U> struct _f { using type = mp_if<P<U>, W, U>; };
  380. using type = L<typename _f<T>::type...>;
  381. #else
  382. template<class U> using _f = mp_if<P<U>, W, U>;
  383. using type = L<_f<T>...>;
  384. #endif
  385. };
  386. } // namespace detail
  387. template<class L, template<class...> class P, class W> using mp_replace_if = typename detail::mp_replace_if_impl<L, P, W>::type;
  388. template<class L, class Q, class W> using mp_replace_if_q = mp_replace_if<L, Q::template fn, W>;
  389. // mp_copy_if<L, P>
  390. // in detail/mp_copy_if.hpp
  391. // mp_remove<L, V>
  392. namespace detail
  393. {
  394. template<class L, class V> struct mp_remove_impl;
  395. template<template<class...> class L, class... T, class V> struct mp_remove_impl<L<T...>, V>
  396. {
  397. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  398. template<class U> struct _f { using type = mp_if<std::is_same<U, V>, mp_list<>, mp_list<U>>; };
  399. using type = mp_append<L<>, typename _f<T>::type...>;
  400. #else
  401. template<class U> using _f = mp_if<std::is_same<U, V>, mp_list<>, mp_list<U>>;
  402. using type = mp_append<L<>, _f<T>...>;
  403. #endif
  404. };
  405. } // namespace detail
  406. template<class L, class V> using mp_remove = typename detail::mp_remove_impl<L, V>::type;
  407. // mp_remove_if<L, P>
  408. // in detail/mp_remove_if.hpp
  409. // mp_flatten<L, L2 = mp_clear<L>>
  410. namespace detail
  411. {
  412. template<class L2> struct mp_flatten_impl
  413. {
  414. template<class T> using fn = mp_if<mp_similar<L2, T>, T, mp_list<T>>;
  415. };
  416. } // namespace detail
  417. 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>>>;
  418. // mp_partition<L, P>
  419. namespace detail
  420. {
  421. template<class L, template<class...> class P> struct mp_partition_impl;
  422. template<template<class...> class L, class... T, template<class...> class P> struct mp_partition_impl<L<T...>, P>
  423. {
  424. using type = L<mp_copy_if<L<T...>, P>, mp_remove_if<L<T...>, P>>;
  425. };
  426. } // namespace detail
  427. template<class L, template<class...> class P> using mp_partition = typename detail::mp_partition_impl<L, P>::type;
  428. template<class L, class Q> using mp_partition_q = mp_partition<L, Q::template fn>;
  429. // mp_sort<L, P>
  430. namespace detail
  431. {
  432. template<class L, template<class...> class P> struct mp_sort_impl;
  433. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  434. template<template<class...> class L, class... T, template<class...> class P> struct mp_sort_impl<L<T...>, P>
  435. {
  436. static_assert( sizeof...(T) == 0, "T... must be empty" );
  437. using type = L<>;
  438. };
  439. #else
  440. template<template<class...> class L, template<class...> class P> struct mp_sort_impl<L<>, P>
  441. {
  442. using type = L<>;
  443. };
  444. #endif
  445. template<template<class...> class L, class T1, template<class...> class P> struct mp_sort_impl<L<T1>, P>
  446. {
  447. using type = L<T1>;
  448. };
  449. template<template<class...> class L, class T1, class... T, template<class...> class P> struct mp_sort_impl<L<T1, T...>, P>
  450. {
  451. template<class U> using F = P<U, T1>;
  452. using part = mp_partition<L<T...>, F>;
  453. using S1 = typename mp_sort_impl<mp_first<part>, P>::type;
  454. using S2 = typename mp_sort_impl<mp_second<part>, P>::type;
  455. using type = mp_append<mp_push_back<S1, T1>, S2>;
  456. };
  457. } // namespace detail
  458. template<class L, template<class...> class P> using mp_sort = typename detail::mp_sort_impl<L, P>::type;
  459. template<class L, class Q> using mp_sort_q = mp_sort<L, Q::template fn>;
  460. // mp_nth_element(_c)<L, I, P>
  461. namespace detail
  462. {
  463. template<class L, std::size_t I, template<class...> class P> struct mp_nth_element_impl;
  464. template<template<class...> class L, class T1, std::size_t I, template<class...> class P> struct mp_nth_element_impl<L<T1>, I, P>
  465. {
  466. static_assert( I == 0, "mp_nth_element index out of range" );
  467. using type = T1;
  468. };
  469. 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>
  470. {
  471. static_assert( I < 1 + sizeof...(T), "mp_nth_element index out of range" );
  472. template<class U> using F = P<U, T1>;
  473. using part = mp_partition<L<T...>, F>;
  474. using L1 = mp_first<part>;
  475. static std::size_t const N1 = mp_size<L1>::value;
  476. using L2 = mp_second<part>;
  477. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  478. struct detail
  479. {
  480. struct mp_nth_element_impl_cuda_workaround
  481. {
  482. using type = mp_cond<
  483. mp_bool<(I < N1)>, mp_nth_element_impl<L1, I, P>,
  484. mp_bool<(I == N1)>, mp_identity<T1>,
  485. mp_true, mp_nth_element_impl<L2, I - N1 - 1, P>
  486. >;
  487. };
  488. };
  489. using type = typename detail::mp_nth_element_impl_cuda_workaround::type::type;
  490. #else
  491. using type = typename mp_cond<
  492. mp_bool<(I < N1)>, mp_nth_element_impl<L1, I, P>,
  493. mp_bool<(I == N1)>, mp_identity<T1>,
  494. mp_true, mp_nth_element_impl<L2, I - N1 - 1, P>
  495. >::type;
  496. #endif
  497. };
  498. } // namespace detail
  499. 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;
  500. 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;
  501. template<class L, class I, class Q> using mp_nth_element_q = mp_nth_element<L, I, Q::template fn>;
  502. // mp_find<L, V>
  503. namespace detail
  504. {
  505. template<class L, class V> struct mp_find_impl;
  506. #if BOOST_MP11_CLANG && defined( BOOST_MP11_HAS_FOLD_EXPRESSIONS )
  507. struct mp_index_holder
  508. {
  509. std::size_t i_;
  510. bool f_;
  511. };
  512. constexpr inline mp_index_holder operator+( mp_index_holder const & v, bool f )
  513. {
  514. if( v.f_ )
  515. {
  516. return v;
  517. }
  518. else if( f )
  519. {
  520. return { v.i_, true };
  521. }
  522. else
  523. {
  524. return { v.i_ + 1, false };
  525. }
  526. }
  527. template<template<class...> class L, class... T, class V> struct mp_find_impl<L<T...>, V>
  528. {
  529. static constexpr mp_index_holder _v{ 0, false };
  530. using type = mp_size_t< (_v + ... + std::is_same<T, V>::value).i_ >;
  531. };
  532. #elif !defined( BOOST_MP11_NO_CONSTEXPR )
  533. template<template<class...> class L, class V> struct mp_find_impl<L<>, V>
  534. {
  535. using type = mp_size_t<0>;
  536. };
  537. #if defined( BOOST_MP11_HAS_CXX14_CONSTEXPR )
  538. constexpr std::size_t cx_find_index( bool const * first, bool const * last )
  539. {
  540. std::size_t m = 0;
  541. while( first != last && !*first )
  542. {
  543. ++m;
  544. ++first;
  545. }
  546. return m;
  547. }
  548. #else
  549. constexpr std::size_t cx_find_index( bool const * first, bool const * last )
  550. {
  551. return first == last || *first? 0: 1 + cx_find_index( first + 1, last );
  552. }
  553. #endif
  554. template<template<class...> class L, class... T, class V> struct mp_find_impl<L<T...>, V>
  555. {
  556. static constexpr bool _v[] = { std::is_same<T, V>::value... };
  557. using type = mp_size_t< cx_find_index( _v, _v + sizeof...(T) ) >;
  558. };
  559. #else
  560. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  561. template<template<class...> class L, class... T, class V> struct mp_find_impl<L<T...>, V>
  562. {
  563. static_assert( sizeof...(T) == 0, "T... must be empty" );
  564. using type = mp_size_t<0>;
  565. };
  566. #else
  567. template<template<class...> class L, class V> struct mp_find_impl<L<>, V>
  568. {
  569. using type = mp_size_t<0>;
  570. };
  571. #endif
  572. template<template<class...> class L, class... T, class V> struct mp_find_impl<L<V, T...>, V>
  573. {
  574. using type = mp_size_t<0>;
  575. };
  576. template<template<class...> class L, class T1, class... T, class V> struct mp_find_impl<L<T1, T...>, V>
  577. {
  578. using _r = typename mp_find_impl<mp_list<T...>, V>::type;
  579. using type = mp_size_t<1 + _r::value>;
  580. };
  581. #endif
  582. } // namespace detail
  583. template<class L, class V> using mp_find = typename detail::mp_find_impl<L, V>::type;
  584. // mp_find_if<L, P>
  585. namespace detail
  586. {
  587. template<class L, template<class...> class P> struct mp_find_if_impl;
  588. #if BOOST_MP11_CLANG && defined( BOOST_MP11_HAS_FOLD_EXPRESSIONS )
  589. template<template<class...> class L, class... T, template<class...> class P> struct mp_find_if_impl<L<T...>, P>
  590. {
  591. static constexpr mp_index_holder _v{ 0, false };
  592. using type = mp_size_t< (_v + ... + P<T>::value).i_ >;
  593. };
  594. #elif !defined( BOOST_MP11_NO_CONSTEXPR )
  595. template<template<class...> class L, template<class...> class P> struct mp_find_if_impl<L<>, P>
  596. {
  597. using type = mp_size_t<0>;
  598. };
  599. template<template<class...> class L, class... T, template<class...> class P> struct mp_find_if_impl<L<T...>, P>
  600. {
  601. static constexpr bool _v[] = { P<T>::value... };
  602. using type = mp_size_t< cx_find_index( _v, _v + sizeof...(T) ) >;
  603. };
  604. #else
  605. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  606. template<template<class...> class L, class... T, template<class...> class P> struct mp_find_if_impl<L<T...>, P>
  607. {
  608. static_assert( sizeof...(T) == 0, "T... must be empty" );
  609. using type = mp_size_t<0>;
  610. };
  611. #else
  612. template<template<class...> class L, template<class...> class P> struct mp_find_if_impl<L<>, P>
  613. {
  614. using type = mp_size_t<0>;
  615. };
  616. #endif
  617. template<class L, template<class...> class P> struct mp_find_if_impl_2
  618. {
  619. using _r = typename mp_find_if_impl<L, P>::type;
  620. using type = mp_size_t<1 + _r::value>;
  621. };
  622. template<template<class...> class L, class T1, class... T, template<class...> class P> struct mp_find_if_impl<L<T1, T...>, P>
  623. {
  624. using type = typename mp_if<P<T1>, mp_identity<mp_size_t<0>>, mp_find_if_impl_2<mp_list<T...>, P>>::type;
  625. };
  626. #endif
  627. } // namespace detail
  628. template<class L, template<class...> class P> using mp_find_if = typename detail::mp_find_if_impl<L, P>::type;
  629. template<class L, class Q> using mp_find_if_q = mp_find_if<L, Q::template fn>;
  630. // mp_reverse<L>
  631. namespace detail
  632. {
  633. template<class L> struct mp_reverse_impl;
  634. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  635. template<template<class...> class L, class... T> struct mp_reverse_impl<L<T...>>
  636. {
  637. static_assert( sizeof...(T) == 0, "T... must be empty" );
  638. using type = L<>;
  639. };
  640. #else
  641. template<template<class...> class L> struct mp_reverse_impl<L<>>
  642. {
  643. using type = L<>;
  644. };
  645. #endif
  646. template<template<class...> class L, class T1> struct mp_reverse_impl<L<T1>>
  647. {
  648. using type = L<T1>;
  649. };
  650. template<template<class...> class L, class T1, class T2> struct mp_reverse_impl<L<T1, T2>>
  651. {
  652. using type = L<T2, T1>;
  653. };
  654. template<template<class...> class L, class T1, class T2, class T3> struct mp_reverse_impl<L<T1, T2, T3>>
  655. {
  656. using type = L<T3, T2, T1>;
  657. };
  658. template<template<class...> class L, class T1, class T2, class T3, class T4> struct mp_reverse_impl<L<T1, T2, T3, T4>>
  659. {
  660. using type = L<T4, T3, T2, T1>;
  661. };
  662. 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>>
  663. {
  664. using type = L<T5, T4, T3, T2, T1>;
  665. };
  666. 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>>
  667. {
  668. using type = L<T6, T5, T4, T3, T2, T1>;
  669. };
  670. 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>>
  671. {
  672. using type = L<T7, T6, T5, T4, T3, T2, T1>;
  673. };
  674. 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>>
  675. {
  676. using type = L<T8, T7, T6, T5, T4, T3, T2, T1>;
  677. };
  678. 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>>
  679. {
  680. using type = L<T9, T8, T7, T6, T5, T4, T3, T2, T1>;
  681. };
  682. 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...>>
  683. {
  684. using type = mp_push_back<typename mp_reverse_impl<L<T...>>::type, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1>;
  685. };
  686. } // namespace detail
  687. template<class L> using mp_reverse = typename detail::mp_reverse_impl<L>::type;
  688. // mp_fold<L, V, F>
  689. // in detail/mp_fold.hpp
  690. // mp_reverse_fold<L, V, F>
  691. namespace detail
  692. {
  693. template<class L, class V, template<class...> class F> struct mp_reverse_fold_impl;
  694. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  695. template<template<class...> class L, class... T, class V, template<class...> class F> struct mp_reverse_fold_impl<L<T...>, V, F>
  696. {
  697. static_assert( sizeof...(T) == 0, "T... must be empty" );
  698. using type = V;
  699. };
  700. #else
  701. template<template<class...> class L, class V, template<class...> class F> struct mp_reverse_fold_impl<L<>, V, F>
  702. {
  703. using type = V;
  704. };
  705. #endif
  706. 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>
  707. {
  708. using rest = typename mp_reverse_fold_impl<L<T...>, V, F>::type;
  709. using type = F<T1, rest>;
  710. };
  711. 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>
  712. {
  713. using rest = typename mp_reverse_fold_impl<L<T...>, V, F>::type;
  714. using type = F<T1, F<T2, F<T3, F<T4, F<T5, F<T6, F<T7, F<T8, F<T9, F<T10, rest> > > > > > > > > >;
  715. };
  716. } // namespace detail
  717. template<class L, class V, template<class...> class F> using mp_reverse_fold = typename detail::mp_reverse_fold_impl<L, V, F>::type;
  718. template<class L, class V, class Q> using mp_reverse_fold_q = mp_reverse_fold<L, V, Q::template fn>;
  719. // mp_unique<L>
  720. namespace detail
  721. {
  722. template<class L> struct mp_unique_impl;
  723. template<template<class...> class L, class... T> struct mp_unique_impl<L<T...>>
  724. {
  725. using type = mp_set_push_back<L<>, T...>;
  726. };
  727. } // namespace detail
  728. template<class L> using mp_unique = typename detail::mp_unique_impl<L>::type;
  729. // mp_unique_if<L, P>
  730. namespace detail
  731. {
  732. template<template<class...> class P> struct mp_unique_if_push_back
  733. {
  734. template<class...> struct impl
  735. {
  736. };
  737. template<template<class...> class L, class... Ts, class T>
  738. struct impl<L<Ts...>, T>
  739. {
  740. using type = mp_if<mp_any<P<Ts, T>...>, L<Ts...>, L<Ts..., T>>;
  741. };
  742. template<class... T> using fn = typename impl<T...>::type;
  743. };
  744. } // namespace detail
  745. template<class L, template<class...> class P>
  746. using mp_unique_if = mp_fold_q<L, mp_clear<L>, detail::mp_unique_if_push_back<P>>;
  747. template<class L, class Q> using mp_unique_if_q = mp_unique_if<L, Q::template fn>;
  748. // mp_all_of<L, P>
  749. template<class L, template<class...> class P> using mp_all_of = mp_bool< mp_count_if<L, P>::value == mp_size<L>::value >;
  750. template<class L, class Q> using mp_all_of_q = mp_all_of<L, Q::template fn>;
  751. // mp_none_of<L, P>
  752. template<class L, template<class...> class P> using mp_none_of = mp_bool< mp_count_if<L, P>::value == 0 >;
  753. template<class L, class Q> using mp_none_of_q = mp_none_of<L, Q::template fn>;
  754. // mp_any_of<L, P>
  755. template<class L, template<class...> class P> using mp_any_of = mp_bool< mp_count_if<L, P>::value != 0 >;
  756. template<class L, class Q> using mp_any_of_q = mp_any_of<L, Q::template fn>;
  757. // mp_replace_at_c<L, I, W>
  758. namespace detail
  759. {
  760. template<class L, class I, class W> struct mp_replace_at_impl
  761. {
  762. static_assert( I::value >= 0, "mp_replace_at<L, I, W>: I must not be negative" );
  763. template<class T1, class T2> using _p = std::is_same<T2, mp_size_t<I::value>>;
  764. template<class T1, class T2> using _f = W;
  765. using type = mp_transform_if<_p, _f, L, mp_iota<mp_size<L> > >;
  766. };
  767. } // namespace detail
  768. template<class L, class I, class W> using mp_replace_at = typename detail::mp_replace_at_impl<L, I, W>::type;
  769. 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;
  770. //mp_for_each<L>(f)
  771. namespace detail
  772. {
  773. template<class... T, class F> BOOST_MP11_CONSTEXPR F mp_for_each_impl( mp_list<T...>, F && f )
  774. {
  775. using A = int[sizeof...(T)];
  776. return (void)A{ ((void)f(T()), 0)... }, std::forward<F>(f);
  777. }
  778. template<class F> BOOST_MP11_CONSTEXPR F mp_for_each_impl( mp_list<>, F && f )
  779. {
  780. return std::forward<F>(f);
  781. }
  782. } // namespace detail
  783. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, >= 1900 )
  784. // msvc has a limit of 1024
  785. template<class L, class F> BOOST_MP11_CONSTEXPR mp_if_c<mp_size<L>::value <= 1024, F> mp_for_each( F && f )
  786. {
  787. return detail::mp_for_each_impl( mp_rename<L, mp_list>(), std::forward<F>(f) );
  788. }
  789. template<class L, class F> BOOST_MP11_CONSTEXPR mp_if_c<mp_size<L>::value >= 1025, F> mp_for_each( F && f )
  790. {
  791. using L2 = mp_rename<L, mp_list>;
  792. using L3 = mp_take_c<L2, 1024>;
  793. using L4 = mp_drop_c<L2, 1024>;
  794. return mp_for_each<L4>( mp_for_each<L3>( std::forward<F>(f) ) );
  795. }
  796. #else
  797. template<class L, class F> BOOST_MP11_CONSTEXPR F mp_for_each( F && f )
  798. {
  799. return detail::mp_for_each_impl( mp_rename<L, mp_list>(), std::forward<F>(f) );
  800. }
  801. #endif
  802. // mp_insert<L, I, T...>
  803. template<class L, class I, class... T> using mp_insert = mp_append<mp_take<L, I>, mp_push_front<mp_drop<L, I>, T...>>;
  804. // mp_insert_c<L, I, T...>
  805. 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...>>;
  806. // mp_erase<L, I, J>
  807. template<class L, class I, class J> using mp_erase = mp_append<mp_take<L, I>, mp_drop<L, J>>;
  808. // mp_erase_c<L, I, J>
  809. 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>>;
  810. // mp_starts_with<L1, L2>
  811. // contributed by Glen Joseph Fernandes ([email protected])
  812. namespace detail {
  813. template<class L1, class L2>
  814. struct mp_starts_with_impl { };
  815. template<template<class...> class L1, class... T1, template<class...> class L2,
  816. class... T2>
  817. struct mp_starts_with_impl<L1<T1...>, L2<T2...> > {
  818. template<class L>
  819. static mp_false check(L);
  820. template<class... T>
  821. static mp_true check(mp_list<T2..., T...>);
  822. using type = decltype(check(mp_list<T1...>()));
  823. };
  824. } // namespace detail
  825. template<class L1, class L2>
  826. using mp_starts_with = typename detail::mp_starts_with_impl<L1, L2>::type;
  827. // mp_rotate_left(_c)<L, N>
  828. namespace detail
  829. {
  830. // limit divisor to 1 to avoid division by 0 and give a rotation of 0 for lists containing 0 or 1 elements
  831. template<std::size_t Ln, std::size_t N> using canonical_left_rotation = mp_size_t<N % (Ln == 0? 1: Ln)>;
  832. // perform right rotation as a left rotation by inverting the number of elements to rotate
  833. template<std::size_t Ln, std::size_t N> using canonical_right_rotation = mp_size_t<Ln - N % (Ln == 0? 1: Ln)>;
  834. // avoid errors when rotating fixed-sized lists by using mp_list for the transformation
  835. 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> >>;
  836. } // namespace detail
  837. 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>>;
  838. template<class L, class N> using mp_rotate_left = mp_rotate_left_c<L, std::size_t{ N::value }>;
  839. // mp_rotate_right(_c)<L, N>
  840. 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>>;
  841. template<class L, class N> using mp_rotate_right = mp_rotate_right_c<L, std::size_t{ N::value }>;
  842. // mp_min_element<L, P>
  843. // mp_max_element<L, P>
  844. // in detail/mp_min_element.hpp
  845. // mp_power_set<L>
  846. namespace detail
  847. {
  848. template<class L> struct mp_power_set_impl;
  849. } // namespace detail
  850. template<class L> using mp_power_set = typename detail::mp_power_set_impl<L>::type;
  851. namespace detail
  852. {
  853. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  854. template<template<class...> class L, class... T> struct mp_power_set_impl< L<T...> >
  855. {
  856. static_assert( sizeof...(T) == 0, "T... must be empty" );
  857. using type = L< L<> >;
  858. };
  859. #else
  860. template<template<class...> class L> struct mp_power_set_impl< L<> >
  861. {
  862. using type = L< L<> >;
  863. };
  864. #endif
  865. template<template<class...> class L, class T1, class... T> struct mp_power_set_impl< L<T1, T...> >
  866. {
  867. using S1 = mp_power_set< L<T...> >;
  868. template<class L2> using _f = mp_push_front<L2, T1>;
  869. using S2 = mp_transform<_f, S1>;
  870. using type = mp_append< S1, S2 >;
  871. };
  872. } // namespace detail
  873. // mp_partial_sum<L, V, F>
  874. namespace detail
  875. {
  876. template<template<class...> class F> struct mp_partial_sum_impl_f
  877. {
  878. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1900 )
  879. 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>> >;
  880. #else
  881. template<class V, class T, class N = F<mp_first<V>, T>> using fn = mp_list<N, mp_push_back<mp_second<V>, N>>;
  882. #endif
  883. };
  884. } // namespace detail
  885. 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>> >;
  886. template<class L, class V, class Q> using mp_partial_sum_q = mp_partial_sum<L, V, Q::template fn>;
  887. // mp_iterate<V, F, R>
  888. namespace detail
  889. {
  890. template<class V, template<class...> class F, template<class...> class R, class N> struct mp_iterate_impl;
  891. } // namespace detail
  892. 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;
  893. namespace detail
  894. {
  895. template<class V, template<class...> class F, template<class...> class R> struct mp_iterate_impl<V, F, R, mp_false>
  896. {
  897. template<class X> using _f = mp_list<F<X>>;
  898. using type = mp_eval_or<mp_list<>, _f, V>;
  899. };
  900. template<class V, template<class...> class F, template<class...> class R> struct mp_iterate_impl<V, F, R, mp_true>
  901. {
  902. using type = mp_push_front<mp_iterate<R<V>, F, R>, F<V>>;
  903. };
  904. } // namespace detail
  905. template<class V, class Qf, class Qr> using mp_iterate_q = mp_iterate<V, Qf::template fn, Qr::template fn>;
  906. // mp_pairwise_fold<L, F>
  907. namespace detail
  908. {
  909. template<class L, class Q> using mp_pairwise_fold_impl = mp_transform_q<Q, mp_pop_back<L>, mp_pop_front<L>>;
  910. } // namespace detail
  911. 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>;
  912. template<class L, template<class...> class F> using mp_pairwise_fold = mp_pairwise_fold_q<L, mp_quote<F>>;
  913. // mp_sliding_fold<L, N, F>
  914. namespace detail
  915. {
  916. template<class C, class L, class Q, class S> struct mp_sliding_fold_impl;
  917. template<class L, class N, class Q> struct mp_sliding_fold_impl<mp_true, L, N, Q>
  918. {
  919. static const std::size_t M = mp_size<L>::value - N::value + 1;
  920. template<class I> using F = mp_slice_c<L, I::value, I::value + M>;
  921. using J = mp_transform<F, mp_iota<N>>;
  922. using type = mp_apply<mp_transform_q, mp_push_front<J, Q>>;
  923. };
  924. template<class L, class N, class Q> struct mp_sliding_fold_impl<mp_false, L, N, Q>
  925. {
  926. using type = mp_clear<L>;
  927. };
  928. } // namespace detail
  929. 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;
  930. template<class L, class N, template<class...> class F> using mp_sliding_fold = mp_sliding_fold_q<L, N, mp_quote<F>>;
  931. // mp_intersperse<L, S>
  932. namespace detail
  933. {
  934. template<class L, class S> struct mp_intersperse_impl
  935. {
  936. };
  937. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  938. template<template<class...> class L, class... T, class S> struct mp_intersperse_impl<L<T...>, S>
  939. {
  940. static_assert( sizeof...(T) == 0, "T... must be empty" );
  941. using type = L<>;
  942. };
  943. #else
  944. template<template<class...> class L, class S> struct mp_intersperse_impl<L<>, S>
  945. {
  946. using type = L<>;
  947. };
  948. #endif
  949. template<template<class...> class L, class T1, class... T, class S> struct mp_intersperse_impl<L<T1, T...>, S>
  950. {
  951. using type = mp_append<L<T1>, L<S, T>...>;
  952. };
  953. } // namespace detail
  954. template<class L, class S> using mp_intersperse = typename detail::mp_intersperse_impl<L, S>::type;
  955. // mp_split<L, S>
  956. namespace detail
  957. {
  958. template<class L, class S, class J> struct mp_split_impl;
  959. } // namespace detail
  960. template<class L, class S> using mp_split = typename detail::mp_split_impl<L, S, mp_find<L, S>>::type;
  961. namespace detail
  962. {
  963. 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>>;
  964. template<class L, class S, class J> struct mp_split_impl
  965. {
  966. 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>;
  967. };
  968. } // namespace detail
  969. // mp_join<L, S>
  970. template<class L, class S> using mp_join = mp_apply<mp_append, mp_intersperse<L, mp_list<S>>>;
  971. } // namespace mp11
  972. } // namespace boost
  973. #if defined(_MSC_VER) || defined(__GNUC__)
  974. # pragma pop_macro( "I" )
  975. #endif
  976. #endif // #ifndef BOOST_MP11_ALGORITHM_HPP_INCLUDED