algorithm.hpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. namespace boost
  27. {
  28. namespace mp11
  29. {
  30. // mp_transform<F, L...>
  31. namespace detail
  32. {
  33. template<template<class...> class F, class... L> struct mp_transform_impl
  34. {
  35. };
  36. template<template<class...> class F, template<class...> class L, class... T> struct mp_transform_impl<F, L<T...>>
  37. {
  38. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  39. template<class... U> struct f { using type = F<U...>; };
  40. using type = L<typename f<T>::type...>;
  41. #else
  42. using type = L<F<T>...>;
  43. #endif
  44. };
  45. 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...>>
  46. {
  47. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  48. template<class... U> struct f { using type = F<U...>; };
  49. using type = L1<typename f<T1, T2>::type...>;
  50. #else
  51. using type = L1<F<T1,T2>...>;
  52. #endif
  53. };
  54. 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...>>
  55. {
  56. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  57. template<class... U> struct f { using type = F<U...>; };
  58. using type = L1<typename f<T1, T2, T3>::type...>;
  59. #else
  60. using type = L1<F<T1,T2,T3>...>;
  61. #endif
  62. };
  63. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, == 1900 ) || BOOST_MP11_WORKAROUND( BOOST_MP11_GCC, < 40800 )
  64. template<class... L> using mp_same_size_1 = mp_same<mp_size<L>...>;
  65. template<class... L> struct mp_same_size_2: mp_defer<mp_same_size_1, L...> {};
  66. #endif
  67. struct list_size_mismatch
  68. {
  69. };
  70. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  71. template<template<class...> class F, class... L> struct mp_transform_cuda_workaround
  72. {
  73. using type = mp_if<mp_same<mp_size<L>...>, detail::mp_transform_impl<F, L...>, detail::list_size_mismatch>;
  74. };
  75. #endif
  76. } // namespace detail
  77. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, == 1900 ) || BOOST_MP11_WORKAROUND( BOOST_MP11_GCC, < 40800 )
  78. 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;
  79. #else
  80. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  81. template<template<class...> class F, class... L> using mp_transform = typename detail::mp_transform_cuda_workaround< F, L...>::type::type;
  82. #else
  83. 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;
  84. #endif
  85. #endif
  86. template<class Q, class... L> using mp_transform_q = mp_transform<Q::template fn, L...>;
  87. namespace detail
  88. {
  89. 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...>
  90. {
  91. using A1 = L1<mp_list<T1, T2, T3, T4>...>;
  92. template<class V, class T> using _f = mp_transform<mp_push_back, V, T>;
  93. using A2 = mp_fold<mp_list<L...>, A1, _f>;
  94. template<class T> using _g = mp_apply<F, T>;
  95. using type = mp_transform<_g, A2>;
  96. };
  97. } // namespace detail
  98. // mp_transform_if<P, F, L...>
  99. namespace detail
  100. {
  101. template<template<class...> class P, template<class...> class F, class... L> struct mp_transform_if_impl
  102. {
  103. // the stupid quote-unquote dance avoids "pack expansion used as argument for non-pack parameter of alias template"
  104. using Qp = mp_quote<P>;
  105. using Qf = mp_quote<F>;
  106. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  107. 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...>; };
  108. template<class... U> using _f = typename _f_<U...>::type;
  109. #else
  110. template<class... U> using _f = mp_eval_if_q<mp_not<mp_invoke_q<Qp, U...>>, mp_first<mp_list<U...>>, Qf, U...>;
  111. #endif
  112. using type = mp_transform<_f, L...>;
  113. };
  114. } // namespace detail
  115. 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;
  116. 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;
  117. // mp_filter<P, L...>
  118. namespace detail
  119. {
  120. template<template<class...> class P, class L1, class... L> struct mp_filter_impl
  121. {
  122. using Qp = mp_quote<P>;
  123. template<class T1, class... T> using _f = mp_if< mp_invoke_q<Qp, T1, T...>, mp_list<T1>, mp_list<> >;
  124. using _t1 = mp_transform<_f, L1, L...>;
  125. using _t2 = mp_apply<mp_append, _t1>;
  126. using type = mp_assign<L1, _t2>;
  127. };
  128. } // namespace detail
  129. template<template<class...> class P, class... L> using mp_filter = typename detail::mp_filter_impl<P, L...>::type;
  130. template<class Q, class... L> using mp_filter_q = typename detail::mp_filter_impl<Q::template fn, L...>::type;
  131. // mp_fill<L, V>
  132. namespace detail
  133. {
  134. template<class L, class V> struct mp_fill_impl;
  135. template<template<class...> class L, class... T, class V> struct mp_fill_impl<L<T...>, V>
  136. {
  137. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1900 )
  138. template<class...> struct _f { using type = V; };
  139. using type = L<typename _f<T>::type...>;
  140. #else
  141. template<class...> using _f = V;
  142. using type = L<_f<T>...>;
  143. #endif
  144. };
  145. } // namespace detail
  146. template<class L, class V> using mp_fill = typename detail::mp_fill_impl<L, V>::type;
  147. // mp_contains<L, V>
  148. template<class L, class V> using mp_contains = mp_to_bool<mp_count<L, V>>;
  149. // mp_repeat(_c)<L, N>
  150. namespace detail
  151. {
  152. template<class L, std::size_t N> struct mp_repeat_c_impl
  153. {
  154. using _l1 = typename mp_repeat_c_impl<L, N/2>::type;
  155. using _l2 = typename mp_repeat_c_impl<L, N%2>::type;
  156. using type = mp_append<_l1, _l1, _l2>;
  157. };
  158. template<class L> struct mp_repeat_c_impl<L, 0>
  159. {
  160. using type = mp_clear<L>;
  161. };
  162. template<class L> struct mp_repeat_c_impl<L, 1>
  163. {
  164. using type = L;
  165. };
  166. } // namespace detail
  167. template<class L, std::size_t N> using mp_repeat_c = typename detail::mp_repeat_c_impl<L, N>::type;
  168. template<class L, class N> using mp_repeat = typename detail::mp_repeat_c_impl<L, std::size_t{ N::value }>::type;
  169. // mp_product<F, L...>
  170. namespace detail
  171. {
  172. template<template<class...> class F, class P, class... L> struct mp_product_impl_2;
  173. template<template<class...> class F, class P> struct mp_product_impl_2<F, P>
  174. {
  175. using type = mp_list<mp_rename<P, F>>;
  176. };
  177. 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...>
  178. {
  179. using type = mp_append<typename mp_product_impl_2<F, mp_push_back<P, T1>, L...>::type...>;
  180. };
  181. template<template<class...> class F, class... L> struct mp_product_impl;
  182. template<template<class...> class F, class L1, class... L> struct mp_product_impl<F, L1, L...>
  183. {
  184. using type = mp_assign<L1, typename mp_product_impl_2<F, mp_list<>, L1, L...>::type>;
  185. };
  186. } // namespace detail
  187. template<template<class...> class F, class... L> using mp_product = typename detail::mp_product_impl<F, L...>::type;
  188. template<class Q, class... L> using mp_product_q = typename detail::mp_product_impl<Q::template fn, L...>::type;
  189. // mp_drop(_c)<L, N>
  190. namespace detail
  191. {
  192. template<class L, class L2> struct mp_drop_impl;
  193. template<template<class...> class L, class... T, template<class...> class L2, class... U> struct mp_drop_impl<L<T...>, L2<U...>>
  194. {
  195. template<class... W> static mp_identity<L<W...>> f( U*..., mp_identity<W>*... );
  196. using R = decltype( f( (mp_identity<T>*)0 ... ) );
  197. using type = typename R::type;
  198. };
  199. } // namespace detail
  200. template<class L, std::size_t N> using mp_drop_c = typename detail::mp_drop_impl<L, mp_repeat_c<mp_list<void>, N>>::type;
  201. template<class L, class N> using mp_drop = typename detail::mp_drop_impl<L, mp_repeat<mp_list<void>, N>>::type;
  202. // mp_from_sequence<S>
  203. namespace detail
  204. {
  205. template<class S> struct mp_from_sequence_impl;
  206. template<template<class T, T... I> class S, class U, U... J> struct mp_from_sequence_impl<S<U, J...>>
  207. {
  208. using type = mp_list<std::integral_constant<U, J>...>;
  209. };
  210. } // namespace detail
  211. template<class S> using mp_from_sequence = typename detail::mp_from_sequence_impl<S>::type;
  212. // mp_iota(_c)<N>
  213. template<std::size_t N> using mp_iota_c = mp_from_sequence<make_index_sequence<N>>;
  214. template<class N> using mp_iota = mp_from_sequence<make_integer_sequence<typename std::remove_const<decltype(N::value)>::type, N::value>>;
  215. // mp_at(_c)<L, I>
  216. namespace detail
  217. {
  218. template<class L, std::size_t I> struct mp_at_c_impl;
  219. #if defined(BOOST_MP11_HAS_TYPE_PACK_ELEMENT)
  220. template<template<class...> class L, class... T, std::size_t I> struct mp_at_c_impl<L<T...>, I>
  221. {
  222. using type = __type_pack_element<I, T...>;
  223. };
  224. #else
  225. template<class L, std::size_t I> struct mp_at_c_impl
  226. {
  227. using _map = mp_transform<mp_list, mp_iota<mp_size<L> >, L>;
  228. using type = mp_second<mp_map_find<_map, mp_size_t<I> > >;
  229. };
  230. #endif
  231. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  232. template<class L, std::size_t I> struct mp_at_c_cuda_workaround
  233. {
  234. using type = mp_if_c<(I < mp_size<L>::value), detail::mp_at_c_impl<L, I>, void>;
  235. };
  236. #endif
  237. } // namespace detail
  238. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  239. template<class L, std::size_t I> using mp_at_c = typename detail::mp_at_c_cuda_workaround< L, I >::type::type;
  240. #else
  241. 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;
  242. #endif
  243. template<class L, class I> using mp_at = mp_at_c<L, std::size_t{ I::value }>;
  244. // mp_take(_c)<L, N>
  245. namespace detail
  246. {
  247. template<std::size_t N, class L, class E = void> struct mp_take_c_impl
  248. {
  249. };
  250. template<template<class...> class L, class... T>
  251. struct mp_take_c_impl<0, L<T...>>
  252. {
  253. using type = L<>;
  254. };
  255. template<template<class...> class L, class T1, class... T>
  256. struct mp_take_c_impl<1, L<T1, T...>>
  257. {
  258. using type = L<T1>;
  259. };
  260. template<template<class...> class L, class T1, class T2, class... T>
  261. struct mp_take_c_impl<2, L<T1, T2, T...>>
  262. {
  263. using type = L<T1, T2>;
  264. };
  265. template<template<class...> class L, class T1, class T2, class T3, class... T>
  266. struct mp_take_c_impl<3, L<T1, T2, T3, T...>>
  267. {
  268. using type = L<T1, T2, T3>;
  269. };
  270. template<template<class...> class L, class T1, class T2, class T3, class T4, class... T>
  271. struct mp_take_c_impl<4, L<T1, T2, T3, T4, T...>>
  272. {
  273. using type = L<T1, T2, T3, T4>;
  274. };
  275. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class... T>
  276. struct mp_take_c_impl<5, L<T1, T2, T3, T4, T5, T...>>
  277. {
  278. using type = L<T1, T2, T3, T4, T5>;
  279. };
  280. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class... T>
  281. struct mp_take_c_impl<6, L<T1, T2, T3, T4, T5, T6, T...>>
  282. {
  283. using type = L<T1, T2, T3, T4, T5, T6>;
  284. };
  285. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class... T>
  286. struct mp_take_c_impl<7, L<T1, T2, T3, T4, T5, T6, T7, T...>>
  287. {
  288. using type = L<T1, T2, T3, T4, T5, T6, T7>;
  289. };
  290. template<template<class...> class L, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class... T>
  291. struct mp_take_c_impl<8, L<T1, T2, T3, T4, T5, T6, T7, T8, T...>>
  292. {
  293. using type = L<T1, T2, T3, T4, T5, T6, T7, T8>;
  294. };
  295. 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>
  296. struct mp_take_c_impl<9, L<T1, T2, T3, T4, T5, T6, T7, T8, T9, T...>>
  297. {
  298. using type = L<T1, T2, T3, T4, T5, T6, T7, T8, T9>;
  299. };
  300. 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>
  301. 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>
  302. {
  303. 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>;
  304. };
  305. } // namespace detail
  306. template<class L, std::size_t N> using mp_take_c = typename detail::mp_take_c_impl<N, L>::type;
  307. template<class L, class N> using mp_take = typename detail::mp_take_c_impl<std::size_t{ N::value }, L>::type;
  308. // mp_back<L>
  309. template<class L> using mp_back = mp_at_c<L, mp_size<L>::value - 1>;
  310. // mp_pop_back<L>
  311. template<class L> using mp_pop_back = mp_take_c<L, mp_size<L>::value - 1>;
  312. // mp_replace<L, V, W>
  313. namespace detail
  314. {
  315. template<class L, class V, class W> struct mp_replace_impl;
  316. template<template<class...> class L, class... T, class V, class W> struct mp_replace_impl<L<T...>, V, W>
  317. {
  318. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  319. template<class A> struct _f { using type = mp_if<std::is_same<A, V>, W, A>; };
  320. using type = L<typename _f<T>::type...>;
  321. #else
  322. template<class A> using _f = mp_if<std::is_same<A, V>, W, A>;
  323. using type = L<_f<T>...>;
  324. #endif
  325. };
  326. } // namespace detail
  327. template<class L, class V, class W> using mp_replace = typename detail::mp_replace_impl<L, V, W>::type;
  328. // mp_replace_if<L, P, W>
  329. namespace detail
  330. {
  331. template<class L, template<class...> class P, class W> struct mp_replace_if_impl;
  332. template<template<class...> class L, class... T, template<class...> class P, class W> struct mp_replace_if_impl<L<T...>, P, W>
  333. {
  334. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  335. template<class U> struct _f { using type = mp_if<P<U>, W, U>; };
  336. using type = L<typename _f<T>::type...>;
  337. #else
  338. template<class U> using _f = mp_if<P<U>, W, U>;
  339. using type = L<_f<T>...>;
  340. #endif
  341. };
  342. } // namespace detail
  343. template<class L, template<class...> class P, class W> using mp_replace_if = typename detail::mp_replace_if_impl<L, P, W>::type;
  344. template<class L, class Q, class W> using mp_replace_if_q = mp_replace_if<L, Q::template fn, W>;
  345. // mp_copy_if<L, P>
  346. // in detail/mp_copy_if.hpp
  347. // mp_remove<L, V>
  348. namespace detail
  349. {
  350. template<class L, class V> struct mp_remove_impl;
  351. template<template<class...> class L, class... T, class V> struct mp_remove_impl<L<T...>, V>
  352. {
  353. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1920 )
  354. template<class U> struct _f { using type = mp_if<std::is_same<U, V>, mp_list<>, mp_list<U>>; };
  355. using type = mp_append<L<>, typename _f<T>::type...>;
  356. #else
  357. template<class U> using _f = mp_if<std::is_same<U, V>, mp_list<>, mp_list<U>>;
  358. using type = mp_append<L<>, _f<T>...>;
  359. #endif
  360. };
  361. } // namespace detail
  362. template<class L, class V> using mp_remove = typename detail::mp_remove_impl<L, V>::type;
  363. // mp_remove_if<L, P>
  364. // in detail/mp_remove_if.hpp
  365. // mp_partition<L, P>
  366. namespace detail
  367. {
  368. template<class L, template<class...> class P> struct mp_partition_impl;
  369. template<template<class...> class L, class... T, template<class...> class P> struct mp_partition_impl<L<T...>, P>
  370. {
  371. using type = L<mp_copy_if<L<T...>, P>, mp_remove_if<L<T...>, P>>;
  372. };
  373. } // namespace detail
  374. template<class L, template<class...> class P> using mp_partition = typename detail::mp_partition_impl<L, P>::type;
  375. template<class L, class Q> using mp_partition_q = mp_partition<L, Q::template fn>;
  376. // mp_sort<L, P>
  377. namespace detail
  378. {
  379. template<class L, template<class...> class P> struct mp_sort_impl;
  380. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  381. template<template<class...> class L, class... T, template<class...> class P> struct mp_sort_impl<L<T...>, P>
  382. {
  383. static_assert( sizeof...(T) == 0, "T... must be empty" );
  384. using type = L<>;
  385. };
  386. #else
  387. template<template<class...> class L, template<class...> class P> struct mp_sort_impl<L<>, P>
  388. {
  389. using type = L<>;
  390. };
  391. #endif
  392. template<template<class...> class L, class T1, template<class...> class P> struct mp_sort_impl<L<T1>, P>
  393. {
  394. using type = L<T1>;
  395. };
  396. template<template<class...> class L, class T1, class... T, template<class...> class P> struct mp_sort_impl<L<T1, T...>, P>
  397. {
  398. template<class U> using F = P<U, T1>;
  399. using part = mp_partition<L<T...>, F>;
  400. using S1 = typename mp_sort_impl<mp_first<part>, P>::type;
  401. using S2 = typename mp_sort_impl<mp_second<part>, P>::type;
  402. using type = mp_append<mp_push_back<S1, T1>, S2>;
  403. };
  404. } // namespace detail
  405. template<class L, template<class...> class P> using mp_sort = typename detail::mp_sort_impl<L, P>::type;
  406. template<class L, class Q> using mp_sort_q = mp_sort<L, Q::template fn>;
  407. // mp_nth_element(_c)<L, I, P>
  408. namespace detail
  409. {
  410. template<class L, std::size_t I, template<class...> class P> struct mp_nth_element_impl;
  411. template<template<class...> class L, class T1, std::size_t I, template<class...> class P> struct mp_nth_element_impl<L<T1>, I, P>
  412. {
  413. static_assert( I == 0, "mp_nth_element index out of range" );
  414. using type = T1;
  415. };
  416. 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>
  417. {
  418. static_assert( I < 1 + sizeof...(T), "mp_nth_element index out of range" );
  419. template<class U> using F = P<U, T1>;
  420. using part = mp_partition<L<T...>, F>;
  421. using L1 = mp_first<part>;
  422. static std::size_t const N1 = mp_size<L1>::value;
  423. using L2 = mp_second<part>;
  424. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  425. struct detail
  426. {
  427. struct mp_nth_element_impl_cuda_workaround
  428. {
  429. using type = mp_cond<
  430. mp_bool<(I < N1)>, mp_nth_element_impl<L1, I, P>,
  431. mp_bool<(I == N1)>, mp_identity<T1>,
  432. mp_true, mp_nth_element_impl<L2, I - N1 - 1, P>
  433. >;
  434. };
  435. };
  436. using type = typename detail::mp_nth_element_impl_cuda_workaround::type::type;
  437. #else
  438. using type = typename mp_cond<
  439. mp_bool<(I < N1)>, mp_nth_element_impl<L1, I, P>,
  440. mp_bool<(I == N1)>, mp_identity<T1>,
  441. mp_true, mp_nth_element_impl<L2, I - N1 - 1, P>
  442. >::type;
  443. #endif
  444. };
  445. } // namespace detail
  446. 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;
  447. 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;
  448. template<class L, class I, class Q> using mp_nth_element_q = mp_nth_element<L, I, Q::template fn>;
  449. // mp_find<L, V>
  450. namespace detail
  451. {
  452. template<class L, class V> struct mp_find_impl;
  453. #if BOOST_MP11_CLANG && defined( BOOST_MP11_HAS_FOLD_EXPRESSIONS )
  454. struct mp_index_holder
  455. {
  456. std::size_t i_;
  457. bool f_;
  458. };
  459. constexpr inline mp_index_holder operator+( mp_index_holder const & v, bool f )
  460. {
  461. if( v.f_ )
  462. {
  463. return v;
  464. }
  465. else if( f )
  466. {
  467. return { v.i_, true };
  468. }
  469. else
  470. {
  471. return { v.i_ + 1, false };
  472. }
  473. }
  474. template<template<class...> class L, class... T, class V> struct mp_find_impl<L<T...>, V>
  475. {
  476. static constexpr mp_index_holder _v{ 0, false };
  477. using type = mp_size_t< (_v + ... + std::is_same<T, V>::value).i_ >;
  478. };
  479. #elif !defined( BOOST_MP11_NO_CONSTEXPR )
  480. template<template<class...> class L, class V> struct mp_find_impl<L<>, V>
  481. {
  482. using type = mp_size_t<0>;
  483. };
  484. #if defined( BOOST_MP11_HAS_CXX14_CONSTEXPR )
  485. constexpr std::size_t cx_find_index( bool const * first, bool const * last )
  486. {
  487. std::size_t m = 0;
  488. while( first != last && !*first )
  489. {
  490. ++m;
  491. ++first;
  492. }
  493. return m;
  494. }
  495. #else
  496. constexpr std::size_t cx_find_index( bool const * first, bool const * last )
  497. {
  498. return first == last || *first? 0: 1 + cx_find_index( first + 1, last );
  499. }
  500. #endif
  501. template<template<class...> class L, class... T, class V> struct mp_find_impl<L<T...>, V>
  502. {
  503. static constexpr bool _v[] = { std::is_same<T, V>::value... };
  504. using type = mp_size_t< cx_find_index( _v, _v + sizeof...(T) ) >;
  505. };
  506. #else
  507. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  508. template<template<class...> class L, class... T, class V> struct mp_find_impl<L<T...>, V>
  509. {
  510. static_assert( sizeof...(T) == 0, "T... must be empty" );
  511. using type = mp_size_t<0>;
  512. };
  513. #else
  514. template<template<class...> class L, class V> struct mp_find_impl<L<>, V>
  515. {
  516. using type = mp_size_t<0>;
  517. };
  518. #endif
  519. template<template<class...> class L, class... T, class V> struct mp_find_impl<L<V, T...>, V>
  520. {
  521. using type = mp_size_t<0>;
  522. };
  523. template<template<class...> class L, class T1, class... T, class V> struct mp_find_impl<L<T1, T...>, V>
  524. {
  525. using _r = typename mp_find_impl<mp_list<T...>, V>::type;
  526. using type = mp_size_t<1 + _r::value>;
  527. };
  528. #endif
  529. } // namespace detail
  530. template<class L, class V> using mp_find = typename detail::mp_find_impl<L, V>::type;
  531. // mp_find_if<L, P>
  532. namespace detail
  533. {
  534. template<class L, template<class...> class P> struct mp_find_if_impl;
  535. #if BOOST_MP11_CLANG && defined( BOOST_MP11_HAS_FOLD_EXPRESSIONS )
  536. template<template<class...> class L, class... T, template<class...> class P> struct mp_find_if_impl<L<T...>, P>
  537. {
  538. static constexpr mp_index_holder _v{ 0, false };
  539. using type = mp_size_t< (_v + ... + P<T>::value).i_ >;
  540. };
  541. #elif !defined( BOOST_MP11_NO_CONSTEXPR )
  542. template<template<class...> class L, template<class...> class P> struct mp_find_if_impl<L<>, P>
  543. {
  544. using type = mp_size_t<0>;
  545. };
  546. template<template<class...> class L, class... T, template<class...> class P> struct mp_find_if_impl<L<T...>, P>
  547. {
  548. static constexpr bool _v[] = { P<T>::value... };
  549. using type = mp_size_t< cx_find_index( _v, _v + sizeof...(T) ) >;
  550. };
  551. #else
  552. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  553. template<template<class...> class L, class... T, template<class...> class P> struct mp_find_if_impl<L<T...>, P>
  554. {
  555. static_assert( sizeof...(T) == 0, "T... must be empty" );
  556. using type = mp_size_t<0>;
  557. };
  558. #else
  559. template<template<class...> class L, template<class...> class P> struct mp_find_if_impl<L<>, P>
  560. {
  561. using type = mp_size_t<0>;
  562. };
  563. #endif
  564. template<class L, template<class...> class P> struct mp_find_if_impl_2
  565. {
  566. using _r = typename mp_find_if_impl<L, P>::type;
  567. using type = mp_size_t<1 + _r::value>;
  568. };
  569. template<template<class...> class L, class T1, class... T, template<class...> class P> struct mp_find_if_impl<L<T1, T...>, P>
  570. {
  571. using type = typename mp_if<P<T1>, mp_identity<mp_size_t<0>>, mp_find_if_impl_2<mp_list<T...>, P>>::type;
  572. };
  573. #endif
  574. } // namespace detail
  575. template<class L, template<class...> class P> using mp_find_if = typename detail::mp_find_if_impl<L, P>::type;
  576. template<class L, class Q> using mp_find_if_q = mp_find_if<L, Q::template fn>;
  577. // mp_reverse<L>
  578. namespace detail
  579. {
  580. template<class L> struct mp_reverse_impl;
  581. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  582. template<template<class...> class L, class... T> struct mp_reverse_impl<L<T...>>
  583. {
  584. static_assert( sizeof...(T) == 0, "T... must be empty" );
  585. using type = L<>;
  586. };
  587. #else
  588. template<template<class...> class L> struct mp_reverse_impl<L<>>
  589. {
  590. using type = L<>;
  591. };
  592. #endif
  593. template<template<class...> class L, class T1> struct mp_reverse_impl<L<T1>>
  594. {
  595. using type = L<T1>;
  596. };
  597. template<template<class...> class L, class T1, class T2> struct mp_reverse_impl<L<T1, T2>>
  598. {
  599. using type = L<T2, T1>;
  600. };
  601. template<template<class...> class L, class T1, class T2, class T3> struct mp_reverse_impl<L<T1, T2, T3>>
  602. {
  603. using type = L<T3, T2, T1>;
  604. };
  605. template<template<class...> class L, class T1, class T2, class T3, class T4> struct mp_reverse_impl<L<T1, T2, T3, T4>>
  606. {
  607. using type = L<T4, T3, T2, T1>;
  608. };
  609. 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>>
  610. {
  611. using type = L<T5, T4, T3, T2, T1>;
  612. };
  613. 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>>
  614. {
  615. using type = L<T6, T5, T4, T3, T2, T1>;
  616. };
  617. 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>>
  618. {
  619. using type = L<T7, T6, T5, T4, T3, T2, T1>;
  620. };
  621. 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>>
  622. {
  623. using type = L<T8, T7, T6, T5, T4, T3, T2, T1>;
  624. };
  625. 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>>
  626. {
  627. using type = L<T9, T8, T7, T6, T5, T4, T3, T2, T1>;
  628. };
  629. 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...>>
  630. {
  631. using type = mp_push_back<typename mp_reverse_impl<L<T...>>::type, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1>;
  632. };
  633. } // namespace detail
  634. template<class L> using mp_reverse = typename detail::mp_reverse_impl<L>::type;
  635. // mp_fold<L, V, F>
  636. // in detail/mp_fold.hpp
  637. // mp_reverse_fold<L, V, F>
  638. namespace detail
  639. {
  640. template<class L, class V, template<class...> class F> struct mp_reverse_fold_impl;
  641. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  642. template<template<class...> class L, class... T, class V, template<class...> class F> struct mp_reverse_fold_impl<L<T...>, V, F>
  643. {
  644. static_assert( sizeof...(T) == 0, "T... must be empty" );
  645. using type = V;
  646. };
  647. #else
  648. template<template<class...> class L, class V, template<class...> class F> struct mp_reverse_fold_impl<L<>, V, F>
  649. {
  650. using type = V;
  651. };
  652. #endif
  653. 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>
  654. {
  655. using rest = typename mp_reverse_fold_impl<L<T...>, V, F>::type;
  656. using type = F<T1, rest>;
  657. };
  658. 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>
  659. {
  660. using rest = typename mp_reverse_fold_impl<L<T...>, V, F>::type;
  661. using type = F<T1, F<T2, F<T3, F<T4, F<T5, F<T6, F<T7, F<T8, F<T9, F<T10, rest> > > > > > > > > >;
  662. };
  663. } // namespace detail
  664. template<class L, class V, template<class...> class F> using mp_reverse_fold = typename detail::mp_reverse_fold_impl<L, V, F>::type;
  665. template<class L, class V, class Q> using mp_reverse_fold_q = mp_reverse_fold<L, V, Q::template fn>;
  666. // mp_unique<L>
  667. namespace detail
  668. {
  669. template<class L> struct mp_unique_impl;
  670. template<template<class...> class L, class... T> struct mp_unique_impl<L<T...>>
  671. {
  672. using type = mp_set_push_back<L<>, T...>;
  673. };
  674. } // namespace detail
  675. template<class L> using mp_unique = typename detail::mp_unique_impl<L>::type;
  676. // mp_all_of<L, P>
  677. template<class L, template<class...> class P> using mp_all_of = mp_bool< mp_count_if<L, P>::value == mp_size<L>::value >;
  678. template<class L, class Q> using mp_all_of_q = mp_all_of<L, Q::template fn>;
  679. // mp_none_of<L, P>
  680. template<class L, template<class...> class P> using mp_none_of = mp_bool< mp_count_if<L, P>::value == 0 >;
  681. template<class L, class Q> using mp_none_of_q = mp_none_of<L, Q::template fn>;
  682. // mp_any_of<L, P>
  683. template<class L, template<class...> class P> using mp_any_of = mp_bool< mp_count_if<L, P>::value != 0 >;
  684. template<class L, class Q> using mp_any_of_q = mp_any_of<L, Q::template fn>;
  685. // mp_replace_at_c<L, I, W>
  686. namespace detail
  687. {
  688. template<class L, class I, class W> struct mp_replace_at_impl
  689. {
  690. static_assert( I::value >= 0, "mp_replace_at<L, I, W>: I must not be negative" );
  691. template<class T1, class T2> using _p = std::is_same<T2, mp_size_t<I::value>>;
  692. template<class T1, class T2> using _f = W;
  693. using type = mp_transform_if<_p, _f, L, mp_iota<mp_size<L> > >;
  694. };
  695. } // namespace detail
  696. template<class L, class I, class W> using mp_replace_at = typename detail::mp_replace_at_impl<L, I, W>::type;
  697. 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;
  698. //mp_for_each<L>(f)
  699. namespace detail
  700. {
  701. template<class... T, class F> BOOST_MP11_CONSTEXPR F mp_for_each_impl( mp_list<T...>, F && f )
  702. {
  703. using A = int[sizeof...(T)];
  704. return (void)A{ ((void)f(T()), 0)... }, std::forward<F>(f);
  705. }
  706. template<class F> BOOST_MP11_CONSTEXPR F mp_for_each_impl( mp_list<>, F && f )
  707. {
  708. return std::forward<F>(f);
  709. }
  710. } // namespace detail
  711. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, >= 1900 )
  712. // msvc has a limit of 1024
  713. template<class L, class F> BOOST_MP11_CONSTEXPR mp_if_c<mp_size<L>::value <= 1024, F> mp_for_each( F && f )
  714. {
  715. return detail::mp_for_each_impl( mp_rename<L, mp_list>(), std::forward<F>(f) );
  716. }
  717. template<class L, class F> BOOST_MP11_CONSTEXPR mp_if_c<mp_size<L>::value >= 1025, F> mp_for_each( F && f )
  718. {
  719. using L2 = mp_rename<L, mp_list>;
  720. using L3 = mp_take_c<L2, 1024>;
  721. using L4 = mp_drop_c<L2, 1024>;
  722. return mp_for_each<L4>( mp_for_each<L3>( std::forward<F>(f) ) );
  723. }
  724. #else
  725. template<class L, class F> BOOST_MP11_CONSTEXPR F mp_for_each( F && f )
  726. {
  727. return detail::mp_for_each_impl( mp_rename<L, mp_list>(), std::forward<F>(f) );
  728. }
  729. #endif
  730. // mp_insert<L, I, T...>
  731. template<class L, class I, class... T> using mp_insert = mp_append<mp_take<L, I>, mp_push_front<mp_drop<L, I>, T...>>;
  732. // mp_insert_c<L, I, T...>
  733. 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...>>;
  734. // mp_erase<L, I, J>
  735. template<class L, class I, class J> using mp_erase = mp_append<mp_take<L, I>, mp_drop<L, J>>;
  736. // mp_erase_c<L, I, J>
  737. 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>>;
  738. // mp_starts_with<L1, L2>
  739. // contributed by Glen Joseph Fernandes (glenjofe@gmail.com)
  740. namespace detail {
  741. template<class L1, class L2>
  742. struct mp_starts_with_impl { };
  743. template<template<class...> class L1, class... T1, template<class...> class L2,
  744. class... T2>
  745. struct mp_starts_with_impl<L1<T1...>, L2<T2...> > {
  746. template<class L>
  747. static mp_false check(L);
  748. template<class... T>
  749. static mp_true check(mp_list<T2..., T...>);
  750. using type = decltype(check(mp_list<T1...>()));
  751. };
  752. } // namespace detail
  753. template<class L1, class L2>
  754. using mp_starts_with = typename detail::mp_starts_with_impl<L1, L2>::type;
  755. // mp_min_element<L, P>
  756. // mp_max_element<L, P>
  757. // in detail/mp_min_element.hpp
  758. } // namespace mp11
  759. } // namespace boost
  760. #endif // #ifndef BOOST_MP11_ALGORITHM_HPP_INCLUDED