]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/mp11/algorithm.hpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / 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_flatten<L, L2 = mp_clear<L>>
523 namespace detail
524 {
525
526 template<class L2> struct mp_flatten_impl
527 {
528 template<class T> using fn = mp_if<mp_similar<L2, T>, T, mp_list<T>>;
529 };
530
531 } // namespace detail
532
533 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>>>;
534
535 // mp_partition<L, P>
536 namespace detail
537 {
538
539 template<class L, template<class...> class P> struct mp_partition_impl;
540
541 template<template<class...> class L, class... T, template<class...> class P> struct mp_partition_impl<L<T...>, P>
542 {
543 using type = L<mp_copy_if<L<T...>, P>, mp_remove_if<L<T...>, P>>;
544 };
545
546 } // namespace detail
547
548 template<class L, template<class...> class P> using mp_partition = typename detail::mp_partition_impl<L, P>::type;
549 template<class L, class Q> using mp_partition_q = mp_partition<L, Q::template fn>;
550
551 // mp_sort<L, P>
552 namespace detail
553 {
554
555 template<class L, template<class...> class P> struct mp_sort_impl;
556
557 #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
558
559 template<template<class...> class L, class... T, template<class...> class P> struct mp_sort_impl<L<T...>, P>
560 {
561 static_assert( sizeof...(T) == 0, "T... must be empty" );
562 using type = L<>;
563 };
564
565 #else
566
567 template<template<class...> class L, template<class...> class P> struct mp_sort_impl<L<>, P>
568 {
569 using type = L<>;
570 };
571
572 #endif
573
574 template<template<class...> class L, class T1, template<class...> class P> struct mp_sort_impl<L<T1>, P>
575 {
576 using type = L<T1>;
577 };
578
579 template<template<class...> class L, class T1, class... T, template<class...> class P> struct mp_sort_impl<L<T1, T...>, P>
580 {
581 template<class U> using F = P<U, T1>;
582
583 using part = mp_partition<L<T...>, F>;
584
585 using S1 = typename mp_sort_impl<mp_first<part>, P>::type;
586 using S2 = typename mp_sort_impl<mp_second<part>, P>::type;
587
588 using type = mp_append<mp_push_back<S1, T1>, S2>;
589 };
590
591 } // namespace detail
592
593 template<class L, template<class...> class P> using mp_sort = typename detail::mp_sort_impl<L, P>::type;
594 template<class L, class Q> using mp_sort_q = mp_sort<L, Q::template fn>;
595
596 // mp_nth_element(_c)<L, I, P>
597 namespace detail
598 {
599
600 template<class L, std::size_t I, template<class...> class P> struct mp_nth_element_impl;
601
602 template<template<class...> class L, class T1, std::size_t I, template<class...> class P> struct mp_nth_element_impl<L<T1>, I, P>
603 {
604 static_assert( I == 0, "mp_nth_element index out of range" );
605 using type = T1;
606 };
607
608 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>
609 {
610 static_assert( I < 1 + sizeof...(T), "mp_nth_element index out of range" );
611
612 template<class U> using F = P<U, T1>;
613
614 using part = mp_partition<L<T...>, F>;
615
616 using L1 = mp_first<part>;
617 static std::size_t const N1 = mp_size<L1>::value;
618
619 using L2 = mp_second<part>;
620
621 #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
622
623 struct detail
624 {
625 struct mp_nth_element_impl_cuda_workaround
626 {
627 using type = mp_cond<
628
629 mp_bool<(I < N1)>, mp_nth_element_impl<L1, I, P>,
630 mp_bool<(I == N1)>, mp_identity<T1>,
631 mp_true, mp_nth_element_impl<L2, I - N1 - 1, P>
632
633 >;
634 };
635 };
636
637 using type = typename detail::mp_nth_element_impl_cuda_workaround::type::type;
638
639 #else
640
641 using type = typename mp_cond<
642
643 mp_bool<(I < N1)>, mp_nth_element_impl<L1, I, P>,
644 mp_bool<(I == N1)>, mp_identity<T1>,
645 mp_true, mp_nth_element_impl<L2, I - N1 - 1, P>
646
647 >::type;
648
649 #endif
650 };
651
652 } // namespace detail
653
654 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;
655 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;
656 template<class L, class I, class Q> using mp_nth_element_q = mp_nth_element<L, I, Q::template fn>;
657
658 // mp_find<L, V>
659 namespace detail
660 {
661
662 template<class L, class V> struct mp_find_impl;
663
664 #if BOOST_MP11_CLANG && defined( BOOST_MP11_HAS_FOLD_EXPRESSIONS )
665
666 struct mp_index_holder
667 {
668 std::size_t i_;
669 bool f_;
670 };
671
672 constexpr inline mp_index_holder operator+( mp_index_holder const & v, bool f )
673 {
674 if( v.f_ )
675 {
676 return v;
677 }
678 else if( f )
679 {
680 return { v.i_, true };
681 }
682 else
683 {
684 return { v.i_ + 1, false };
685 }
686 }
687
688 template<template<class...> class L, class... T, class V> struct mp_find_impl<L<T...>, V>
689 {
690 static constexpr mp_index_holder _v{ 0, false };
691 using type = mp_size_t< (_v + ... + std::is_same<T, V>::value).i_ >;
692 };
693
694 #elif !defined( BOOST_MP11_NO_CONSTEXPR )
695
696 template<template<class...> class L, class V> struct mp_find_impl<L<>, V>
697 {
698 using type = mp_size_t<0>;
699 };
700
701 #if defined( BOOST_MP11_HAS_CXX14_CONSTEXPR )
702
703 constexpr std::size_t cx_find_index( bool const * first, bool const * last )
704 {
705 std::size_t m = 0;
706
707 while( first != last && !*first )
708 {
709 ++m;
710 ++first;
711 }
712
713 return m;
714 }
715
716 #else
717
718 constexpr std::size_t cx_find_index( bool const * first, bool const * last )
719 {
720 return first == last || *first? 0: 1 + cx_find_index( first + 1, last );
721 }
722
723 #endif
724
725 template<template<class...> class L, class... T, class V> struct mp_find_impl<L<T...>, V>
726 {
727 static constexpr bool _v[] = { std::is_same<T, V>::value... };
728 using type = mp_size_t< cx_find_index( _v, _v + sizeof...(T) ) >;
729 };
730
731 #else
732
733 #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
734
735 template<template<class...> class L, class... T, class V> struct mp_find_impl<L<T...>, V>
736 {
737 static_assert( sizeof...(T) == 0, "T... must be empty" );
738 using type = mp_size_t<0>;
739 };
740
741 #else
742
743 template<template<class...> class L, class V> struct mp_find_impl<L<>, V>
744 {
745 using type = mp_size_t<0>;
746 };
747
748 #endif
749
750 template<template<class...> class L, class... T, class V> struct mp_find_impl<L<V, T...>, V>
751 {
752 using type = mp_size_t<0>;
753 };
754
755 template<template<class...> class L, class T1, class... T, class V> struct mp_find_impl<L<T1, T...>, V>
756 {
757 using _r = typename mp_find_impl<mp_list<T...>, V>::type;
758 using type = mp_size_t<1 + _r::value>;
759 };
760
761 #endif
762
763 } // namespace detail
764
765 template<class L, class V> using mp_find = typename detail::mp_find_impl<L, V>::type;
766
767 // mp_find_if<L, P>
768 namespace detail
769 {
770
771 template<class L, template<class...> class P> struct mp_find_if_impl;
772
773 #if BOOST_MP11_CLANG && defined( BOOST_MP11_HAS_FOLD_EXPRESSIONS )
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 mp_index_holder _v{ 0, false };
778 using type = mp_size_t< (_v + ... + P<T>::value).i_ >;
779 };
780
781 #elif !defined( BOOST_MP11_NO_CONSTEXPR )
782
783 template<template<class...> class L, template<class...> class P> struct mp_find_if_impl<L<>, P>
784 {
785 using type = mp_size_t<0>;
786 };
787
788 template<template<class...> class L, class... T, template<class...> class P> struct mp_find_if_impl<L<T...>, P>
789 {
790 static constexpr bool _v[] = { P<T>::value... };
791 using type = mp_size_t< cx_find_index( _v, _v + sizeof...(T) ) >;
792 };
793
794 #else
795
796 #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
797
798 template<template<class...> class L, class... T, template<class...> class P> struct mp_find_if_impl<L<T...>, P>
799 {
800 static_assert( sizeof...(T) == 0, "T... must be empty" );
801 using type = mp_size_t<0>;
802 };
803
804 #else
805
806 template<template<class...> class L, template<class...> class P> struct mp_find_if_impl<L<>, P>
807 {
808 using type = mp_size_t<0>;
809 };
810
811 #endif
812
813 template<class L, template<class...> class P> struct mp_find_if_impl_2
814 {
815 using _r = typename mp_find_if_impl<L, P>::type;
816 using type = mp_size_t<1 + _r::value>;
817 };
818
819 template<template<class...> class L, class T1, class... T, template<class...> class P> struct mp_find_if_impl<L<T1, T...>, P>
820 {
821 using type = typename mp_if<P<T1>, mp_identity<mp_size_t<0>>, mp_find_if_impl_2<mp_list<T...>, P>>::type;
822 };
823
824 #endif
825
826 } // namespace detail
827
828 template<class L, template<class...> class P> using mp_find_if = typename detail::mp_find_if_impl<L, P>::type;
829 template<class L, class Q> using mp_find_if_q = mp_find_if<L, Q::template fn>;
830
831 // mp_reverse<L>
832 namespace detail
833 {
834
835 template<class L> struct mp_reverse_impl;
836
837 #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
838
839 template<template<class...> class L, class... T> struct mp_reverse_impl<L<T...>>
840 {
841 static_assert( sizeof...(T) == 0, "T... must be empty" );
842 using type = L<>;
843 };
844
845 #else
846
847 template<template<class...> class L> struct mp_reverse_impl<L<>>
848 {
849 using type = L<>;
850 };
851
852 #endif
853
854 template<template<class...> class L, class T1> struct mp_reverse_impl<L<T1>>
855 {
856 using type = L<T1>;
857 };
858
859 template<template<class...> class L, class T1, class T2> struct mp_reverse_impl<L<T1, T2>>
860 {
861 using type = L<T2, T1>;
862 };
863
864 template<template<class...> class L, class T1, class T2, class T3> struct mp_reverse_impl<L<T1, T2, T3>>
865 {
866 using type = L<T3, T2, T1>;
867 };
868
869 template<template<class...> class L, class T1, class T2, class T3, class T4> struct mp_reverse_impl<L<T1, T2, T3, T4>>
870 {
871 using type = L<T4, T3, T2, T1>;
872 };
873
874 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>>
875 {
876 using type = L<T5, T4, T3, T2, T1>;
877 };
878
879 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>>
880 {
881 using type = L<T6, T5, T4, T3, T2, T1>;
882 };
883
884 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>>
885 {
886 using type = L<T7, T6, T5, T4, T3, T2, T1>;
887 };
888
889 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>>
890 {
891 using type = L<T8, T7, T6, T5, T4, T3, T2, T1>;
892 };
893
894 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>>
895 {
896 using type = L<T9, T8, T7, T6, T5, T4, T3, T2, T1>;
897 };
898
899 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...>>
900 {
901 using type = mp_push_back<typename mp_reverse_impl<L<T...>>::type, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1>;
902 };
903
904 } // namespace detail
905
906 template<class L> using mp_reverse = typename detail::mp_reverse_impl<L>::type;
907
908 // mp_fold<L, V, F>
909 // in detail/mp_fold.hpp
910
911 // mp_reverse_fold<L, V, F>
912 namespace detail
913 {
914
915 template<class L, class V, template<class...> class F> struct mp_reverse_fold_impl;
916
917 #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
918
919 template<template<class...> class L, class... T, class V, template<class...> class F> struct mp_reverse_fold_impl<L<T...>, V, F>
920 {
921 static_assert( sizeof...(T) == 0, "T... must be empty" );
922 using type = V;
923 };
924
925 #else
926
927 template<template<class...> class L, class V, template<class...> class F> struct mp_reverse_fold_impl<L<>, V, F>
928 {
929 using type = V;
930 };
931
932 #endif
933
934 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>
935 {
936 using rest = typename mp_reverse_fold_impl<L<T...>, V, F>::type;
937 using type = F<T1, rest>;
938 };
939
940 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>
941 {
942 using rest = typename mp_reverse_fold_impl<L<T...>, V, F>::type;
943 using type = F<T1, F<T2, F<T3, F<T4, F<T5, F<T6, F<T7, F<T8, F<T9, F<T10, rest> > > > > > > > > >;
944 };
945
946 } // namespace detail
947
948 template<class L, class V, template<class...> class F> using mp_reverse_fold = typename detail::mp_reverse_fold_impl<L, V, F>::type;
949 template<class L, class V, class Q> using mp_reverse_fold_q = mp_reverse_fold<L, V, Q::template fn>;
950
951 // mp_unique<L>
952 namespace detail
953 {
954
955 template<class L> struct mp_unique_impl;
956
957 template<template<class...> class L, class... T> struct mp_unique_impl<L<T...>>
958 {
959 using type = mp_set_push_back<L<>, T...>;
960 };
961
962 } // namespace detail
963
964 template<class L> using mp_unique = typename detail::mp_unique_impl<L>::type;
965
966 // mp_unique_if<L, P>
967 namespace detail
968 {
969
970 template<template<class...> class P> struct mp_unique_if_push_back
971 {
972 template<class...> struct impl
973 {
974 };
975
976 template<template<class...> class L, class... Ts, class T>
977 struct impl<L<Ts...>, T>
978 {
979 using type = mp_if<mp_any<P<Ts, T>...>, L<Ts...>, L<Ts..., T>>;
980 };
981
982 template<class... T> using fn = typename impl<T...>::type;
983 };
984
985 } // namespace detail
986
987 template<class L, template<class...> class P>
988 using mp_unique_if = mp_fold_q<L, mp_clear<L>, detail::mp_unique_if_push_back<P>>;
989
990 template<class L, class Q> using mp_unique_if_q = mp_unique_if<L, Q::template fn>;
991
992 // mp_all_of<L, P>
993 template<class L, template<class...> class P> using mp_all_of = mp_bool< mp_count_if<L, P>::value == mp_size<L>::value >;
994 template<class L, class Q> using mp_all_of_q = mp_all_of<L, Q::template fn>;
995
996 // mp_none_of<L, P>
997 template<class L, template<class...> class P> using mp_none_of = mp_bool< mp_count_if<L, P>::value == 0 >;
998 template<class L, class Q> using mp_none_of_q = mp_none_of<L, Q::template fn>;
999
1000 // mp_any_of<L, P>
1001 template<class L, template<class...> class P> using mp_any_of = mp_bool< mp_count_if<L, P>::value != 0 >;
1002 template<class L, class Q> using mp_any_of_q = mp_any_of<L, Q::template fn>;
1003
1004 // mp_replace_at_c<L, I, W>
1005 namespace detail
1006 {
1007
1008 template<class L, class I, class W> struct mp_replace_at_impl
1009 {
1010 static_assert( I::value >= 0, "mp_replace_at<L, I, W>: I must not be negative" );
1011
1012 template<class T1, class T2> using _p = std::is_same<T2, mp_size_t<I::value>>;
1013 template<class T1, class T2> using _f = W;
1014
1015 using type = mp_transform_if<_p, _f, L, mp_iota<mp_size<L> > >;
1016 };
1017
1018 } // namespace detail
1019
1020 template<class L, class I, class W> using mp_replace_at = typename detail::mp_replace_at_impl<L, I, W>::type;
1021 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;
1022
1023 //mp_for_each<L>(f)
1024 namespace detail
1025 {
1026
1027 template<class... T, class F> BOOST_MP11_CONSTEXPR F mp_for_each_impl( mp_list<T...>, F && f )
1028 {
1029 using A = int[sizeof...(T)];
1030 return (void)A{ ((void)f(T()), 0)... }, std::forward<F>(f);
1031 }
1032
1033 template<class F> BOOST_MP11_CONSTEXPR F mp_for_each_impl( mp_list<>, F && f )
1034 {
1035 return std::forward<F>(f);
1036 }
1037
1038 } // namespace detail
1039
1040 #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, >= 1900 )
1041
1042 // msvc has a limit of 1024
1043
1044 template<class L, class F> BOOST_MP11_CONSTEXPR mp_if_c<mp_size<L>::value <= 1024, F> mp_for_each( F && f )
1045 {
1046 return detail::mp_for_each_impl( mp_rename<L, mp_list>(), std::forward<F>(f) );
1047 }
1048
1049 template<class L, class F> BOOST_MP11_CONSTEXPR mp_if_c<mp_size<L>::value >= 1025, F> mp_for_each( F && f )
1050 {
1051 using L2 = mp_rename<L, mp_list>;
1052
1053 using L3 = mp_take_c<L2, 1024>;
1054 using L4 = mp_drop_c<L2, 1024>;
1055
1056 return mp_for_each<L4>( mp_for_each<L3>( std::forward<F>(f) ) );
1057 }
1058
1059 #else
1060
1061 template<class L, class F> BOOST_MP11_CONSTEXPR F mp_for_each( F && f )
1062 {
1063 return detail::mp_for_each_impl( mp_rename<L, mp_list>(), std::forward<F>(f) );
1064 }
1065
1066 #endif
1067
1068 // mp_insert<L, I, T...>
1069 template<class L, class I, class... T> using mp_insert = mp_append<mp_take<L, I>, mp_push_front<mp_drop<L, I>, T...>>;
1070
1071 // mp_insert_c<L, I, T...>
1072 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...>>;
1073
1074 // mp_erase<L, I, J>
1075 template<class L, class I, class J> using mp_erase = mp_append<mp_take<L, I>, mp_drop<L, J>>;
1076
1077 // mp_erase_c<L, I, J>
1078 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>>;
1079
1080 // mp_starts_with<L1, L2>
1081 // contributed by Glen Joseph Fernandes (glenjofe@gmail.com)
1082 namespace detail {
1083
1084 template<class L1, class L2>
1085 struct mp_starts_with_impl { };
1086
1087 template<template<class...> class L1, class... T1, template<class...> class L2,
1088 class... T2>
1089 struct mp_starts_with_impl<L1<T1...>, L2<T2...> > {
1090 template<class L>
1091 static mp_false check(L);
1092
1093 template<class... T>
1094 static mp_true check(mp_list<T2..., T...>);
1095
1096 using type = decltype(check(mp_list<T1...>()));
1097 };
1098
1099 } // namespace detail
1100
1101 template<class L1, class L2>
1102 using mp_starts_with = typename detail::mp_starts_with_impl<L1, L2>::type;
1103
1104 // mp_rotate_left(_c)<L, N>
1105 namespace detail
1106 {
1107
1108 // limit divisor to 1 to avoid division by 0 and give a rotation of 0 for lists containing 0 or 1 elements
1109 template<std::size_t Ln, std::size_t N> using canonical_left_rotation = mp_size_t<N % (Ln == 0? 1: Ln)>;
1110
1111 // perform right rotation as a left rotation by inverting the number of elements to rotate
1112 template<std::size_t Ln, std::size_t N> using canonical_right_rotation = mp_size_t<Ln - N % (Ln == 0? 1: Ln)>;
1113
1114 // avoid errors when rotating fixed-sized lists by using mp_list for the transformation
1115 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> >>;
1116
1117 } // namespace detail
1118
1119 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>>;
1120 template<class L, class N> using mp_rotate_left = mp_rotate_left_c<L, std::size_t{ N::value }>;
1121
1122 // mp_rotate_right(_c)<L, N>
1123 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>>;
1124 template<class L, class N> using mp_rotate_right = mp_rotate_right_c<L, std::size_t{ N::value }>;
1125
1126 // mp_min_element<L, P>
1127 // mp_max_element<L, P>
1128 // in detail/mp_min_element.hpp
1129
1130 // mp_power_set<L>
1131 namespace detail
1132 {
1133
1134 template<class L> struct mp_power_set_impl;
1135
1136 } // namespace detail
1137
1138 template<class L> using mp_power_set = typename detail::mp_power_set_impl<L>::type;
1139
1140 namespace detail
1141 {
1142
1143 #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
1144
1145 template<template<class...> class L, class... T> struct mp_power_set_impl< L<T...> >
1146 {
1147 static_assert( sizeof...(T) == 0, "T... must be empty" );
1148 using type = L< L<> >;
1149 };
1150
1151 #else
1152
1153 template<template<class...> class L> struct mp_power_set_impl< L<> >
1154 {
1155 using type = L< L<> >;
1156 };
1157
1158 #endif
1159
1160 template<template<class...> class L, class T1, class... T> struct mp_power_set_impl< L<T1, T...> >
1161 {
1162 using S1 = mp_power_set< L<T...> >;
1163
1164 template<class L2> using _f = mp_push_front<L2, T1>;
1165
1166 using S2 = mp_transform<_f, S1>;
1167
1168 using type = mp_append< S1, S2 >;
1169 };
1170
1171 } // namespace detail
1172
1173 // mp_partial_sum<L, V, F>
1174 namespace detail
1175 {
1176
1177 template<template<class...> class F> struct mp_partial_sum_impl_f
1178 {
1179 #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1900 )
1180
1181 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>> >;
1182
1183 #else
1184
1185 template<class V, class T, class N = F<mp_first<V>, T>> using fn = mp_list<N, mp_push_back<mp_second<V>, N>>;
1186
1187 #endif
1188 };
1189
1190 } // namespace detail
1191
1192 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>> >;
1193 template<class L, class V, class Q> using mp_partial_sum_q = mp_partial_sum<L, V, Q::template fn>;
1194
1195 // mp_iterate<V, F, R>
1196 namespace detail
1197 {
1198
1199 template<class V, template<class...> class F, template<class...> class R, class N> struct mp_iterate_impl;
1200
1201 } // namespace detail
1202
1203 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;
1204
1205 namespace detail
1206 {
1207
1208 template<class V, template<class...> class F, template<class...> class R> struct mp_iterate_impl<V, F, R, mp_false>
1209 {
1210 template<class X> using _f = mp_list<F<X>>;
1211 using type = mp_eval_or<mp_list<>, _f, V>;
1212 };
1213
1214 template<class V, template<class...> class F, template<class...> class R> struct mp_iterate_impl<V, F, R, mp_true>
1215 {
1216 using type = mp_push_front<mp_iterate<R<V>, F, R>, F<V>>;
1217 };
1218
1219 } // namespace detail
1220
1221 template<class V, class Qf, class Qr> using mp_iterate_q = mp_iterate<V, Qf::template fn, Qr::template fn>;
1222
1223 } // namespace mp11
1224 } // namespace boost
1225
1226 #endif // #ifndef BOOST_MP11_ALGORITHM_HPP_INCLUDED