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